UDP(4)

πŸ“¦
version 15.1 Β· 4 versions
, in freebsd 15.1, nixpkgs unstable

πŸ’Œ UDP(4) β€” like, the Internet User Datagram Protocol, bestie πŸ’Œ

okay so this is the udp page from the Kernel Interfaces Manual β€” section 4, kernel interfaces, the whole vibe. sit down, grab a strawberry drink πŸ“, we're learning today.


✨ NAME ✨

udp β€” the Internet User Datagram Protocol. yes THAT udp. the "fire and forget" one. she's iconic, she's unreliable, and we love her anyway πŸ’…


🫢 SYNOPSIS 🫢

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int
socket(AF_INET, SOCK_DGRAM, 0);

literally that's it. that's the whole entrance. so clean, so demure ✨


🌸 DESCRIPTION 🌸

okay so first thing: UDP is a simple, unreliable datagram protocol, no cap. that's not me being mean, that's the actual spec, it's her whole brand 😭 she exists to support the SOCK_DGRAM abstraction for the Internet protocol family.

UDP sockets are connectionless β€” like, she doesn't do commitment β€” and normally you use them with the sendto(2) and recvfrom(2) calls. but bestie, the connect(2) call can also be used to fix the destination for future packets. and in THAT case, you get to use the recv(2) or read(2) and send(2) or write(2) system calls. slay, options πŸ’…

Now, the address formats: UDP addresses are identical to those used by TCP. specifically, UDP gives you a port identifier on top of the normal Internet address format. and listen β€” the UDP port space is separate from the TCP port space. i.e., a UDP port may not be β€œconnected” to a TCP port. two different girls, two different worlds πŸ’€

Also! broadcast packets may be sent (assuming the underlying network supports this) by using a reserved β€œbroadcast address” β€” and that address is network interface dependent. so like, it's not one universal broadcast address, it's giving situationship with your NIC πŸ™ˆ

Options at the IP transport level may be used with UDP; see ip(4). and the UDP_ENCAP socket option may be used at the IPPROTO_UDP level to encapsulate ESP packets in UDP. only ONE value is supported for this option: UDP_ENCAP_ESPINUDP from RFC 3948, defined in <netinet/udp.h>. one value. one. she's not flexible on this and honestly? respect πŸ‘‘


πŸŽ€ FIB support πŸŽ€

UDP sockets are FIB-aware 🌈 they inherit the FIB of the process which created the socket. main character energy, inheriting your whole situation from your creator.

By default, a UDP socket bound to an address can receive datagrams originating from any FIB. but if the net.inet.udp.bind_all_fibs tunable is set to 0, all UDP sockets will receive only datagrams originating from the same FIB as the socket. and in this mode, multiple sockets can be bound to the same address β€” so long as each socket belongs to a different FIB β€” similar to the behavior of the SO_REUSEPORT option. so yes, sharing is caring, but only across FIBs, iykyk 🫢


⭐ MIB (sysctl) Variables ⭐

the udp protocol implements a number of variables in the net.inet.udp branch of the sysctl(3) MIB, which can also be read or modified with sysctl(8). she's got a whole little closet of settings πŸŽ€

  • blackhole β€” when a datagram is received on a port where there is no socket listening, do not return an ICMP port unreachable message. (Disabled by default. See blackhole(4).) literally ghosting on purpose πŸ’€
  • checksum β€” enable UDP checksums (enabled by default). she's checking. always checking. πŸ”₯
  • log_in_vain β€” for all UDP datagrams, to ports on which there is no socket listening, log the connection attempt (disabled by default).
  • maxdgram β€” Maximum outgoing UDP datagram size. big girls only πŸ’…
  • recvspace β€” Maximum space for incoming UDP datagrams.

πŸ’€ ERRORS πŸ’€

A socket operation may fail with one of the following errors returned β€” yes, she has a whole roster of ways to say "no":

  • [EISCONN] β€” when trying to establish a connection on a socket which already has one, or when trying to send a datagram with the destination address specified and the socket is already connected. girl, you ARE connected. sit DOWN 😭
  • [ENOTCONN] β€” when trying to send a datagram, but no destination address is specified, and the socket has not been connected. she's lost. she has no address. she is simply vibing into the void πŸ¦‹
  • [ENOBUFS] β€” when the system runs out of memory for an internal data structure. the system is full bestie, there's no room πŸ’€
  • [EADDRINUSE] β€” when an attempt is made to create a socket with a port which has already been allocated. taken. occupied. not yours. πŸ™ˆ
  • [EADDRNOTAVAIL] β€” when an attempt is made to create a socket with a network address for which no network interface exists. that address is not real, she made it up, it's giving delusion ✨

πŸ¦‹ SEE ALSO πŸ¦‹

getsockopt(2), recv(2), send(2), socket(2), blackhole(4), inet(4), intro(4), ip(4), udplite(4)

β€” the whole friend group, go say hi πŸ’–


πŸ‘‘ HISTORY πŸ‘‘

The udp protocol appeared in 4.2BSD. an OG. a legend. she was there and she's still here. periodt πŸ’…


real talk though β€” remember she's unreliable. that's not a flaw, that's the design. if you need someone who texts back, that's a different protocol. don't come for her 🫢

okay that's the whole page, mwah, go write some datagrams πŸ’œβœ¨πŸŽ€

signing off from the Debian edition of this page, dated January 20, 2025 β€” love u, mean it, see u in the next manpage πŸ¦‹πŸ’–

πŸ“¦ where this page comes from