1 /* $NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";
33 static char *sccsid = "from: @(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";
35 /* from: $OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp $ */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Copyright (c) 1987 by Sun Microsystems, Inc.
42 * Portions Copyright(C) 1996, Jason Downs. All rights reserved.
45 #include "namespace.h"
46 #include <sys/types.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
58 #include "un-namespace.h"
61 * Bind a socket to a privileged IP port
64 bindresvport(int sd, struct sockaddr_in *sin)
66 return bindresvport_sa(sd, (struct sockaddr *)sin);
70 * Bind a socket to a privileged IP port
73 bindresvport_sa(int sd, struct sockaddr *sa)
76 struct sockaddr_storage myaddr;
77 struct sockaddr_in *sin;
79 struct sockaddr_in6 *sin6;
81 int proto, portrange, portlow;
86 salen = sizeof(myaddr);
87 sa = (struct sockaddr *)&myaddr;
89 if (_getsockname(sd, sa, &salen) == -1)
90 return -1; /* errno is correctly set */
100 portrange = IP_PORTRANGE;
101 portlow = IP_PORTRANGE_LOW;
102 sin = (struct sockaddr_in *)sa;
103 salen = sizeof(struct sockaddr_in);
104 portp = &sin->sin_port;
108 proto = IPPROTO_IPV6;
109 portrange = IPV6_PORTRANGE;
110 portlow = IPV6_PORTRANGE_LOW;
111 sin6 = (struct sockaddr_in6 *)sa;
112 salen = sizeof(struct sockaddr_in6);
113 portp = &sin6->sin6_port;
117 errno = EPFNOSUPPORT;
124 socklen_t oldlen = sizeof(old);
126 error = _getsockopt(sd, proto, portrange, &old, &oldlen);
130 error = _setsockopt(sd, proto, portrange, &portlow,
136 error = _bind(sd, sa, salen);
139 int saved_errno = errno;
142 if (_setsockopt(sd, proto, portrange, &old,
148 if (sa != (struct sockaddr *)&myaddr) {
149 /* Hmm, what did the kernel assign? */
150 if (_getsockname(sd, sa, &salen) < 0)