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
;
65 if (bindaddr
== NULL
) {
67 bzero(bindaddr
, sizeof (*bindaddr
));
68 bindaddr
->sin_family
= AF_INET
;
69 } else if (bindaddr
->sin_family
!= AF_INET
) {
74 len
= sizeof (optval
);
75 if (getsockopt(sd
, SOL_SOCKET
, SO_TYPE
, &optval
, &len
) < 0) {
79 * Use *_ANONPRIVBIND to ask the kernel to pick a port in the
80 * priviledged range for us.
82 if (optval
== SOCK_STREAM
) {
84 optname
= TCP_ANONPRIVBIND
;
85 } else if (optval
== SOCK_DGRAM
) {
87 optname
= UDP_ANONPRIVBIND
;
89 errno
= EPROTONOSUPPORT
;
94 if (setsockopt(sd
, level
, optname
, &optval
, sizeof (optval
)) < 0) {
98 bindaddr
->sin_port
= 0;
99 ret
= bind(sd
, (struct sockaddr
*)bindaddr
,
100 sizeof (struct sockaddr_in
));
103 * Always turn off the option when we are done. Note that by doing
104 * this, if the caller has set this option before calling
105 * bindresvport(), it will be unset. But this should never happen...
108 (void) setsockopt(sd
, level
, optname
, &optval
, sizeof (optval
));
110 if (ret
>= 0 && sin
!= NULL
) {
114 * Past versions of this bindresvport() code have
115 * returned with the reserved port number bound
116 * filled in its "sin" parameter (if passed in), perhaps
117 * "accidently" because of the structure of historical code.
119 * This is not documented but the behavior is
120 * explicitly retained here for compatibility to minimize
121 * risk to applications, even though it is not clear if this
122 * was a design intent.
124 len
= sizeof (struct sockaddr_in
);
125 (void) getsockname(sd
, (struct sockaddr
*)bindaddr
, &len
);