8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsocket / inet / bindresvport.c
blobd3cc2f5b8abac74bdeb04c7f36da0127d5ae75b2
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
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>
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>
46 #ifdef SYSV
47 #define bzero(s, len) (void) memset((s), 0, (len))
48 #endif
52 * Bind a socket to a privileged IP port
54 int
55 bindresvport(int sd, struct sockaddr_in *sin)
57 struct sockaddr_in myaddr;
58 struct sockaddr_in *bindaddr;
59 int level, optname;
60 int optval, len;
61 int ret;
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);
73 len = sizeof (optval);
74 if (getsockopt(sd, SOL_SOCKET, SO_TYPE, &optval, &len) < 0) {
75 return (-1);
78 * Use *_ANONPRIVBIND to ask the kernel to pick a port in the
79 * priviledged range for us.
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);
92 optval = 1;
93 if (setsockopt(sd, level, optname, &optval, sizeof (optval)) < 0) {
94 return (-1);
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...
106 optval = 0;
107 (void) setsockopt(sd, level, optname, &optval, sizeof (optval));
109 if (ret >= 0 && sin != NULL) {
111 * Historical note:
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);
126 return (ret);