1 /* lib/rpc/bindresvport.c */ 2 /* 3 * Copyright (c) 2010, Oracle America, Inc. 4 * 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * * Neither the name of the "Oracle America, Inc." nor the names of 19 * its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <string.h> 36 #include <stdlib.h> 37 #include <stdint.h> 38 #include <unistd.h> 39 #include <sys/types.h> 40 #include <sys/errno.h> 41 #include <sys/socket.h> 42 #include <netinet/in.h> 43 #include <gssrpc/rpc.h> 44 #include <errno.h> 45 #include "socket-utils.h" 46 47 /* 48 * Bind a socket to a privileged IP port 49 */ 50 int 51 bindresvport_sa(int sd, struct sockaddr *sa) 52 { 53 int res; 54 static short port; 55 struct sockaddr_storage myaddr; 56 socklen_t salen; 57 int i; 58 59 #define STARTPORT 600 60 #define ENDPORT (IPPORT_RESERVED - 1) 61 #define NPORTS (ENDPORT - STARTPORT + 1) 62 if (sa == NULL) { 63 salen = sizeof(myaddr); 64 sa = ss2sa(&myaddr); 65 res = getsockname(sd, sa, &salen); 66 if (res < 0) 67 return (-1); 68 } 69 if (!sa_is_inet(sa)) { 70 errno = EPFNOSUPPORT; 71 return (-1); 72 } 73 if (port == 0) { 74 port = (getpid() % NPORTS) + STARTPORT; 75 } 76 res = -1; 77 errno = EADDRINUSE; 78 for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; i++) { 79 sa_setport(sa, port++); 80 if (port > ENDPORT) { 81 port = STARTPORT; 82 } 83 res = bind(sd, sa, sa_socklen(sa)); 84 } 85 return (res); 86 } 87 88 int 89 bindresvport(int sd, struct sockaddr_in *sockin) 90 { 91 return (bindresvport_sa(sd, (struct sockaddr *)sockin)); 92 } 93