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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <netinet/udp.h>
47 #define bzero(s, len) (void) memset((s), 0, (len))
52 * Bind a socket to a privileged IP port
55 bindresvport(int sd
, struct sockaddr_in
*sin
)
57 struct sockaddr_in myaddr
;
58 struct sockaddr_in
*bindaddr
;
64 if (bindaddr
== (struct sockaddr_in
*)0) {
66 bzero(bindaddr
, sizeof (*bindaddr
));
67 bindaddr
->sin_family
= AF_INET
;
68 } else if (bindaddr
->sin_family
!= AF_INET
) {
73 len
= sizeof (optval
);
74 if (getsockopt(sd
, SOL_SOCKET
, SO_TYPE
, &optval
, &len
) < 0) {
78 * Use *_ANONPRIVBIND to ask the kernel to pick a port in the
79 * priviledged range for us.
81 if (optval
== SOCK_STREAM
) {
83 optname
= TCP_ANONPRIVBIND
;
84 } else if (optval
== SOCK_DGRAM
) {
86 optname
= UDP_ANONPRIVBIND
;
88 errno
= EPROTONOSUPPORT
;
93 if (setsockopt(sd
, level
, optname
, &optval
, sizeof (optval
)) < 0) {
97 bindaddr
->sin_port
= 0;
98 ret
= bind(sd
, (struct sockaddr
*)bindaddr
,
99 sizeof (struct sockaddr_in
));
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...
107 (void) setsockopt(sd
, level
, optname
, &optval
, sizeof (optval
));
109 if (ret
>= 0 && sin
!= NULL
) {
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.
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.
123 len
= sizeof (struct sockaddr_in
);
124 (void) getsockname(sd
, (struct sockaddr
*)bindaddr
, &len
);