1 /*- 2 * Copyright (c) 2009, Sun Microsystems, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * - Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * - Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 * and/or other materials provided with the distribution. 12 * - Neither the name of Sun Microsystems, Inc. nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * @(#)bindresvport.c 1.8 88/02/08 SMI 29 * @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC 30 * $NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $ 31 * $FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.16 2004/10/16 06:11:34 obrien Exp $ 32 */ 33 34 /* 35 * Copyright (c) 1987 by Sun Microsystems, Inc. 36 * 37 * Portions Copyright(C) 1996, Jason Downs. All rights reserved. 38 */ 39 40 #include "namespace.h" 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 44 #include <netinet/in.h> 45 46 #include <errno.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include <rpc/rpc.h> 51 #include "un-namespace.h" 52 53 /* 54 * Bind a socket to a privileged IP port 55 */ 56 int 57 bindresvport(int sd, struct sockaddr_in *sin) 58 { 59 return bindresvport_sa(sd, (struct sockaddr *)sin); 60 } 61 62 /* 63 * Bind a socket to a privileged IP port 64 */ 65 int 66 bindresvport_sa(int sd, struct sockaddr *sa) 67 { 68 int old, error, af; 69 struct sockaddr_storage myaddr; 70 struct sockaddr_in *sin; 71 #ifdef INET6 72 struct sockaddr_in6 *sin6; 73 #endif 74 int proto, portrange, portlow; 75 u_int16_t *portp; 76 socklen_t salen; 77 78 if (sa == NULL) { 79 salen = sizeof(myaddr); 80 sa = (struct sockaddr *)&myaddr; 81 82 if (_getsockname(sd, sa, &salen) == -1) 83 return -1; /* errno is correctly set */ 84 85 af = sa->sa_family; 86 memset(sa, 0, salen); 87 } else 88 af = sa->sa_family; 89 90 switch (af) { 91 case AF_INET: 92 proto = IPPROTO_IP; 93 portrange = IP_PORTRANGE; 94 portlow = IP_PORTRANGE_LOW; 95 sin = (struct sockaddr_in *)sa; 96 salen = sizeof(struct sockaddr_in); 97 portp = &sin->sin_port; 98 break; 99 #ifdef INET6 100 case AF_INET6: 101 proto = IPPROTO_IPV6; 102 portrange = IPV6_PORTRANGE; 103 portlow = IPV6_PORTRANGE_LOW; 104 sin6 = (struct sockaddr_in6 *)sa; 105 salen = sizeof(struct sockaddr_in6); 106 portp = &sin6->sin6_port; 107 break; 108 #endif 109 default: 110 errno = EPFNOSUPPORT; 111 return (-1); 112 } 113 sa->sa_family = af; 114 sa->sa_len = salen; 115 116 if (*portp == 0) { 117 socklen_t oldlen = sizeof(old); 118 119 error = _getsockopt(sd, proto, portrange, &old, &oldlen); 120 if (error < 0) 121 return (error); 122 123 error = _setsockopt(sd, proto, portrange, &portlow, 124 sizeof(portlow)); 125 if (error < 0) 126 return (error); 127 } 128 129 error = _bind(sd, sa, salen); 130 131 if (*portp == 0) { 132 int saved_errno = errno; 133 134 if (error < 0) { 135 if (_setsockopt(sd, proto, portrange, &old, 136 sizeof(old)) < 0) 137 errno = saved_errno; 138 return (error); 139 } 140 141 if (sa != (struct sockaddr *)&myaddr) { 142 /* Hmm, what did the kernel assign? */ 143 if (_getsockname(sd, sa, &salen) < 0) 144 errno = saved_errno; 145 return (error); 146 } 147 } 148 return (error); 149 } 150