Back to index

glibc  2.9
bindrsvprt.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 /*
00030  * Copyright (c) 1987 by Sun Microsystems, Inc.
00031  */
00032 
00033 #include <errno.h>
00034 #include <unistd.h>
00035 #include <string.h>
00036 #include <sys/types.h>
00037 #include <sys/socket.h>
00038 #include <netinet/in.h>
00039 
00040 /*
00041  * Bind a socket to a privileged IP port
00042  */
00043 int
00044 bindresvport (int sd, struct sockaddr_in *sin)
00045 {
00046   static short port;
00047   struct sockaddr_in myaddr;
00048   int i;
00049 
00050 #define STARTPORT 600
00051 #define LOWPORT 512
00052 #define ENDPORT (IPPORT_RESERVED - 1)
00053 #define NPORTS       (ENDPORT - STARTPORT + 1)
00054   static short startport = STARTPORT;
00055 
00056   if (sin == (struct sockaddr_in *) 0)
00057     {
00058       sin = &myaddr;
00059       __bzero (sin, sizeof (*sin));
00060       sin->sin_family = AF_INET;
00061     }
00062   else if (sin->sin_family != AF_INET)
00063     {
00064       __set_errno (EAFNOSUPPORT);
00065       return -1;
00066     }
00067 
00068   if (port == 0)
00069     {
00070       port = (__getpid () % NPORTS) + STARTPORT;
00071     }
00072 
00073   /* Initialize to make gcc happy.  */
00074   int res = -1;
00075 
00076   int nports = ENDPORT - startport + 1;
00077   int endport = ENDPORT;
00078  again:
00079   for (i = 0; i < nports; ++i)
00080     {
00081       sin->sin_port = htons (port++);
00082       if (port > endport)
00083        port = startport;
00084       res = __bind (sd, sin, sizeof (struct sockaddr_in));
00085       if (res >= 0 || errno != EADDRINUSE)
00086        break;
00087     }
00088 
00089   if (i == nports && startport != LOWPORT)
00090     {
00091       startport = LOWPORT;
00092       endport = STARTPORT - 1;
00093       nports = STARTPORT - LOWPORT;
00094       port = LOWPORT + port % (STARTPORT - LOWPORT);
00095       goto again;
00096     }
00097 
00098   return res;
00099 }
00100 libc_hidden_def (bindresvport)