Blame docs/src/udp.rst

Packit b5b901
Packit b5b901
.. _udp:
Packit b5b901
Packit b5b901
:c:type:`uv_udp_t` --- UDP handle
Packit b5b901
=================================
Packit b5b901
Packit b5b901
UDP handles encapsulate UDP communication for both clients and servers.
Packit b5b901
Packit b5b901
Packit b5b901
Data types
Packit b5b901
----------
Packit b5b901
Packit b5b901
.. c:type:: uv_udp_t
Packit b5b901
Packit b5b901
    UDP handle type.
Packit b5b901
Packit b5b901
.. c:type:: uv_udp_send_t
Packit b5b901
Packit b5b901
    UDP send request type.
Packit b5b901
Packit b5b901
.. c:type:: uv_udp_flags
Packit b5b901
Packit b5b901
    Flags used in :c:func:`uv_udp_bind` and :c:type:`uv_udp_recv_cb`..
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        enum uv_udp_flags {
Packit b5b901
            /* Disables dual stack mode. */
Packit b5b901
            UV_UDP_IPV6ONLY = 1,
Packit b5b901
            /*
Packit b5b901
            * Indicates message was truncated because read buffer was too small. The
Packit b5b901
            * remainder was discarded by the OS. Used in uv_udp_recv_cb.
Packit b5b901
            */
Packit b5b901
            UV_UDP_PARTIAL = 2,
Packit b5b901
            /*
Packit b5b901
            * Indicates if SO_REUSEADDR will be set when binding the handle in
Packit b5b901
            * uv_udp_bind.
Packit b5b901
            * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
Packit b5b901
            * Unix platforms, it sets the SO_REUSEADDR flag. What that means is that
Packit b5b901
            * multiple threads or processes can bind to the same address without error
Packit b5b901
            * (provided they all set the flag) but only the last one to bind will receive
Packit b5b901
            * any traffic, in effect "stealing" the port from the previous listener.
Packit b5b901
            */
Packit Service e08953
            UV_UDP_REUSEADDR = 4,
Packit Service e08953
            /*
Packit Service e08953
             * Indicates that the message was received by recvmmsg, so the buffer provided
Packit Service e08953
             * must not be freed by the recv_cb callback.
Packit Service e08953
             */
Packit Service e08953
            UV_UDP_MMSG_CHUNK = 8,
Packit Service e08953
            /*
Packit Service e08953
            * Indicates that recvmmsg should be used, if available.
Packit Service e08953
            */
Packit Service e08953
            UV_UDP_RECVMMSG = 256
Packit b5b901
        };
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_udp_send_cb)(uv_udp_send_t* req, int status)
Packit b5b901
Packit b5b901
    Type definition for callback passed to :c:func:`uv_udp_send`, which is
Packit b5b901
    called after the data was sent.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_udp_recv_cb)(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags)
Packit b5b901
Packit b5b901
    Type definition for callback passed to :c:func:`uv_udp_recv_start`, which
Packit b5b901
    is called when the endpoint receives data.
Packit b5b901
Packit b5b901
    * `handle`: UDP handle
Packit b5b901
    * `nread`:  Number of bytes that have been received.
Packit Service e08953
      0 if there is no more data to read. Note that 0 may also mean that an
Packit Service e08953
      empty datagram was received (in this case `addr` is not NULL). < 0 if
Packit Service e08953
      a transmission error was detected.
Packit b5b901
    * `buf`: :c:type:`uv_buf_t` with the received data.
Packit b5b901
    * `addr`: ``struct sockaddr*`` containing the address of the sender.
Packit b5b901
      Can be NULL. Valid for the duration of the callback only.
Packit Service e08953
    * `flags`: One or more or'ed UV_UDP_* constants.
Packit Service e08953
Packit Service e08953
    The callee is responsible for freeing the buffer, libuv does not reuse it.
Packit Service e08953
    The buffer may be a null buffer (where `buf->base` == NULL and `buf->len` == 0)
Packit Service e08953
    on error.
Packit Service e08953
Packit Service e08953
    When using :man:`recvmmsg(2)`, chunks will have the `UV_UDP_MMSG_CHUNK` flag set,
Packit Service e08953
    those must not be freed. There will be a final callback with `nread` set to 0,
Packit Service e08953
    `addr` set to NULL and the buffer pointing at the initially allocated data with
Packit Service e08953
    the `UV_UDP_MMSG_CHUNK` flag cleared. This is a good chance for the callee to
Packit Service e08953
    free the provided buffer.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        The receive callback will be called with `nread` == 0 and `addr` == NULL when there is
Packit b5b901
        nothing to read, and with `nread` == 0 and `addr` != NULL when an empty UDP packet is
Packit b5b901
        received.
Packit b5b901
Packit b5b901
.. c:type:: uv_membership
Packit b5b901
Packit b5b901
    Membership type for a multicast address.
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        typedef enum {
Packit b5b901
            UV_LEAVE_GROUP = 0,
Packit b5b901
            UV_JOIN_GROUP
Packit b5b901
        } uv_membership;
Packit b5b901
Packit b5b901
Packit b5b901
Public members
Packit b5b901
^^^^^^^^^^^^^^
Packit b5b901
Packit b5b901
.. c:member:: size_t uv_udp_t.send_queue_size
Packit b5b901
Packit b5b901
    Number of bytes queued for sending. This field strictly shows how much
Packit b5b901
    information is currently queued.
Packit b5b901
Packit b5b901
.. c:member:: size_t uv_udp_t.send_queue_count
Packit b5b901
Packit b5b901
    Number of send requests currently in the queue awaiting to be processed.
Packit b5b901
Packit b5b901
.. c:member:: uv_udp_t* uv_udp_send_t.handle
Packit b5b901
Packit b5b901
    UDP handle where this send request is taking place.
Packit b5b901
Packit b5b901
.. seealso:: The :c:type:`uv_handle_t` members also apply.
Packit b5b901
Packit b5b901
Packit b5b901
API
Packit b5b901
---
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle)
Packit b5b901
Packit b5b901
    Initialize a new UDP handle. The actual socket is created lazily.
Packit b5b901
    Returns 0 on success.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_init_ex(uv_loop_t* loop, uv_udp_t* handle, unsigned int flags)
Packit b5b901
Packit Service e08953
    Initialize the handle with the specified flags. The lower 8 bits of the `flags`
Packit Service e08953
    parameter are used as the socket domain. A socket will be created for the given domain.
Packit Service e08953
    If the specified domain is ``AF_UNSPEC`` no socket is created, just like :c:func:`uv_udp_init`.
Packit Service e08953
Packit Service e08953
    The remaining bits can be used to set one of these flags:
Packit Service e08953
Packit Service e08953
    * `UV_UDP_RECVMMSG`: if set, and the platform supports it, :man:`recvmmsg(2)` will
Packit Service e08953
      be used.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.7.0
Packit Service e08953
    .. versionchanged:: 1.37.0 added the `UV_UDP_RECVMMSG` flag.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock)
Packit b5b901
Packit b5b901
    Opens an existing file descriptor or Windows SOCKET as a UDP handle.
Packit b5b901
Packit b5b901
    Unix only:
Packit b5b901
    The only requirement of the `sock` argument is that it follows the datagram
Packit b5b901
    contract (works in unconnected mode, supports sendmsg()/recvmsg(), etc).
Packit b5b901
    In other words, other datagram-type sockets like raw sockets or netlink
Packit b5b901
    sockets can also be passed to this function.
Packit b5b901
Packit b5b901
    .. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        The passed file descriptor or SOCKET is not checked for its type, but
Packit b5b901
        it's required that it represents a valid datagram socket.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_bind(uv_udp_t* handle, const struct sockaddr* addr, unsigned int flags)
Packit b5b901
Packit b5b901
    Bind the UDP handle to an IP address and port.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param addr: `struct sockaddr_in` or `struct sockaddr_in6`
Packit b5b901
        with the address and port to bind to.
Packit b5b901
Packit b5b901
    :param flags: Indicate how the socket will be bound,
Packit b5b901
        ``UV_UDP_IPV6ONLY`` and ``UV_UDP_REUSEADDR`` are supported.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit Service e08953
.. c:function:: int uv_udp_connect(uv_udp_t* handle, const struct sockaddr* addr)
Packit Service e08953
Packit Service e08953
    Associate the UDP handle to a remote address and port, so every
Packit Service e08953
    message sent by this handle is automatically sent to that destination.
Packit Service e08953
    Calling this function with a `NULL` `addr` disconnects the handle.
Packit Service e08953
    Trying to call `uv_udp_connect()` on an already connected handle will result
Packit Service e08953
    in an `UV_EISCONN` error. Trying to disconnect a handle that is not
Packit Service e08953
    connected will return an `UV_ENOTCONN` error.
Packit Service e08953
Packit Service e08953
    :param handle: UDP handle. Should have been initialized with
Packit Service e08953
        :c:func:`uv_udp_init`.
Packit Service e08953
Packit Service e08953
    :param addr: `struct sockaddr_in` or `struct sockaddr_in6`
Packit Service e08953
        with the address and port to associate to.
Packit Service e08953
Packit Service e08953
    :returns: 0 on success, or an error code < 0 on failure.
Packit Service e08953
Packit Service e08953
    .. versionadded:: 1.27.0
Packit Service e08953
Packit Service e08953
.. c:function:: int uv_udp_getpeername(const uv_udp_t* handle, struct sockaddr* name, int* namelen)
Packit Service e08953
Packit Service e08953
    Get the remote IP and port of the UDP handle on connected UDP handles.
Packit Service e08953
    On unconnected handles, it returns `UV_ENOTCONN`.
Packit Service e08953
Packit Service e08953
    :param handle: UDP handle. Should have been initialized with
Packit Service e08953
        :c:func:`uv_udp_init` and bound.
Packit Service e08953
Packit Service e08953
    :param name: Pointer to the structure to be filled with the address data.
Packit Service e08953
        In order to support IPv4 and IPv6 `struct sockaddr_storage` should be
Packit Service e08953
        used.
Packit Service e08953
Packit Service e08953
    :param namelen: On input it indicates the data of the `name` field. On
Packit Service e08953
        output it indicates how much of it was filled.
Packit Service e08953
Packit Service e08953
    :returns: 0 on success, or an error code < 0 on failure
Packit Service e08953
Packit Service e08953
    .. versionadded:: 1.27.0
Packit Service e08953
Packit b5b901
.. c:function:: int uv_udp_getsockname(const uv_udp_t* handle, struct sockaddr* name, int* namelen)
Packit b5b901
Packit b5b901
    Get the local IP and port of the UDP handle.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init` and bound.
Packit b5b901
Packit b5b901
    :param name: Pointer to the structure to be filled with the address data.
Packit b5b901
        In order to support IPv4 and IPv6 `struct sockaddr_storage` should be
Packit b5b901
        used.
Packit b5b901
Packit b5b901
    :param namelen: On input it indicates the data of the `name` field. On
Packit b5b901
        output it indicates how much of it was filled.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr, const char* interface_addr, uv_membership membership)
Packit b5b901
Packit b5b901
    Set membership for a multicast address
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param multicast_addr: Multicast address to set membership for.
Packit b5b901
Packit b5b901
    :param interface_addr: Interface address.
Packit b5b901
Packit b5b901
    :param membership: Should be ``UV_JOIN_GROUP`` or ``UV_LEAVE_GROUP``.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit Service e08953
.. c:function:: int uv_udp_set_source_membership(uv_udp_t* handle, const char* multicast_addr, const char* interface_addr, const char* source_addr, uv_membership membership)
Packit Service e08953
Packit Service e08953
    Set membership for a source-specific multicast group.
Packit Service e08953
Packit Service e08953
    :param handle: UDP handle. Should have been initialized with
Packit Service e08953
        :c:func:`uv_udp_init`.
Packit Service e08953
Packit Service e08953
    :param multicast_addr: Multicast address to set membership for.
Packit Service e08953
Packit Service e08953
    :param interface_addr: Interface address.
Packit Service e08953
Packit Service e08953
    :param source_addr: Source address.
Packit Service e08953
Packit Service e08953
    :param membership: Should be ``UV_JOIN_GROUP`` or ``UV_LEAVE_GROUP``.
Packit Service e08953
Packit Service e08953
    :returns: 0 on success, or an error code < 0 on failure.
Packit Service e08953
Packit Service e08953
    .. versionadded:: 1.32.0
Packit Service e08953
Packit b5b901
.. c:function:: int uv_udp_set_multicast_loop(uv_udp_t* handle, int on)
Packit b5b901
Packit b5b901
    Set IP multicast loop flag. Makes multicast packets loop back to
Packit b5b901
    local sockets.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param on: 1 for on, 0 for off.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl)
Packit b5b901
Packit b5b901
    Set the multicast ttl.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param ttl: 1 through 255.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_set_multicast_interface(uv_udp_t* handle, const char* interface_addr)
Packit b5b901
Packit b5b901
    Set the multicast interface to send or receive data on.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param interface_addr: interface address.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_set_broadcast(uv_udp_t* handle, int on)
Packit b5b901
Packit b5b901
    Set broadcast on or off.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param on: 1 for on, 0 for off.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_set_ttl(uv_udp_t* handle, int ttl)
Packit b5b901
Packit b5b901
    Set the time to live.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param ttl: 1 through 255.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_udp_send(uv_udp_send_t* req, uv_udp_t* handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr, uv_udp_send_cb send_cb)
Packit b5b901
Packit b5b901
    Send data over the UDP socket. If the socket has not previously been bound
Packit b5b901
    with :c:func:`uv_udp_bind` it will be bound to 0.0.0.0
Packit b5b901
    (the "all interfaces" IPv4 address) and a random port number.
Packit b5b901
Packit b5b901
    On Windows if the `addr` is initialized to point to an unspecified address
Packit b5b901
    (``0.0.0.0`` or ``::``) it will be changed to point to ``localhost``.
Packit b5b901
    This is done to match the behavior of Linux systems.
Packit b5b901
Packit Service e08953
    For connected UDP handles, `addr` must be set to `NULL`, otherwise it will
Packit Service e08953
    return `UV_EISCONN` error.
Packit Service e08953
Packit Service e08953
    For connectionless UDP handles, `addr` cannot be `NULL`, otherwise it will
Packit Service e08953
    return `UV_EDESTADDRREQ` error.
Packit Service e08953
Packit b5b901
    :param req: UDP request handle. Need not be initialized.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param bufs: List of buffers to send.
Packit b5b901
Packit b5b901
    :param nbufs: Number of buffers in `bufs`.
Packit b5b901
Packit b5b901
    :param addr: `struct sockaddr_in` or `struct sockaddr_in6` with the
Packit b5b901
        address and port of the remote peer.
Packit b5b901
Packit b5b901
    :param send_cb: Callback to invoke when the data has been sent out.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
    .. versionchanged:: 1.19.0 added ``0.0.0.0`` and ``::`` to ``localhost``
Packit b5b901
        mapping
Packit b5b901
Packit Service e08953
    .. versionchanged:: 1.27.0 added support for connected sockets
Packit Service e08953
Packit b5b901
.. c:function:: int uv_udp_try_send(uv_udp_t* handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr)
Packit b5b901
Packit b5b901
    Same as :c:func:`uv_udp_send`, but won't queue a send request if it can't
Packit b5b901
    be completed immediately.
Packit b5b901
Packit Service e08953
    For connected UDP handles, `addr` must be set to `NULL`, otherwise it will
Packit Service e08953
    return `UV_EISCONN` error.
Packit Service e08953
Packit Service e08953
    For connectionless UDP handles, `addr` cannot be `NULL`, otherwise it will
Packit Service e08953
    return `UV_EDESTADDRREQ` error.
Packit Service e08953
Packit b5b901
    :returns: >= 0: number of bytes sent (it matches the given buffer size).
Packit b5b901
        < 0: negative error code (``UV_EAGAIN`` is returned when the message
Packit b5b901
        can't be sent immediately).
Packit b5b901
Packit Service e08953
    .. versionchanged:: 1.27.0 added support for connected sockets
Packit Service e08953
Packit b5b901
.. c:function:: int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
Packit b5b901
Packit b5b901
    Prepare for receiving data. If the socket has not previously been bound
Packit b5b901
    with :c:func:`uv_udp_bind` it is bound to 0.0.0.0 (the "all interfaces"
Packit b5b901
    IPv4 address) and a random port number.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :param alloc_cb: Callback to invoke when temporary storage is needed.
Packit b5b901
Packit b5b901
    :param recv_cb: Callback to invoke with received data.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit Service e08953
    .. versionchanged:: 1.35.0 added support for :man:`recvmmsg(2)` on supported platforms).
Packit Service e08953
                        The use of this feature requires a buffer larger than
Packit Service e08953
                        2 * 64KB to be passed to `alloc_cb`.
Packit Service e08953
    .. versionchanged:: 1.37.0 :man:`recvmmsg(2)` support is no longer enabled implicitly,
Packit Service e08953
                        it must be explicitly requested by passing the `UV_UDP_RECVMMSG` flag to
Packit Service e08953
                        :c:func:`uv_udp_init_ex`.
Packit Service e08953
Packit b5b901
.. c:function:: int uv_udp_recv_stop(uv_udp_t* handle)
Packit b5b901
Packit b5b901
    Stop listening for incoming datagrams.
Packit b5b901
Packit b5b901
    :param handle: UDP handle. Should have been initialized with
Packit b5b901
        :c:func:`uv_udp_init`.
Packit b5b901
Packit b5b901
    :returns: 0 on success, or an error code < 0 on failure.
Packit b5b901
Packit b5b901
.. c:function:: size_t uv_udp_get_send_queue_size(const uv_udp_t* handle)
Packit b5b901
Packit b5b901
    Returns `handle->send_queue_size`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. c:function:: size_t uv_udp_get_send_queue_count(const uv_udp_t* handle)
Packit b5b901
Packit b5b901
    Returns `handle->send_queue_count`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. seealso:: The :c:type:`uv_handle_t` API functions also apply.