xref: /openbsd-src/lib/libc/rpc/bindresvport.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 /*
35  * Copyright (c) 1987 by Sun Microsystems, Inc.
36  *
37  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
38  */
39 
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/errno.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 
46 /*
47  * Bind a socket to a privileged IP port
48  */
49 int
50 bindresvport(sd, sin)
51 	int sd;
52 	struct sockaddr_in *sin;
53 {
54 	return bindresvport_sa(sd, (struct sockaddr *)sin);
55 }
56 
57 /*
58  * Bind a socket to a privileged port for whatever protocol.
59  */
60 int
61 bindresvport_sa(sd, sa)
62 	int sd;
63 	struct sockaddr *sa;
64 {
65 	int old, error, af;
66 	struct sockaddr_storage myaddr;
67 	struct sockaddr_in *sin;
68 	struct sockaddr_in6 *sin6;
69 	int proto, portrange, portlow;
70 	u_int16_t port;
71 	int salen;
72 
73 	if (sa == NULL) {
74 		salen = sizeof(myaddr);
75 		sa = (struct sockaddr *)&myaddr;
76 
77 		if (getsockname(sd, sa, &salen) == -1)
78 			return -1;	/* errno is correctly set */
79 
80 		af = sa->sa_family;
81 		memset(&myaddr, 0, salen);
82 	} else
83 		af = sa->sa_family;
84 
85 	if (af == AF_INET) {
86 		proto = IPPROTO_IP;
87 		portrange = IP_PORTRANGE;
88 		portlow = IP_PORTRANGE_LOW;
89 		sin = (struct sockaddr_in *)sa;
90 		salen = sizeof(struct sockaddr_in);
91 		port = sin->sin_port;
92 	} else if (af == AF_INET6) {
93 		proto = IPPROTO_IPV6;
94 		portrange = IPV6_PORTRANGE;
95 		portlow = IPV6_PORTRANGE_LOW;
96 		sin6 = (struct sockaddr_in6 *)sa;
97 		salen = sizeof(struct sockaddr_in6);
98 		port = sin6->sin6_port;
99 	} else {
100 		errno = EPFNOSUPPORT;
101 		return (-1);
102 	}
103 	sa->sa_family = af;
104 	sa->sa_len = salen;
105 
106 	if (port == 0) {
107 		int oldlen = sizeof(old);
108 
109 		error = getsockopt(sd, proto, portrange, &old, &oldlen);
110 		if (error < 0)
111 			return (error);
112 
113 		error = setsockopt(sd, proto, portrange, &portlow,
114 		    sizeof(portlow));
115 		if (error < 0)
116 			return (error);
117 	}
118 
119 	error = bind(sd, sa, salen);
120 
121 	if (port == 0) {
122 		int saved_errno = errno;
123 
124 		if (error) {
125 			if (setsockopt(sd, proto, portrange, &old,
126 			    sizeof(old)) < 0)
127 				errno = saved_errno;
128 			return (error);
129 		}
130 
131 		if (sa != (struct sockaddr *)&myaddr) {
132 			/* Hmm, what did the kernel assign... */
133 			if (getsockname(sd, sa, &salen) < 0)
134 				errno = saved_errno;
135 			return (error);
136 		}
137 	}
138 	return (error);
139 }
140