1 /* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos 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
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #ident "@(#)svc_dg.c 1.17 94/04/24 SMI"
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * svc_dg.c, Server side for connectionless RPC.
46 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/mutex.h>
52 #include <sys/protosw.h>
53 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
57 #include <sys/systm.h>
64 #include <rpc/rpc_com.h>
66 static enum xprt_stat svc_dg_stat(SVCXPRT *);
67 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *,
68 struct sockaddr **, struct mbuf **);
69 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *,
70 struct sockaddr *, struct mbuf *);
71 static void svc_dg_destroy(SVCXPRT *);
72 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
73 static int svc_dg_soupcall(struct socket *so, void *arg, int waitflag);
75 static struct xp_ops svc_dg_ops = {
76 .xp_recv = svc_dg_recv,
77 .xp_stat = svc_dg_stat,
78 .xp_reply = svc_dg_reply,
79 .xp_destroy = svc_dg_destroy,
80 .xp_control = svc_dg_control,
85 * xprt = svc_dg_create(sock, sendsize, recvsize);
86 * Does other connectionless specific initializations.
87 * Once *xprt is initialized, it is registered.
88 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
89 * system defaults are chosen.
90 * The routines returns NULL if a problem occurred.
92 static const char svc_dg_str[] = "svc_dg_create: %s";
93 static const char svc_dg_err1[] = "could not get transport information";
94 static const char svc_dg_err2[] = "transport does not support data transfer";
95 static const char __no_mem_str[] = "out of memory";
98 svc_dg_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
102 struct __rpc_sockinfo si;
106 CURVNET_SET(so->so_vnet);
107 if (!__rpc_socket2sockinfo(so, &si)) {
108 printf(svc_dg_str, svc_dg_err1);
113 * Find the receive and the send size
115 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
116 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
117 if ((sendsize == 0) || (recvsize == 0)) {
118 printf(svc_dg_str, svc_dg_err2);
123 xprt = svc_xprt_alloc();
124 sx_init(&xprt->xp_lock, "xprt->xp_lock");
125 xprt->xp_pool = pool;
126 xprt->xp_socket = so;
129 xprt->xp_ops = &svc_dg_ops;
131 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
136 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
141 SOCKBUF_LOCK(&so->so_rcv);
142 soupcall_set(so, SO_RCV, svc_dg_soupcall, xprt);
143 SOCKBUF_UNLOCK(&so->so_rcv);
147 (void) printf(svc_dg_str, __no_mem_str);
155 static enum xprt_stat
156 svc_dg_stat(SVCXPRT *xprt)
159 if (soreadable(xprt->xp_socket))
160 return (XPRT_MOREREQS);
166 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg,
167 struct sockaddr **addrp, struct mbuf **mp)
170 struct sockaddr *raddr;
176 * Serialise access to the socket.
178 sx_xlock(&xprt->xp_lock);
181 * The socket upcall calls xprt_active() which will eventually
182 * cause the server to call us here. We attempt to read a
183 * packet from the socket and process it. If the read fails,
184 * we have drained all pending requests so we call
187 uio.uio_resid = 1000000000;
188 uio.uio_td = curthread;
190 rcvflag = MSG_DONTWAIT;
191 error = soreceive(xprt->xp_socket, &raddr, &uio, &mreq, NULL, &rcvflag);
193 if (error == EWOULDBLOCK) {
195 * We must re-test for readability after taking the
196 * lock to protect us in the case where a new packet
197 * arrives on the socket after our call to soreceive
198 * fails with EWOULDBLOCK. The pool lock protects us
199 * from racing the upcall after our soreadable() call
202 mtx_lock(&xprt->xp_pool->sp_lock);
203 if (!soreadable(xprt->xp_socket))
204 xprt_inactive_locked(xprt);
205 mtx_unlock(&xprt->xp_pool->sp_lock);
206 sx_xunlock(&xprt->xp_lock);
211 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
212 soupcall_clear(xprt->xp_socket, SO_RCV);
213 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
215 sx_xunlock(&xprt->xp_lock);
219 sx_xunlock(&xprt->xp_lock);
221 xdrmbuf_create(&xdrs, mreq, XDR_DECODE);
222 if (! xdr_callmsg(&xdrs, msg)) {
228 *mp = xdrmbuf_getall(&xdrs);
235 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg,
236 struct sockaddr *addr, struct mbuf *m)
243 MGETHDR(mrep, M_WAIT, MT_DATA);
246 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
248 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
249 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
250 if (!xdr_replymsg(&xdrs, msg))
253 xdrmbuf_append(&xdrs, m);
255 stat = xdr_replymsg(&xdrs, msg);
260 error = sosend(xprt->xp_socket, addr, NULL, mrep, NULL,
276 svc_dg_destroy(SVCXPRT *xprt)
279 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
280 soupcall_clear(xprt->xp_socket, SO_RCV);
281 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
283 sx_destroy(&xprt->xp_lock);
285 (void)soclose(xprt->xp_socket);
288 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
294 svc_dg_control(xprt, rq, in)
304 svc_dg_soupcall(struct socket *so, void *arg, int waitflag)
306 SVCXPRT *xprt = (SVCXPRT *) arg;