Kernel

Reference Manual

Version 6.5

Table of Contents

net

Module

net

Module Summary

Network interface.

Since

Module net was introduced in OTP 22.0.

Description

This module provides an API for the network interface.

Note

There is currently no support for Windows.

Note

The content of this file is only valid if the system has been built with 'socket' (esock) support, which is the default.

Data Types

address_info() =
    #{family := term(),
      socktype := term(),
      protocol := term(),
      address := term()}

ifaddrs() =
    #{name := string(),
      flags := [ifaddrs_flag()],
      addr := socket:sockaddr(),
      netmask := socket:sockaddr(),
      broadaddr := socket:sockaddr(),
      dstaddr := socket:sockaddr()}

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

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

ifaddrs_flag() =
    up | broadcast | debug | loopback | pointopoint | notrailers |
    running | noarp | promisc | master | slave | multicast |
    portsel | automedia | dynamic

ifaddrs_filter_map() =
    #{family := default | inet | inet6 | packet | all,
      flags := any | [ifaddrs_flag()]}

The 'default' value for family is interpreted as inet and inet6.

Also, the family field can only have the specified values (and not the 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 address family inet and the flags broadcast and multicast will be listen.

name_info() = #{host := string(), service := string()}
name_info_flag() =
    namereqd | dgram | nofqdn | numerichost | numericserv

network_interface_index() = integer() >= 0

Exports

gethostname() -> {ok, HostName} | {error, Reason}

Types

HostName = string()
Reason = term()

Returns the name of the current host.

getnameinfo(SockAddr) -> {ok, Info} | {error, Reason}
OTP 22.0
getnameinfo(SockAddr, Flags) -> {ok, Info} | {error, Reason}
OTP 22.0

Types

SockAddr = term()
Flags = name_info_flags() | undefined
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.

getaddrinfo(Host) -> {ok, Info} | {error, Reason}
OTP 22.0
getaddrinfo(Host, Service :: undefined) ->
               {ok, Info} | {error, Reason}
OTP 22.0
getaddrinfo(Host :: undefined, Service) ->
               {ok, Info} | {error, Reason}
OTP 22.0
getaddrinfo(Host, Service) -> {ok, Info} | {error, Reason}
OTP 22.0

Types

Host = Service = string()
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.

getifaddrs() -> {ok, IfAddrs} | {error, Reason}
@OTP-16212@
getifaddrs(FilterMap) -> {ok, IfAddrs} | {error, Reason}
@OTP-16212@
getifaddrs(Namespace) -> {ok, IfAddrs} | {error, Reason}
@OTP-16212@
getifaddrs(FilterMap, Namespace) ->
              {ok, IfAddrs} | {error, Reason}
@OTP-16212@

Types

IfAddrs = [ifaddrs()]
Reason = term()

Get interface addresses.

This function is used to get the machines interface addresses, possibly filtered with the FilterMap.

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

if_name2index(Name) -> {ok, Idx} | {error, Reason}
OTP 22.0

Types

Reason = term()

Mappings between network interface names and indexes.

if_index2name(Idx) -> {ok, Name} | {error, Reason}
OTP 22.0

Types

Reason = term()

Mappings between network interface index and names.

if_names() -> Names | {error, Reason}
OTP 22.0

Types

Names = [{Idx, If}]
Reason = term()

Get network interface names and indexes.