BINDRESVPORT(3)

NAME
bindresvport, bindresvport_sa - bind a socket to a privileged IP port

SYNOPSIS
#include <sys/types.h>
#include <netinet/in.h>
int
bindresvport(int sd, struct sockaddr_in *sin);
int
bindresvport_sa(int sd, struct sockaddr *sa);

DESCRIPTION
bindresvport() and bindresvport_sa() are used to bind a socket descriptor to a privileged IP port, that is, a port number in the range 0-1023. sd is a socket descriptor that was returned by a call to socket(2).

Only root can bind to a privileged port; this call will fail for any other users.

When sin is not null, sin->sin_family must initialized to the address family of the socket, passed by sd. If the value of sin->sin_port is non-zero, bindresvport() attempts to use the specified port. If that fails, it chooses another privileged port number automatically.

It is legal to pass null pointer to sin. In this case, the caller cannot get the port number bindresvport() has picked.

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.

RETURN VALUES
bindresvport() returns 0 if it is successful, otherwise -1 is returned and errno set to reflect the cause of the error.

ERRORS
The bindresvport() function fails if:

[EBADF]
    sd is not a valid descriptor.

[ENOTSOCK]
    sd is not a socket.

[EADDRNOTAVAIL]
    The specified address is not available from the local machine.

[EADDRINUSE]
    The specified address is already in use.

[EINVAL]
    The socket is already bound to an address.

[EACCES]
    The requested address is protected, and the current user has inadequate permission to access it.

[EFAULT]
    The name parameter is not in a valid part of the user address space.

[ENOBUFS]
    Insufficient resources were available in the system to perform the operation.

[EPFNOSUPPORT]
    The protocol family has not been configured into the system, no implementation for it exists, or address family did not match between arguments.

SEE ALSO
bind(2), socket(2), rresvport(3), rresvport_af(3)