<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="udp-socket-listen">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>udp-socket-listen</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="manual">user commands</refmiscinfo>
<refmiscinfo class="source">nosh</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv>
<refname>udp-socket-listen</refname>
<refpurpose>create a socket listening for incoming UDP datagrams and chain</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>udp-socket-listen</command>
<arg choice='opt'>--no-reuse-address</arg>
<arg choice='opt'>--reuse-port</arg>
<arg choice='opt'>--bind-to-any</arg>
<arg choice='opt'>--numeric-host</arg>
<arg choice='opt'>--numeric-service</arg>
<arg choice='opt'>--check-interfaces</arg>
<arg choice='opt'>--combine4and6</arg>
<arg choice='opt'>--upstart-compatibility</arg>
<arg choice='opt'>--systemd-compatibility</arg>
<arg choice='req'><replaceable>host</replaceable></arg>
<arg choice='req'><replaceable>service</replaceable></arg>
<arg choice='req'><replaceable>next-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>udp-socket-listen</command> is a chain-loading utility that opens a UDP socket bound to <replaceable>host</replaceable> and <replaceable>service</replaceable>, sets the socket to file descriptor 3+<replaceable>n</replaceable> (where <replaceable>n</replaceable> is the number of already-opened listening file descriptors), closing whatever that descriptor was, and then chain loads to <replaceable>next-prog</replaceable> with the <citerefentry><refentrytitle>execvp</refentrytitle><manvolnum>3</manvolnum></citerefentry> function.
</para>

<para>
<replaceable>next-prog</replaceable> may contain its own command line options, which <command>udp-socket-listen</command> will ignore.
</para>

<para>
For compatibility with d&#xe6;mons that expect to be run under <citerefentry><refentrytitle>inetd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, inheriting the listening socket as their standard input, simply use <command>fdmove</command> <arg choice="plain">0</arg> <arg choice="plain">3</arg> in <replaceable>next-prog</replaceable>.
See <citerefentry><refentrytitle>fdmove</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>

<para>
If the <arg choice='plain'>--numeric-host</arg> command line option is used, then no name lookup is done on <replaceable>host</replaceable> and it must be (a human readable form of) an IP address.
If the <arg choice='plain'>--numeric-service</arg> command line option is used, then no name lookup is done on <replaceable>service</replaceable> and it must be a port number.
Otherwise, <replaceable>host</replaceable> is looked up using the system name resolution facility, with the first IP address found being used, and <replaceable>service</replaceable> can be an alphanumeric service name.
</para>

<para>
The <arg choice='plain'>--reuse-port</arg> option sets a flag that allows the same IP address and port combination to be used by multiple UDP listeners, as long as they all use the flag.
The <arg choice='plain'>--check-interfaces</arg> option prevents the use of any IPv4 addresses if there are no IPv4 addresses on any network interface, and the use of any IPv6 addresses if there are no IPv6 addresses.
This isn't particularly useful on a dynamically configured system where network interface IP addresses can come and go.
Conversely, the <arg choice='plain'>--bind-to-any</arg> option is quite useful on such a system, as it allows binding to any IPV4 or IPV6 address, even one that is not on any network interface.
</para>

<para>
The <arg choice='plain'>--upstart-compatibility</arg> option causes <command>udp-socket-listen</command> to set the <envar>UPSTART_FDS</envar> environment variable to 3, and the <envar>UPSTART_EVENTS</envar> environment variable to <literal>socket</literal>.
This is for compatibility with d&#xe6;mons that expect to be run under <citerefentry><refentrytitle>upstart</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>

<para>
The <arg choice='plain'>--systemd-compatibility</arg> option is for compatibility with d&#xe6;mons that expect to be run under <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
It causes <command>udp-socket-listen</command> to set the <envar>LISTEN_FDS</envar> environment variable to the number of listening file descriptors, and the <envar>LISTEN_PID</envar> environment variable to its own process ID.
If the command is started up with the <envar>LISTEN_FDS</envar> and the <envar>LISTEN_PID</envar> environment variables already appropriately set, the number of listening file descriptors is taken from them and increased by one, otherwise the number of file descriptors is 1.
Thus a list of listening file descriptors can be built up with multiple commands, as long as they all use the <arg choice='plain'>--systemd-compatibility</arg> option.
</para>

<para>
On systems that support such, the <arg choice='plain'>--combine4and6</arg> option allows a listener to communicate with both IPv4 and IPv6 clients.
(<replaceable>host</replaceable> must denote an IPv6 address for this to work.)
On other systems, and also the default if <arg choice='plain'>--combine4and6</arg> is not used, one must run an IPv4 listener for IPv4 clients and an IPv6 listener for IPv6 clients.
See RFCs 3493 and 4038 for more details.
</para>

</refsection><refsection><title>Protocols</title>

<para>
<command>udp-socket-listen</command> is fully IPv6 capable, albeit that by the nature of IP versions it is not possible on some operating system kernels to handle both IPv4 and IPv6 with a single socket.
To listen on an IPv4 address and an IPv6 address on such kernels, one must have two <command>udp-socket-listen</command> processes.
</para>

</refsection><refsection><title>Usage</title>

<para>
<command>udp-socket-listen</command> can be used as a simple d&#xe6;mon monitored by
<citerefentry><refentrytitle>service-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
<command>udp-socket-listen</command> overlaps the functionality of
<citerefentry><refentrytitle>inetd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
and
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
(in the systemd-recommended configuration of <code>Accept=false</code> sockets).
</para>

<para>
To change the process' UID and GID after opening the socket, simply chain through <citerefentry><refentrytitle>setuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>

</refsection>

<refsection><title>See also</title>
<variablelist>
	<varlistentry>
		<term><citerefentry><refentrytitle>tcp-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>
		<term><citerefentry><refentrytitle>local-datagram-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>
		<term><citerefentry><refentrytitle>local-stream-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>
		<term><citerefentry><refentrytitle>local-seqpacket-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>
		<term><citerefentry><refentrytitle>netlink-datagram-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>
		<listitem>
			<para>
				listeners for other protocols
			</para>
		</listitem>
	</varlistentry>
</variablelist>

</refsection><refsection><title>Author</title>
<para><author><personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname></author></para>
</refsection>

</refentry>
