Home | History | Annotate | Line # | Download | only in rpc
rpc_soc.c revision 1.13.8.1
      1 /*	$NetBSD: rpc_soc.c,v 1.13.8.1 2013/03/14 22:03:15 riz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, Oracle America, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  *       notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  *       copyright notice, this list of conditions and the following
     14  *       disclaimer in the documentation and/or other materials
     15  *       provided with the distribution.
     16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  *       contributors may be used to endorse or promote products derived
     18  *       from this software without specific prior written permission.
     19  *
     20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /* #ident	"@(#)rpc_soc.c	1.17	94/04/24 SMI" */
     35 
     36 /*
     37  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     38  * In addition, portions of such source code were derived from Berkeley
     39  * 4.3 BSD under license from the Regents of the University of
     40  * California.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 #if defined(LIBC_SCCS) && !defined(lint)
     45 #if 0
     46 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
     47 #else
     48 __RCSID("$NetBSD: rpc_soc.c,v 1.13.8.1 2013/03/14 22:03:15 riz Exp $");
     49 #endif
     50 #endif
     51 
     52 #ifdef PORTMAP
     53 /*
     54  * rpc_soc.c
     55  *
     56  * The backward compatibility routines for the earlier implementation
     57  * of RPC, where the only transports supported were tcp/ip and udp/ip.
     58  * Based on berkeley socket abstraction, now implemented on the top
     59  * of TLI/Streams
     60  */
     61 
     62 #include "namespace.h"
     63 #include "reentrant.h"
     64 #include <sys/types.h>
     65 #include <sys/socket.h>
     66 #include <stdio.h>
     67 #include <rpc/rpc.h>
     68 #include <rpc/pmap_clnt.h>
     69 #include <rpc/pmap_prot.h>
     70 #include <rpc/nettype.h>
     71 #include <netinet/in.h>
     72 #include <assert.h>
     73 #include <errno.h>
     74 #include <netdb.h>
     75 #include <stdlib.h>
     76 #include <string.h>
     77 #include <syslog.h>
     78 #include <unistd.h>
     79 
     80 #include "rpc_internal.h"
     81 
     82 #ifdef __weak_alias
     83 __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
     84 __weak_alias(clntudp_create,_clntudp_create)
     85 __weak_alias(clnttcp_create,_clnttcp_create)
     86 __weak_alias(clntraw_create,_clntraw_create)
     87 __weak_alias(get_myaddress,_get_myaddress)
     88 __weak_alias(svcfd_create,_svcfd_create)
     89 __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
     90 __weak_alias(svcudp_create,_svcudp_create)
     91 __weak_alias(svctcp_create,_svctcp_create)
     92 __weak_alias(svcraw_create,_svcraw_create)
     93 __weak_alias(callrpc,_callrpc)
     94 __weak_alias(registerrpc,_registerrpc)
     95 __weak_alias(clnt_broadcast,_clnt_broadcast)
     96 #endif
     97 
     98 #ifdef _REENTRANT
     99 extern mutex_t	rpcsoc_lock;
    100 #endif
    101 
    102 static CLIENT *clnt_com_create __P((struct sockaddr_in *, rpcprog_t, rpcvers_t,
    103 				    int *, u_int, u_int, const char *));
    104 static SVCXPRT *svc_com_create __P((int, u_int, u_int, const char *));
    105 static bool_t rpc_wrap_bcast __P((char *, struct netbuf *, struct netconfig *));
    106 
    107 /*
    108  * A common clnt create routine
    109  */
    110 static CLIENT *
    111 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
    112 	struct sockaddr_in *raddr;
    113 	rpcprog_t prog;
    114 	rpcvers_t vers;
    115 	int *sockp;
    116 	u_int sendsz;
    117 	u_int recvsz;
    118 	const char *tp;
    119 {
    120 	CLIENT *cl;
    121 	int madefd = FALSE;
    122 	int fd;
    123 	struct netconfig *nconf;
    124 	struct netbuf bindaddr;
    125 
    126 	_DIAGASSERT(raddr != NULL);
    127 	_DIAGASSERT(sockp != NULL);
    128 	_DIAGASSERT(tp != NULL);
    129 
    130 	fd = *sockp;
    131 
    132 	mutex_lock(&rpcsoc_lock);
    133 	if ((nconf = __rpc_getconfip(tp)) == NULL) {
    134 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    135 		mutex_unlock(&rpcsoc_lock);
    136 		return (NULL);
    137 	}
    138 	if (fd == RPC_ANYSOCK) {
    139 		fd = __rpc_nconf2fd(nconf);
    140 		if (fd == -1)
    141 			goto syserror;
    142 		madefd = TRUE;
    143 	}
    144 
    145 	if (raddr->sin_port == 0) {
    146 		u_int proto;
    147 		u_short sport;
    148 
    149 		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
    150 		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
    151 		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
    152 		    proto);
    153 		if (sport == 0) {
    154 			goto err;
    155 		}
    156 		raddr->sin_port = htons(sport);
    157 		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
    158 	}
    159 
    160 	/* Transform sockaddr_in to netbuf */
    161 	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
    162 	bindaddr.buf = raddr;
    163 
    164 	bindresvport(fd, NULL);
    165 	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
    166 				sendsz, recvsz);
    167 	if (cl) {
    168 		if (madefd == TRUE) {
    169 			/*
    170 			 * The fd should be closed while destroying the handle.
    171 			 */
    172 			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
    173 			*sockp = fd;
    174 		}
    175 		(void) freenetconfigent(nconf);
    176 		mutex_unlock(&rpcsoc_lock);
    177 		return (cl);
    178 	}
    179 	goto err;
    180 
    181 syserror:
    182 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    183 	rpc_createerr.cf_error.re_errno = errno;
    184 
    185 err:	if (madefd == TRUE)
    186 		(void) close(fd);
    187 	(void) freenetconfigent(nconf);
    188 	mutex_unlock(&rpcsoc_lock);
    189 	return (NULL);
    190 }
    191 
    192 CLIENT *
    193 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
    194 	struct sockaddr_in *raddr;
    195 	u_long prog;
    196 	u_long vers;
    197 	struct timeval wait;
    198 	int *sockp;
    199 	u_int sendsz;
    200 	u_int recvsz;
    201 {
    202 	CLIENT *cl;
    203 
    204 	_DIAGASSERT(raddr != NULL);
    205 	_DIAGASSERT(sockp != NULL);
    206 
    207 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
    208 	    sendsz, recvsz, "udp");
    209 	if (cl == NULL) {
    210 		return (NULL);
    211 	}
    212 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait);
    213 	return (cl);
    214 }
    215 
    216 CLIENT *
    217 clntudp_create(raddr, program, version, wait, sockp)
    218 	struct sockaddr_in *raddr;
    219 	u_long program;
    220 	u_long version;
    221 	struct timeval wait;
    222 	int *sockp;
    223 {
    224 	return clntudp_bufcreate(raddr, program, version, wait, sockp,
    225 					UDPMSGSIZE, UDPMSGSIZE);
    226 }
    227 
    228 CLIENT *
    229 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
    230 	struct sockaddr_in *raddr;
    231 	u_long prog;
    232 	u_long vers;
    233 	int *sockp;
    234 	u_int sendsz;
    235 	u_int recvsz;
    236 {
    237 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
    238 	    sendsz, recvsz, "tcp");
    239 }
    240 
    241 CLIENT *
    242 clntraw_create(prog, vers)
    243 	u_long prog;
    244 	u_long vers;
    245 {
    246 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
    247 }
    248 
    249 /*
    250  * A common server create routine
    251  */
    252 static SVCXPRT *
    253 svc_com_create(fd, sendsize, recvsize, netid)
    254 	int fd;
    255 	u_int sendsize;
    256 	u_int recvsize;
    257 	const char *netid;
    258 {
    259 	struct netconfig *nconf;
    260 	SVCXPRT *svc;
    261 	int madefd = FALSE;
    262 	int port;
    263 	struct sockaddr_in sccsin;
    264 
    265 	_DIAGASSERT(netid != NULL);
    266 
    267 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
    268 		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
    269 		return (NULL);
    270 	}
    271 	if (fd == RPC_ANYSOCK) {
    272 		fd = __rpc_nconf2fd(nconf);
    273 		if (fd == -1) {
    274 			(void) freenetconfigent(nconf);
    275 			(void) syslog(LOG_ERR,
    276 			"svc%s_create: could not open connection", netid);
    277 			return (NULL);
    278 		}
    279 		madefd = TRUE;
    280 	}
    281 
    282 	memset(&sccsin, 0, sizeof sccsin);
    283 	sccsin.sin_family = AF_INET;
    284 	bindresvport(fd, &sccsin);
    285 	listen(fd, SOMAXCONN);
    286 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
    287 	(void) freenetconfigent(nconf);
    288 	if (svc == NULL) {
    289 		if (madefd)
    290 			(void) close(fd);
    291 		return (NULL);
    292 	}
    293 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
    294 	svc->xp_port = ntohs(port);
    295 	return (svc);
    296 }
    297 
    298 SVCXPRT *
    299 svctcp_create(fd, sendsize, recvsize)
    300 	int fd;
    301 	u_int sendsize;
    302 	u_int recvsize;
    303 {
    304 	return svc_com_create(fd, sendsize, recvsize, "tcp");
    305 }
    306 
    307 SVCXPRT *
    308 svcudp_bufcreate(fd, sendsz, recvsz)
    309 	int fd;
    310 	u_int sendsz, recvsz;
    311 {
    312 	return svc_com_create(fd, sendsz, recvsz, "udp");
    313 }
    314 
    315 SVCXPRT *
    316 svcfd_create(fd, sendsize, recvsize)
    317 	int fd;
    318 	u_int sendsize;
    319 	u_int recvsize;
    320 {
    321 	return svc_fd_create(fd, sendsize, recvsize);
    322 }
    323 
    324 
    325 SVCXPRT *
    326 svcudp_create(fd)
    327 	int fd;
    328 {
    329 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
    330 }
    331 
    332 SVCXPRT *
    333 svcraw_create()
    334 {
    335 	return svc_raw_create();
    336 }
    337 
    338 int
    339 get_myaddress(addr)
    340 	struct sockaddr_in *addr;
    341 {
    342 
    343 	_DIAGASSERT(addr != NULL);
    344 
    345 	memset((void *) addr, 0, sizeof(*addr));
    346 	addr->sin_family = AF_INET;
    347 	addr->sin_port = htons(PMAPPORT);
    348 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    349 	return (0);
    350 }
    351 
    352 /*
    353  * For connectionless "udp" transport. Obsoleted by rpc_call().
    354  */
    355 int
    356 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
    357 	char *host;
    358 	int prognum, versnum, procnum;
    359 	xdrproc_t inproc, outproc;
    360 	char *in, *out;
    361 {
    362 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
    363 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
    364 }
    365 
    366 /*
    367  * For connectionless kind of transport. Obsoleted by rpc_reg()
    368  */
    369 int
    370 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
    371 	int prognum, versnum, procnum;
    372 	char *(*progname) __P((char [UDPMSGSIZE]));
    373 	xdrproc_t inproc, outproc;
    374 {
    375 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
    376 	    (rpcproc_t)procnum, progname, inproc, outproc, __UNCONST("udp"));
    377 }
    378 
    379 /*
    380  * All the following clnt_broadcast stuff is convulated; it supports
    381  * the earlier calling style of the callback function
    382  */
    383 #ifdef _REENTRANT
    384 static thread_key_t	clnt_broadcast_key;
    385 #endif
    386 static resultproc_t	clnt_broadcast_result_main;
    387 
    388 /*
    389  * Need to translate the netbuf address into sockaddr_in address.
    390  * Dont care about netid here.
    391  */
    392 /* ARGSUSED */
    393 static bool_t
    394 rpc_wrap_bcast(resultp, addr, nconf)
    395 	char *resultp;		/* results of the call */
    396 	struct netbuf *addr;	/* address of the guy who responded */
    397 	struct netconfig *nconf; /* Netconf of the transport */
    398 {
    399 	resultproc_t clnt_broadcast_result;
    400 
    401 	_DIAGASSERT(resultp != NULL);
    402 	_DIAGASSERT(addr != NULL);
    403 	_DIAGASSERT(nconf != NULL);
    404 
    405 	if (strcmp(nconf->nc_netid, "udp"))
    406 		return (FALSE);
    407 #ifdef _REENTRANT
    408 	if (__isthreaded == 0)
    409 		clnt_broadcast_result = clnt_broadcast_result_main;
    410 	else
    411 		clnt_broadcast_result = thr_getspecific(clnt_broadcast_key);
    412 #else
    413 	clnt_broadcast_result = clnt_broadcast_result_main;
    414 #endif
    415 	return (*clnt_broadcast_result)(resultp,
    416 				(struct sockaddr_in *)addr->buf);
    417 }
    418 
    419 #ifdef _REENTRANT
    420 static once_t clnt_broadcast_once = ONCE_INITIALIZER;
    421 
    422 static void
    423 clnt_broadcast_setup(void)
    424 {
    425 
    426 	thr_keycreate(&clnt_broadcast_key, free);
    427 }
    428 #endif
    429 
    430 /*
    431  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
    432  */
    433 enum clnt_stat
    434 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
    435 	u_long		prog;		/* program number */
    436 	u_long		vers;		/* version number */
    437 	u_long		proc;		/* procedure number */
    438 	xdrproc_t	xargs;		/* xdr routine for args */
    439 	caddr_t		argsp;		/* pointer to args */
    440 	xdrproc_t	xresults;	/* xdr routine for results */
    441 	caddr_t		resultsp;	/* pointer to results */
    442 	resultproc_t	eachresult;	/* call with each result obtained */
    443 {
    444 #ifdef _REENTRANT
    445 	if (__isthreaded == 0)
    446 		clnt_broadcast_result_main = eachresult;
    447 	else {
    448 		thr_once(&clnt_broadcast_once, clnt_broadcast_setup);
    449 		thr_setspecific(clnt_broadcast_key, (void *) eachresult);
    450 	}
    451 #else
    452 	clnt_broadcast_result_main = eachresult;
    453 #endif
    454 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
    455 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
    456 	    (resultproc_t) rpc_wrap_bcast, "udp");
    457 }
    458 
    459 #endif /* PORTMAP */
    460