xref: /titanic_41/usr/src/lib/libsocket/inet/bindresvport.c (revision 40cb5e5daa7b80bb70fcf8dadfb20f9281566331)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/types.h>
38 #include <errno.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <netinet/udp.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #ifdef SYSV
47 #define	bzero(s, len)	(void) memset((s), 0, (len))
48 #endif
49 
50 
51 /*
52  * Bind a socket to a privileged IP port
53  */
54 int
55 bindresvport(int sd, struct sockaddr_in *sin)
56 {
57 	struct sockaddr_in myaddr;
58 	struct sockaddr_in *bindaddr;
59 	int level, optname;
60 	int optval, len;
61 	int ret;
62 
63 	bindaddr = sin;
64 	if (bindaddr == (struct sockaddr_in *)0) {
65 		bindaddr = &myaddr;
66 		bzero(bindaddr, sizeof (*bindaddr));
67 		bindaddr->sin_family = AF_INET;
68 	} else if (bindaddr->sin_family != AF_INET) {
69 		errno = EPFNOSUPPORT;
70 		return (-1);
71 	}
72 
73 	len = sizeof (optval);
74 	if (getsockopt(sd, SOL_SOCKET, SO_TYPE, &optval, &len) < 0) {
75 		return (-1);
76 	}
77 	/*
78 	 * Use *_ANONPRIVBIND to ask the kernel to pick a port in the
79 	 * priviledged range for us.
80 	 */
81 	if (optval == SOCK_STREAM) {
82 		level = IPPROTO_TCP;
83 		optname = TCP_ANONPRIVBIND;
84 	} else if (optval == SOCK_DGRAM) {
85 		level = IPPROTO_UDP;
86 		optname = UDP_ANONPRIVBIND;
87 	} else {
88 		errno = EPROTONOSUPPORT;
89 		return (-1);
90 	}
91 
92 	optval = 1;
93 	if (setsockopt(sd, level, optname, &optval, sizeof (optval)) < 0) {
94 		return (-1);
95 	}
96 
97 	bindaddr->sin_port = 0;
98 	ret = bind(sd, (struct sockaddr *)bindaddr,
99 	    sizeof (struct sockaddr_in));
100 
101 	/*
102 	 * Always turn off the option when we are done.  Note that by doing
103 	 * this, if the caller has set this option before calling
104 	 * bindresvport(), it will be unset.  But this should never happen...
105 	 */
106 	optval = 0;
107 	(void) setsockopt(sd, level, optname, &optval, sizeof (optval));
108 
109 	if (ret >= 0 && sin != NULL) {
110 		/*
111 		 * Historical note:
112 		 *
113 		 * Past versions of this bindresvport() code have
114 		 * returned with the reserved port number bound
115 		 * filled in its "sin" parameter (if passed in), perhaps
116 		 * "accidently" because of the structure of historical code.
117 		 *
118 		 * This is not documented but the behavior is
119 		 * explicitly retained here for compatibility to minimize
120 		 * risk to applications, even though it is not clear if this
121 		 * was a design intent.
122 		 */
123 		len = sizeof (struct sockaddr_in);
124 		(void) getsockname(sd, (struct sockaddr *)bindaddr, &len);
125 	}
126 	return (ret);
127 }
128