Network interface.
This module provides an API for the network interface.
Summary
Types
Interface addresses and flags.
Interface address filtering selector.
Interface address filtering selector function/0.
Interface address filtering selector map.
Functions
Network address and service translation.
Return the name of the current host.
Get interface addresses.
Get interface addresses in a namespace.
Address-to-name translation in a protocol-independant manner.
Get service by name.
Get service by name.
Mappings between network interface index and names.
Mappings between network interface names and indexes.
Get network interface names and indexes.
Types
-type address_info() :: #{family := socket:domain(), socktype := any | socket:type() | integer(), protocol := socket:protocol(), address := socket:sockaddr()}.
-type ifaddrs() :: #{name := string(), flags := ifaddrs_flags(), addr => socket:sockaddr(), netmask => socket:sockaddr(), broadaddr => socket:sockaddr(), dstaddr => socket:sockaddr()}.
Interface addresses and flags.
This type defines addresses and flags for an 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.
-type ifaddrs_filter() :: all | default | inet | inet6 | packet | link | hwaddr | ifaddrs_filter_map() | ifaddrs_filter_fun().
Interface address filtering selector.
all - All interfaces
default - Interfaces with address family
inetorinet6inet | inet6 | packet | link - Interfaces with only the specified address family
hwaddr - Interfaces with address family
packetorlink
Interface address filtering selector function/0.
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).
-type ifaddrs_filter_map() :: #{family := all | default | local | inet | inet6 | packet | link | [local | inet | inet6 | packet | link], flags := any | [ifaddrs_flag()]}.
Interface address filtering selector map.
The family field can only have the (above) specified values
(and not all the values of socket:domain()).
It can also be a list of values, to cover the situation when
any of the specified families are accepted.
For example, family can be set to [inet,inet6] if either inet or inet6
is accepted.
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.
-type ifaddrs_flag() ::
up | broadcast | debug | loopback | pointopoint | notrailers | running | noarp | promisc |
master | slave | multicast | portsel | automedia | dynamic.
-type ifaddrs_flags() :: [ifaddrs_flag()].
-type name_info_flag() :: namereqd | dgram | nofqdn | numerichost | numericserv.
-type name_info_flag_ext() :: idn.
-type name_info_flags() :: [name_info_flag() | name_info_flag_ext()].
-type network_interface_index() :: non_neg_integer().
-type network_interface_name() :: string().
Functions
-spec getaddrinfo(Host) -> {ok, Info} | {error, Reason} when Host :: string(), Info :: [address_info()], Reason :: term().
-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.
Return the name of the current host.
-spec getifaddrs() -> {ok, IfAddrs} | {error, Reason} when IfAddrs :: [ifaddrs()], Reason :: term().
-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().
Get interface addresses.
With argument 'Filter: get the machines interface addresses,
filtered according to Filter.
With argument Namespace: equivalent to
getifaddrs(default, Namespace).
Types
Filter = ifaddrs_filter()
Namespace = file:filename_all()
Result = {ok, IfAddrs} | {error, Reason}
Reason = term()
IfAddrs = [ifaddrs()]
Get interface addresses in a namespace.
The same as getifaddrs(Filter) but
in the specified Namespace.
Types
SockAddr = socket:sockaddr()
Flags = name_info_flags() | undefined
Result = {ok, Info} | {error, Reason}
Reason = term()
Info = name_info()
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.
Types
Name = atom() | string()
Protocol = any | socket:protocol()
Result = {ok, PortNumber} | {error, Reason}
Reason = term()
PortNumber = socket:port_number()
Get service by name.
This function is used to get the port number of the specified protocol for the named service.
Types
PortNumber = socket:port_number()
Protocol = any | socket:protocol()
Result = {ok, Name} | {error, Reason}
Reason = term()
Name = atom() | string()
Get service by name.
This function is used to get the service name of the specified protocol for the given port number.
Mappings between network interface index and names.
Mappings between network interface names and indexes.
Types
Result = {ok, Names} | {error, Reason}
Reason = term()
Names = [{Idx, If}]
If = network_interface_name()
Idx = network_interface_index()
Get network interface names and indexes.