xref: /onnv-gate/usr/src/lib/librpcsvc/common/bindresvport.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * XXX This routine should be changed to use
39*0Sstevel@tonic-gate  * ND_CHECK_RESERVED_PORT and ND_SET_RESERVED_PORT
40*0Sstevel@tonic-gate  * which can be invoked via netdir_options.
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <rpc/rpc.h>
44*0Sstevel@tonic-gate #include <netinet/in.h>
45*0Sstevel@tonic-gate #include <sys/socket.h>
46*0Sstevel@tonic-gate #include <netdb.h>
47*0Sstevel@tonic-gate #include <errno.h>
48*0Sstevel@tonic-gate #include <rpc/nettype.h>
49*0Sstevel@tonic-gate #include <stropts.h>
50*0Sstevel@tonic-gate #include <string.h>
51*0Sstevel@tonic-gate #include <tiuser.h>
52*0Sstevel@tonic-gate #include <unistd.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	STARTPORT 600
55*0Sstevel@tonic-gate #define	ENDPORT (IPPORT_RESERVED - 1)
56*0Sstevel@tonic-gate #define	NPORTS	(ENDPORT - STARTPORT + 1)
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  * The argument is a client handle for a UDP connection.
60*0Sstevel@tonic-gate  * Unbind its transport endpoint from the existing port
61*0Sstevel@tonic-gate  * and rebind it to a reserved port.
62*0Sstevel@tonic-gate  * On failure, the client handle can be unbound even if it
63*0Sstevel@tonic-gate  * was previously bound.  Callers should destroy the client
64*0Sstevel@tonic-gate  * handle after a failure.
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate int
__clnt_bindresvport(cl)67*0Sstevel@tonic-gate __clnt_bindresvport(cl)
68*0Sstevel@tonic-gate 	CLIENT *cl;
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	int fd;
71*0Sstevel@tonic-gate 	int res;
72*0Sstevel@tonic-gate 	short port;
73*0Sstevel@tonic-gate 	struct sockaddr_in *sin;
74*0Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
75*0Sstevel@tonic-gate 	extern int errno;
76*0Sstevel@tonic-gate 	/* extern int t_errno; */
77*0Sstevel@tonic-gate 	struct t_bind *tbind, *tres;
78*0Sstevel@tonic-gate 	int i;
79*0Sstevel@tonic-gate 	bool_t	ipv6_fl = FALSE;
80*0Sstevel@tonic-gate 	struct netconfig *nconf;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	/* make sure it's a UDP connection */
83*0Sstevel@tonic-gate 	nconf = getnetconfigent(cl->cl_netid);
84*0Sstevel@tonic-gate 	if (nconf == NULL)
85*0Sstevel@tonic-gate 		return (-1);
86*0Sstevel@tonic-gate 	if ((nconf->nc_semantics != NC_TPI_CLTS) ||
87*0Sstevel@tonic-gate 		(strcmp(nconf->nc_protofmly, NC_INET) &&
88*0Sstevel@tonic-gate 		strcmp(nconf->nc_protofmly, NC_INET)) ||
89*0Sstevel@tonic-gate 		strcmp(nconf->nc_proto, NC_UDP)) {
90*0Sstevel@tonic-gate 		freenetconfigent(nconf);
91*0Sstevel@tonic-gate 		return (0);	/* not udp - don't need resv port */
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 	if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
94*0Sstevel@tonic-gate 		ipv6_fl = TRUE;
95*0Sstevel@tonic-gate 	freenetconfigent(nconf);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	if (!clnt_control(cl, CLGET_FD, (char *)&fd)) {
98*0Sstevel@tonic-gate 		return (-1);
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/* If fd is already bound - unbind it */
102*0Sstevel@tonic-gate 	if (t_getstate(fd) != T_UNBND) {
103*0Sstevel@tonic-gate 		while ((t_unbind(fd) < 0) && (t_errno == TLOOK)) {
104*0Sstevel@tonic-gate 			/*
105*0Sstevel@tonic-gate 			 * If there is a message queued to this descriptor,
106*0Sstevel@tonic-gate 			 * remove it.
107*0Sstevel@tonic-gate 			 */
108*0Sstevel@tonic-gate 			struct strbuf ctl[1], data[1];
109*0Sstevel@tonic-gate 			char ctlbuf[sizeof (union T_primitives) + 32];
110*0Sstevel@tonic-gate 			char databuf[256];
111*0Sstevel@tonic-gate 			int flags;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 			ctl->maxlen = sizeof (ctlbuf);
114*0Sstevel@tonic-gate 			ctl->buf = ctlbuf;
115*0Sstevel@tonic-gate 			data->maxlen = sizeof (databuf);
116*0Sstevel@tonic-gate 			data->buf = databuf;
117*0Sstevel@tonic-gate 			flags = 0;
118*0Sstevel@tonic-gate 			if (getmsg(fd, ctl, data, &flags) < 0)
119*0Sstevel@tonic-gate 				return (-1);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 		if (t_getstate(fd) != T_UNBND)
123*0Sstevel@tonic-gate 			return (-1);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
127*0Sstevel@tonic-gate 	if (tbind == NULL) {
128*0Sstevel@tonic-gate 		if (t_errno == TBADF)
129*0Sstevel@tonic-gate 			errno = EBADF;
130*0Sstevel@tonic-gate 		return (-1);
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
133*0Sstevel@tonic-gate 	if (tres == NULL) {
134*0Sstevel@tonic-gate 		(void) t_free((char *)tbind, T_BIND);
135*0Sstevel@tonic-gate 		return (-1);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	(void) memset((char *)tbind->addr.buf, 0, tbind->addr.len);
139*0Sstevel@tonic-gate 	/* warning: this sockaddr_in is truncated to 8 bytes */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (ipv6_fl == TRUE) {
142*0Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)tbind->addr.buf;
143*0Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
144*0Sstevel@tonic-gate 	} else {
145*0Sstevel@tonic-gate 		sin = (struct sockaddr_in *)tbind->addr.buf;
146*0Sstevel@tonic-gate 		sin->sin_family = AF_INET;
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	tbind->qlen = 0;
150*0Sstevel@tonic-gate 	tbind->addr.len = tbind->addr.maxlen;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/*
153*0Sstevel@tonic-gate 	 * Need to find a reserved port in the interval
154*0Sstevel@tonic-gate 	 * STARTPORT - ENDPORT.  Choose a random starting
155*0Sstevel@tonic-gate 	 * place in the interval based on the process pid
156*0Sstevel@tonic-gate 	 * and sequentially search the ports for one
157*0Sstevel@tonic-gate 	 * that is available.
158*0Sstevel@tonic-gate 	 */
159*0Sstevel@tonic-gate 	port = (getpid() % NPORTS) + STARTPORT;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	for (i = 0; i < NPORTS; i++) {
162*0Sstevel@tonic-gate 		sin->sin_port = htons(port++);
163*0Sstevel@tonic-gate 		if (port > ENDPORT)
164*0Sstevel@tonic-gate 			port = STARTPORT;
165*0Sstevel@tonic-gate 		/*
166*0Sstevel@tonic-gate 		 * Try to bind to the requested address.  If
167*0Sstevel@tonic-gate 		 * the call to t_bind succeeds, then we need
168*0Sstevel@tonic-gate 		 * to make sure that the address that we bound
169*0Sstevel@tonic-gate 		 * to was the address that we requested.  If it
170*0Sstevel@tonic-gate 		 * was, then we are done.  If not, we fake an
171*0Sstevel@tonic-gate 		 * EADDRINUSE error by setting res, t_errno,
172*0Sstevel@tonic-gate 		 * and errno to indicate that a bind failure
173*0Sstevel@tonic-gate 		 * occurred.  Otherwise, if the t_bind call
174*0Sstevel@tonic-gate 		 * failed, we check to see whether it makes
175*0Sstevel@tonic-gate 		 * sense to continue trying to t_bind requests.
176*0Sstevel@tonic-gate 		 */
177*0Sstevel@tonic-gate 		res = t_bind(fd, tbind, tres);
178*0Sstevel@tonic-gate 		if (res == 0) {
179*0Sstevel@tonic-gate 			if (memcmp(tbind->addr.buf, tres->addr.buf,
180*0Sstevel@tonic-gate 					(int)tres->addr.len) == 0)
181*0Sstevel@tonic-gate 				break;
182*0Sstevel@tonic-gate 			(void) t_unbind(fd);
183*0Sstevel@tonic-gate 			res = -1;
184*0Sstevel@tonic-gate 			t_errno = TSYSERR;
185*0Sstevel@tonic-gate 			errno = EADDRINUSE;
186*0Sstevel@tonic-gate 		} else if (t_errno != TSYSERR || errno != EADDRINUSE) {
187*0Sstevel@tonic-gate 			if (t_errno == TACCES)
188*0Sstevel@tonic-gate 				errno = EACCES;
189*0Sstevel@tonic-gate 			break;
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	(void) t_free((char *)tbind, T_BIND);
194*0Sstevel@tonic-gate 	(void) t_free((char *)tres,  T_BIND);
195*0Sstevel@tonic-gate 	return (res);
196*0Sstevel@tonic-gate }
197