bindresvport() and
bindresvport_sa() are used to bind a socket descriptor to a reserved privileged IP port, that is, a port number in the range 0-1023. The routine returns 0 if it is successful, otherwise -1 is returned and
errno set to reflect the cause of the error.
If
sin is a pointer to a
struct sockaddr_in then the appropriate fields in the structure should be defined. Note that
sin->sin_family must be initialized to the address family of the socket, passed by
sd. If
sin->sin_port is ‘0' then a port (in the range 600-1023) will be chosen, and if
bind(2) is successful, the
sin->sin_port will be updated to contain the allocated port.
If
sin is the
NULL pointer, a port will be allocated (as above). However, there is no way for
bindresvport() to return the allocated port in this case.
getsockname(2) can be used to determine the assigned port.
Only root can bind to a privileged port; this call will fail for any other users.
Function prototype of
bindresvport() is biased to
AF_INET socket.
bindresvport_sa() acts exactly the same, with more neutral function prototype. Note that both functions behave exactly the same, and both support
AF_INET6 sockets as well as
AF_INET sockets.