1*0Sstevel@tonic-gate /* This file has be modified from the original OpenBSD source */
2*0Sstevel@tonic-gate 
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5*0Sstevel@tonic-gate  * unrestricted use provided that this legend is included on all tape
6*0Sstevel@tonic-gate  * media and as a part of the software program in whole or part.  Users
7*0Sstevel@tonic-gate  * may copy or modify Sun RPC without charge, but are not authorized
8*0Sstevel@tonic-gate  * to license or distribute it to anyone else except as part of a product or
9*0Sstevel@tonic-gate  * program developed by the user.
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12*0Sstevel@tonic-gate  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13*0Sstevel@tonic-gate  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * Sun RPC is provided with no support and without any obligation on the
16*0Sstevel@tonic-gate  * part of Sun Microsystems, Inc. to assist in its use, correction,
17*0Sstevel@tonic-gate  * modification or enhancement.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20*0Sstevel@tonic-gate  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21*0Sstevel@tonic-gate  * OR ANY PART THEREOF.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24*0Sstevel@tonic-gate  * or profits or other special, indirect and consequential damages, even if
25*0Sstevel@tonic-gate  * Sun has been advised of the possibility of such damages.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * 2550 Garcia Avenue
29*0Sstevel@tonic-gate  * Mountain View, California  94043
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include "includes.h"
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #ifndef HAVE_BINDRESVPORT_SA
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
37*0Sstevel@tonic-gate static char *rcsid = "$OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $";
38*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * Copyright (c) 1987 by Sun Microsystems, Inc.
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include "includes.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define STARTPORT 600
49*0Sstevel@tonic-gate #define ENDPORT (IPPORT_RESERVED - 1)
50*0Sstevel@tonic-gate #define NPORTS	(ENDPORT - STARTPORT + 1)
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * Bind a socket to a privileged IP port
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate int
bindresvport_sa(sd,sa)56*0Sstevel@tonic-gate bindresvport_sa(sd, sa)
57*0Sstevel@tonic-gate 	int sd;
58*0Sstevel@tonic-gate 	struct sockaddr *sa;
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	int error, af;
61*0Sstevel@tonic-gate 	struct sockaddr_storage myaddr;
62*0Sstevel@tonic-gate 	struct sockaddr_in *sin;
63*0Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
64*0Sstevel@tonic-gate 	u_int16_t *portp;
65*0Sstevel@tonic-gate 	u_int16_t port;
66*0Sstevel@tonic-gate 	socklen_t salen;
67*0Sstevel@tonic-gate 	int i;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (sa == NULL) {
70*0Sstevel@tonic-gate 		memset(&myaddr, 0, sizeof(myaddr));
71*0Sstevel@tonic-gate 		sa = (struct sockaddr *)&myaddr;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 		if (getsockname(sd, sa, &salen) == -1)
74*0Sstevel@tonic-gate 			return -1;	/* errno is correctly set */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 		af = sa->sa_family;
77*0Sstevel@tonic-gate 		memset(&myaddr, 0, salen);
78*0Sstevel@tonic-gate 	} else
79*0Sstevel@tonic-gate 		af = sa->sa_family;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (af == AF_INET) {
82*0Sstevel@tonic-gate 		/* LINTED */
83*0Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
84*0Sstevel@tonic-gate 		salen = sizeof(struct sockaddr_in);
85*0Sstevel@tonic-gate 		portp = &sin->sin_port;
86*0Sstevel@tonic-gate 	} else if (af == AF_INET6) {
87*0Sstevel@tonic-gate 		/* LINTED */
88*0Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
89*0Sstevel@tonic-gate 		salen = sizeof(struct sockaddr_in6);
90*0Sstevel@tonic-gate 		portp = &sin6->sin6_port;
91*0Sstevel@tonic-gate 	} else {
92*0Sstevel@tonic-gate 		errno = EPFNOSUPPORT;
93*0Sstevel@tonic-gate 		return (-1);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	sa->sa_family = af;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	port = ntohs(*portp);
98*0Sstevel@tonic-gate 	if (port == 0)
99*0Sstevel@tonic-gate 		port = (arc4random() % NPORTS) + STARTPORT;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/* Avoid warning */
102*0Sstevel@tonic-gate 	error = -1;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	for(i = 0; i < NPORTS; i++) {
105*0Sstevel@tonic-gate 		*portp = htons(port);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 		error = bind(sd, sa, salen);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 		/* Terminate on success */
110*0Sstevel@tonic-gate 		if (error == 0)
111*0Sstevel@tonic-gate 			break;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 		/* Terminate on errors, except "address already in use" */
114*0Sstevel@tonic-gate 		if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
115*0Sstevel@tonic-gate 			break;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 		port++;
118*0Sstevel@tonic-gate 		if (port > ENDPORT)
119*0Sstevel@tonic-gate 			port = STARTPORT;
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	return (error);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate #endif /* HAVE_BINDRESVPORT_SA */
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
128