Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/rpc/svc_dg.c
39478 views
1
/* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */
2
3
/*-
4
* SPDX-License-Identifier: BSD-3-Clause
5
*
6
* Copyright (c) 2009, Sun Microsystems, Inc.
7
* All rights reserved.
8
*
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions are met:
11
* - Redistributions of source code must retain the above copyright notice,
12
* this list of conditions and the following disclaimer.
13
* - Redistributions in binary form must reproduce the above copyright notice,
14
* this list of conditions and the following disclaimer in the documentation
15
* and/or other materials provided with the distribution.
16
* - Neither the name of Sun Microsystems, 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 "AS IS"
21
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
* POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
/*
34
* Copyright (c) 1986-1991 by Sun Microsystems Inc.
35
*/
36
37
/*
38
* svc_dg.c, Server side for connectionless RPC.
39
*/
40
41
#include <sys/param.h>
42
#include <sys/jail.h>
43
#include <sys/lock.h>
44
#include <sys/kernel.h>
45
#include <sys/malloc.h>
46
#include <sys/mbuf.h>
47
#include <sys/mutex.h>
48
#include <sys/proc.h>
49
#include <sys/protosw.h>
50
#include <sys/queue.h>
51
#include <sys/socket.h>
52
#include <sys/socketvar.h>
53
#include <sys/sx.h>
54
#include <sys/systm.h>
55
#include <sys/uio.h>
56
57
#include <net/vnet.h>
58
59
#include <rpc/rpc.h>
60
61
#include <rpc/rpc_com.h>
62
63
static enum xprt_stat svc_dg_stat(SVCXPRT *);
64
static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *,
65
struct sockaddr **, struct mbuf **);
66
static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *,
67
struct sockaddr *, struct mbuf *, uint32_t *);
68
static void svc_dg_destroy(SVCXPRT *);
69
static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
70
static int svc_dg_soupcall(struct socket *so, void *arg, int waitflag);
71
72
static const struct xp_ops svc_dg_ops = {
73
.xp_recv = svc_dg_recv,
74
.xp_stat = svc_dg_stat,
75
.xp_reply = svc_dg_reply,
76
.xp_destroy = svc_dg_destroy,
77
.xp_control = svc_dg_control,
78
};
79
80
/*
81
* Usage:
82
* xprt = svc_dg_create(sock, sendsize, recvsize);
83
* Does other connectionless specific initializations.
84
* Once *xprt is initialized, it is registered.
85
* see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
86
* system defaults are chosen.
87
* The routines returns NULL if a problem occurred.
88
*/
89
static const char svc_dg_str[] = "svc_dg_create: %s";
90
static const char svc_dg_err1[] = "could not get transport information";
91
static const char svc_dg_err2[] = "transport does not support data transfer";
92
static const char __no_mem_str[] = "out of memory";
93
94
SVCXPRT *
95
svc_dg_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
96
size_t recvsize)
97
{
98
SVCXPRT *xprt;
99
struct __rpc_sockinfo si;
100
int error;
101
102
if (jailed(curthread->td_ucred))
103
return (NULL);
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
xprt->xp_ltaddr.ss_len = sizeof(xprt->xp_ltaddr);
127
error = sosockaddr(so, (struct sockaddr *)&xprt->xp_ltaddr);
128
if (error)
129
goto freedata;
130
131
xprt_register(xprt);
132
133
SOCK_RECVBUF_LOCK(so);
134
soupcall_set(so, SO_RCV, svc_dg_soupcall, xprt);
135
SOCK_RECVBUF_UNLOCK(so);
136
137
return (xprt);
138
freedata:
139
(void) printf(svc_dg_str, __no_mem_str);
140
svc_xprt_free(xprt);
141
142
return (NULL);
143
}
144
145
/*ARGSUSED*/
146
static enum xprt_stat
147
svc_dg_stat(SVCXPRT *xprt)
148
{
149
150
if (soreadable(xprt->xp_socket))
151
return (XPRT_MOREREQS);
152
153
return (XPRT_IDLE);
154
}
155
156
static bool_t
157
svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg,
158
struct sockaddr **addrp, struct mbuf **mp)
159
{
160
struct uio uio;
161
struct sockaddr *raddr;
162
struct mbuf *mreq;
163
XDR xdrs;
164
int error, rcvflag;
165
166
/*
167
* Serialise access to the socket.
168
*/
169
sx_xlock(&xprt->xp_lock);
170
171
/*
172
* The socket upcall calls xprt_active() which will eventually
173
* cause the server to call us here. We attempt to read a
174
* packet from the socket and process it. If the read fails,
175
* we have drained all pending requests so we call
176
* xprt_inactive().
177
*/
178
uio.uio_resid = 1000000000;
179
uio.uio_td = curthread;
180
mreq = NULL;
181
rcvflag = MSG_DONTWAIT;
182
error = soreceive(xprt->xp_socket, &raddr, &uio, &mreq, NULL, &rcvflag);
183
184
if (error == EWOULDBLOCK) {
185
/*
186
* We must re-test for readability after taking the
187
* lock to protect us in the case where a new packet
188
* arrives on the socket after our call to soreceive
189
* fails with EWOULDBLOCK. The pool lock protects us
190
* from racing the upcall after our soreadable() call
191
* returns false.
192
*/
193
SOCK_RECVBUF_LOCK(xprt->xp_socket);
194
if (!soreadable(xprt->xp_socket))
195
xprt_inactive_self(xprt);
196
SOCK_RECVBUF_UNLOCK(xprt->xp_socket);
197
sx_xunlock(&xprt->xp_lock);
198
return (FALSE);
199
}
200
201
if (error) {
202
SOCK_RECVBUF_LOCK(xprt->xp_socket);
203
soupcall_clear(xprt->xp_socket, SO_RCV);
204
SOCK_RECVBUF_UNLOCK(xprt->xp_socket);
205
xprt_inactive_self(xprt);
206
sx_xunlock(&xprt->xp_lock);
207
return (FALSE);
208
}
209
210
sx_xunlock(&xprt->xp_lock);
211
212
xdrmbuf_create(&xdrs, mreq, XDR_DECODE);
213
if (! xdr_callmsg(&xdrs, msg)) {
214
XDR_DESTROY(&xdrs);
215
return (FALSE);
216
}
217
218
*addrp = raddr;
219
*mp = xdrmbuf_getall(&xdrs);
220
XDR_DESTROY(&xdrs);
221
222
return (TRUE);
223
}
224
225
static bool_t
226
svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg,
227
struct sockaddr *addr, struct mbuf *m, uint32_t *seq)
228
{
229
XDR xdrs;
230
struct mbuf *mrep;
231
bool_t stat = TRUE;
232
int error;
233
234
mrep = m_gethdr(M_WAITOK, MT_DATA);
235
236
xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
237
238
if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
239
msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
240
if (!xdr_replymsg(&xdrs, msg))
241
stat = FALSE;
242
else
243
(void)xdr_putmbuf(&xdrs, m);
244
} else {
245
stat = xdr_replymsg(&xdrs, msg);
246
}
247
248
if (stat) {
249
m_fixhdr(mrep);
250
error = sosend(xprt->xp_socket, addr, NULL, mrep, NULL,
251
0, curthread);
252
if (!error) {
253
stat = TRUE;
254
}
255
} else {
256
m_freem(mrep);
257
}
258
259
XDR_DESTROY(&xdrs);
260
xprt->xp_p2 = NULL;
261
262
return (stat);
263
}
264
265
static void
266
svc_dg_destroy(SVCXPRT *xprt)
267
{
268
269
SOCK_RECVBUF_LOCK(xprt->xp_socket);
270
soupcall_clear(xprt->xp_socket, SO_RCV);
271
SOCK_RECVBUF_UNLOCK(xprt->xp_socket);
272
273
sx_destroy(&xprt->xp_lock);
274
if (xprt->xp_socket)
275
(void)soclose(xprt->xp_socket);
276
277
if (xprt->xp_netid)
278
(void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
279
svc_xprt_free(xprt);
280
}
281
282
static bool_t
283
/*ARGSUSED*/
284
svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in)
285
{
286
287
return (FALSE);
288
}
289
290
static int
291
svc_dg_soupcall(struct socket *so, void *arg, int waitflag)
292
{
293
SVCXPRT *xprt = (SVCXPRT *) arg;
294
295
xprt_active(xprt);
296
return (SU_OK);
297
}
298
299