10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1987 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
28*722Smuffin
290Sstevel@tonic-gate #include <sys/types.h>
30*722Smuffin #include <errno.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <netinet/in.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * Bind a socket to a privileged IP port
360Sstevel@tonic-gate */
37*722Smuffin int
bindresvport(int sd,struct sockaddr_in * sin)38*722Smuffin bindresvport(int sd, struct sockaddr_in *sin)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate int res;
410Sstevel@tonic-gate static short port;
420Sstevel@tonic-gate struct sockaddr_in myaddr;
430Sstevel@tonic-gate int i;
440Sstevel@tonic-gate
450Sstevel@tonic-gate #define STARTPORT 600
460Sstevel@tonic-gate #define ENDPORT (IPPORT_RESERVED - 1)
470Sstevel@tonic-gate #define NPORTS (ENDPORT - STARTPORT + 1)
480Sstevel@tonic-gate
490Sstevel@tonic-gate if (sin == (struct sockaddr_in *)0) {
500Sstevel@tonic-gate sin = &myaddr;
510Sstevel@tonic-gate bzero(sin, sizeof (*sin));
520Sstevel@tonic-gate sin->sin_family = AF_INET;
530Sstevel@tonic-gate } else if (sin->sin_family != AF_INET) {
540Sstevel@tonic-gate errno = EPFNOSUPPORT;
550Sstevel@tonic-gate return (-1);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate if (port == 0) {
580Sstevel@tonic-gate port = (getpid() % NPORTS) + STARTPORT;
590Sstevel@tonic-gate }
600Sstevel@tonic-gate res = -1;
610Sstevel@tonic-gate errno = EADDRINUSE;
620Sstevel@tonic-gate for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; i++) {
630Sstevel@tonic-gate sin->sin_port = htons(port++);
640Sstevel@tonic-gate if (port > ENDPORT) {
650Sstevel@tonic-gate port = STARTPORT;
660Sstevel@tonic-gate }
670Sstevel@tonic-gate res = bind(sd, sin, sizeof(struct sockaddr_in));
680Sstevel@tonic-gate }
690Sstevel@tonic-gate return (res);
700Sstevel@tonic-gate }
71