1/*	$NetBSD: bindresvport.c,v 1.21 2003/01/18 11:29:03 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above
13 *       copyright notice, this list of conditions and the following
14 *       disclaimer in the documentation and/or other materials
15 *       provided with the distribution.
16 *     * Neither the name of the "Oracle America, Inc." nor the names of its
17 *       contributors may be used to endorse or promote products derived
18 *       from this software without specific prior written permission.
19 *
20 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35#if defined(LIBC_SCCS) && !defined(lint)
36#if 0
37static char *sccsid = "@(#)bindresvport.c 1.8 88/02/08 SMI";
38static char *sccsid = "@(#)bindresvport.c	2.2 88/07/29 4.0 RPCSRC";
39#else
40__RCSID("$NetBSD: bindresvport.c,v 1.21 2003/01/18 11:29:03 thorpej Exp $");
41#endif
42#endif
43
44/*
45 * Copyright (c) 1987 by Sun Microsystems, Inc.
46 */
47
48#include "namespace.h"
49
50#include <sys/types.h>
51#include <sys/socket.h>
52
53#include <netinet/in.h>
54
55#include <errno.h>
56#include <string.h>
57#include <unistd.h>
58
59#include <rpc/rpc.h>
60
61#ifdef __weak_alias
62__weak_alias(bindresvport,_bindresvport)
63__weak_alias(bindresvport_sa,_bindresvport_sa)
64#endif
65
66/*
67 * Bind a socket to a privileged IP port
68 */
69int
70bindresvport(sd, brsin)
71	int sd;
72	struct sockaddr_in *brsin;
73{
74	return bindresvport_sa(sd, (struct sockaddr *)(void *)brsin);
75}
76
77/*
78 * Bind a socket to a privileged IP port
79 */
80int
81bindresvport_sa(sd, sa)
82	int sd;
83	struct sockaddr *sa;
84{
85	int error, old;
86	struct sockaddr_storage myaddr;
87	struct sockaddr_in *brsin;
88#ifdef INET6
89	struct sockaddr_in6 *brsin6;
90#endif
91	int proto, portrange, portlow;
92	u_int16_t *portp;
93	socklen_t salen;
94	int af;
95
96	if (sa == NULL) {
97		salen = sizeof(myaddr);
98		sa = (struct sockaddr *)(void *)&myaddr;
99
100		if (getsockname(sd, sa, &salen) == -1)
101			return -1;	/* errno is correctly set */
102
103		af = sa->sa_family;
104		memset(sa, 0, salen);
105	} else
106		af = sa->sa_family;
107
108	switch (af) {
109	case AF_INET:
110		proto = IPPROTO_IP;
111		portrange = IP_PORTRANGE;
112		portlow = IP_PORTRANGE_LOW;
113		brsin = (struct sockaddr_in *)(void *)sa;
114		salen = sizeof(struct sockaddr_in);
115		portp = &brsin->sin_port;
116		break;
117#ifdef INET6
118	case AF_INET6:
119		proto = IPPROTO_IPV6;
120		portrange = IPV6_PORTRANGE;
121		portlow = IPV6_PORTRANGE_LOW;
122		brsin6 = (struct sockaddr_in6 *)(void *)sa;
123		salen = sizeof(struct sockaddr_in6);
124		portp = &brsin6->sin6_port;
125		break;
126#endif
127	default:
128		errno = EPFNOSUPPORT;
129		return (-1);
130	}
131	sa->sa_family = af;
132	sa->sa_len = salen;
133
134	if (*portp == 0) {
135		socklen_t oldlen = sizeof(old);
136
137		error = getsockopt(sd, proto, portrange, &old, &oldlen);
138		if (error < 0)
139			return (error);
140		error = setsockopt(sd, proto, portrange, &portlow,
141		    sizeof(portlow));
142		if (error < 0)
143			return (error);
144	}
145
146	error = bind(sd, sa, salen);
147
148	if (*portp == 0) {
149		int saved_errno = errno;
150
151		if (error < 0) {
152			if (setsockopt(sd, proto, portrange, &old,
153			    sizeof(old)) < 0)
154				errno = saved_errno;
155			return (error);
156		}
157
158		if (sa != (struct sockaddr *)(void *)&myaddr) {
159			/* What did the kernel assign? */
160			if (getsockname(sd, sa, &salen) < 0)
161				errno = saved_errno;
162			return (error);
163		}
164	}
165	return (error);
166}
167