1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 * 29 * @(#)bindresvport.c 1.8 88/02/08 SMI 30 * @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC 31 * $NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $ 32 * $FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.16 2004/10/16 06:11:34 obrien Exp $ 33 */ 34 35 /* 36 * Copyright (c) 1987 by Sun Microsystems, Inc. 37 * 38 * Portions Copyright(C) 1996, Jason Downs. All rights reserved. 39 */ 40 41 #include "namespace.h" 42 #include <sys/types.h> 43 #include <sys/socket.h> 44 45 #include <netinet/in.h> 46 47 #include <errno.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include <rpc/rpc.h> 52 #include "un-namespace.h" 53 54 /* 55 * Bind a socket to a privileged IP port 56 */ 57 int 58 bindresvport(int sd, struct sockaddr_in *sin) 59 { 60 return bindresvport_sa(sd, (struct sockaddr *)sin); 61 } 62 63 /* 64 * Bind a socket to a privileged IP port 65 */ 66 int 67 bindresvport_sa(int sd, struct sockaddr *sa) 68 { 69 int old, error, af; 70 struct sockaddr_storage myaddr; 71 struct sockaddr_in *sin; 72 #ifdef INET6 73 struct sockaddr_in6 *sin6; 74 #endif 75 int proto, portrange, portlow; 76 u_int16_t *portp; 77 socklen_t salen; 78 79 if (sa == NULL) { 80 salen = sizeof(myaddr); 81 sa = (struct sockaddr *)&myaddr; 82 83 if (_getsockname(sd, sa, &salen) == -1) 84 return -1; /* errno is correctly set */ 85 86 af = sa->sa_family; 87 memset(sa, 0, salen); 88 } else 89 af = sa->sa_family; 90 91 switch (af) { 92 case AF_INET: 93 proto = IPPROTO_IP; 94 portrange = IP_PORTRANGE; 95 portlow = IP_PORTRANGE_LOW; 96 sin = (struct sockaddr_in *)sa; 97 salen = sizeof(struct sockaddr_in); 98 portp = &sin->sin_port; 99 break; 100 #ifdef INET6 101 case AF_INET6: 102 proto = IPPROTO_IPV6; 103 portrange = IPV6_PORTRANGE; 104 portlow = IPV6_PORTRANGE_LOW; 105 sin6 = (struct sockaddr_in6 *)sa; 106 salen = sizeof(struct sockaddr_in6); 107 portp = &sin6->sin6_port; 108 break; 109 #endif 110 default: 111 errno = EPFNOSUPPORT; 112 return (-1); 113 } 114 sa->sa_family = af; 115 sa->sa_len = salen; 116 117 if (*portp == 0) { 118 socklen_t oldlen = sizeof(old); 119 120 error = _getsockopt(sd, proto, portrange, &old, &oldlen); 121 if (error < 0) 122 return (error); 123 124 error = _setsockopt(sd, proto, portrange, &portlow, 125 sizeof(portlow)); 126 if (error < 0) 127 return (error); 128 } 129 130 error = _bind(sd, sa, salen); 131 132 if (*portp == 0) { 133 int saved_errno = errno; 134 135 if (error < 0) { 136 if (_setsockopt(sd, proto, portrange, &old, 137 sizeof(old)) < 0) 138 errno = saved_errno; 139 return (error); 140 } 141 142 if (sa != (struct sockaddr *)&myaddr) { 143 /* Hmm, what did the kernel assign? */ 144 if (_getsockname(sd, sa, &salen) < 0) 145 errno = saved_errno; 146 return (error); 147 } 148 } 149 return (error); 150 } 151