bindresvport -- bind socket to privileged IP port


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

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


If the process has appropriate privilege, the bindresvport() function shall bind a socket to an anonymous privileged IP port, that is, arbitrarily selected from the range 512 through 1023.

If the bind is successful and sin is not NULL, and the port number bound to is returned in the sin_port member of sin. Any caller-supplied value of sin_port is ignored.

If sin is NULL, the address family is taken to be AF_INET and an available privileged port is bound to. Since there is no sockaddr_in structure, the port number chosen cannot be returned. The getsockname() may be used to query for this information.

Return Value

On success, 0 is returned. On error, -1 is returned and errno is set to indicate the error.


bindresvport() may fail in the same way as bind() in POSIX 1003.1-2008 (ISO/IEC 9945-2009). The following additional or differing failures may occur:


All privileged ports are in use.


The specified address is not a valid address for the address family of the specified socket, or the address family is not supported.


The same meaning as EAFNOSUPPORT. Some older implementations may return this error instead.

Note: At this time, only AF_INET is supported. Applications should be prepared for either the EAFNOSUPPORT or EPFNOSUPPORT error to be indicated.