View Source net (kernel v9.3)

Network interface.

This module provides an API for the network interface.

Summary

Types

This type defines all addresses (and flags) associated with the interface.

  • all - All interfaces

For each ifaddrs entry, return either true to keep the entry or false to discard the entry.

The family field can only have the (above) specified values (and not all the values of socket:domain()).

Functions

Network address and service translation.

Returns the name of the current host.

Equivalent to getifaddrs/2.

Equivalent to getifaddrs/2.

Get interface addresses.

Address-to-name translation in a protocol-independant manner.

Mappings between network interface index and names.

Mappings between network interface names and indexes.

Get network interface names and indexes.

Types

Link to this type

address_info()

View Source (since OTP 22.0)
-type address_info() ::
    #{family := socket:domain(),
      socktype := socket:type(),
      protocol := socket:protocol(),
      address := socket:sockaddr()}.
Link to this type

ifaddrs()

View Source (since OTP 22.0)
-type ifaddrs() ::
    #{name := string(),
      flags := ifaddrs_flags(),
      addr => socket:sockaddr(),
      netmask => socket:sockaddr(),
      broadaddr => socket:sockaddr(),
      dstaddr => socket:sockaddr()}.

This type defines all addresses (and flags) associated with the interface.

Note

Not all fields of this map has to be present. The flags field can be used to test for some of the fields. For example broadaddr will only be present if the broadcast flag is present in flags.

Link to this type

ifaddrs_filter()

View Source (not exported) (since OTP 22.0)
-type ifaddrs_filter() ::
    all | default | inet | inet6 | packet | ifaddrs_filter_map() | ifaddrs_filter_fun().
  • all - All interfaces

  • default - Interfaces with address family inet and inet6

  • inet | inet6 | packet - Interfaces with only the specified address family

Link to this type

ifaddrs_filter_fun()

View Source (not exported) (since OTP 22.0)
-type ifaddrs_filter_fun() :: fun((ifaddrs()) -> boolean()).

For each ifaddrs entry, return either true to keep the entry or false to discard the entry.

For example, to get an interface list which only contains non-loopback inet interfaces:

	net:getifaddrs(fun(#{addr  := #{family := inet},
	                     flags := Flags}) ->
			       not lists:member(loopback, Flags);
			  (_) ->
			       false
		       end).
Link to this type

ifaddrs_filter_map()

View Source (not exported) (since OTP 22.0)
-type ifaddrs_filter_map() ::
    #{family := default | local | inet | inet6 | packet | all, flags := any | [ifaddrs_flag()]}.

The family field can only have the (above) specified values (and not all the values of socket:domain()).

The use of the flags field is that any flag provided must exist for the interface.

For example, if family is set to inet and flags to [broadcast, multicast] only interfaces with address family inet and the flags broadcast and multicast will be listed.

Link to this type

ifaddrs_flag()

View Source (since OTP 22.0)
-type ifaddrs_flag() ::
    up | broadcast | debug | loopback | pointopoint | notrailers | running | noarp | promisc |
    master | slave | multicast | portsel | automedia | dynamic.
Link to this type

ifaddrs_flags()

View Source (since OTP 22.0)
-type ifaddrs_flags() :: [ifaddrs_flag()].
Link to this type

name_info()

View Source (since OTP 22.0)
-type name_info() :: #{host := string(), service := string()}.
Link to this type

name_info_flag()

View Source (since OTP 22.0)
-type name_info_flag() :: namereqd | dgram | nofqdn | numerichost | numericserv.
Link to this type

name_info_flag_ext()

View Source (since OTP 22.0)
-type name_info_flag_ext() :: idn.
Link to this type

name_info_flags()

View Source (since OTP 22.0)
-type name_info_flags() :: [name_info_flag() | name_info_flag_ext()].
Link to this type

network_interface_index()

View Source (since OTP 22.0)
-type network_interface_index() :: non_neg_integer().
Link to this type

network_interface_name()

View Source (since OTP 22.0)
-type network_interface_name() :: string().

Functions

Link to this function

getaddrinfo(Host)

View Source (since OTP 22.0)
-spec getaddrinfo(Host) -> {ok, Info} | {error, Reason}
               when Host :: string(), Info :: [address_info()], Reason :: term().

Equivalent to getaddrinfo/2.

Link to this function

getaddrinfo(Host, Service)

View Source (since OTP 22.0)
-spec getaddrinfo(Host, undefined) -> {ok, Info} | {error, Reason}
               when Host :: string(), Info :: [address_info()], Reason :: term();
           (undefined, Service) -> {ok, Info} | {error, Reason}
               when Service :: string(), Info :: [address_info()], Reason :: term();
           (Host, Service) -> {ok, Info} | {error, Reason}
               when
                   Host :: string(),
                   Service :: string(),
                   Info :: [address_info()],
                   Reason :: term().

Network address and service translation.

This function is the inverse of getnameinfo. It converts host and service to a corresponding socket address.

One of the Host and Service may be undefined but not both.

Link to this function

gethostname()

View Source (since OTP 22.0)
-spec gethostname() -> {ok, HostName} | {error, Reason} when HostName :: string(), Reason :: term().

Returns the name of the current host.

Link to this function

getifaddrs()

View Source (since OTP 22.3)
-spec getifaddrs() -> {ok, IfAddrs} | {error, Reason} when IfAddrs :: [ifaddrs()], Reason :: term().

Equivalent to getifaddrs/2.

Link to this function

getifaddrs/1

View Source (since OTP 22.3)
-spec getifaddrs(Filter) -> {ok, IfAddrs} | {error, Reason}
              when Filter :: ifaddrs_filter(), IfAddrs :: [ifaddrs()], Reason :: term();
          (Namespace) -> {ok, IfAddrs} | {error, Reason}
              when Namespace :: file:filename_all(), IfAddrs :: [ifaddrs()], Reason :: term().

Equivalent to getifaddrs/2.

Link to this function

getifaddrs(Filter, Namespace)

View Source (since OTP 22.3)
-spec getifaddrs(Filter, Namespace) -> {ok, IfAddrs} | {error, Reason}
              when
                  Filter :: ifaddrs_filter(),
                  Namespace :: file:filename_all(),
                  IfAddrs :: [ifaddrs()],
                  Reason :: term().

Get interface addresses.

This function is used to get the machines interface addresses, possibly filtered according to Filter.

By default, a filter with the content: #{family => default, flags => any} is used. This will return all interfaces with addresses in the inet and inet6 families.

Link to this function

getnameinfo(SockAddr)

View Source (since OTP 22.0)
-spec getnameinfo(SockAddr) -> {ok, Info} | {error, Reason}
               when SockAddr :: socket:sockaddr(), Info :: name_info(), Reason :: term().

Equivalent to getnameinfo/2.

Link to this function

getnameinfo(SockAddr, Flags)

View Source (since OTP 22.0)
-spec getnameinfo(SockAddr, Flags) -> {ok, Info} | {error, Reason}
               when
                   SockAddr :: socket:sockaddr(),
                   Flags :: name_info_flags() | undefined,
                   Info :: name_info(),
                   Reason :: term().

Address-to-name translation in a protocol-independant manner.

This function is the inverse of getaddrinfo. It converts a socket address to a corresponding host and service.

Link to this function

if_index2name(Idx)

View Source (since OTP 22.0)
-spec if_index2name(Idx) -> {ok, Name} | {error, Reason}
                 when
                     Idx :: network_interface_index(),
                     Name :: network_interface_name(),
                     Reason :: term().

Mappings between network interface index and names.

Link to this function

if_name2index(Name)

View Source (since OTP 22.0)
-spec if_name2index(Name) -> {ok, Idx} | {error, Reason}
                 when
                     Name :: network_interface_name(),
                     Idx :: network_interface_index(),
                     Reason :: term().

Mappings between network interface names and indexes.

Link to this function

if_names()

View Source (since OTP 22.0)
-spec if_names() -> {ok, Names} | {error, Reason}
            when
                Names :: [{Idx, If}],
                Idx :: network_interface_index(),
                If :: network_interface_name(),
                Reason :: term().

Get network interface names and indexes.