Libinfo-173.tar.gz
[apple/libinfo.git] / rpc.subproj / bindresvport.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
32 *
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 *
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
40 *
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
44 *
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
53
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";*/
56 /*static char *sccsid = "from: @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";*/
57 /*from: OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp */
58 static char *rcsid = "$FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.12 2000/01/26 09:02:42 shin Exp $";
59 #endif
60
61 /*
62 * Copyright (c) 1987 by Sun Microsystems, Inc.
63 *
64 * Portions Copyright(C) 1996, Jason Downs. All rights reserved.
65 */
66
67 #include <string.h>
68 #include <unistd.h>
69 #include <sys/types.h>
70 #include <sys/errno.h>
71 #include <sys/socket.h>
72 #include <netinet/in.h>
73
74 /*
75 * Bind a socket to a privileged port for whatever protocol.
76 */
77 int
78 bindresvport_sa(sd, sa)
79 int sd;
80 struct sockaddr *sa;
81 {
82 int old, error, af;
83 struct sockaddr_storage myaddr;
84 struct sockaddr_in *sin;
85 struct sockaddr_in6 *sin6;
86 int proto, portrange, portlow;
87 u_int16_t port;
88 int salen;
89
90 if (sa == NULL) {
91 salen = sizeof(myaddr);
92 sa = (struct sockaddr *)&myaddr;
93
94 if (getsockname(sd, sa, &salen) == -1)
95 return -1; /* errno is correctly set */
96
97 af = sa->sa_family;
98 memset(&myaddr, 0, salen);
99 } else
100 af = sa->sa_family;
101
102 if (af == 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 port = sin->sin_port;
109 } else if (af == AF_INET6) {
110 proto = IPPROTO_IPV6;
111 portrange = IPV6_PORTRANGE;
112 portlow = IPV6_PORTRANGE_LOW;
113 sin6 = (struct sockaddr_in6 *)sa;
114 salen = sizeof(struct sockaddr_in6);
115 port = sin6->sin6_port;
116 } else {
117 errno = EPFNOSUPPORT;
118 return (-1);
119 }
120 sa->sa_family = af;
121 sa->sa_len = salen;
122
123 if (port == 0) {
124 int oldlen = sizeof(old);
125
126 error = getsockopt(sd, proto, portrange, &old, &oldlen);
127 if (error < 0)
128 return (error);
129
130 error = setsockopt(sd, proto, portrange, &portlow,
131 sizeof(portlow));
132 if (error < 0)
133 return (error);
134 }
135
136 error = bind(sd, sa, salen);
137
138 if (port == 0) {
139 int saved_errno = errno;
140
141 if (error) {
142 if (setsockopt(sd, proto, portrange, &old,
143 sizeof(old)) < 0)
144 errno = saved_errno;
145 return (error);
146 }
147
148 if (sa != (struct sockaddr *)&myaddr) {
149 /* Hmm, what did the kernel assign... */
150 if (getsockname(sd, sa, &salen) < 0)
151 errno = saved_errno;
152 return (error);
153 }
154 }
155 return (error);
156 }
157
158 /*
159 * Bind a socket to a privileged IP port
160 */
161 int
162 bindresvport(sd, sin)
163 int sd;
164 struct sockaddr_in *sin;
165 {
166 return bindresvport_sa(sd, (struct sockaddr *)sin);
167 }