[Ericsson AB]

inet

MODULE

inet

MODULE SUMMARY

Access to TCP/IP Protocols

DESCRIPTION

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.

DATA TYPES

#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}}
    

EXPORTS

close(Socket) -> ok

Types:

Socket = socket()

Closes a socket of any type.

get_rc() -> [{Par, Val}]

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} | {error, posix()}

Types:

Hostname = string()

Returns the local hostname. Will never fail.

getopts(Socket, Options) -> OptionValues

Types:

Socket = term()
Options = [Opt]
OptionValues = [{Opt, Val}]

Gets one or more options for a socket. See inet:setopts/2 for a list of available options.

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.

port(Socket) -> {ok, Port}

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}]
 Opt, Val -- see below

Sets one or more options for a socket. The following options are available:

{active, Boolean}
If the value is true, which is the default, everything received from the socket will be sent as messages to the receiving process. If the value is false (passive mode), the process must explicitly receive incoming data by calling gen_tcp:recv/2,3 or gen_udp:recv/2,3 (depending on the type of socket).
If the value is once (active once), one data message from the socket will be sent to the process. To receive one more message, setopts/2 must be called again with the {active, once} option.
Note: Active mode provides no flow control; a fast sender could easily overflow the receiver with incoming messages. Use active mode only if your high-level protocol provides its own flow control (for instance, acknowledging received messages) or the amount of data exchanged is small. Passive mode or active-once mode provides flow control; the other side will not be able send faster than the receiver can read.
{broadcast, Boolean} (UDP sockets)
Enable/disable permission to send broadcasts.
{delay_send, Boolean}
Normally, when an Erlang process sends to a socket, the driver will try to immediately send the data. If that fails, the driver will use any means available to queue up the message to be sent whenever the operating system says it can handle it. Setting {delay_send, true} will make all messages queue up. This makes the messages actually sent onto the network be larger but fewer. The option actually affects the scheduling of send requests versus Erlang processes instead of changing any real property of the socket. Needless to say it is an implementation specific option. Default is false.
{dontroute, Boolean}
Enable/disable routing bypass for outgoing messages.
{exit_on_close, Boolean}
By default this option is set to true.
The only reason to set it to false is if you want to continue sending data to the socket after a close has been detected, for instance if the peer has used gen_tcp:shutdown/2 to shutdown the write side.
{header, Size}
This option is only meaningful if the binary option was specified when the socket was created. If the header option is specified, the first Size number bytes of data received from the socket will be elements of a list, and the rest of the data will be a binary given as the tail of the same list. If for example Size == 2, the data received will match [Byte1,Byte2|Binary].
{keepalive, Boolean} (TCP/IP sockets)
Enables/disables periodic transmission on a connected socket, when no other data is being exchanged. If the other end does not respond, the connection is considered broken and an error message will be sent to the controlling process. Default disabled.
{nodelay, Boolean} (TCP/IP sockets)
If Boolean == true, the TCP_NODELAY option is turned on for the socket, which means that even small amounts of data will be sent immediately.
{packet, PacketType} (TCP/IP sockets)
Defines the type of packets to use for a socket. The following values are valid:
raw | 0
No packaging is done.
1 | 2 | 4
Packets consist of a header specifying the number of bytes in the packet, followed by that number of bytes. The length of header can be one, two, or four bytes; the order of the bytes is big-endian. Each send operation will generate the header, and the header will be stripped off on each receive operation.
asn1 | cdr | sunrm | fcgi | tpkt | line
These packet types only have effect on receiving. When sending a packet, it is the responsibility of the application to supply a correct header. On receiving, however, there will be one message sent to the controlling process for each complete packet received, and, similarly, each call to gen_tcp:recv/2,3 returns one complete packet. The header is not stripped off.
The meanings of the packet types are as follows:
asn1 - ASN.1 BER,
sunrm - Sun's RPC encoding,
cdr - CORBA (GIOP 1.1),
fcgi - Fast CGI,
tpkt - TPKT format [RFC1006],
line - Line mode, a packet is a line terminated with newline, lines longer than the receive buffer are truncated.
{packet_size, Integer} (TCP/IP sockets)
Sets the max allowed length of the packet body. If the packet header indicates that the length of the packet is longer than the max allowed length, the packet is considered invalid. The same happens if the packet header is too big for the socket receive buffer.
{read_packets, Integer} (UDP sockets)
Sets the max number of UDP packets to read without intervention from the socket when data is available. When this many packets have been read and delivered to the destination process, new packets are not read until a new notification of available data has arrived. The default is 5, and if this parameter is set too high the system can become unresponsive due to UDP packet flooding.
{recbuf, Integer}
Gives the size of the receive buffer to use for the socket.
{reuseaddr, Boolean}
Allows or disallows local reuse of port numbers. By default, reuse is disallowed.
{sndbuf, Integer}
Gives the size of the send buffer to use for the socket.
{priority, Integer}
Sets the SO_PRIORITY socket level option on platforms where this is implemented. The behaviour and allowed range varies on different systems. The option is ignored on platforms where the option is not implemented. Use with caution.
{tos, Integer}
Sets IP_TOS IP level options on platforms where this is implemented. The behaviour and allowed range varies on different systems. The option is ignored on platforms where the option is not implemented. Use with caution.

Note that the default options for TCP/IP sockets can be changed with the Kernel configuration parameters mentioned in the beginning of this document.

POSIX Error Codes

AUTHORS

tony@erix.ericsson.se - support@erlang.ericsson.se

kernel 2.11.2
Copyright © 1991-2006 Ericsson AB