xref: /csrg-svn/lib/librpc/rpc/bindresvport.c (revision 46639)
145074Smckusick static  char sccsid[] = "@(#)bindresvport.c	2.2 88/07/29 4.0 RPCSRC 1.8 88/02/08 SMI";
245074Smckusick /*
345074Smckusick  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
445074Smckusick  * unrestricted use provided that this legend is included on all tape
545074Smckusick  * media and as a part of the software program in whole or part.  Users
645074Smckusick  * may copy or modify Sun RPC without charge, but are not authorized
745074Smckusick  * to license or distribute it to anyone else except as part of a product or
845074Smckusick  * program developed by the user.
945074Smckusick  *
1045074Smckusick  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1145074Smckusick  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1245074Smckusick  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1345074Smckusick  *
1445074Smckusick  * Sun RPC is provided with no support and without any obligation on the
1545074Smckusick  * part of Sun Microsystems, Inc. to assist in its use, correction,
1645074Smckusick  * modification or enhancement.
1745074Smckusick  *
1845074Smckusick  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1945074Smckusick  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2045074Smckusick  * OR ANY PART THEREOF.
2145074Smckusick  *
2245074Smckusick  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2345074Smckusick  * or profits or other special, indirect and consequential damages, even if
2445074Smckusick  * Sun has been advised of the possibility of such damages.
2545074Smckusick  *
2645074Smckusick  * Sun Microsystems, Inc.
2745074Smckusick  * 2550 Garcia Avenue
2845074Smckusick  * Mountain View, California  94043
2945074Smckusick  */
3045074Smckusick 
3145074Smckusick /*
3245074Smckusick  * Copyright (c) 1987 by Sun Microsystems, Inc.
3345074Smckusick  */
3445074Smckusick 
3545074Smckusick #include <sys/types.h>
3645074Smckusick #include <sys/errno.h>
3745074Smckusick #include <sys/socket.h>
3845074Smckusick #include <netinet/in.h>
3945074Smckusick 
4045074Smckusick /*
4145074Smckusick  * Bind a socket to a privileged IP port
4245074Smckusick  */
bindresvport(sd,sin)4345074Smckusick bindresvport(sd, sin)
4445074Smckusick 	int sd;
4545074Smckusick 	struct sockaddr_in *sin;
4645074Smckusick {
4745074Smckusick 	int res;
4845074Smckusick 	static short port;
4945074Smckusick 	struct sockaddr_in myaddr;
5045074Smckusick 	extern int errno;
5145074Smckusick 	int i;
5245074Smckusick 
5345074Smckusick #define STARTPORT 600
5445074Smckusick #define ENDPORT (IPPORT_RESERVED - 1)
5545074Smckusick #define NPORTS	(ENDPORT - STARTPORT + 1)
5645074Smckusick 
5745074Smckusick 	if (sin == (struct sockaddr_in *)0) {
5845074Smckusick 		sin = &myaddr;
5945074Smckusick 		bzero(sin, sizeof (*sin));
6045074Smckusick 		sin->sin_family = AF_INET;
6145074Smckusick 	} else if (sin->sin_family != AF_INET) {
6245074Smckusick 		errno = EPFNOSUPPORT;
6345074Smckusick 		return (-1);
6445074Smckusick 	}
6545074Smckusick 	if (port == 0) {
6645074Smckusick 		port = (getpid() % NPORTS) + STARTPORT;
6745074Smckusick 	}
6845074Smckusick 	res = -1;
6945074Smckusick 	errno = EADDRINUSE;
7045074Smckusick 	for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; i++) {
7145074Smckusick 		sin->sin_port = htons(port++);
7245074Smckusick 		if (port > ENDPORT) {
7345074Smckusick 			port = STARTPORT;
7445074Smckusick 		}
75*46639Sbostic 		res = bind(sd,
76*46639Sbostic 		    (struct sockaddr *)sin, sizeof(struct sockaddr_in));
7745074Smckusick 	}
7845074Smckusick 	return (res);
7945074Smckusick }
80