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 * $FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.12 2000/01/26 09:02:42 shin Exp $ 32 * $DragonFly: src/lib/libc/rpc/bindresvport.c,v 1.3 2005/01/31 22:29:38 dillon 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/errno.h> 44 #include <sys/socket.h> 45 #include <netinet/in.h> 46 #include <unistd.h> 47 #include <string.h> 48 #include "un-namespace.h" 49 50 /* 51 * Bind a socket to a privileged IP port 52 */ 53 int 54 bindresvport(sd, sin) 55 int sd; 56 struct sockaddr_in *sin; 57 { 58 return bindresvport_sa(sd, (struct sockaddr *)sin); 59 } 60 61 /* 62 * Bind a socket to a privileged port for whatever protocol. 63 */ 64 int 65 bindresvport_sa(sd, sa) 66 int sd; 67 struct sockaddr *sa; 68 { 69 int old, error, af; 70 struct sockaddr_storage myaddr; 71 struct sockaddr_in *sin; 72 struct sockaddr_in6 *sin6; 73 int proto, portrange, portlow; 74 u_int16_t port; 75 int salen; 76 77 if (sa == NULL) { 78 salen = sizeof(myaddr); 79 sa = (struct sockaddr *)&myaddr; 80 81 if (_getsockname(sd, sa, &salen) == -1) 82 return -1; /* errno is correctly set */ 83 84 af = sa->sa_family; 85 memset(&myaddr, 0, salen); 86 } else 87 af = sa->sa_family; 88 89 if (af == AF_INET) { 90 proto = IPPROTO_IP; 91 portrange = IP_PORTRANGE; 92 portlow = IP_PORTRANGE_LOW; 93 sin = (struct sockaddr_in *)sa; 94 salen = sizeof(struct sockaddr_in); 95 port = sin->sin_port; 96 } else if (af == AF_INET6) { 97 proto = IPPROTO_IPV6; 98 portrange = IPV6_PORTRANGE; 99 portlow = IPV6_PORTRANGE_LOW; 100 sin6 = (struct sockaddr_in6 *)sa; 101 salen = sizeof(struct sockaddr_in6); 102 port = sin6->sin6_port; 103 } else { 104 errno = EPFNOSUPPORT; 105 return (-1); 106 } 107 sa->sa_family = af; 108 sa->sa_len = salen; 109 110 if (port == 0) { 111 int oldlen = sizeof(old); 112 113 error = _getsockopt(sd, proto, portrange, &old, &oldlen); 114 if (error < 0) 115 return (error); 116 117 error = setsockopt(sd, proto, portrange, &portlow, 118 sizeof(portlow)); 119 if (error < 0) 120 return (error); 121 } 122 123 error = _bind(sd, sa, salen); 124 125 if (port == 0) { 126 int saved_errno = errno; 127 128 if (error) { 129 if (_setsockopt(sd, proto, portrange, &old, 130 sizeof(old)) < 0) 131 errno = saved_errno; 132 return (error); 133 } 134 135 if (sa != (struct sockaddr *)&myaddr) { 136 /* Hmm, what did the kernel assign... */ 137 if (_getsockname(sd, sa, &salen) < 0) 138 errno = saved_errno; 139 return (error); 140 } 141 } 142 return (error); 143 } 144