BINDRESVPORT(3)

NAME
bindresvport, bindresvport_sa

SYNOPSIS
#include <rpc/rpc.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.

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 be initialized to the address family of the socket, passed by sd. If the value of sin->sin_port is non-zero bindresvport() will attempt to use that specific port. If it fails, it chooses another privileged port 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() and bindresvport_sa() return 0 if they are successful, otherwise -1 is returned and errno set to reflect the cause of the error.

ERRORS
The bindresvport() and bindresvport_sa() functions fail 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, or the socket family and the family of specified address mismatch.

[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)