bindresvport.c revision 1.1
11.1Scgd/* 21.1Scgd * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 31.1Scgd * unrestricted use provided that this legend is included on all tape 41.1Scgd * media and as a part of the software program in whole or part. Users 51.1Scgd * may copy or modify Sun RPC without charge, but are not authorized 61.1Scgd * to license or distribute it to anyone else except as part of a product or 71.1Scgd * program developed by the user. 81.1Scgd * 91.1Scgd * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 101.1Scgd * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 111.1Scgd * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 121.1Scgd * 131.1Scgd * Sun RPC is provided with no support and without any obligation on the 141.1Scgd * part of Sun Microsystems, Inc. to assist in its use, correction, 151.1Scgd * modification or enhancement. 161.1Scgd * 171.1Scgd * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 181.1Scgd * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 191.1Scgd * OR ANY PART THEREOF. 201.1Scgd * 211.1Scgd * In no event will Sun Microsystems, Inc. be liable for any lost revenue 221.1Scgd * or profits or other special, indirect and consequential damages, even if 231.1Scgd * Sun has been advised of the possibility of such damages. 241.1Scgd * 251.1Scgd * Sun Microsystems, Inc. 261.1Scgd * 2550 Garcia Avenue 271.1Scgd * Mountain View, California 94043 281.1Scgd */ 291.1Scgd 301.1Scgd#if defined(LIBC_SCCS) && !defined(lint) 311.1Scgd/*static char *sccsid = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";*/ 321.1Scgd/*static char *sccsid = "from: @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";*/ 331.1Scgdstatic char *rcsid = "$Id: bindresvport.c,v 1.1 1993/10/07 07:29:40 cgd Exp $"; 341.1Scgd#endif 351.1Scgd 361.1Scgd/* 371.1Scgd * Copyright (c) 1987 by Sun Microsystems, Inc. 381.1Scgd */ 391.1Scgd 401.1Scgd#include <sys/types.h> 411.1Scgd#include <sys/errno.h> 421.1Scgd#include <sys/socket.h> 431.1Scgd#include <netinet/in.h> 441.1Scgd 451.1Scgd/* 461.1Scgd * Bind a socket to a privileged IP port 471.1Scgd */ 481.1Scgdbindresvport(sd, sin) 491.1Scgd int sd; 501.1Scgd struct sockaddr_in *sin; 511.1Scgd{ 521.1Scgd int res; 531.1Scgd static short port; 541.1Scgd struct sockaddr_in myaddr; 551.1Scgd extern int errno; 561.1Scgd int i; 571.1Scgd 581.1Scgd#define STARTPORT 600 591.1Scgd#define ENDPORT (IPPORT_RESERVED - 1) 601.1Scgd#define NPORTS (ENDPORT - STARTPORT + 1) 611.1Scgd 621.1Scgd if (sin == (struct sockaddr_in *)0) { 631.1Scgd sin = &myaddr; 641.1Scgd bzero(sin, sizeof (*sin)); 651.1Scgd sin->sin_family = AF_INET; 661.1Scgd } else if (sin->sin_family != AF_INET) { 671.1Scgd errno = EPFNOSUPPORT; 681.1Scgd return (-1); 691.1Scgd } 701.1Scgd if (port == 0) { 711.1Scgd port = (getpid() % NPORTS) + STARTPORT; 721.1Scgd } 731.1Scgd res = -1; 741.1Scgd errno = EADDRINUSE; 751.1Scgd for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; i++) { 761.1Scgd sin->sin_port = htons(port++); 771.1Scgd if (port > ENDPORT) { 781.1Scgd port = STARTPORT; 791.1Scgd } 801.1Scgd res = bind(sd, 811.1Scgd (struct sockaddr *)sin, sizeof(struct sockaddr_in)); 821.1Scgd } 831.1Scgd return (res); 841.1Scgd} 85