<sys/{ipc,stat}.h>: Remove duplicate inclusion of <sys/cdefs.h>.
[dragonfly.git] / lib / libc / rpc / bindresvport.c
blob1f07b8fa26d9281738156ef12f5470440a659f11
1 /*-
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)bindresvport.c 1.8 88/02/08 SMI
29 * @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC
30 * $NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.16 2004/10/16 06:11:34 obrien Exp $
35 * Copyright (c) 1987 by Sun Microsystems, Inc.
37 * Portions Copyright(C) 1996, Jason Downs. All rights reserved.
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include <rpc/rpc.h>
51 #include "un-namespace.h"
54 * Bind a socket to a privileged IP port
56 int
57 bindresvport(int sd, struct sockaddr_in *sin)
59 return bindresvport_sa(sd, (struct sockaddr *)sin);
63 * Bind a socket to a privileged IP port
65 int
66 bindresvport_sa(int sd, struct sockaddr *sa)
68 int old, error, af;
69 struct sockaddr_storage myaddr;
70 struct sockaddr_in *sin;
71 #ifdef INET6
72 struct sockaddr_in6 *sin6;
73 #endif
74 int proto, portrange, portlow;
75 u_int16_t *portp;
76 socklen_t salen;
78 if (sa == NULL) {
79 salen = sizeof(myaddr);
80 sa = (struct sockaddr *)&myaddr;
82 if (_getsockname(sd, sa, &salen) == -1)
83 return -1; /* errno is correctly set */
85 af = sa->sa_family;
86 memset(sa, 0, salen);
87 } else
88 af = sa->sa_family;
90 switch (af) {
91 case AF_INET:
92 proto = IPPROTO_IP;
93 portrange = IP_PORTRANGE;
94 portlow = IP_PORTRANGE_LOW;
95 sin = (struct sockaddr_in *)sa;
96 salen = sizeof(struct sockaddr_in);
97 portp = &sin->sin_port;
98 break;
99 #ifdef INET6
100 case AF_INET6:
101 proto = IPPROTO_IPV6;
102 portrange = IPV6_PORTRANGE;
103 portlow = IPV6_PORTRANGE_LOW;
104 sin6 = (struct sockaddr_in6 *)sa;
105 salen = sizeof(struct sockaddr_in6);
106 portp = &sin6->sin6_port;
107 break;
108 #endif
109 default:
110 errno = EPFNOSUPPORT;
111 return (-1);
113 sa->sa_family = af;
114 sa->sa_len = salen;
116 if (*portp == 0) {
117 socklen_t oldlen = sizeof(old);
119 error = _getsockopt(sd, proto, portrange, &old, &oldlen);
120 if (error < 0)
121 return (error);
123 error = _setsockopt(sd, proto, portrange, &portlow,
124 sizeof(portlow));
125 if (error < 0)
126 return (error);
129 error = _bind(sd, sa, salen);
131 if (*portp == 0) {
132 int saved_errno = errno;
134 if (error < 0) {
135 if (_setsockopt(sd, proto, portrange, &old,
136 sizeof(old)) < 0)
137 errno = saved_errno;
138 return (error);
141 if (sa != (struct sockaddr *)&myaddr) {
142 /* Hmm, what did the kernel assign? */
143 if (_getsockname(sd, sa, &salen) < 0)
144 errno = saved_errno;
145 return (error);
148 return (error);