1.\" @(#)bindresvport.3n 2.2 88/08/02 4.0 RPCSRC; from 1.7 88/03/14 SMI 2.\" $NetBSD: bindresvport.3,v 1.14 2017/07/03 21:32:49 wiz Exp $ 3.\" 4.Dd January 27, 2007 5.Dt BINDRESVPORT 3 6.Os 7.Sh NAME 8.Nm bindresvport , 9.Nm bindresvport_sa 10.Nd bind a socket to a reserved privileged IP port 11.Sh LIBRARY 12.Lb libc 13.Sh SYNOPSIS 14.In sys/types.h 15.In rpc/rpc.h 16.Ft int 17.Fn bindresvport "int sd" "struct sockaddr_in *sin" 18.Ft int 19.Fn bindresvport_sa "int sd" "struct sockaddr *sa" 20.Sh DESCRIPTION 21.Fn bindresvport 22and 23.Fn bindresvport_sa 24are used to bind a socket descriptor to a reserved privileged 25.Tn IP 26port, that is, a 27port number in the range 0-1023. 28The routine returns 0 if it is successful, 29otherwise -1 is returned and 30.Va errno 31set to reflect the cause of the error. 32.Pp 33If 34.Fa sin 35is a pointer to a 36.Ft "struct sockaddr_in" 37then the appropriate fields in the structure should be defined. 38Note that 39.Fa sin->sin_family 40must be initialized to the address family of the socket, passed by 41.Fa sd . 42If 43.Fa sin->sin_port 44is 45.Sq 0 46then a port (in the range 600-1023) will be 47chosen, and if 48.Xr bind 2 49is successful, the 50.Fa sin->sin_port 51will be updated to contain the allocated port. 52.Pp 53If 54.Fa sin 55is the 56.Dv NULL 57pointer, 58a port will be allocated (as above). 59However, there is no way for 60.Fn bindresvport 61to return the allocated port in this case. 62.Xr getsockname 2 63can be used to determine the assigned port. 64.Pp 65Only root can bind to a privileged port; this call will fail for any 66other users. 67.Pp 68Function prototype of 69.Fn bindresvport 70is biased to 71.Dv AF_INET 72socket. 73.Fn bindresvport_sa 74acts exactly the same, with more neutral function prototype. 75Note that both functions behave exactly the same, and 76both support 77.Dv AF_INET6 78sockets as well as 79.Dv AF_INET 80sockets. 81.Sh RETURN VALUES 82If the bind is successful, a 0 value is returned. 83A return value of -1 indicates an error, which is 84further specified in the global 85.Va errno . 86.Sh ERRORS 87.Bl -tag -width Er 88.It Bq Er EPFNOSUPPORT 89If second argument was supplied, 90and address family did not match between arguments. 91.El 92.Pp 93.Fn bindresvport 94may also fail and set 95.Va errno 96for any of the errors specified for the calls 97.Xr bind 2 , 98.Xr getsockopt 2 , 99or 100.Xr setsockopt 2 . 101.Sh SEE ALSO 102.Xr bind 2 , 103.Xr getsockname 2 , 104.Xr getsockopt 2 , 105.Xr setsockopt 2 , 106.Xr ip 4 107