bindresvport.c (55918) | bindresvport.c (56629) |
---|---|
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 * --- 17 unchanged lines hidden (view full) --- 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) 31/*static char *sccsid = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";*/ 32/*static char *sccsid = "from: @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";*/ 33/*from: OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp */ | 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 * --- 17 unchanged lines hidden (view full) --- 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) 31/*static char *sccsid = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";*/ 32/*static char *sccsid = "from: @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";*/ 33/*from: OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp */ |
34static char *rcsid = "$FreeBSD: head/lib/libc/rpc/bindresvport.c 55918 2000-01-13 15:09:48Z shin $"; | 34static char *rcsid = "$FreeBSD: head/lib/libc/rpc/bindresvport.c 56629 2000-01-26 09:02:42Z shin $"; |
35#endif 36 37/* 38 * Copyright (c) 1987 by Sun Microsystems, Inc. 39 * 40 * Portions Copyright(C) 1996, Jason Downs. All rights reserved. 41 */ 42 --- 7 unchanged lines hidden (view full) --- 50/* 51 * Bind a socket to a privileged IP port 52 */ 53int 54bindresvport(sd, sin) 55 int sd; 56 struct sockaddr_in *sin; 57{ | 35#endif 36 37/* 38 * Copyright (c) 1987 by Sun Microsystems, Inc. 39 * 40 * Portions Copyright(C) 1996, Jason Downs. All rights reserved. 41 */ 42 --- 7 unchanged lines hidden (view full) --- 50/* 51 * Bind a socket to a privileged IP port 52 */ 53int 54bindresvport(sd, sin) 55 int sd; 56 struct sockaddr_in *sin; 57{ |
58 struct sockaddr_in myaddr; 59 int sinlen = sizeof(struct sockaddr_in); 60 61 if (sin == (struct sockaddr_in *)0) { 62 sin = &myaddr; 63 memset(sin, 0, sinlen); 64 sin->sin_len = sinlen; 65 sin->sin_family = AF_INET; 66 } else if (sin->sin_family != AF_INET) { 67 errno = EPFNOSUPPORT; 68 return (-1); 69 } 70 71 return (bindresvport2(sd, sin, sinlen)); | 58 return bindresvport_sa(sd, (struct sockaddr *)sin); |
72} 73 | 59} 60 |
61/* 62 * Bind a socket to a privileged port for whatever protocol. 63 */ |
|
74int | 64int |
75bindresvport2(sd, sa, addrlen) | 65bindresvport_sa(sd, sa) |
76 int sd; 77 struct sockaddr *sa; | 66 int sd; 67 struct sockaddr *sa; |
78 socklen_t addrlen; | |
79{ | 68{ |
80 int on, old, error, level, optname; 81 u_short port; | 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; |
82 83 if (sa == NULL) { | 76 77 if (sa == NULL) { |
84 errno = EINVAL; | 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; |
85 return (-1); 86 } | 105 return (-1); 106 } |
87 switch (sa->sa_family) { 88 case AF_INET: 89 port = ntohs(((struct sockaddr_in *)sa)->sin_port); 90 level = IPPROTO_IP; 91 optname = IP_PORTRANGE; 92 on = IP_PORTRANGE_LOW; 93 break; 94#ifdef INET6 95 case AF_INET6: 96 port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 97 level = IPPROTO_IPV6; 98 optname = IPV6_PORTRANGE; 99 on = IPV6_PORTRANGE_LOW; 100 break; 101#endif 102 default: 103 errno = EAFNOSUPPORT; 104 return (-1); 105 } | 107 sa->sa_family = af; 108 sa->sa_len = salen; |
106 107 if (port == 0) { 108 int oldlen = sizeof(old); | 109 110 if (port == 0) { 111 int oldlen = sizeof(old); |
109 error = getsockopt(sd, level, optname, &old, &oldlen); | 112 113 error = getsockopt(sd, proto, portrange, &old, &oldlen); |
110 if (error < 0) | 114 if (error < 0) |
111 return(error); | 115 return (error); |
112 | 116 |
113 error = setsockopt(sd, level, optname, &on, sizeof(on)); | 117 error = setsockopt(sd, proto, portrange, &portlow, 118 sizeof(portlow)); |
114 if (error < 0) | 119 if (error < 0) |
115 return(error); | 120 return (error); |
116 } 117 | 121 } 122 |
118 error = bind(sd, sa, addrlen); | 123 error = bind(sd, sa, salen); |
119 | 124 |
120 switch (sa->sa_family) { 121 case AF_INET: 122 port = ntohs(((struct sockaddr_in *)sa)->sin_port); 123 break; 124#ifdef INET6 125 case AF_INET6: 126 port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 127 break; 128#endif 129 default: /* shoud not match here */ 130 errno = EAFNOSUPPORT; 131 return (-1); 132 } | |
133 if (port == 0) { 134 int saved_errno = errno; 135 136 if (error) { | 125 if (port == 0) { 126 int saved_errno = errno; 127 128 if (error) { |
137 if (setsockopt(sd, level, optname, 138 &old, sizeof(old)) < 0) | 129 if (setsockopt(sd, proto, portrange, &old, 130 sizeof(old)) < 0) |
139 errno = saved_errno; 140 return (error); 141 } 142 | 131 errno = saved_errno; 132 return (error); 133 } 134 |
143 /* Hmm, what did the kernel assign... */ 144 if (getsockname(sd, (struct sockaddr *)sa, &addrlen) < 0) 145 errno = saved_errno; 146 return (error); | 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 } |
147 } 148 return (error); 149} | 141 } 142 return (error); 143} |