1*45074Smckusick static char sccsid[] = "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC 1.8 88/02/08 SMI"; 2*45074Smckusick /* 3*45074Smckusick * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 4*45074Smckusick * unrestricted use provided that this legend is included on all tape 5*45074Smckusick * media and as a part of the software program in whole or part. Users 6*45074Smckusick * may copy or modify Sun RPC without charge, but are not authorized 7*45074Smckusick * to license or distribute it to anyone else except as part of a product or 8*45074Smckusick * program developed by the user. 9*45074Smckusick * 10*45074Smckusick * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11*45074Smckusick * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12*45074Smckusick * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13*45074Smckusick * 14*45074Smckusick * Sun RPC is provided with no support and without any obligation on the 15*45074Smckusick * part of Sun Microsystems, Inc. to assist in its use, correction, 16*45074Smckusick * modification or enhancement. 17*45074Smckusick * 18*45074Smckusick * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19*45074Smckusick * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20*45074Smckusick * OR ANY PART THEREOF. 21*45074Smckusick * 22*45074Smckusick * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23*45074Smckusick * or profits or other special, indirect and consequential damages, even if 24*45074Smckusick * Sun has been advised of the possibility of such damages. 25*45074Smckusick * 26*45074Smckusick * Sun Microsystems, Inc. 27*45074Smckusick * 2550 Garcia Avenue 28*45074Smckusick * Mountain View, California 94043 29*45074Smckusick */ 30*45074Smckusick 31*45074Smckusick /* 32*45074Smckusick * Copyright (c) 1987 by Sun Microsystems, Inc. 33*45074Smckusick */ 34*45074Smckusick 35*45074Smckusick #include <sys/types.h> 36*45074Smckusick #include <sys/errno.h> 37*45074Smckusick #include <sys/socket.h> 38*45074Smckusick #include <netinet/in.h> 39*45074Smckusick 40*45074Smckusick /* 41*45074Smckusick * Bind a socket to a privileged IP port 42*45074Smckusick */ 43*45074Smckusick bindresvport(sd, sin) 44*45074Smckusick int sd; 45*45074Smckusick struct sockaddr_in *sin; 46*45074Smckusick { 47*45074Smckusick int res; 48*45074Smckusick static short port; 49*45074Smckusick struct sockaddr_in myaddr; 50*45074Smckusick extern int errno; 51*45074Smckusick int i; 52*45074Smckusick 53*45074Smckusick #define STARTPORT 600 54*45074Smckusick #define ENDPORT (IPPORT_RESERVED - 1) 55*45074Smckusick #define NPORTS (ENDPORT - STARTPORT + 1) 56*45074Smckusick 57*45074Smckusick if (sin == (struct sockaddr_in *)0) { 58*45074Smckusick sin = &myaddr; 59*45074Smckusick bzero(sin, sizeof (*sin)); 60*45074Smckusick sin->sin_family = AF_INET; 61*45074Smckusick } else if (sin->sin_family != AF_INET) { 62*45074Smckusick errno = EPFNOSUPPORT; 63*45074Smckusick return (-1); 64*45074Smckusick } 65*45074Smckusick if (port == 0) { 66*45074Smckusick port = (getpid() % NPORTS) + STARTPORT; 67*45074Smckusick } 68*45074Smckusick res = -1; 69*45074Smckusick errno = EADDRINUSE; 70*45074Smckusick for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; i++) { 71*45074Smckusick sin->sin_port = htons(port++); 72*45074Smckusick if (port > ENDPORT) { 73*45074Smckusick port = STARTPORT; 74*45074Smckusick } 75*45074Smckusick res = bind(sd, sin, sizeof(struct sockaddr_in)); 76*45074Smckusick } 77*45074Smckusick return (res); 78*45074Smckusick } 79