bindresvport(3)

✨ bindresvport ✨

okay bestie, we're looking at bindresvport(3) from the Library Functions Manual and honestly? it's giving "tiny function, big privileges" πŸ’…


πŸŽ€ NAME πŸŽ€

bindresvport β€” bind a socket to a privileged IP port πŸ‘‘


πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) 🫢


πŸ¦‹ SYNOPSIS πŸ¦‹

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

int bindresvport(int sockfd, struct sockaddr_in *sin);

πŸ’– DESCRIPTION πŸ’–

so bindresvport() is here to bind the socket that's referenced by the file descriptor sockfd to a privileged anonymous IP port β€” and by that we mean a port number that's just... arbitrarily selected from the range 512 to 1023. she picks for you, you don't pick for her, periodt ✨

if the bind(2) that bindresvport() performs actually goes through, and sin is not NULL, then sin->sin_port hands you back the port number that was actually allocated. slay πŸŽ€

now, sin can be NULL, in which case sin->sin_family is implicitly taken to be AF_INET. but lowkey there's a catch: in that case, bindresvport() has literally no way to return the port number actually allocated 😭. (you can still get that info later using getsockname(2), so it's not the end of the world bestie)


🌸 RETURN VALUE 🌸

  • she returns 0 on success πŸ’–
  • otherwise -1 is returned, and errno is set to indicate the error πŸ™ˆ

πŸ’€ ERRORS πŸ’€

bindresvport() can fail for any of the same reasons as bind(2). on top of that, these errors can also happen:

  • EACCES β€” the calling process was not privileged. (on Linux specifically: the calling process did not have the CAP_NET_BIND_SERVICE capability in the user namespace governing its network namespace.) not me getting denied at the door πŸ’…
  • EADDRINUSE β€” all privileged ports are in use. every single one. she's booked, fully, no vacancy 😭
  • EAFNOSUPPORT (EPFNOSUPPORT in glibc 2.7 and earlier) β€” sin is not NULL and sin->sin_family is not AF_INET. wrong family, we don't serve that here 🚫

🌈 ATTRIBUTES 🌈

for an explanation of the terms used in this section, see attributes(7). here's the vibe check:

  • Interface: bindresvport()
  • Attribute: Thread safety
  • Value: glibc >= 2.17: MT-Safe; glibc < 2.17: MT-Unsafe

and the lore behind that: the bindresvport() function uses a static variable that was not protected by a lock before glibc 2.17, which made the function MT-Unsafe. she was messy before the glow-up, ngl πŸ’€


⭐ VERSIONS ⭐

present on the BSDs, Solaris, and many other systems. she gets around, iconic behavior πŸ‘‘


🧚 NOTES 🧚

unlike some bindresvport() implementations, the glibc implementation ignores any value that the caller supplies in sin->sin_port. like... she didn't ask, she doesn't care, she chooses 😌


πŸ’œ STANDARDS πŸ’œ

BSD.


πŸ”₯ SEE ALSO πŸ”₯

bind(2), getsockname(2)


fr fr, that's the whole page. go bind something privileged, but stay safe out there πŸ’…βœ¨

signing off with the receipts: this is Linux man-pages 6.20, dated 2026-02-08, and you were reading bindresvport(3) πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms