BINDRESVPORT(3)

πŸ’– BINDRESVPORT(3) β€” Library Functions Manual πŸ’–

okay bestie so we're in section 3 of the library functions manual, and today's main character is bindresvport plus its equally iconic sibling bindresvport_sa. pull up a chair, we're binding sockets to privileged ports and it's giving root access energy ✨


✨ NAME ✨

bindresvport, bindresvport_sa β€” bind a socket to a privileged IP port πŸŽ€


✨ SYNOPSIS ✨

#include <sys/types.h>
#include <rpc/rpc.h>

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

int
bindresvport_sa(int sd, struct sockaddr *sa);

🌸 DESCRIPTION 🌸

so basically, bindresvport() and bindresvport_sa() are the girlies you call when you wanna bind a socket descriptor to a privileged IP port β€” and by privileged we mean a port number in the range 0-1023. yeah. the fancy zone. πŸ‘‘

if sin is a pointer to a struct sockaddr_in, then bestie, you gotta define the appropriate fields in that structure yourself. and heads up: sin->sin_family must be initialized to the address family of the socket that's being passed in through sd. no skipping that step, it's not optional, iykyk.

if sin->sin_port is '0', then an anonymous port will be chosen β€” specifically one in the range 600-1023 β€” and if bind(2) succeeds, sin->sin_port gets updated to hold the allocated port. cute, right? it tells you what it picked. 🫢

if sin is the NULL pointer, an anonymous port gets allocated too (same deal as above). but β€” and this is the sad part 😭 β€” there is no way for bindresvport() to return that allocated port back to you in this case. it's gone. a mystery. we don't know her.

also only root can bind to a privileged port. literally just root. this call will fail for every other user, no exceptions, no cap.

now, the function prototype of bindresvport() is biased toward AF_INET sockets. the bindresvport_sa() function acts exactly the same, it just has a more neutral function prototype. and like β€” note that both functions behave exactly the same, and both support AF_INET6 sockets as well as AF_INET sockets. equal opportunity socket binding. periodt. 🌈


πŸ’… RETURN VALUES πŸ’…

the bindresvport() function returns the value 0 if it's successful. otherwise, the value -1 is returned and the global variable errno is set to indicate the error. so like, check your errno, bestie, she's trying to tell you something.


πŸ’€ ERRORS πŸ’€

  • [EPFNOSUPPORT] β€” if a second argument was supplied, and the address family did not match between the arguments. mismatched vibes. rejected. πŸ™ˆ

the bindresvport() function may also fail and set errno for any of the errors specified for the calls bind(2), getsockopt(2), or setsockopt(2). so if it's acting up, go read those pages too, they're the supporting cast.


πŸ¦‹ AVAILABILITY πŸ¦‹

the bindresvport() function is part of libtirpc. that's where she lives. 🏠


⭐ SEE ALSO ⭐


and that's the whole page, bestie!! this lil masterpiece comes to you courtesy of Debian, dated November 22, 1987 β€” yes, 1987, she's an elder and she's still slaying πŸ’– go bind something privileged (if you're root, otherwise, respectfully, no) πŸŽ€βœ¨

πŸ“¦ where this page comes from