Provides access to TCP/IP protocols.
See also ERTS User's Guide, Inet configuration for more information on how to configure an Erlang runtime system for IP communication.
Two Kernel configuration parameters affect the behaviour of all sockets opened on an Erlang node: inet_default_connect_options can contain a list of default options used for all sockets returned when doing connect, and inet_default_listen_options can contain a list of default options used when issuing a listen call. When accept is issued, the values of the listensocket options are inherited, why no such application variable is needed for accept.
Using the Kernel configuration parameters mentioned above, one can set default options for all TCP sockets on a node. This should be used with care, but options like {delay_send,true} might be specified in this way. An example of starting an Erlang node with all sockets using delayed send could look like this:
$ erl -sname test -kernel \ inet_default_connect_options '[{delay_send,true}]' \ inet_default_listen_options '[{delay_send,true}]'
Note that the default option {active, true} currently cannot be changed, for internal reasons.
#hostent{h_addr_list = [ip_address()] % list of addresses for this host h_addrtype = inet | inet6 h_aliases = [hostname()] % list of aliases h_length = int() % length of address in bytes h_name = hostname() % official name for host The record is defined in the Kernel include file "inet.hrl" Add the following directive to the module: -include_lib("kernel/include/inet.hrl"). hostname() = atom() | string() ip_address() = {N1,N2,N3,N4} % IPv4 | {K1,K2,K3,K4,K5,K6,K7,K8} % IPv6 Ni = 0..255 Ki = 0..65535 posix() an atom which is named from the Posix error codes used in Unix, and in the runtime libraries of most C compilers socket() see gen_tcp(3), gen_udp(3)
Addresses as inputs to functions can be either a string or a tuple. For instance, the IP address 150.236.20.73 can be passed to gethostbyaddr/1 either as the string "150.236.20.73" or as the tuple {150, 236, 20, 73}.
IPv4 address examples:
Address ip_address() ------- ------------ 127.0.0.1 {127,0,0,1} 192.168.42.2 {192,168,42,2}
IPv6 address examples:
Address ip_address() ------- ------------ ::1 {0,0,0,0,0,0,0,1} ::192.168.42.2 {0,0,0,0,0,0,(192 bsl 8) bor 168,(42 bsl 8) bor 2} FFFF::192.168.42.2 {16#FFFF,0,0,0,0,0,(192 bsl 8) bor 168,(42 bsl 8) bor 2} 3ffe:b80:1f8d:2:204:acff:fe17:bf38 {16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38} fe80::204:acff:fe17:bf38 {16#fe80,0,0,0,0,16#204,16#acff,16#fe17,16#bf38}
A function that may be useful is inet_parse:address/1:
1> inet_parse:address("192.168.42.2"). {ok,{192,168,42,2}} 2> inet_parse:address("FFFF::192.168.42.2"). {ok,{65535,0,0,0,0,0,49320,10754}}
Types:
Socket = socket()
Closes a socket of any type.
Types:
Par, Val -- see below
Returns the state of the Inet configuration database in form of a list of recorded configuration parameters. (See the ERTS User's Guide, Inet configuration, for more information). Only parameters with other than default values are returned.
format_error(Posix) -> string()
Types:
Posix = posix()
Returns a diagnostic error string. See the section below for possible Posix values and the corresponding strings.
getaddr(Host, Family) -> {ok, Address} | {error, posix()}
Types:
Host = ip_address() | string() | atom()
Family = inet | inet6
Address = ip_address()
posix() = term()
Returns the IP-address for Host as a tuple of integers. Host can be an IP-address, a single hostname or a fully qualified hostname.
getaddrs(Host, Family) -> {ok, Addresses} | {error, posix()}
Types:
Host = ip_address() | string() | atom()
Addresses = [ip_address()]
Family = inet | inet6
Returns a list of all IP-addresses for Host. Host can be an IP-adress, a single hostname or a fully qualified hostname.
gethostbyaddr(Address) -> {ok, Hostent} | {error, posix()}
Types:
Address = string() | ip_address()
Hostent = #hostent{}
Returns a hostent record given an address.
gethostbyname(Name) -> {ok, Hostent} | {error, posix()}
Types:
Hostname = hostname()
Hostent = #hostent{}
Returns a hostent record given a hostname.
gethostbyname(Name, Family) -> {ok, Hostent} | {error, posix()}
Types:
Hostname = hostname()
Family = inet | inet6
Hostent = #hostent{}
Returns a hostent record given a hostname, restricted to the given address family.
gethostname() -> {ok, Hostname}
Types:
Hostname = string()
Returns the local hostname. Will never fail.
getopts(Socket, Options) -> OptionValues | {error, posix()}
Types:
Socket = term()
Options = [Opt | RawOptReq]
Opt = atom()
RawOptReq = {raw, Protocol, OptionNum, ValueSpec}
Protocol = int()
OptionNum = int()
ValueSpec = ValueSize | ValueBin
ValueSize = int()
ValueBin = binary()
OptionValues = [{Opt, Val} | {raw, Protocol, OptionNum, ValueBin}]
Gets one or more options for a socket. See setopts/2 for a list of available options.
The number of elements in the returned OptionValues list does not necessarily correspond to the number of options asked for. If the operating system fails to support an option, it is simply left out in the returned list. An error tuple is only returned when getting options for the socket is impossible (i.e. the socket is closed or the buffer size in a raw request is too large). This behavior is kept for backward compatibility reasons.
A RawOptReq can be used to get information about socket options not (explicitly) supported by the emulator. The use of raw socket options makes the code non portable, but allows the Erlang programmer to take advantage of unusual features present on the current platform.
The RawOptReq consists of the tag raw followed by the protocol level, the option number and either a binary or the size, in bytes, of the buffer in which the option value is to be stored. A binary should be used when the underlying getsockopt requires input in the argument field, in which case the size of the binary should correspond to the required buffer size of the return value. The supplied values in a RawOptReq correspond to the second, third and fourth/fifth parameters to the getsockopt call in the C socket API. The value stored in the buffer is returned as a binary ValueBin where all values are coded in the native endianess.
Asking for and inspecting raw socket options require low level information about the current operating system and TCP stack.
As an example, consider a Linux machine where the TCP_INFO option could be used to collect TCP statistics for a socket. Lets say we're interested in the tcpi_sacked field of the struct tcp_info filled in when asking for TCP_INFO. To be able to access this information, we need to know both the numeric value of the protocol level IPPROTO_TCP, the numeric value of the option TCP_INFO, the size of the struct tcp_info and the size and offset of the specific field. By inspecting the headers or writing a small C program, we found IPPROTO_TCP to be 6, TCP_INFO to be 11, the structure size to be 92 (bytes), the offset of tcpi_sacked to be 28 bytes and the actual value to be a 32 bit integer. We could use the following code to retrieve the value:
get_tcpi_sacked(Sock) -> {ok,[{raw,_,_,Info}]} = inet:getopts(Sock,[{raw,6,11,92}]), <<_:28/binary,TcpiSacked:32/native,_/binary>> = Info, TcpiSacked.
Preferably, you would check the machine type, the OS and the kernel version prior to executing anything similar to the code above.
getstat(Socket)
getstat(Socket, Options) -> {ok, OptionValues} | {error, posix()}
Types:
Socket = term()
Options = [Opt]
OptionValues = [{Opt, Val}]
Opt, Val -- see below
Gets one or more statistic options for a socket.
getstat(Socket) is equivalent to getstat(Socket, [recv_avg, recv_cnt, recv_dvi, recv_max, recv_oct, send_avg, send_cnt, send_dvi, send_max, send_oct])
The following options are available:
peername(Socket) -> {ok, {Address, Port}} | {error, posix()}
Types:
Socket = socket()
Address = ip_address()
Port = int()
Returns the address and port for the other end of a connection.
Types:
Socket = socket()
Port = int()
Returns the local port number for a socket.
sockname(Socket) -> {ok, {Address, Port}} | {error, posix()}
Types:
Socket = socket()
Address = ip_address()
Port = int()
Returns the local address and port number for a socket.
setopts(Socket, Options) -> ok | {error, posix()}
Types:
Socket = term()
Options = [{Opt, Val} | {raw, Protocol, Option, ValueBin}]
Protocol = int()
OptionNum = int()
ValueBin = binary()
Opt, Val -- see below
Sets one or more options for a socket. The following options are available:
In addition to the options mentioned above, raw option specifications can be used. The raw options are specified as a tuple of arity four, beginning with the tag raw, followed by the protocol level, the option number and the actual option value specified as a binary. This corresponds to the second, third and fourth argument to the setsockopt call in the C socket API. The option value needs to be coded in the native endianess of the platform and, if a structure is required, needs to follow the struct alignment conventions on the specific platform.
Using raw socket options require detailed knowledge about the current operating system and TCP stack.
As an example of the usage of raw options, consider a Linux system where you want to set the TCP_LINGER2 option on the IPPROTO_TCP protocol level in the stack. You know that on this particular system it defaults to 60 (seconds), but you would like to lower it to 30 for a particular socket. The TCP_LINGER2 option is not explicitly supported by inet, but you know that the protocol level translates to the number 6, the option number to the number 8 and the value is to be given as a 32 bit integer. You can use this line of code to set the option for the socket named Sock:
inet:setopts(Sock,[{raw,6,8,<<30:32/native>>}]),
As many options are silently discarded by the stack if they are given out of range, it could be a good idea to check that a raw option really got accepted. This code places the value in the variable TcpLinger2:
{ok,[{raw,6,8,<<TcpLinger2:32/native>>}]}=inet:getopts(Sock,[{raw,6,8,4}]),
Code such as the examples above is inherently non portable, even different versions of the same OS on the same platform may respond differently to this kind of option manipulation. Use with care.
Note that the default options for TCP/IP sockets can be changed with the Kernel configuration parameters mentioned in the beginning of this document.