1 /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * svc_vc.c, Server side for Connection Oriented based RPC.
42 * Actually implements two flavors of transporter -
43 * a tcp rendezvouser (a listner and connection establisher)
44 * and a record/tcp stream.
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <sys/types.h>
50 #include <sys/param.h>
52 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <netinet/tcp.h>
69 #include "un-namespace.h"
73 struct cmsgcred cmcred;
76 static SVCXPRT *makefd_xprt(int, u_int, u_int);
77 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
78 static enum xprt_stat rendezvous_stat(SVCXPRT *);
79 static void svc_vc_destroy(SVCXPRT *);
80 static int read_vc(void *, void *, int);
81 static int write_vc(void *, void *, int);
82 static enum xprt_stat svc_vc_stat(SVCXPRT *);
83 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
84 static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
85 static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
86 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
87 static void svc_vc_rendezvous_ops(SVCXPRT *);
88 static void svc_vc_ops(SVCXPRT *);
89 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
90 static int __msgread_withcred(int, void *, size_t, struct cmessage *);
91 static int __msgwrite(int, void *, size_t);
93 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
98 struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
99 enum xprt_stat strm_stat;
102 char verf_body[MAX_AUTH_BYTES];
107 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
109 * Creates, registers, and returns a (rpc) tcp based transporter.
110 * Once *xprt is initialized, it is registered as a transporter
111 * see (svc.h, xprt_register). This routine returns
112 * a NULL if a problem occurred.
114 * The filedescriptor passed in is expected to refer to a bound, but
115 * not yet connected socket.
117 * Since streams do buffered io similar to stdio, the caller can specify
118 * how big the send and receive buffers are via the second and third parms;
119 * 0 => use the system default.
122 svc_vc_create(fd, sendsize, recvsize)
128 struct cf_rendezvous *r = NULL;
129 struct __rpc_sockinfo si;
130 struct sockaddr_storage sslocal;
133 r = mem_alloc(sizeof(*r));
135 warnx("svc_vc_create: out of memory");
136 goto cleanup_svc_vc_create;
138 if (!__rpc_fd2sockinfo(fd, &si))
140 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
141 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
142 xprt = mem_alloc(sizeof(SVCXPRT));
144 warnx("svc_vc_create: out of memory");
145 goto cleanup_svc_vc_create;
151 xprt->xp_verf = _null_auth;
152 svc_vc_rendezvous_ops(xprt);
153 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
156 slen = sizeof (struct sockaddr_storage);
157 if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
158 warnx("svc_vc_create: could not retrieve local addr");
159 goto cleanup_svc_vc_create;
162 xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
163 xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
164 if (xprt->xp_ltaddr.buf == NULL) {
165 warnx("svc_vc_create: no mem for local addr");
166 goto cleanup_svc_vc_create;
168 memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
170 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
173 cleanup_svc_vc_create:
175 mem_free(r, sizeof(*r));
180 * Like svtcp_create(), except the routine takes any *open* UNIX file
181 * descriptor as its first input.
184 svc_fd_create(fd, sendsize, recvsize)
189 struct sockaddr_storage ss;
195 ret = makefd_xprt(fd, sendsize, recvsize);
199 slen = sizeof (struct sockaddr_storage);
200 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
201 warnx("svc_fd_create: could not retrieve local addr");
204 ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
205 ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
206 if (ret->xp_ltaddr.buf == NULL) {
207 warnx("svc_fd_create: no mem for local addr");
210 memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
212 slen = sizeof (struct sockaddr_storage);
213 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
214 warnx("svc_fd_create: could not retrieve remote addr");
217 ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
218 ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
219 if (ret->xp_rtaddr.buf == NULL) {
220 warnx("svc_fd_create: no mem for local addr");
223 memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
225 if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
226 ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
227 ret->xp_addrlen = sizeof (struct sockaddr_in);
234 if (ret->xp_ltaddr.buf != NULL)
235 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
241 makefd_xprt(fd, sendsize, recvsize)
249 struct __rpc_sockinfo si;
253 xprt = mem_alloc(sizeof(SVCXPRT));
255 warnx("svc_vc: makefd_xprt: out of memory");
258 memset(xprt, 0, sizeof *xprt);
259 cd = mem_alloc(sizeof(struct cf_conn));
261 warnx("svc_tcp: makefd_xprt: out of memory");
262 mem_free(xprt, sizeof(SVCXPRT));
266 cd->strm_stat = XPRT_IDLE;
267 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
268 xprt, read_vc, write_vc);
270 xprt->xp_verf.oa_base = cd->verf_body;
271 svc_vc_ops(xprt); /* truely deals with calls */
272 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
274 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
275 xprt->xp_netid = strdup(netid);
284 rendezvous_request(xprt, msg)
289 struct cf_rendezvous *r;
290 struct sockaddr_storage addr;
292 struct __rpc_sockinfo si;
294 assert(xprt != NULL);
297 r = (struct cf_rendezvous *)xprt->xp_p1;
300 if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
307 * make a new transporter (re-uses xprt)
309 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
310 xprt->xp_rtaddr.buf = mem_alloc(len);
311 if (xprt->xp_rtaddr.buf == NULL)
313 memcpy(xprt->xp_rtaddr.buf, &addr, len);
314 xprt->xp_rtaddr.len = len;
316 if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
317 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
318 xprt->xp_addrlen = sizeof (struct sockaddr_in);
321 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
323 /* XXX fvdl - is this useful? */
324 _setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
326 return (FALSE); /* there is never an rpc msg to be processed */
330 static enum xprt_stat
331 rendezvous_stat(xprt)
343 struct cf_rendezvous *r;
345 assert(xprt != NULL);
347 cd = (struct cf_conn *)xprt->xp_p1;
349 xprt_unregister(xprt);
350 if (xprt->xp_fd != RPC_ANYFD)
351 (void)_close(xprt->xp_fd);
352 if (xprt->xp_port != 0) {
353 /* a rendezvouser socket */
354 r = (struct cf_rendezvous *)xprt->xp_p1;
355 mem_free(r, sizeof (struct cf_rendezvous));
358 /* an actual connection socket */
359 XDR_DESTROY(&(cd->xdrs));
360 mem_free(cd, sizeof(struct cf_conn));
362 if (xprt->xp_rtaddr.buf)
363 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
364 if (xprt->xp_ltaddr.buf)
365 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
369 free(xprt->xp_netid);
370 mem_free(xprt, sizeof(SVCXPRT));
375 svc_vc_control(xprt, rq, in)
384 * reads data from the tcp or uip connection.
385 * any error is fatal and the connection is closed.
386 * (And a read of zero bytes is a half closed stream => error.)
387 * All read operations timeout after 35 seconds. A timeout is
388 * fatal for the connection.
391 read_vc(xprtp, buf, len)
398 int milliseconds = 35 * 1000;
399 struct pollfd pollfd;
403 xprt = (SVCXPRT *)xprtp;
404 assert(xprt != NULL);
410 pollfd.events = POLLIN;
412 switch (_poll(&pollfd, 1, milliseconds)) {
423 } while ((pollfd.revents & POLLIN) == 0);
426 sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
427 if (sa->sa_family == AF_LOCAL) {
428 cm = (struct cmessage *)xprt->xp_verf.oa_base;
429 if ((len = __msgread_withcred(sock, buf, len, cm)) > 0) {
430 xprt->xp_p2 = &cm->cmcred;
435 if ((len = _read(sock, buf, (size_t)len)) > 0)
440 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
445 * writes data to the tcp connection.
446 * Any error is fatal and the connection is closed.
449 write_vc(xprtp, buf, len)
458 xprt = (SVCXPRT *)xprtp;
459 assert(xprt != NULL);
461 sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
462 if (sa->sa_family == AF_LOCAL) {
463 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
464 if ((i = __msgwrite(xprt->xp_fd, buf,
466 ((struct cf_conn *)(xprt->xp_p1))->strm_stat =
472 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
473 if ((i = _write(xprt->xp_fd, buf,
475 ((struct cf_conn *)(xprt->xp_p1))->strm_stat =
485 static enum xprt_stat
491 assert(xprt != NULL);
493 cd = (struct cf_conn *)(xprt->xp_p1);
495 if (cd->strm_stat == XPRT_DIED)
497 if (! xdrrec_eof(&(cd->xdrs)))
498 return (XPRT_MOREREQS);
503 svc_vc_recv(xprt, msg)
510 assert(xprt != NULL);
513 cd = (struct cf_conn *)(xprt->xp_p1);
516 xdrs->x_op = XDR_DECODE;
517 (void)xdrrec_skiprecord(xdrs);
518 if (xdr_callmsg(xdrs, msg)) {
519 cd->x_id = msg->rm_xid;
522 cd->strm_stat = XPRT_DIED;
527 svc_vc_getargs(xprt, xdr_args, args_ptr)
533 assert(xprt != NULL);
534 /* args_ptr may be NULL */
535 return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
540 svc_vc_freeargs(xprt, xdr_args, args_ptr)
547 assert(xprt != NULL);
548 /* args_ptr may be NULL */
550 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
552 xdrs->x_op = XDR_FREE;
553 return ((*xdr_args)(xdrs, args_ptr));
557 svc_vc_reply(xprt, msg)
565 assert(xprt != NULL);
568 cd = (struct cf_conn *)(xprt->xp_p1);
571 xdrs->x_op = XDR_ENCODE;
572 msg->rm_xid = cd->x_id;
573 stat = xdr_replymsg(xdrs, msg);
574 (void)xdrrec_endofrecord(xdrs, TRUE);
582 static struct xp_ops ops;
583 static struct xp_ops2 ops2;
584 extern mutex_t ops_lock;
586 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
588 mutex_lock(&ops_lock);
589 if (ops.xp_recv == NULL) {
590 ops.xp_recv = svc_vc_recv;
591 ops.xp_stat = svc_vc_stat;
592 ops.xp_getargs = svc_vc_getargs;
593 ops.xp_reply = svc_vc_reply;
594 ops.xp_freeargs = svc_vc_freeargs;
595 ops.xp_destroy = svc_vc_destroy;
596 ops2.xp_control = svc_vc_control;
599 xprt->xp_ops2 = &ops2;
600 mutex_unlock(&ops_lock);
604 svc_vc_rendezvous_ops(xprt)
607 static struct xp_ops ops;
608 static struct xp_ops2 ops2;
609 extern mutex_t ops_lock;
611 mutex_lock(&ops_lock);
612 if (ops.xp_recv == NULL) {
613 ops.xp_recv = rendezvous_request;
614 ops.xp_stat = rendezvous_stat;
616 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
618 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
620 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
621 ops.xp_destroy = svc_vc_destroy;
622 ops2.xp_control = svc_vc_control;
625 xprt->xp_ops2 = &ops2;
626 mutex_unlock(&ops_lock);
630 __msgread_withcred(sock, buf, cnt, cmp)
634 struct cmessage *cmp;
640 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
645 bzero(&cm, sizeof(cm));
646 iov[0].iov_base = buf;
647 iov[0].iov_len = cnt;
653 msg.msg_control = &cm;
654 msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
657 ret = _recvmsg(sock, &msg, 0);
658 bcopy(&cm.cmsg, &cmp->cmsg, sizeof(cmp->cmsg));
659 bcopy(CMSG_DATA(&cm), &cmp->cmcred, sizeof(cmp->cmcred));
661 if (msg.msg_controllen == 0 ||
662 (msg.msg_flags & MSG_CTRUNC) != 0)
669 __msgwrite(sock, buf, cnt)
678 bzero((char *)&cm, sizeof(cm));
679 iov[0].iov_base = buf;
680 iov[0].iov_len = cnt;
682 cm.cmsg.cmsg_type = SCM_CREDS;
683 cm.cmsg.cmsg_level = SOL_SOCKET;
684 cm.cmsg.cmsg_len = sizeof(struct cmessage);
690 msg.msg_control = &cm;
691 msg.msg_controllen = sizeof(struct cmessage);
694 return(_sendmsg(sock, &msg, 0));
698 * Get the effective UID of the sending process. Used by rpcbind and keyserv
702 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid)
704 struct cmsgcred *cmcred;
708 cm = (struct cmessage *)transp->xp_verf.oa_base;
713 if (cmp == NULL || cmp->cmsg_level != SOL_SOCKET ||
714 cmp->cmsg_type != SCM_CREDS)
717 cmcred = __svc_getcallercreds(transp);
720 *uid = cmcred->cmcred_euid;