Deleted Added
full compact
svc_dg.c (184588) svc_dg.c (193272)
1/* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */
2
3/*
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.
10 *
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.
14 *
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.
18 *
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.
22 *
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.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32/*
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37#ident "@(#)svc_dg.c 1.17 94/04/24 SMI"
38#endif
39#include <sys/cdefs.h>
1/* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */
2
3/*
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.
10 *
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.
14 *
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.
18 *
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.
22 *
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.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32/*
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37#ident "@(#)svc_dg.c 1.17 94/04/24 SMI"
38#endif
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/rpc/svc_dg.c 184588 2008-11-03 10:38:00Z dfr $");
40__FBSDID("$FreeBSD: head/sys/rpc/svc_dg.c 193272 2009-06-01 21:17:03Z jhb $");
41
42/*
43 * svc_dg.c, Server side for connectionless RPC.
44 */
45
46#include <sys/param.h>
47#include <sys/lock.h>
48#include <sys/kernel.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.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>
56#include <sys/sx.h>
57#include <sys/systm.h>
58#include <sys/uio.h>
59
60#include <rpc/rpc.h>
61
62#include <rpc/rpc_com.h>
63
64static enum xprt_stat svc_dg_stat(SVCXPRT *);
65static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *,
66 struct sockaddr **, struct mbuf **);
67static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *,
68 struct sockaddr *, struct mbuf *);
69static void svc_dg_destroy(SVCXPRT *);
70static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
41
42/*
43 * svc_dg.c, Server side for connectionless RPC.
44 */
45
46#include <sys/param.h>
47#include <sys/lock.h>
48#include <sys/kernel.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.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>
56#include <sys/sx.h>
57#include <sys/systm.h>
58#include <sys/uio.h>
59
60#include <rpc/rpc.h>
61
62#include <rpc/rpc_com.h>
63
64static enum xprt_stat svc_dg_stat(SVCXPRT *);
65static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *,
66 struct sockaddr **, struct mbuf **);
67static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *,
68 struct sockaddr *, struct mbuf *);
69static void svc_dg_destroy(SVCXPRT *);
70static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
71static void svc_dg_soupcall(struct socket *so, void *arg, int waitflag);
71static int svc_dg_soupcall(struct socket *so, void *arg, int waitflag);
72
73static struct xp_ops svc_dg_ops = {
74 .xp_recv = svc_dg_recv,
75 .xp_stat = svc_dg_stat,
76 .xp_reply = svc_dg_reply,
77 .xp_destroy = svc_dg_destroy,
78 .xp_control = svc_dg_control,
79};
80
81/*
82 * Usage:
83 * xprt = svc_dg_create(sock, sendsize, recvsize);
84 * Does other connectionless specific initializations.
85 * Once *xprt is initialized, it is registered.
86 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
87 * system defaults are chosen.
88 * The routines returns NULL if a problem occurred.
89 */
90static const char svc_dg_str[] = "svc_dg_create: %s";
91static const char svc_dg_err1[] = "could not get transport information";
92static const char svc_dg_err2[] = "transport does not support data transfer";
93static const char __no_mem_str[] = "out of memory";
94
95SVCXPRT *
96svc_dg_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
97 size_t recvsize)
98{
99 SVCXPRT *xprt;
100 struct __rpc_sockinfo si;
101 struct sockaddr* sa;
102 int error;
103
104 if (!__rpc_socket2sockinfo(so, &si)) {
105 printf(svc_dg_str, svc_dg_err1);
106 return (NULL);
107 }
108 /*
109 * Find the receive and the send size
110 */
111 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
112 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
113 if ((sendsize == 0) || (recvsize == 0)) {
114 printf(svc_dg_str, svc_dg_err2);
115 return (NULL);
116 }
117
118 xprt = svc_xprt_alloc();
119 sx_init(&xprt->xp_lock, "xprt->xp_lock");
120 xprt->xp_pool = pool;
121 xprt->xp_socket = so;
122 xprt->xp_p1 = NULL;
123 xprt->xp_p2 = NULL;
124 xprt->xp_ops = &svc_dg_ops;
125
126 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
127 if (error)
128 goto freedata;
129
130 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
131 free(sa, M_SONAME);
132
133 xprt_register(xprt);
134
135 SOCKBUF_LOCK(&so->so_rcv);
72
73static struct xp_ops svc_dg_ops = {
74 .xp_recv = svc_dg_recv,
75 .xp_stat = svc_dg_stat,
76 .xp_reply = svc_dg_reply,
77 .xp_destroy = svc_dg_destroy,
78 .xp_control = svc_dg_control,
79};
80
81/*
82 * Usage:
83 * xprt = svc_dg_create(sock, sendsize, recvsize);
84 * Does other connectionless specific initializations.
85 * Once *xprt is initialized, it is registered.
86 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
87 * system defaults are chosen.
88 * The routines returns NULL if a problem occurred.
89 */
90static const char svc_dg_str[] = "svc_dg_create: %s";
91static const char svc_dg_err1[] = "could not get transport information";
92static const char svc_dg_err2[] = "transport does not support data transfer";
93static const char __no_mem_str[] = "out of memory";
94
95SVCXPRT *
96svc_dg_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
97 size_t recvsize)
98{
99 SVCXPRT *xprt;
100 struct __rpc_sockinfo si;
101 struct sockaddr* sa;
102 int error;
103
104 if (!__rpc_socket2sockinfo(so, &si)) {
105 printf(svc_dg_str, svc_dg_err1);
106 return (NULL);
107 }
108 /*
109 * Find the receive and the send size
110 */
111 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
112 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
113 if ((sendsize == 0) || (recvsize == 0)) {
114 printf(svc_dg_str, svc_dg_err2);
115 return (NULL);
116 }
117
118 xprt = svc_xprt_alloc();
119 sx_init(&xprt->xp_lock, "xprt->xp_lock");
120 xprt->xp_pool = pool;
121 xprt->xp_socket = so;
122 xprt->xp_p1 = NULL;
123 xprt->xp_p2 = NULL;
124 xprt->xp_ops = &svc_dg_ops;
125
126 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
127 if (error)
128 goto freedata;
129
130 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
131 free(sa, M_SONAME);
132
133 xprt_register(xprt);
134
135 SOCKBUF_LOCK(&so->so_rcv);
136 so->so_upcallarg = xprt;
137 so->so_upcall = svc_dg_soupcall;
138 so->so_rcv.sb_flags |= SB_UPCALL;
136 soupcall_set(so, SO_RCV, svc_dg_soupcall, xprt);
139 SOCKBUF_UNLOCK(&so->so_rcv);
140
141 return (xprt);
142freedata:
143 (void) printf(svc_dg_str, __no_mem_str);
144 if (xprt) {
145 svc_xprt_free(xprt);
146 }
147 return (NULL);
148}
149
150/*ARGSUSED*/
151static enum xprt_stat
152svc_dg_stat(SVCXPRT *xprt)
153{
154
155 if (soreadable(xprt->xp_socket))
156 return (XPRT_MOREREQS);
157
158 return (XPRT_IDLE);
159}
160
161static bool_t
162svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg,
163 struct sockaddr **addrp, struct mbuf **mp)
164{
165 struct uio uio;
166 struct sockaddr *raddr;
167 struct mbuf *mreq;
168 XDR xdrs;
169 int error, rcvflag;
170
171 /*
172 * Serialise access to the socket.
173 */
174 sx_xlock(&xprt->xp_lock);
175
176 /*
177 * The socket upcall calls xprt_active() which will eventually
178 * cause the server to call us here. We attempt to read a
179 * packet from the socket and process it. If the read fails,
180 * we have drained all pending requests so we call
181 * xprt_inactive().
182 */
183 uio.uio_resid = 1000000000;
184 uio.uio_td = curthread;
185 mreq = NULL;
186 rcvflag = MSG_DONTWAIT;
187 error = soreceive(xprt->xp_socket, &raddr, &uio, &mreq, NULL, &rcvflag);
188
189 if (error == EWOULDBLOCK) {
190 /*
191 * We must re-test for readability after taking the
192 * lock to protect us in the case where a new packet
193 * arrives on the socket after our call to soreceive
194 * fails with EWOULDBLOCK. The pool lock protects us
195 * from racing the upcall after our soreadable() call
196 * returns false.
197 */
198 mtx_lock(&xprt->xp_pool->sp_lock);
199 if (!soreadable(xprt->xp_socket))
200 xprt_inactive_locked(xprt);
201 mtx_unlock(&xprt->xp_pool->sp_lock);
202 sx_xunlock(&xprt->xp_lock);
203 return (FALSE);
204 }
205
206 if (error) {
207 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
137 SOCKBUF_UNLOCK(&so->so_rcv);
138
139 return (xprt);
140freedata:
141 (void) printf(svc_dg_str, __no_mem_str);
142 if (xprt) {
143 svc_xprt_free(xprt);
144 }
145 return (NULL);
146}
147
148/*ARGSUSED*/
149static enum xprt_stat
150svc_dg_stat(SVCXPRT *xprt)
151{
152
153 if (soreadable(xprt->xp_socket))
154 return (XPRT_MOREREQS);
155
156 return (XPRT_IDLE);
157}
158
159static bool_t
160svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg,
161 struct sockaddr **addrp, struct mbuf **mp)
162{
163 struct uio uio;
164 struct sockaddr *raddr;
165 struct mbuf *mreq;
166 XDR xdrs;
167 int error, rcvflag;
168
169 /*
170 * Serialise access to the socket.
171 */
172 sx_xlock(&xprt->xp_lock);
173
174 /*
175 * The socket upcall calls xprt_active() which will eventually
176 * cause the server to call us here. We attempt to read a
177 * packet from the socket and process it. If the read fails,
178 * we have drained all pending requests so we call
179 * xprt_inactive().
180 */
181 uio.uio_resid = 1000000000;
182 uio.uio_td = curthread;
183 mreq = NULL;
184 rcvflag = MSG_DONTWAIT;
185 error = soreceive(xprt->xp_socket, &raddr, &uio, &mreq, NULL, &rcvflag);
186
187 if (error == EWOULDBLOCK) {
188 /*
189 * We must re-test for readability after taking the
190 * lock to protect us in the case where a new packet
191 * arrives on the socket after our call to soreceive
192 * fails with EWOULDBLOCK. The pool lock protects us
193 * from racing the upcall after our soreadable() call
194 * returns false.
195 */
196 mtx_lock(&xprt->xp_pool->sp_lock);
197 if (!soreadable(xprt->xp_socket))
198 xprt_inactive_locked(xprt);
199 mtx_unlock(&xprt->xp_pool->sp_lock);
200 sx_xunlock(&xprt->xp_lock);
201 return (FALSE);
202 }
203
204 if (error) {
205 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
208 xprt->xp_socket->so_upcallarg = NULL;
209 xprt->xp_socket->so_upcall = NULL;
210 xprt->xp_socket->so_rcv.sb_flags &= ~SB_UPCALL;
206 soupcall_clear(xprt->xp_socket, SO_RCV);
211 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
212 xprt_inactive(xprt);
213 sx_xunlock(&xprt->xp_lock);
214 return (FALSE);
215 }
216
217 sx_xunlock(&xprt->xp_lock);
218
219 xdrmbuf_create(&xdrs, mreq, XDR_DECODE);
220 if (! xdr_callmsg(&xdrs, msg)) {
221 XDR_DESTROY(&xdrs);
222 return (FALSE);
223 }
224
225 *addrp = raddr;
226 *mp = xdrmbuf_getall(&xdrs);
227 XDR_DESTROY(&xdrs);
228
229 return (TRUE);
230}
231
232static bool_t
233svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg,
234 struct sockaddr *addr, struct mbuf *m)
235{
236 XDR xdrs;
237 struct mbuf *mrep;
238 bool_t stat = TRUE;
239 int error;
240
241 MGETHDR(mrep, M_WAIT, MT_DATA);
242 mrep->m_len = 0;
243
244 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
245
246 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
247 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
248 if (!xdr_replymsg(&xdrs, msg))
249 stat = FALSE;
250 else
251 xdrmbuf_append(&xdrs, m);
252 } else {
253 stat = xdr_replymsg(&xdrs, msg);
254 }
255
256 if (stat) {
257 m_fixhdr(mrep);
258 error = sosend(xprt->xp_socket, addr, NULL, mrep, NULL,
259 0, curthread);
260 if (!error) {
261 stat = TRUE;
262 }
263 } else {
264 m_freem(mrep);
265 }
266
267 XDR_DESTROY(&xdrs);
268 xprt->xp_p2 = NULL;
269
270 return (stat);
271}
272
273static void
274svc_dg_destroy(SVCXPRT *xprt)
275{
276
277 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
207 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
208 xprt_inactive(xprt);
209 sx_xunlock(&xprt->xp_lock);
210 return (FALSE);
211 }
212
213 sx_xunlock(&xprt->xp_lock);
214
215 xdrmbuf_create(&xdrs, mreq, XDR_DECODE);
216 if (! xdr_callmsg(&xdrs, msg)) {
217 XDR_DESTROY(&xdrs);
218 return (FALSE);
219 }
220
221 *addrp = raddr;
222 *mp = xdrmbuf_getall(&xdrs);
223 XDR_DESTROY(&xdrs);
224
225 return (TRUE);
226}
227
228static bool_t
229svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg,
230 struct sockaddr *addr, struct mbuf *m)
231{
232 XDR xdrs;
233 struct mbuf *mrep;
234 bool_t stat = TRUE;
235 int error;
236
237 MGETHDR(mrep, M_WAIT, MT_DATA);
238 mrep->m_len = 0;
239
240 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
241
242 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
243 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
244 if (!xdr_replymsg(&xdrs, msg))
245 stat = FALSE;
246 else
247 xdrmbuf_append(&xdrs, m);
248 } else {
249 stat = xdr_replymsg(&xdrs, msg);
250 }
251
252 if (stat) {
253 m_fixhdr(mrep);
254 error = sosend(xprt->xp_socket, addr, NULL, mrep, NULL,
255 0, curthread);
256 if (!error) {
257 stat = TRUE;
258 }
259 } else {
260 m_freem(mrep);
261 }
262
263 XDR_DESTROY(&xdrs);
264 xprt->xp_p2 = NULL;
265
266 return (stat);
267}
268
269static void
270svc_dg_destroy(SVCXPRT *xprt)
271{
272
273 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
278 xprt->xp_socket->so_upcallarg = NULL;
279 xprt->xp_socket->so_upcall = NULL;
280 xprt->xp_socket->so_rcv.sb_flags &= ~SB_UPCALL;
274 soupcall_clear(xprt->xp_socket, SO_RCV);
281 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
282
283 sx_destroy(&xprt->xp_lock);
284 if (xprt->xp_socket)
285 (void)soclose(xprt->xp_socket);
286
287 if (xprt->xp_netid)
288 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
289 svc_xprt_free(xprt);
290}
291
292static bool_t
293/*ARGSUSED*/
294svc_dg_control(xprt, rq, in)
295 SVCXPRT *xprt;
296 const u_int rq;
297 void *in;
298{
299
300 return (FALSE);
301}
302
275 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
276
277 sx_destroy(&xprt->xp_lock);
278 if (xprt->xp_socket)
279 (void)soclose(xprt->xp_socket);
280
281 if (xprt->xp_netid)
282 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
283 svc_xprt_free(xprt);
284}
285
286static bool_t
287/*ARGSUSED*/
288svc_dg_control(xprt, rq, in)
289 SVCXPRT *xprt;
290 const u_int rq;
291 void *in;
292{
293
294 return (FALSE);
295}
296
303static void
297static int
304svc_dg_soupcall(struct socket *so, void *arg, int waitflag)
305{
306 SVCXPRT *xprt = (SVCXPRT *) arg;
307
308 xprt_active(xprt);
298svc_dg_soupcall(struct socket *so, void *arg, int waitflag)
299{
300 SVCXPRT *xprt = (SVCXPRT *) arg;
301
302 xprt_active(xprt);
303 return (SU_OK);
309}
304}