2 * Copyright (c) 2009, Sun Microsystems, Inc.
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>
51 #include "un-namespace.h"
54 * Bind a socket to a privileged IP port
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
66 bindresvport_sa(int sd, struct sockaddr *sa)
69 struct sockaddr_storage myaddr;
70 struct sockaddr_in *sin;
72 struct sockaddr_in6 *sin6;
74 int proto, portrange, portlow;
79 salen = sizeof(myaddr);
80 sa = (struct sockaddr *)&myaddr;
82 if (_getsockname(sd, sa, &salen) == -1)
83 return -1; /* errno is correctly set */
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;
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;
110 errno = EPFNOSUPPORT;
117 socklen_t oldlen = sizeof(old);
119 error = _getsockopt(sd, proto, portrange, &old, &oldlen);
123 error = _setsockopt(sd, proto, portrange, &portlow,
129 error = _bind(sd, sa, salen);
132 int saved_errno = errno;
135 if (_setsockopt(sd, proto, portrange, &old,
141 if (sa != (struct sockaddr *)&myaddr) {
142 /* Hmm, what did the kernel assign? */
143 if (_getsockname(sd, sa, &salen) < 0)