xref: /openbsd/lib/libc/rpc/bindresvport.c (revision 21859ee8)
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.12 2000/01/24 02:24: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 	if (sin)
55 		return bindresvport_af(sd, (struct sockaddr *)sin, sin->sin_family);
56 	return bindresvport_af(sd, NULL, AF_INET);
57 }
58 
59 /*
60  * Bind a socket to a privileged IP port
61  */
62 int
63 bindresvport_af(sd, sa, af)
64 	int sd;
65 	struct sockaddr *sa;
66 	int af;
67 {
68 	int old, error;
69 	struct sockaddr_storage myaddr;
70 	struct sockaddr_in *sin;
71 	struct sockaddr_in6 *sin6;
72 	int proto, portrange, portlow;
73 	u_int16_t *portp;
74 	int salen;
75 
76 	if (sa == NULL) {
77 		memset(&myaddr, 0, sizeof(myaddr));
78 		sa = (struct sockaddr *)&myaddr;
79 	}
80 
81 	if (af == AF_INET) {
82 		proto = IPPROTO_IP;
83 		portrange = IP_PORTRANGE;
84 		portlow = IP_PORTRANGE_LOW;
85 		sin = (struct sockaddr_in *)sa;
86 		salen = sizeof(struct sockaddr_in);
87 		portp = &sin->sin_port;
88 	} else if (af == AF_INET6) {
89 		proto = IPPROTO_IPV6;
90 		portrange = IPV6_PORTRANGE;
91 		portlow = IPV6_PORTRANGE_LOW;
92 		sin6 = (struct sockaddr_in6 *)sa;
93 		salen = sizeof(struct sockaddr_in6);
94 		portp = &sin6->sin6_port;
95 	} else {
96 		errno = EPFNOSUPPORT;
97 		return (-1);
98 	}
99 	sa->sa_family = af;
100 	sa->sa_len = salen;
101 
102 	if (*portp == 0) {
103 		int oldlen = sizeof(old);
104 
105 		error = getsockopt(sd, proto, portrange, &old, &oldlen);
106 		if (error < 0)
107 			return(error);
108 
109 		error = setsockopt(sd, proto, portrange, &portlow,
110 		    sizeof(portlow));
111 		if (error < 0)
112 			return(error);
113 	}
114 
115 	error = bind(sd, sa, salen);
116 
117 	if (*portp == 0) {
118 		int saved_errno = errno;
119 
120 		if (error) {
121 			if (setsockopt(sd, proto, portrange, &old,
122 			    sizeof(old)) < 0)
123 				errno = saved_errno;
124 			return (error);
125 		}
126 
127 		if (sa != (struct sockaddr *)&myaddr) {
128 			/* Hmm, what did the kernel assign... */
129 			if (getsockname(sd, sa, &salen) < 0)
130 				errno = saved_errno;
131 			return (error);
132 		}
133 	}
134 	return (error);
135 }
136