xref: /freebsd/lib/libc/rpc/bindresvport.c (revision 42b4f28ebdf07192c1a09a8711cb7c31a09b9381)
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 *sccsid = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";*/
32 /*static char *sccsid = "from: @(#)bindresvport.c	2.2 88/07/29 4.0 RPCSRC";*/
33 /*from: OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp */
34 static char *rcsid = "$FreeBSD$";
35 #endif
36 
37 /*
38  * Copyright (c) 1987 by Sun Microsystems, Inc.
39  *
40  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
41  */
42 
43 #include <sys/types.h>
44 #include <sys/errno.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <unistd.h>
48 #include <string.h>
49 
50 /*
51  * Bind a socket to a privileged IP port
52  */
53 int
54 bindresvport(sd, sin)
55 	int sd;
56 	struct sockaddr_in *sin;
57 {
58 	struct sockaddr_in myaddr;
59 	int sinlen = sizeof(struct sockaddr_in);
60 
61 	if (sin == (struct sockaddr_in *)0) {
62 		sin = &myaddr;
63 		memset(sin, 0, sinlen);
64 		sin->sin_len = sinlen;
65 		sin->sin_family = AF_INET;
66 	} else if (sin->sin_family != AF_INET) {
67 		errno = EPFNOSUPPORT;
68 		return (-1);
69 	}
70 
71 	return (bindresvport2(sd, sin, sinlen));
72 }
73 
74 int
75 bindresvport2(sd, sa, addrlen)
76 	int sd;
77 	struct sockaddr *sa;
78 	socklen_t addrlen;
79 {
80 	int on, old, error, level, optname;
81 	u_short port;
82 
83 	if (sa == NULL) {
84 		errno = EINVAL;
85 		return (-1);
86 	}
87 	switch (sa->sa_family) {
88 	case AF_INET:
89 		port = ntohs(((struct sockaddr_in *)sa)->sin_port);
90 		level = IPPROTO_IP;
91 		optname = IP_PORTRANGE;
92 		on = IP_PORTRANGE_LOW;
93 		break;
94 #ifdef INET6
95 	case AF_INET6:
96 		port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
97 		level = IPPROTO_IPV6;
98 		optname = IPV6_PORTRANGE;
99 		on = IPV6_PORTRANGE_LOW;
100 		break;
101 #endif
102 	default:
103 		errno = EAFNOSUPPORT;
104 		return (-1);
105 	}
106 
107 	if (port == 0) {
108 		int oldlen = sizeof(old);
109 		error = getsockopt(sd, level, optname, &old, &oldlen);
110 		if (error < 0)
111 			return(error);
112 
113 		error = setsockopt(sd, level, optname, &on, sizeof(on));
114 		if (error < 0)
115 			return(error);
116 	}
117 
118 	error = bind(sd, sa, addrlen);
119 
120 	switch (sa->sa_family) {
121 	case AF_INET:
122 		port = ntohs(((struct sockaddr_in *)sa)->sin_port);
123 		break;
124 #ifdef INET6
125 	case AF_INET6:
126 		port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
127 		break;
128 #endif
129 	default: /* shoud not match here */
130 		errno = EAFNOSUPPORT;
131 		return (-1);
132 	}
133 	if (port == 0) {
134 		int saved_errno = errno;
135 
136 		if (error) {
137 			if (setsockopt(sd, level, optname,
138 			    &old, sizeof(old)) < 0)
139 				errno = saved_errno;
140 			return (error);
141 		}
142 
143 		/* Hmm, what did the kernel assign... */
144 		if (getsockname(sd, (struct sockaddr *)sa, &addrlen) < 0)
145 			errno = saved_errno;
146 		return (error);
147 	}
148 	return (error);
149 }
150