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