xref: /illumos-gate/usr/src/lib/libsocket/inet/bindresvport.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #include <sys/types.h>
36 #include <errno.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <netinet/udp.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #ifdef SYSV
45 #define	bzero(s, len)	(void) memset((s), 0, (len))
46 #endif
47 
48 
49 /*
50  * Bind a socket to a privileged IP port
51  */
52 int
53 bindresvport(int sd, struct sockaddr_in *sin)
54 {
55 	struct sockaddr_in myaddr;
56 	struct sockaddr_in *bindaddr;
57 	int level, optname;
58 	int optval, len;
59 	int ret;
60 
61 	bindaddr = sin;
62 	if (bindaddr == (struct sockaddr_in *)0) {
63 		bindaddr = &myaddr;
64 		bzero(bindaddr, sizeof (*bindaddr));
65 		bindaddr->sin_family = AF_INET;
66 	} else if (bindaddr->sin_family != AF_INET) {
67 		errno = EPFNOSUPPORT;
68 		return (-1);
69 	}
70 
71 	len = sizeof (optval);
72 	if (getsockopt(sd, SOL_SOCKET, SO_TYPE, &optval, &len) < 0) {
73 		return (-1);
74 	}
75 	/*
76 	 * Use *_ANONPRIVBIND to ask the kernel to pick a port in the
77 	 * priviledged range for us.
78 	 */
79 	if (optval == SOCK_STREAM) {
80 		level = IPPROTO_TCP;
81 		optname = TCP_ANONPRIVBIND;
82 	} else if (optval == SOCK_DGRAM) {
83 		level = IPPROTO_UDP;
84 		optname = UDP_ANONPRIVBIND;
85 	} else {
86 		errno = EPROTONOSUPPORT;
87 		return (-1);
88 	}
89 
90 	optval = 1;
91 	if (setsockopt(sd, level, optname, &optval, sizeof (optval)) < 0) {
92 		return (-1);
93 	}
94 
95 	bindaddr->sin_port = 0;
96 	ret = bind(sd, (struct sockaddr *)bindaddr,
97 	    sizeof (struct sockaddr_in));
98 
99 	/*
100 	 * Always turn off the option when we are done.  Note that by doing
101 	 * this, if the caller has set this option before calling
102 	 * bindresvport(), it will be unset.  But this should never happen...
103 	 */
104 	optval = 0;
105 	(void) setsockopt(sd, level, optname, &optval, sizeof (optval));
106 
107 	if (ret >= 0 && sin != NULL) {
108 		/*
109 		 * Historical note:
110 		 *
111 		 * Past versions of this bindresvport() code have
112 		 * returned with the reserved port number bound
113 		 * filled in its "sin" parameter (if passed in), perhaps
114 		 * "accidently" because of the structure of historical code.
115 		 *
116 		 * This is not documented but the behavior is
117 		 * explicitly retained here for compatibility to minimize
118 		 * risk to applications, even though it is not clear if this
119 		 * was a design intent.
120 		 */
121 		len = sizeof (struct sockaddr_in);
122 		(void) getsockname(sd, (struct sockaddr *)bindaddr, &len);
123 	}
124 	return (ret);
125 }
126