View Source gen_udp (kernel v9.3)
Interface to UDP sockets.
This module provides functions for communicating with sockets using the UDP protocol.
Note
Functions that create sockets can take an optional option;
{inet_backend, Backend}
that, if specified, has to be the first option. This selects the implementation backend towards the platform's socket API.This is a temporary option that will be ignored in a future release.
The default is
Backend = inet
that selects the traditionalinet_drv.c
driver. The other choice isBackend = socket
that selects the newsocket
module and its NIF implementation.The system default can be changed when the node is started with the application
kernel
's configuration variableinet_backend
.For
gen_udp
withinet_backend = socket
we have tried to be as "compatible" as possible which has sometimes been impossible. Here is a list of cases when the behaviour of inet-backendinet
(default) andsocket
are different:
The option read_packets is currently ignored.
Windows require sockets (domain =
inet | inet6
) to be bound.Currently all sockets created on Windows with
inet_backend = socket
will be bound. If the user does not provide an address, gen_udp will try to 'figure out' an address itself.
Summary
Types
For IPv6 this is an interface index (an integer).
The tuple with size 3 is not supported on all platforms. 'ifindex' defaults to zero (0) on platforms that supports the 3-tuple variant.
Functions
Closes a UDP socket.
Assigns a new controlling process Pid
to Socket
. The controlling process is
the process that receives messages from the socket. If called by any other
process than the current controlling process, {error, not_owner}
is returned.
If the process identified by Pid
is not an existing local pid,
{error, badarg}
is returned. {error, badarg}
may also be returned in some
cases when Socket
is closed during the execution of this function.
Equivalent to open/2
Associates a UDP port number (Port
) with the calling process.
Equivalent to recv/3
Receives a packet from a socket in passive mode. Optional parameter Timeout
specifies a time-out in milliseconds. Defaults to infinity
.
Sends a packet to the specified Destination
.
Sends a packet to the specified Host
and Port
, with ancillary data
AncData
.
Types
-type ip6_membership() :: {MultiAddress :: inet:ip6_address(), IfIndex :: integer()}.
-type ip6_multicast_if() :: integer().
For IPv6 this is an interface index (an integer).
-type ip_membership() :: {MultiAddress :: inet:ip4_address(), Interface :: inet:ip4_address()} | {MultiAddress :: inet:ip4_address(), Address :: inet:ip4_address(), IfIndex :: integer()}.
The tuple with size 3 is not supported on all platforms. 'ifindex' defaults to zero (0) on platforms that supports the 3-tuple variant.
-type ip_multicast_if() :: inet:ip4_address().
-type membership() :: ip_membership() | ip6_membership().
-type multicast_if() :: ip_multicast_if() | ip6_multicast_if().
-type open_option() :: {ip, inet:socket_address()} | {fd, non_neg_integer()} | {ifaddr, socket:sockaddr_in() | socket:sockaddr_in6() | inet:socket_address()} | inet:address_family() | {port, inet:port_number()} | {netns, file:filename_all()} | {bind_to_device, binary()} | option().
-type option() :: {active, true | false | once | -32768..32767} | {add_membership, membership()} | {broadcast, boolean()} | {buffer, non_neg_integer()} | {debug, boolean()} | {deliver, port | term} | {dontroute, boolean()} | {drop_membership, membership()} | {exclusiveaddruse, boolean()} | {header, non_neg_integer()} | {high_msgq_watermark, pos_integer()} | {low_msgq_watermark, pos_integer()} | {mode, list | binary} | list | binary | {multicast_if, multicast_if()} | {multicast_loop, boolean()} | {multicast_ttl, non_neg_integer()} | {priority, non_neg_integer()} | {raw, Protocol :: non_neg_integer(), OptionNum :: non_neg_integer(), ValueBin :: binary()} | {read_packets, non_neg_integer()} | {recbuf, non_neg_integer()} | {reuseaddr, boolean()} | {reuseport, boolean()} | {reuseport_lb, boolean()} | {sndbuf, non_neg_integer()} | {tos, non_neg_integer()} | {tclass, non_neg_integer()} | {ttl, non_neg_integer()} | {recvtos, boolean()} | {recvtclass, boolean()} | {recvttl, boolean()} | {ipv6_v6only, boolean()}.
-type option_name() :: active | broadcast | buffer | debug | deliver | dontroute | exclusiveaddruse | header | high_msgq_watermark | low_msgq_watermark | mode | multicast_if | multicast_loop | multicast_ttl | priority | {raw, Protocol :: non_neg_integer(), OptionNum :: non_neg_integer(), ValueSpec :: (ValueSize :: non_neg_integer()) | (ValueBin :: binary())} | read_packets | recbuf | reuseaddr | reuseport | reuseport_lb | sndbuf | tos | tclass | ttl | recvtos | recvtclass | recvttl | pktoptions | ipv6_v6only.
-type socket() :: inet:socket().
As returned by open/1,2
.
Functions
-spec close(Socket) -> ok when Socket :: socket().
Closes a UDP socket.
-spec connect(Socket, SockAddr) -> ok | {error, Reason} when Socket :: socket(), SockAddr :: socket:sockaddr_in() | socket:sockaddr_in6(), Reason :: inet:posix().
Connecting a UDP socket only means storing the specified (destination) socket
address, as specified by SockAddr
, so that the system knows where to send
data.
This means that it is not necessary to specify the destination address when
sending a datagram. That is, we can use send/2
.
It also means that the socket will only receive data from this address.
-spec connect(Socket, Address, Port) -> ok | {error, Reason} when Socket :: socket(), Address :: inet:socket_address() | inet:hostname(), Port :: inet:port_number(), Reason :: inet:posix().
Connecting a UDP socket only means storing the specified (destination) socket
address, as specified by Address
and Port
, so that the system knows where to
send data.
This means that it is not necessary to specify the destination address when
sending a datagram. That is, we can use send/2
.
It also means that the socket will only receive data from this address.
-spec controlling_process(Socket, Pid) -> ok | {error, Reason} when Socket :: socket(), Pid :: pid(), Reason :: closed | not_owner | badarg | inet:posix().
Assigns a new controlling process Pid
to Socket
. The controlling process is
the process that receives messages from the socket. If called by any other
process than the current controlling process, {error, not_owner}
is returned.
If the process identified by Pid
is not an existing local pid,
{error, badarg}
is returned. {error, badarg}
may also be returned in some
cases when Socket
is closed during the execution of this function.
-spec open(Port) -> {ok, Socket} | {error, Reason} when Port :: inet:port_number(), Socket :: socket(), Reason :: system_limit | inet:posix().
Equivalent to open/2
-spec open(Port, Opts) -> {ok, Socket} | {error, Reason} when Port :: inet:port_number(), Opts :: [inet:inet_backend() | open_option()], Socket :: socket(), Reason :: system_limit | inet:posix().
Associates a UDP port number (Port
) with the calling process.
The following options are available:
list
- ReceivedPacket
is delivered as a list.binary
- ReceivedPacket
is delivered as a binary.{ip, Address}
- If the host has many network interfaces, this option specifies which one to use.{ifaddr, Address}
- Same as{ip, Address}
. If the host has many network interfaces, this option specifies which one to use.However, if this instead is an
socket:sockaddr_in/0
orsocket:sockaddr_in6/0
this takes precedence over any value previously set with theip
options. If theip
option comes after theifaddr
option, it may be used to update its corresponding field of theifaddr
option (theaddr
field).{fd, integer() >= 0}
- If a socket has somehow been opened without usinggen_udp
, use this option to pass the file descriptor for it. IfPort
is not set to0
and/or{ip, ip_address()}
is combined with this option, thefd
is bound to the specified interface and port after it is being opened. If these options are not specified, it is assumed that thefd
is already bound appropriately.inet6
- Sets up the socket for IPv6.inet
- Sets up the socket for IPv4.local
- Sets up a Unix Domain Socket. Seeinet:local_address/0
{udp_module, module()}
- Overrides which callback module is used. Defaults toinet_udp
for IPv4 andinet6_udp
for IPv6.{multicast_if, Address}
- Sets the local device for a multicast socket.{multicast_loop, true | false}
- Whentrue
, sent multicast packets are looped back to the local sockets.{multicast_ttl, Integer}
- Optionmulticast_ttl
changes the time-to-live (TTL) for outgoing multicast datagrams to control the scope of the multicasts.Datagrams with a TTL of 1 are not forwarded beyond the local network. Defaults to
1
.{add_membership, {MultiAddress, InterfaceAddress}}
- Joins a multicast group.{drop_membership, {MultiAddress, InterfaceAddress}}
- Leaves a multicast group.Opt
- Seeinet:setopts/2
.
The returned socket Socket
is used to send packets from this port with
send/4
. When UDP packets arrive at the opened port, if the socket is in an
active mode, the packets are delivered as messages to the controlling process:
{udp, Socket, IP, InPortNo, Packet} % Without ancillary data
{udp, Socket, IP, InPortNo, AncData, Packet} % With ancillary data
The message contains an AncData
field if any of the socket
options recvtos
,
recvtclass
or
recvttl
are active, otherwise it does not.
If the socket is not in an active mode, data can be retrieved through the
recv/2,3
calls. Notice that arriving UDP packets that are longer
than the receive buffer option specifies can be truncated without warning.
When a socket in {active, N}
mode (see inet:setopts/2
for details),
transitions to passive ({active, false}
) mode, the controlling process is
notified by a message of the following form:
{udp_passive, Socket}
IP
and InPortNo
define the address from which Packet
comes. Packet
is a
list of bytes if option list
is specified. Packet
is a binary if option
binary
is specified.
Default value for the receive buffer option is {recbuf, 8192}
.
If Port == 0
, the underlying OS assigns a free UDP port, use inet:port/1
to
retrieve it.
-spec recv(Socket, Length) -> {ok, RecvData} | {error, Reason} when Socket :: socket(), Length :: non_neg_integer(), RecvData :: {Address, Port, Packet} | {Address, Port, AncData, Packet}, Address :: inet:ip_address() | inet:returned_non_ip_address(), Port :: inet:port_number(), AncData :: inet:ancillary_data(), Packet :: string() | binary(), Reason :: not_owner | inet:posix().
Equivalent to recv/3
-spec recv(Socket, Length, Timeout) -> {ok, RecvData} | {error, Reason} when Socket :: socket(), Length :: non_neg_integer(), Timeout :: timeout(), RecvData :: {Address, Port, Packet} | {Address, Port, AncData, Packet}, Address :: inet:ip_address() | inet:returned_non_ip_address(), Port :: inet:port_number(), AncData :: inet:ancillary_data(), Packet :: string() | binary(), Reason :: not_owner | timeout | inet:posix().
Receives a packet from a socket in passive mode. Optional parameter Timeout
specifies a time-out in milliseconds. Defaults to infinity
.
If any of the socket options
recvtos
, recvtclass
or recvttl
are active, the RecvData
tuple
contains an AncData
field, otherwise it does not.
-spec send(Socket, Packet) -> ok | {error, Reason} when Socket :: socket(), Packet :: iodata(), Reason :: not_owner | inet:posix().
Sends a packet on a connected socket (see
connect/2
and
connect/3
).
-spec send(Socket, Destination, Packet) -> ok | {error, Reason} when Socket :: socket(), Destination :: {inet:ip_address(), inet:port_number()} | inet:family_address() | socket:sockaddr_in() | socket:sockaddr_in6(), Packet :: iodata(), Reason :: not_owner | inet:posix().
Sends a packet to the specified Destination
.
This function is equivalent to
send(Socket, Destination, [], Packet)
.
-spec send(Socket, Host, Port, Packet) -> ok | {error, Reason} when Socket :: socket(), Host :: inet:hostname() | inet:ip_address(), Port :: inet:port_number() | atom(), Packet :: iodata(), Reason :: not_owner | inet:posix(); (Socket, Destination, AncData, Packet) -> ok | {error, Reason} when Socket :: socket(), Destination :: {inet:ip_address(), inet:port_number()} | inet:family_address() | socket:sockaddr_in() | socket:sockaddr_in6(), AncData :: inet:ancillary_data(), Packet :: iodata(), Reason :: not_owner | inet:posix(); (Socket, Destination, PortZero, Packet) -> ok | {error, Reason} when Socket :: socket(), Destination :: {inet:ip_address(), inet:port_number()} | inet:family_address(), PortZero :: inet:port_number(), Packet :: iodata(), Reason :: not_owner | inet:posix().
Sends a packet to the specified Host
and Port
.
This clause is equivalent to send(Socket, Host, Port, [], Packet)
.
Sends a packet to the specified Destination
with ancillary data AncData
.
Note
The ancillary data
AncData
contains options that for this single message override the default options for the socket, an operation that may not be supported on all platforms, and if so return{error, einval}
. Using more than one of an ancillary data item type may also not be supported.AncData =:= []
is always supported.
Sends a packet to the specified Destination
. Since Destination
is complete,
PortZero
is redundant and has to be 0
.
This is a legacy clause mostly for Destination = {local, Binary}
where
PortZero
is superfluous. It is equivalent to
send(Socket, Destination, [], Packet)
, the
clause right above here.
-spec send(Socket, Host, Port, AncData, Packet) -> ok | {error, Reason} when Socket :: socket(), Host :: inet:hostname() | inet:ip_address() | inet:local_address(), Port :: inet:port_number() | atom(), AncData :: inet:ancillary_data(), Packet :: iodata(), Reason :: not_owner | inet:posix().
Sends a packet to the specified Host
and Port
, with ancillary data
AncData
.
Argument Host
can be a hostname or a socket address, and Port
can be a port
number or a service name atom. These are resolved into a Destination
and after
that this function is equivalent to
send(Socket, Destination, AncData, Packet)
, read
there about ancillary data.