Home | History | Annotate | Line # | Download | only in rpc
svc_vc.c revision 1.8.2.3
      1  1.8.2.3   nathanw /*	$NetBSD: svc_vc.c,v 1.8.2.3 2002/03/22 20:42:24 nathanw Exp $	*/
      2      1.1      fvdl 
      3      1.1      fvdl /*
      4      1.1      fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5      1.1      fvdl  * unrestricted use provided that this legend is included on all tape
      6      1.1      fvdl  * media and as a part of the software program in whole or part.  Users
      7      1.1      fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      8      1.1      fvdl  * to license or distribute it to anyone else except as part of a product or
      9      1.1      fvdl  * program developed by the user.
     10      1.1      fvdl  *
     11      1.1      fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12      1.1      fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13      1.1      fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14      1.1      fvdl  *
     15      1.1      fvdl  * Sun RPC is provided with no support and without any obligation on the
     16      1.1      fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17      1.1      fvdl  * modification or enhancement.
     18      1.1      fvdl  *
     19      1.1      fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20      1.1      fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21      1.1      fvdl  * OR ANY PART THEREOF.
     22      1.1      fvdl  *
     23      1.1      fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24      1.1      fvdl  * or profits or other special, indirect and consequential damages, even if
     25      1.1      fvdl  * Sun has been advised of the possibility of such damages.
     26      1.1      fvdl  *
     27      1.1      fvdl  * Sun Microsystems, Inc.
     28      1.1      fvdl  * 2550 Garcia Avenue
     29      1.1      fvdl  * Mountain View, California  94043
     30      1.1      fvdl  */
     31      1.1      fvdl 
     32      1.1      fvdl #include <sys/cdefs.h>
     33      1.1      fvdl #if defined(LIBC_SCCS) && !defined(lint)
     34      1.1      fvdl #if 0
     35      1.1      fvdl static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
     36      1.1      fvdl static char *sccsid = "@(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC";
     37      1.1      fvdl #else
     38  1.8.2.3   nathanw __RCSID("$NetBSD: svc_vc.c,v 1.8.2.3 2002/03/22 20:42:24 nathanw Exp $");
     39      1.1      fvdl #endif
     40      1.1      fvdl #endif
     41      1.1      fvdl 
     42      1.1      fvdl /*
     43      1.1      fvdl  * svc_vc.c, Server side for Connection Oriented based RPC.
     44      1.1      fvdl  *
     45      1.1      fvdl  * Actually implements two flavors of transporter -
     46      1.1      fvdl  * a tcp rendezvouser (a listner and connection establisher)
     47      1.1      fvdl  * and a record/tcp stream.
     48      1.1      fvdl  */
     49      1.1      fvdl 
     50      1.1      fvdl #include "namespace.h"
     51      1.1      fvdl #include "reentrant.h"
     52      1.1      fvdl #include <sys/types.h>
     53      1.1      fvdl #include <sys/param.h>
     54      1.1      fvdl #include <sys/poll.h>
     55      1.1      fvdl #include <sys/socket.h>
     56      1.1      fvdl #include <sys/un.h>
     57      1.1      fvdl #include <netinet/in.h>
     58      1.1      fvdl #include <netinet/tcp.h>
     59      1.1      fvdl 
     60      1.1      fvdl #include <assert.h>
     61      1.1      fvdl #include <err.h>
     62      1.1      fvdl #include <errno.h>
     63      1.1      fvdl #include <stdio.h>
     64      1.1      fvdl #include <stdlib.h>
     65      1.1      fvdl #include <string.h>
     66      1.1      fvdl #include <unistd.h>
     67      1.1      fvdl 
     68      1.1      fvdl #include <rpc/rpc.h>
     69      1.1      fvdl 
     70      1.1      fvdl #include "rpc_com.h"
     71      1.1      fvdl 
     72      1.1      fvdl #ifdef __weak_alias
     73      1.1      fvdl __weak_alias(svc_fd_create,_svc_fd_create)
     74      1.1      fvdl __weak_alias(svc_vc_create,_svc_vc_create)
     75      1.1      fvdl #endif
     76      1.1      fvdl 
     77      1.1      fvdl static SVCXPRT *makefd_xprt __P((int, u_int, u_int));
     78      1.1      fvdl static bool_t rendezvous_request __P((SVCXPRT *, struct rpc_msg *));
     79      1.1      fvdl static enum xprt_stat rendezvous_stat __P((SVCXPRT *));
     80      1.1      fvdl static void svc_vc_destroy __P((SVCXPRT *));
     81      1.1      fvdl static int read_vc __P((caddr_t, caddr_t, int));
     82      1.1      fvdl static int write_vc __P((caddr_t, caddr_t, int));
     83      1.1      fvdl static enum xprt_stat svc_vc_stat __P((SVCXPRT *));
     84      1.1      fvdl static bool_t svc_vc_recv __P((SVCXPRT *, struct rpc_msg *));
     85      1.1      fvdl static bool_t svc_vc_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     86      1.1      fvdl static bool_t svc_vc_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     87      1.1      fvdl static bool_t svc_vc_reply __P((SVCXPRT *, struct rpc_msg *));
     88      1.1      fvdl static void svc_vc_rendezvous_ops __P((SVCXPRT *));
     89      1.1      fvdl static void svc_vc_ops __P((SVCXPRT *));
     90      1.1      fvdl static bool_t svc_vc_control __P((SVCXPRT *xprt, const u_int rq, void *in));
     91      1.1      fvdl 
     92      1.1      fvdl struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
     93      1.1      fvdl 	u_int sendsize;
     94      1.1      fvdl 	u_int recvsize;
     95      1.1      fvdl };
     96      1.1      fvdl 
     97      1.1      fvdl struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
     98      1.1      fvdl 	enum xprt_stat strm_stat;
     99      1.1      fvdl 	u_int32_t x_id;
    100      1.1      fvdl 	XDR xdrs;
    101      1.1      fvdl 	char verf_body[MAX_AUTH_BYTES];
    102      1.1      fvdl };
    103      1.1      fvdl 
    104      1.1      fvdl /*
    105      1.1      fvdl  * Usage:
    106      1.1      fvdl  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
    107      1.1      fvdl  *
    108      1.1      fvdl  * Creates, registers, and returns a (rpc) tcp based transporter.
    109      1.1      fvdl  * Once *xprt is initialized, it is registered as a transporter
    110      1.1      fvdl  * see (svc.h, xprt_register).  This routine returns
    111      1.1      fvdl  * a NULL if a problem occurred.
    112      1.1      fvdl  *
    113      1.1      fvdl  * The filedescriptor passed in is expected to refer to a bound, but
    114      1.1      fvdl  * not yet connected socket.
    115      1.1      fvdl  *
    116      1.1      fvdl  * Since streams do buffered io similar to stdio, the caller can specify
    117      1.1      fvdl  * how big the send and receive buffers are via the second and third parms;
    118      1.1      fvdl  * 0 => use the system default.
    119      1.1      fvdl  */
    120      1.1      fvdl SVCXPRT *
    121      1.1      fvdl svc_vc_create(fd, sendsize, recvsize)
    122      1.1      fvdl 	int fd;
    123      1.1      fvdl 	u_int sendsize;
    124      1.1      fvdl 	u_int recvsize;
    125      1.1      fvdl {
    126      1.1      fvdl 	SVCXPRT *xprt;
    127      1.1      fvdl 	struct cf_rendezvous *r = NULL;
    128      1.1      fvdl 	struct __rpc_sockinfo si;
    129      1.1      fvdl 	struct sockaddr_storage sslocal;
    130      1.1      fvdl 	socklen_t slen;
    131      1.3   thorpej 	int one = 1;
    132      1.1      fvdl 
    133      1.5  christos 	r = mem_alloc(sizeof(*r));
    134      1.1      fvdl 	if (r == NULL) {
    135      1.1      fvdl 		warnx("svc_vc_create: out of memory");
    136      1.1      fvdl 		goto cleanup_svc_vc_create;
    137      1.1      fvdl 	}
    138      1.1      fvdl 	if (!__rpc_fd2sockinfo(fd, &si))
    139      1.1      fvdl 		return NULL;
    140      1.5  christos 	r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
    141      1.5  christos 	r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
    142      1.5  christos 	xprt = mem_alloc(sizeof(SVCXPRT));
    143      1.1      fvdl 	if (xprt == NULL) {
    144      1.1      fvdl 		warnx("svc_vc_create: out of memory");
    145      1.1      fvdl 		goto cleanup_svc_vc_create;
    146      1.1      fvdl 	}
    147      1.1      fvdl 	xprt->xp_tp = NULL;
    148      1.1      fvdl 	xprt->xp_p1 = (caddr_t)(void *)r;
    149      1.1      fvdl 	xprt->xp_p2 = NULL;
    150      1.1      fvdl 	xprt->xp_p3 = NULL;
    151      1.1      fvdl 	xprt->xp_verf = _null_auth;
    152      1.6    kleink 	svc_vc_rendezvous_ops(xprt);
    153      1.5  christos 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
    154      1.1      fvdl 	xprt->xp_fd = fd;
    155      1.1      fvdl 
    156      1.1      fvdl 	slen = sizeof (struct sockaddr_storage);
    157      1.5  christos 	if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
    158      1.1      fvdl 		warnx("svc_vc_create: could not retrieve local addr");
    159      1.1      fvdl 		goto cleanup_svc_vc_create;
    160      1.1      fvdl 	}
    161      1.1      fvdl 
    162      1.1      fvdl 	/*
    163      1.1      fvdl 	 * We want to be able to check credentials on local sockets.
    164      1.1      fvdl 	 */
    165      1.1      fvdl 	if (sslocal.ss_family == AF_LOCAL)
    166      1.1      fvdl 		if (setsockopt(fd, 0, LOCAL_CREDS, &one, sizeof one) < 0)
    167      1.1      fvdl 			goto cleanup_svc_vc_create;
    168      1.1      fvdl 
    169      1.1      fvdl 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
    170      1.5  christos 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
    171      1.1      fvdl 	if (xprt->xp_ltaddr.buf == NULL) {
    172      1.1      fvdl 		warnx("svc_vc_create: no mem for local addr");
    173      1.1      fvdl 		goto cleanup_svc_vc_create;
    174      1.1      fvdl 	}
    175      1.5  christos 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
    176      1.1      fvdl 
    177      1.1      fvdl 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
    178      1.1      fvdl 	xprt_register(xprt);
    179      1.1      fvdl 	return (xprt);
    180      1.1      fvdl cleanup_svc_vc_create:
    181      1.1      fvdl 	if (r != NULL)
    182      1.1      fvdl 		mem_free(r, sizeof(*r));
    183      1.5  christos 	return (NULL);
    184      1.1      fvdl }
    185      1.1      fvdl 
    186      1.1      fvdl /*
    187      1.1      fvdl  * Like svtcp_create(), except the routine takes any *open* UNIX file
    188      1.1      fvdl  * descriptor as its first input.
    189      1.1      fvdl  */
    190      1.1      fvdl SVCXPRT *
    191      1.1      fvdl svc_fd_create(fd, sendsize, recvsize)
    192      1.1      fvdl 	int fd;
    193      1.1      fvdl 	u_int sendsize;
    194      1.1      fvdl 	u_int recvsize;
    195      1.1      fvdl {
    196      1.1      fvdl 	struct sockaddr_storage ss;
    197      1.1      fvdl 	socklen_t slen;
    198      1.1      fvdl 	SVCXPRT *ret;
    199      1.1      fvdl 
    200      1.1      fvdl 	_DIAGASSERT(fd != -1);
    201      1.1      fvdl 
    202      1.1      fvdl 	ret = makefd_xprt(fd, sendsize, recvsize);
    203      1.1      fvdl 	if (ret == NULL)
    204      1.1      fvdl 		return NULL;
    205      1.1      fvdl 
    206      1.1      fvdl 	slen = sizeof (struct sockaddr_storage);
    207      1.5  christos 	if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    208      1.4      fvdl 		warnx("svc_fd_create: could not retrieve local addr");
    209      1.1      fvdl 		goto freedata;
    210      1.1      fvdl 	}
    211      1.1      fvdl 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
    212      1.5  christos 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
    213      1.1      fvdl 	if (ret->xp_ltaddr.buf == NULL) {
    214      1.1      fvdl 		warnx("svc_fd_create: no mem for local addr");
    215      1.1      fvdl 		goto freedata;
    216      1.1      fvdl 	}
    217      1.5  christos 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
    218      1.1      fvdl 
    219      1.1      fvdl 	slen = sizeof (struct sockaddr_storage);
    220      1.5  christos 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    221      1.4      fvdl 		warnx("svc_fd_create: could not retrieve remote addr");
    222      1.1      fvdl 		goto freedata;
    223      1.1      fvdl 	}
    224      1.1      fvdl 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
    225      1.5  christos 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
    226      1.1      fvdl 	if (ret->xp_rtaddr.buf == NULL) {
    227      1.1      fvdl 		warnx("svc_fd_create: no mem for local addr");
    228      1.1      fvdl 		goto freedata;
    229      1.1      fvdl 	}
    230      1.5  christos 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
    231      1.1      fvdl #ifdef PORTMAP
    232      1.1      fvdl 	if (ss.ss_family == AF_INET) {
    233      1.1      fvdl 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
    234      1.1      fvdl 		ret->xp_addrlen = sizeof (struct sockaddr_in);
    235      1.1      fvdl 	}
    236      1.1      fvdl #endif
    237      1.1      fvdl 
    238      1.1      fvdl 	return ret;
    239      1.1      fvdl 
    240      1.1      fvdl freedata:
    241      1.1      fvdl 	if (ret->xp_ltaddr.buf != NULL)
    242      1.1      fvdl 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
    243      1.1      fvdl 
    244      1.1      fvdl 	return NULL;
    245      1.1      fvdl }
    246      1.1      fvdl 
    247      1.1      fvdl static SVCXPRT *
    248      1.1      fvdl makefd_xprt(fd, sendsize, recvsize)
    249      1.1      fvdl 	int fd;
    250      1.1      fvdl 	u_int sendsize;
    251      1.1      fvdl 	u_int recvsize;
    252      1.1      fvdl {
    253      1.1      fvdl 	SVCXPRT *xprt;
    254      1.1      fvdl 	struct cf_conn *cd;
    255      1.7      fvdl 	const char *netid;
    256      1.7      fvdl 	struct __rpc_sockinfo si;
    257      1.1      fvdl 
    258      1.1      fvdl 	_DIAGASSERT(fd != -1);
    259      1.1      fvdl 
    260      1.5  christos 	xprt = mem_alloc(sizeof(SVCXPRT));
    261      1.5  christos 	if (xprt == NULL) {
    262      1.7      fvdl 		warnx("svc_vc: makefd_xprt: out of memory");
    263      1.1      fvdl 		goto done;
    264      1.1      fvdl 	}
    265      1.2      fvdl 	memset(xprt, 0, sizeof *xprt);
    266      1.5  christos 	cd = mem_alloc(sizeof(struct cf_conn));
    267      1.5  christos 	if (cd == NULL) {
    268      1.1      fvdl 		warnx("svc_tcp: makefd_xprt: out of memory");
    269      1.1      fvdl 		mem_free(xprt, sizeof(SVCXPRT));
    270      1.5  christos 		xprt = NULL;
    271      1.1      fvdl 		goto done;
    272      1.1      fvdl 	}
    273      1.1      fvdl 	cd->strm_stat = XPRT_IDLE;
    274      1.1      fvdl 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
    275      1.1      fvdl 	    (caddr_t)(void *)xprt, read_vc, write_vc);
    276      1.1      fvdl 	xprt->xp_p1 = (caddr_t)(void *)cd;
    277      1.1      fvdl 	xprt->xp_verf.oa_base = cd->verf_body;
    278      1.1      fvdl 	svc_vc_ops(xprt);  /* truely deals with calls */
    279      1.1      fvdl 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
    280      1.1      fvdl 	xprt->xp_fd = fd;
    281      1.8     lukem 	if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
    282      1.7      fvdl 		xprt->xp_netid = strdup(netid);
    283      1.7      fvdl 
    284      1.1      fvdl 	xprt_register(xprt);
    285      1.1      fvdl done:
    286      1.1      fvdl 	return (xprt);
    287      1.1      fvdl }
    288      1.1      fvdl 
    289      1.1      fvdl /*ARGSUSED*/
    290      1.1      fvdl static bool_t
    291      1.1      fvdl rendezvous_request(xprt, msg)
    292      1.1      fvdl 	SVCXPRT *xprt;
    293      1.1      fvdl 	struct rpc_msg *msg;
    294      1.1      fvdl {
    295      1.1      fvdl 	int sock;
    296      1.1      fvdl 	struct cf_rendezvous *r;
    297      1.1      fvdl 	struct sockaddr_storage addr;
    298      1.1      fvdl 	socklen_t len;
    299      1.1      fvdl 	struct __rpc_sockinfo si;
    300      1.1      fvdl 
    301      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    302      1.1      fvdl 	_DIAGASSERT(msg != NULL);
    303      1.1      fvdl 
    304      1.1      fvdl 	r = (struct cf_rendezvous *)xprt->xp_p1;
    305      1.1      fvdl again:
    306      1.1      fvdl 	len = sizeof addr;
    307      1.5  christos 	if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
    308      1.5  christos 	    &len)) < 0) {
    309      1.1      fvdl 		if (errno == EINTR)
    310      1.1      fvdl 			goto again;
    311      1.1      fvdl 	       return (FALSE);
    312      1.1      fvdl 	}
    313      1.1      fvdl 	/*
    314      1.1      fvdl 	 * make a new transporter (re-uses xprt)
    315      1.1      fvdl 	 */
    316      1.1      fvdl 	xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
    317      1.1      fvdl 	xprt->xp_rtaddr.buf = mem_alloc(len);
    318      1.1      fvdl 	if (xprt->xp_rtaddr.buf == NULL)
    319      1.1      fvdl 		return (FALSE);
    320      1.1      fvdl 	memcpy(xprt->xp_rtaddr.buf, &addr, len);
    321      1.1      fvdl 	xprt->xp_rtaddr.len = len;
    322      1.1      fvdl #ifdef PORTMAP
    323      1.1      fvdl 	if (addr.ss_family == AF_INET) {
    324      1.1      fvdl 		xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
    325      1.1      fvdl 		xprt->xp_addrlen = sizeof (struct sockaddr_in);
    326      1.1      fvdl 	}
    327      1.1      fvdl #endif
    328      1.1      fvdl 	if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
    329      1.1      fvdl 		len = 1;
    330      1.1      fvdl 		/* XXX fvdl - is this useful? */
    331      1.1      fvdl 		setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
    332      1.1      fvdl 	}
    333      1.1      fvdl 	return (FALSE); /* there is never an rpc msg to be processed */
    334      1.1      fvdl }
    335      1.1      fvdl 
    336      1.1      fvdl /*ARGSUSED*/
    337      1.1      fvdl static enum xprt_stat
    338      1.1      fvdl rendezvous_stat(xprt)
    339      1.1      fvdl 	SVCXPRT *xprt;
    340      1.1      fvdl {
    341      1.1      fvdl 
    342      1.1      fvdl 	return (XPRT_IDLE);
    343      1.1      fvdl }
    344      1.1      fvdl 
    345      1.1      fvdl static void
    346      1.1      fvdl svc_vc_destroy(xprt)
    347      1.1      fvdl 	SVCXPRT *xprt;
    348      1.1      fvdl {
    349      1.1      fvdl 	struct cf_conn *cd;
    350      1.1      fvdl 	struct cf_rendezvous *r;
    351      1.1      fvdl 
    352      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    353      1.1      fvdl 
    354      1.1      fvdl 	cd = (struct cf_conn *)xprt->xp_p1;
    355      1.1      fvdl 
    356      1.1      fvdl 	xprt_unregister(xprt);
    357      1.1      fvdl 	if (xprt->xp_fd != RPC_ANYFD)
    358      1.1      fvdl 		(void)close(xprt->xp_fd);
    359      1.1      fvdl 	if (xprt->xp_port != 0) {
    360      1.1      fvdl 		/* a rendezvouser socket */
    361      1.1      fvdl 		r = (struct cf_rendezvous *)xprt->xp_p1;
    362      1.1      fvdl 		mem_free(r, sizeof (struct cf_rendezvous));
    363      1.1      fvdl 		xprt->xp_port = 0;
    364      1.1      fvdl 	} else {
    365      1.1      fvdl 		/* an actual connection socket */
    366      1.1      fvdl 		XDR_DESTROY(&(cd->xdrs));
    367      1.1      fvdl 		mem_free(cd, sizeof(struct cf_conn));
    368      1.1      fvdl 	}
    369      1.1      fvdl 	if (xprt->xp_rtaddr.buf)
    370      1.1      fvdl 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
    371      1.1      fvdl 	if (xprt->xp_ltaddr.buf)
    372      1.1      fvdl 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
    373      1.1      fvdl 	if (xprt->xp_tp)
    374      1.1      fvdl 		free(xprt->xp_tp);
    375      1.1      fvdl 	if (xprt->xp_netid)
    376      1.1      fvdl 		free(xprt->xp_netid);
    377      1.1      fvdl 	mem_free(xprt, sizeof(SVCXPRT));
    378      1.1      fvdl }
    379      1.1      fvdl 
    380      1.5  christos /*ARGSUSED*/
    381      1.1      fvdl static bool_t
    382      1.1      fvdl svc_vc_control(xprt, rq, in)
    383      1.1      fvdl 	SVCXPRT *xprt;
    384      1.1      fvdl 	const u_int rq;
    385      1.1      fvdl 	void *in;
    386      1.1      fvdl {
    387      1.1      fvdl 	return (FALSE);
    388      1.1      fvdl }
    389      1.1      fvdl 
    390      1.1      fvdl /*
    391  1.8.2.2   nathanw  * reads data from the tcp connection.
    392      1.1      fvdl  * any error is fatal and the connection is closed.
    393      1.1      fvdl  * (And a read of zero bytes is a half closed stream => error.)
    394      1.1      fvdl  * All read operations timeout after 35 seconds.  A timeout is
    395      1.1      fvdl  * fatal for the connection.
    396      1.1      fvdl  */
    397      1.1      fvdl static int
    398      1.1      fvdl read_vc(xprtp, buf, len)
    399      1.1      fvdl 	caddr_t xprtp;
    400      1.1      fvdl 	caddr_t buf;
    401      1.1      fvdl 	int len;
    402      1.1      fvdl {
    403      1.1      fvdl 	SVCXPRT *xprt;
    404      1.1      fvdl 	int sock;
    405      1.1      fvdl 	int milliseconds = 35 * 1000;
    406      1.1      fvdl 	struct pollfd pollfd;
    407      1.1      fvdl 	struct sockaddr *sa;
    408      1.1      fvdl 	struct msghdr msg;
    409      1.1      fvdl 	struct cmsghdr *cmp;
    410      1.3   thorpej 	void *crmsg = NULL;
    411      1.1      fvdl 	struct sockcred *sc;
    412      1.3   thorpej 	socklen_t crmsgsize;
    413      1.1      fvdl 
    414      1.1      fvdl 	xprt = (SVCXPRT *)(void *)xprtp;
    415      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    416      1.1      fvdl 
    417      1.1      fvdl 	sock = xprt->xp_fd;
    418      1.1      fvdl 
    419      1.1      fvdl 	sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
    420      1.1      fvdl 	if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) {
    421      1.1      fvdl 		memset(&msg, 0, sizeof msg);
    422      1.3   thorpej 		crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
    423      1.3   thorpej 		crmsg = malloc(crmsgsize);
    424      1.3   thorpej 		if (crmsg == NULL)
    425      1.3   thorpej 			goto fatal_err;
    426      1.3   thorpej 		memset(crmsg, 0, crmsgsize);
    427      1.3   thorpej 
    428      1.3   thorpej 		msg.msg_control = crmsg;
    429      1.3   thorpej 		msg.msg_controllen = crmsgsize;
    430      1.1      fvdl 
    431      1.1      fvdl 		if (recvmsg(sock, &msg, 0) < 0)
    432      1.1      fvdl 			goto fatal_err;
    433      1.1      fvdl 
    434      1.3   thorpej 		if (msg.msg_controllen == 0 ||
    435      1.3   thorpej 		    (msg.msg_flags & MSG_CTRUNC) != 0)
    436      1.3   thorpej 			goto fatal_err;
    437      1.3   thorpej 
    438      1.1      fvdl 		cmp = CMSG_FIRSTHDR(&msg);
    439      1.1      fvdl 		if (cmp->cmsg_level != SOL_SOCKET ||
    440      1.1      fvdl 		    cmp->cmsg_type != SCM_CREDS)
    441      1.1      fvdl 			goto fatal_err;
    442      1.1      fvdl 
    443      1.5  christos 		sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
    444      1.1      fvdl 
    445      1.1      fvdl 		xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups));
    446      1.1      fvdl 		if (xprt->xp_p2 == NULL)
    447      1.1      fvdl 			goto fatal_err;
    448      1.1      fvdl 
    449      1.1      fvdl 		memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups));
    450      1.3   thorpej 		free(crmsg);
    451      1.3   thorpej 		crmsg = NULL;
    452      1.1      fvdl 	}
    453      1.1      fvdl 
    454      1.1      fvdl 	do {
    455      1.1      fvdl 		pollfd.fd = sock;
    456      1.1      fvdl 		pollfd.events = POLLIN;
    457      1.1      fvdl 		switch (poll(&pollfd, 1, milliseconds)) {
    458      1.1      fvdl 		case -1:
    459      1.1      fvdl 			if (errno == EINTR) {
    460      1.1      fvdl 				continue;
    461      1.1      fvdl 			}
    462      1.1      fvdl 			/*FALLTHROUGH*/
    463      1.1      fvdl 		case 0:
    464      1.1      fvdl 			goto fatal_err;
    465      1.1      fvdl 
    466      1.1      fvdl 		default:
    467      1.1      fvdl 			break;
    468      1.1      fvdl 		}
    469      1.1      fvdl 	} while ((pollfd.revents & POLLIN) == 0);
    470      1.1      fvdl 
    471      1.1      fvdl 	if ((len = read(sock, buf, (size_t)len)) > 0)
    472      1.1      fvdl 		return (len);
    473      1.1      fvdl 
    474      1.1      fvdl fatal_err:
    475      1.3   thorpej 	if (crmsg != NULL)
    476      1.3   thorpej 		free(crmsg);
    477      1.1      fvdl 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
    478      1.1      fvdl 	return (-1);
    479      1.1      fvdl }
    480      1.1      fvdl 
    481      1.1      fvdl /*
    482      1.1      fvdl  * writes data to the tcp connection.
    483      1.1      fvdl  * Any error is fatal and the connection is closed.
    484      1.1      fvdl  */
    485      1.1      fvdl static int
    486      1.1      fvdl write_vc(xprtp, buf, len)
    487      1.1      fvdl 	caddr_t xprtp;
    488      1.1      fvdl 	caddr_t buf;
    489      1.1      fvdl 	int len;
    490      1.1      fvdl {
    491      1.1      fvdl 	SVCXPRT *xprt;
    492      1.1      fvdl 	int i, cnt;
    493      1.1      fvdl 
    494      1.1      fvdl 	xprt = (SVCXPRT *)(void *)xprtp;
    495      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    496      1.1      fvdl 
    497      1.1      fvdl 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
    498      1.1      fvdl 		if ((i = write(xprt->xp_fd, buf, (size_t)cnt)) < 0) {
    499      1.1      fvdl 			((struct cf_conn *)(xprt->xp_p1))->strm_stat =
    500      1.1      fvdl 			    XPRT_DIED;
    501      1.1      fvdl 			return (-1);
    502      1.1      fvdl 		}
    503      1.1      fvdl 	}
    504      1.1      fvdl 	return (len);
    505      1.1      fvdl }
    506      1.1      fvdl 
    507      1.1      fvdl static enum xprt_stat
    508      1.1      fvdl svc_vc_stat(xprt)
    509      1.1      fvdl 	SVCXPRT *xprt;
    510      1.1      fvdl {
    511      1.1      fvdl 	struct cf_conn *cd;
    512      1.1      fvdl 
    513      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    514      1.1      fvdl 
    515      1.1      fvdl 	cd = (struct cf_conn *)(xprt->xp_p1);
    516      1.1      fvdl 
    517      1.1      fvdl 	if (cd->strm_stat == XPRT_DIED)
    518      1.1      fvdl 		return (XPRT_DIED);
    519      1.1      fvdl 	if (! xdrrec_eof(&(cd->xdrs)))
    520      1.1      fvdl 		return (XPRT_MOREREQS);
    521      1.1      fvdl 	return (XPRT_IDLE);
    522      1.1      fvdl }
    523      1.1      fvdl 
    524      1.1      fvdl static bool_t
    525      1.1      fvdl svc_vc_recv(xprt, msg)
    526      1.1      fvdl 	SVCXPRT *xprt;
    527      1.1      fvdl 	struct rpc_msg *msg;
    528      1.1      fvdl {
    529      1.1      fvdl 	struct cf_conn *cd;
    530      1.1      fvdl 	XDR *xdrs;
    531      1.1      fvdl 
    532      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    533      1.1      fvdl 	_DIAGASSERT(msg != NULL);
    534      1.1      fvdl 
    535      1.1      fvdl 	cd = (struct cf_conn *)(xprt->xp_p1);
    536      1.1      fvdl 	xdrs = &(cd->xdrs);
    537      1.1      fvdl 
    538      1.1      fvdl 	xdrs->x_op = XDR_DECODE;
    539      1.1      fvdl 	(void)xdrrec_skiprecord(xdrs);
    540      1.1      fvdl 	if (xdr_callmsg(xdrs, msg)) {
    541      1.1      fvdl 		cd->x_id = msg->rm_xid;
    542      1.1      fvdl 		return (TRUE);
    543      1.1      fvdl 	}
    544      1.1      fvdl 	cd->strm_stat = XPRT_DIED;
    545      1.1      fvdl 	return (FALSE);
    546      1.1      fvdl }
    547      1.1      fvdl 
    548      1.1      fvdl static bool_t
    549      1.1      fvdl svc_vc_getargs(xprt, xdr_args, args_ptr)
    550      1.1      fvdl 	SVCXPRT *xprt;
    551      1.1      fvdl 	xdrproc_t xdr_args;
    552      1.1      fvdl 	caddr_t args_ptr;
    553      1.1      fvdl {
    554      1.1      fvdl 
    555      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    556      1.1      fvdl 	/* args_ptr may be NULL */
    557      1.1      fvdl 
    558      1.1      fvdl 	return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
    559      1.1      fvdl 	    args_ptr));
    560      1.1      fvdl }
    561      1.1      fvdl 
    562      1.1      fvdl static bool_t
    563      1.1      fvdl svc_vc_freeargs(xprt, xdr_args, args_ptr)
    564      1.1      fvdl 	SVCXPRT *xprt;
    565      1.1      fvdl 	xdrproc_t xdr_args;
    566      1.1      fvdl 	caddr_t args_ptr;
    567      1.1      fvdl {
    568      1.1      fvdl 	XDR *xdrs;
    569      1.1      fvdl 
    570      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    571      1.1      fvdl 	/* args_ptr may be NULL */
    572      1.1      fvdl 
    573      1.1      fvdl 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
    574      1.1      fvdl 
    575      1.1      fvdl 	xdrs->x_op = XDR_FREE;
    576      1.1      fvdl 	return ((*xdr_args)(xdrs, args_ptr));
    577      1.1      fvdl }
    578      1.1      fvdl 
    579      1.1      fvdl static bool_t
    580      1.1      fvdl svc_vc_reply(xprt, msg)
    581      1.1      fvdl 	SVCXPRT *xprt;
    582      1.1      fvdl 	struct rpc_msg *msg;
    583      1.1      fvdl {
    584      1.1      fvdl 	struct cf_conn *cd;
    585      1.1      fvdl 	XDR *xdrs;
    586      1.1      fvdl 	bool_t stat;
    587      1.1      fvdl 
    588      1.1      fvdl 	_DIAGASSERT(xprt != NULL);
    589      1.1      fvdl 	_DIAGASSERT(msg != NULL);
    590      1.1      fvdl 
    591      1.1      fvdl 	cd = (struct cf_conn *)(xprt->xp_p1);
    592      1.1      fvdl 	xdrs = &(cd->xdrs);
    593      1.1      fvdl 
    594      1.1      fvdl 	xdrs->x_op = XDR_ENCODE;
    595      1.1      fvdl 	msg->rm_xid = cd->x_id;
    596      1.1      fvdl 	stat = xdr_replymsg(xdrs, msg);
    597      1.1      fvdl 	(void)xdrrec_endofrecord(xdrs, TRUE);
    598      1.1      fvdl 	return (stat);
    599      1.1      fvdl }
    600      1.1      fvdl 
    601      1.1      fvdl static void
    602      1.1      fvdl svc_vc_ops(xprt)
    603      1.1      fvdl 	SVCXPRT *xprt;
    604      1.1      fvdl {
    605      1.1      fvdl 	static struct xp_ops ops;
    606      1.1      fvdl 	static struct xp_ops2 ops2;
    607  1.8.2.1   nathanw #ifdef _REENTRANT
    608      1.1      fvdl 	extern mutex_t ops_lock;
    609      1.1      fvdl #endif
    610      1.1      fvdl 
    611      1.1      fvdl /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
    612      1.1      fvdl 
    613      1.1      fvdl 	mutex_lock(&ops_lock);
    614      1.1      fvdl 	if (ops.xp_recv == NULL) {
    615      1.1      fvdl 		ops.xp_recv = svc_vc_recv;
    616      1.1      fvdl 		ops.xp_stat = svc_vc_stat;
    617      1.1      fvdl 		ops.xp_getargs = svc_vc_getargs;
    618      1.1      fvdl 		ops.xp_reply = svc_vc_reply;
    619      1.1      fvdl 		ops.xp_freeargs = svc_vc_freeargs;
    620      1.1      fvdl 		ops.xp_destroy = svc_vc_destroy;
    621      1.1      fvdl 		ops2.xp_control = svc_vc_control;
    622      1.1      fvdl 	}
    623      1.1      fvdl 	xprt->xp_ops = &ops;
    624      1.1      fvdl 	xprt->xp_ops2 = &ops2;
    625      1.1      fvdl 	mutex_unlock(&ops_lock);
    626      1.1      fvdl }
    627      1.1      fvdl 
    628      1.1      fvdl static void
    629      1.1      fvdl svc_vc_rendezvous_ops(xprt)
    630      1.1      fvdl 	SVCXPRT *xprt;
    631      1.1      fvdl {
    632      1.1      fvdl 	static struct xp_ops ops;
    633      1.1      fvdl 	static struct xp_ops2 ops2;
    634  1.8.2.1   nathanw #ifdef _REENTRANT
    635      1.1      fvdl 	extern mutex_t ops_lock;
    636      1.1      fvdl #endif
    637      1.1      fvdl 
    638      1.1      fvdl 	mutex_lock(&ops_lock);
    639      1.1      fvdl 	if (ops.xp_recv == NULL) {
    640      1.1      fvdl 		ops.xp_recv = rendezvous_request;
    641      1.1      fvdl 		ops.xp_stat = rendezvous_stat;
    642      1.1      fvdl 		ops.xp_getargs =
    643      1.1      fvdl 		    (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
    644      1.1      fvdl 		ops.xp_reply =
    645      1.1      fvdl 		    (bool_t (*) __P((SVCXPRT *, struct rpc_msg *)))abort;
    646      1.1      fvdl 		ops.xp_freeargs =
    647      1.1      fvdl 		    (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort,
    648      1.1      fvdl 		ops.xp_destroy = svc_vc_destroy;
    649      1.1      fvdl 		ops2.xp_control = svc_vc_control;
    650      1.1      fvdl 	}
    651      1.1      fvdl 	xprt->xp_ops = &ops;
    652      1.1      fvdl 	xprt->xp_ops2 = &ops2;
    653      1.1      fvdl 	mutex_unlock(&ops_lock);
    654      1.1      fvdl }
    655