# 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.
`{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

- `e2big` - argument list too long
- `eacces` - permission denied
- `eaddrinuse` - address already in use
- `eaddrnotavail` - cannot assign requested address
- `eadv` - advertise error
- `eafnosupport` - address family not supported by protocol family
- `eagain` - resource temporarily unavailable
- `ealign` - EALIGN
- `ealready` - operation already in progress
- `ebade` - bad exchange descriptor
- `ebadf` - bad file number
- `ebadfd` - file descriptor in bad state
- `ebadmsg` - not a data message
- `ebadr` - bad request descriptor
- `ebadrpc` - RPC structure is bad
- `ebadrqc` - bad request code
- `ebadslt` - invalid slot
- `ebfont` - bad font file format
- `ebusy` - file busy
- `echild` - no children
- `echrng` - channel number out of range
- `ecomm` - communication error on send
- `econnaborted` - software caused connection abort
- `econnrefused` - connection refused
- `econnreset` - connection reset by peer
- `edeadlk` - resource deadlock avoided
- `edeadlock` - resource deadlock avoided
- `edestaddrreq` - destination address required
- `edirty` - mounting a dirty fs w/o force
- `edom` - math argument out of range
- `edotdot` - cross mount point
- `edquot` - disk quota exceeded
- `eduppkg` - duplicate package name
- `eexist` - file already exists
- `efault` - bad address in system call argument
- `efbig` - file too large
- `ehostdown` - host is down
- `ehostunreach` - host is unreachable
- `eidrm` - identifier removed
- `einit` - initialization error
- `einprogress` - operation now in progress
- `eintr` - interrupted system call
- `einval` - invalid argument
- `eio` - I/O error
- `eisconn` - socket is already connected
- `eisdir` - illegal operation on a directory
- `eisnam` - is a named file
- `el2hlt` - level 2 halted
- `el2nsync` - level 2 not synchronized
- `el3hlt` - level 3 halted
- `el3rst` - level 3 reset
- `elbin` - ELBIN
- `elibacc` - cannot access a needed shared library
- `elibbad` - accessing a corrupted shared library
- `elibexec` - cannot exec a shared library directly
- `elibmax` - attempting to link in more shared libraries than system limit
- `elibscn` - .lib section in a.out corrupted
- `elnrng` - link number out of range
- `eloop` - too many levels of symbolic links
- `emfile` - too many open files
- `emlink` - too many links
- `emsgsize` - message too long
- `emultihop` - multihop attempted
- `enametoolong` - file name too long
- `enavail` - not available
- `enet` - ENET
- `enetdown` - network is down
- `enetreset` - network dropped connection on reset
- `enetunreach` - network is unreachable
- `enfile` - file table overflow
- `enoano` - anode table overflow
- `enobufs` - no buffer space available
- `enocsi` - no CSI structure available
- `enodata` - no data available
- `enodev` - no such device
- `enoent` - no such file or directory
- `enoexec` - exec format error
- `enolck` - no locks available
- `enolink` - link has be severed
- `enomem` - not enough memory
- `enomsg` - no message of desired type
- `enonet` - machine is not on the network
- `enopkg` - package not installed
- `enoprotoopt` - bad proocol option
- `enospc` - no space left on device
- `enosr` - out of stream resources or not a stream device
- `enosym` - unresolved symbol name
- `enosys` - function not implemented
- `enotblk` - block device required
- `enotconn` - socket is not connected
- `enotdir` - not a directory
- `enotempty` - directory not empty
- `enotnam` - not a named file
- `enotsock` - socket operation on non-socket
- `enotsup` - operation not supported
- `enotty` - inappropriate device for ioctl
- `enotuniq` - name not unique on network
- `enxio` - no such device or address
- `eopnotsupp` - operation not supported on socket
- `eperm` - not owner
- `epfnosupport` - protocol family not supported
- `epipe` - broken pipe
- `eproclim` - too many processes
- `eprocunavail` - bad procedure for program
- `eprogmismatch` - program version wrong
- `eprogunavail` - RPC program not available
- `eproto` - protocol error
- `eprotonosupport` - protocol not supported
- `eprototype` - protocol wrong type for socket
- `erange` - math result unrepresentable
- `erefused` - EREFUSED
- `eremchg` - remote address changed
- `eremdev` - remote device
- `eremote` - pathname hit remote file system
- `eremoteio` - remote i/o error
- `eremoterelease` - EREMOTERELEASE
- `erofs` - read-only file system
- `erpcmismatch` - RPC version is wrong
- `erremote` - object is remote
- `eshutdown` - cannot send after socket shutdown
- `esocktnosupport` - socket type not supported
- `espipe` - invalid seek
- `esrch` - no such process
- `esrmnt` - srmount error
- `estale` - stale remote file handle
- `esuccess` - Error 0
- `etime` - timer expired
- `etimedout` - connection timed out
- `etoomanyrefs` - too many references
- `etxtbsy` - text file or pseudo-device busy
- `euclean` - structure needs cleaning
- `eunatch` - protocol driver not attached
- `eusers` - too many users
- `eversion` - version mismatch
- `ewouldblock` - operation would block
- `exdev` - cross-domain link
- `exfull` - message tables full
- `nxdomain` - the hostname or domain name could not be found

### AUTHORS

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

kernel 2.11.1
Copyright © 1991-2006 Ericsson AB