UDP(4)

πŸ’Œ UDP(4) β€” the kernel interfaces manual, but make it cute

okay bestie, pull up a chair, we're talking about udp β€” Internet User Datagram Protocol. this is a page from the Kernel Interfaces Manual and it's genuinely iconic, no cap. πŸ’–


✨ NAME ✨

udp β€” Internet User Datagram Protocol πŸ¦‹


🌸 SYNOPSIS 🌸

the way you summon one is literally just:

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

int
socket(AF_INET, SOCK_DGRAM, 0);

look at her. three includes and a socket() call. lowkey elegant. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so. UDP is a simple, unreliable datagram protocol and her whole job is supporting the SOCK_DGRAM abstraction for the Internet protocol family. simple! unreliable! unbothered! πŸ’…

UDP sockets are connectionless, and they're normally used with the sendto(2) and recvfrom(2) calls β€” though the connect(2) call may also be used to fix the destination for future packets. and if you do that, you get to use recv(2) or read(2) and send(2) or write(2) system calls instead. main character flexibility, honestly. ✨

UDP address formats are identical to those used by TCP. in particular, UDP provides a port identifier on top of the normal Internet address format. and here's the tea, bestie:

note that the UDP port space is separate from the TCP port space β€” i.e., a UDP port may not be "connected" to a TCP port. πŸ™ˆ

also you can send broadcast packets (assuming the underlying network supports this, don't come for me) by using a reserved "broadcast address" β€” and that address is network interface dependent. iykyk. 🌈

options at the IP transport level may be used with UDP; go 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>. that's it. one value. she said pick one and i did. 🫢


πŸ’œ MIB VARIABLES πŸ’œ

the udp protocol implements a number of variables in the net.inet branch of the sysctl(3) MIB. slay. here's the whole roster, bestie, no skipping:

  • UDPCTL_CHECKSUM (udp.checksum) β€” enable udp checksums. enabled by default, as she should be. βœ…
  • UDPCTL_MAXDGRAM (udp.maxdgram) β€” maximum outgoing UDP datagram size. πŸ“
  • UDPCTL_RECVSPACE (udp.recvspace) β€” maximum space for incoming UDP datagrams. πŸ“₯
  • udp.log_in_vain β€” for all udp datagrams sent to ports on which there is no socket listening, log the connection attempt. disabled by default β€” it's giving "you're not on the guest list". 😭
  • udp.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) for the lore. πŸ•³οΈ

πŸ’€ ERRORS πŸ’€

a socket operation may fail with one of the following errors returned. brace yourself, this is the vibe check section:

  • [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're already taken!! 😭
  • [ENOTCONN] β€” when trying to send a datagram, but no destination address is specified, and the socket has not been connected. you cannot send a message with no address bestie, that's not how this works πŸ™ˆ
  • [ENOBUFS] β€” when the system runs out of memory for an internal data structure. not her fault, she's just full πŸ₯Ί
  • [EADDRINUSE] β€” when an attempt is made to create a socket with a port which has already been allocated. that port is TAKEN. find your own. πŸ’…
  • [EADDRNOTAVAIL] β€” when an attempt is made to create a socket with a network address for which no network interface exists. that address does not exist, she's imaginary, she's a ghost πŸ‘»

🌟 SEE ALSO 🌟

go read these next, they're all bangers: getsockopt(2), recv(2), send(2), socket(2), blackhole(4), inet(4), intro(4), ip(4), udplite(4). ✨


πŸ¦‹ HISTORY πŸ¦‹

the udp protocol appeared in 4.2BSD. an OG. a legend. a founding mother. πŸ‘‘


and that's the whole page bestie β€” this version of UDP(4) comes to you from Debian, dated February 6, 2017. πŸ’– thanks for reading, go forth and send some datagrams, unreliably and with confidence. periodt. ✨🫢

πŸ“¦ where this page comes from