svc_vc.c revision 1.16 1 1.16 christos /* $NetBSD: svc_vc.c,v 1.16 2006/03/22 00:00:16 christos 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.16 christos __RCSID("$NetBSD: svc_vc.c,v 1.16 2006/03/22 00:00:16 christos 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.10 fvdl #include <sys/time.h>
58 1.1 fvdl #include <netinet/in.h>
59 1.1 fvdl #include <netinet/tcp.h>
60 1.1 fvdl
61 1.1 fvdl #include <assert.h>
62 1.1 fvdl #include <err.h>
63 1.1 fvdl #include <errno.h>
64 1.10 fvdl #include <fcntl.h>
65 1.1 fvdl #include <stdio.h>
66 1.1 fvdl #include <stdlib.h>
67 1.1 fvdl #include <string.h>
68 1.1 fvdl #include <unistd.h>
69 1.1 fvdl
70 1.1 fvdl #include <rpc/rpc.h>
71 1.1 fvdl
72 1.10 fvdl #include "rpc_internal.h"
73 1.1 fvdl
74 1.1 fvdl #ifdef __weak_alias
75 1.1 fvdl __weak_alias(svc_fd_create,_svc_fd_create)
76 1.1 fvdl __weak_alias(svc_vc_create,_svc_vc_create)
77 1.11 skrll #endif
78 1.11 skrll
79 1.12 thorpej #ifdef _REENTRANT
80 1.11 skrll extern rwlock_t svc_fd_lock;
81 1.1 fvdl #endif
82 1.1 fvdl
83 1.1 fvdl static SVCXPRT *makefd_xprt __P((int, u_int, u_int));
84 1.1 fvdl static bool_t rendezvous_request __P((SVCXPRT *, struct rpc_msg *));
85 1.1 fvdl static enum xprt_stat rendezvous_stat __P((SVCXPRT *));
86 1.1 fvdl static void svc_vc_destroy __P((SVCXPRT *));
87 1.10 fvdl static void __svc_vc_dodestroy __P((SVCXPRT *));
88 1.1 fvdl static int read_vc __P((caddr_t, caddr_t, int));
89 1.1 fvdl static int write_vc __P((caddr_t, caddr_t, int));
90 1.1 fvdl static enum xprt_stat svc_vc_stat __P((SVCXPRT *));
91 1.1 fvdl static bool_t svc_vc_recv __P((SVCXPRT *, struct rpc_msg *));
92 1.1 fvdl static bool_t svc_vc_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
93 1.1 fvdl static bool_t svc_vc_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
94 1.1 fvdl static bool_t svc_vc_reply __P((SVCXPRT *, struct rpc_msg *));
95 1.1 fvdl static void svc_vc_rendezvous_ops __P((SVCXPRT *));
96 1.1 fvdl static void svc_vc_ops __P((SVCXPRT *));
97 1.1 fvdl static bool_t svc_vc_control __P((SVCXPRT *xprt, const u_int rq, void *in));
98 1.10 fvdl static bool_t svc_vc_rendezvous_control __P((SVCXPRT *xprt, const u_int rq,
99 1.10 fvdl void *in));
100 1.1 fvdl
101 1.1 fvdl struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
102 1.1 fvdl u_int sendsize;
103 1.1 fvdl u_int recvsize;
104 1.10 fvdl int maxrec;
105 1.1 fvdl };
106 1.1 fvdl
107 1.1 fvdl struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
108 1.1 fvdl enum xprt_stat strm_stat;
109 1.1 fvdl u_int32_t x_id;
110 1.1 fvdl XDR xdrs;
111 1.1 fvdl char verf_body[MAX_AUTH_BYTES];
112 1.10 fvdl u_int sendsize;
113 1.10 fvdl u_int recvsize;
114 1.10 fvdl int maxrec;
115 1.10 fvdl bool_t nonblock;
116 1.10 fvdl struct timeval last_recv_time;
117 1.1 fvdl };
118 1.1 fvdl
119 1.1 fvdl /*
120 1.1 fvdl * Usage:
121 1.1 fvdl * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
122 1.1 fvdl *
123 1.1 fvdl * Creates, registers, and returns a (rpc) tcp based transporter.
124 1.1 fvdl * Once *xprt is initialized, it is registered as a transporter
125 1.1 fvdl * see (svc.h, xprt_register). This routine returns
126 1.1 fvdl * a NULL if a problem occurred.
127 1.1 fvdl *
128 1.1 fvdl * The filedescriptor passed in is expected to refer to a bound, but
129 1.1 fvdl * not yet connected socket.
130 1.1 fvdl *
131 1.1 fvdl * Since streams do buffered io similar to stdio, the caller can specify
132 1.1 fvdl * how big the send and receive buffers are via the second and third parms;
133 1.1 fvdl * 0 => use the system default.
134 1.1 fvdl */
135 1.1 fvdl SVCXPRT *
136 1.1 fvdl svc_vc_create(fd, sendsize, recvsize)
137 1.1 fvdl int fd;
138 1.1 fvdl u_int sendsize;
139 1.1 fvdl u_int recvsize;
140 1.1 fvdl {
141 1.1 fvdl SVCXPRT *xprt;
142 1.1 fvdl struct cf_rendezvous *r = NULL;
143 1.1 fvdl struct __rpc_sockinfo si;
144 1.1 fvdl struct sockaddr_storage sslocal;
145 1.1 fvdl socklen_t slen;
146 1.3 thorpej int one = 1;
147 1.1 fvdl
148 1.5 christos r = mem_alloc(sizeof(*r));
149 1.1 fvdl if (r == NULL) {
150 1.1 fvdl warnx("svc_vc_create: out of memory");
151 1.1 fvdl goto cleanup_svc_vc_create;
152 1.1 fvdl }
153 1.1 fvdl if (!__rpc_fd2sockinfo(fd, &si))
154 1.1 fvdl return NULL;
155 1.5 christos r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
156 1.5 christos r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
157 1.10 fvdl r->maxrec = __svc_maxrec;
158 1.5 christos xprt = mem_alloc(sizeof(SVCXPRT));
159 1.1 fvdl if (xprt == NULL) {
160 1.1 fvdl warnx("svc_vc_create: out of memory");
161 1.1 fvdl goto cleanup_svc_vc_create;
162 1.1 fvdl }
163 1.1 fvdl xprt->xp_tp = NULL;
164 1.1 fvdl xprt->xp_p1 = (caddr_t)(void *)r;
165 1.1 fvdl xprt->xp_p2 = NULL;
166 1.1 fvdl xprt->xp_p3 = NULL;
167 1.1 fvdl xprt->xp_verf = _null_auth;
168 1.6 kleink svc_vc_rendezvous_ops(xprt);
169 1.5 christos xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
170 1.1 fvdl xprt->xp_fd = fd;
171 1.1 fvdl
172 1.1 fvdl slen = sizeof (struct sockaddr_storage);
173 1.5 christos if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
174 1.1 fvdl warnx("svc_vc_create: could not retrieve local addr");
175 1.1 fvdl goto cleanup_svc_vc_create;
176 1.1 fvdl }
177 1.1 fvdl
178 1.1 fvdl /*
179 1.1 fvdl * We want to be able to check credentials on local sockets.
180 1.1 fvdl */
181 1.1 fvdl if (sslocal.ss_family == AF_LOCAL)
182 1.1 fvdl if (setsockopt(fd, 0, LOCAL_CREDS, &one, sizeof one) < 0)
183 1.1 fvdl goto cleanup_svc_vc_create;
184 1.1 fvdl
185 1.1 fvdl xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
186 1.5 christos xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
187 1.1 fvdl if (xprt->xp_ltaddr.buf == NULL) {
188 1.1 fvdl warnx("svc_vc_create: no mem for local addr");
189 1.1 fvdl goto cleanup_svc_vc_create;
190 1.1 fvdl }
191 1.5 christos memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
192 1.1 fvdl
193 1.1 fvdl xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
194 1.1 fvdl xprt_register(xprt);
195 1.1 fvdl return (xprt);
196 1.1 fvdl cleanup_svc_vc_create:
197 1.16 christos if (xprt)
198 1.16 christos mem_free(xprt, sizeof(*xprt));
199 1.1 fvdl if (r != NULL)
200 1.1 fvdl mem_free(r, sizeof(*r));
201 1.5 christos return (NULL);
202 1.1 fvdl }
203 1.1 fvdl
204 1.1 fvdl /*
205 1.1 fvdl * Like svtcp_create(), except the routine takes any *open* UNIX file
206 1.1 fvdl * descriptor as its first input.
207 1.1 fvdl */
208 1.1 fvdl SVCXPRT *
209 1.1 fvdl svc_fd_create(fd, sendsize, recvsize)
210 1.1 fvdl int fd;
211 1.1 fvdl u_int sendsize;
212 1.1 fvdl u_int recvsize;
213 1.1 fvdl {
214 1.1 fvdl struct sockaddr_storage ss;
215 1.1 fvdl socklen_t slen;
216 1.1 fvdl SVCXPRT *ret;
217 1.1 fvdl
218 1.1 fvdl _DIAGASSERT(fd != -1);
219 1.1 fvdl
220 1.1 fvdl ret = makefd_xprt(fd, sendsize, recvsize);
221 1.1 fvdl if (ret == NULL)
222 1.1 fvdl return NULL;
223 1.1 fvdl
224 1.1 fvdl slen = sizeof (struct sockaddr_storage);
225 1.5 christos if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
226 1.4 fvdl warnx("svc_fd_create: could not retrieve local addr");
227 1.1 fvdl goto freedata;
228 1.1 fvdl }
229 1.1 fvdl ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
230 1.5 christos ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
231 1.1 fvdl if (ret->xp_ltaddr.buf == NULL) {
232 1.1 fvdl warnx("svc_fd_create: no mem for local addr");
233 1.1 fvdl goto freedata;
234 1.1 fvdl }
235 1.5 christos memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
236 1.1 fvdl
237 1.1 fvdl slen = sizeof (struct sockaddr_storage);
238 1.5 christos if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
239 1.4 fvdl warnx("svc_fd_create: could not retrieve remote addr");
240 1.1 fvdl goto freedata;
241 1.1 fvdl }
242 1.1 fvdl ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
243 1.5 christos ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
244 1.1 fvdl if (ret->xp_rtaddr.buf == NULL) {
245 1.1 fvdl warnx("svc_fd_create: no mem for local addr");
246 1.1 fvdl goto freedata;
247 1.1 fvdl }
248 1.5 christos memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
249 1.1 fvdl #ifdef PORTMAP
250 1.1 fvdl if (ss.ss_family == AF_INET) {
251 1.1 fvdl ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
252 1.1 fvdl ret->xp_addrlen = sizeof (struct sockaddr_in);
253 1.1 fvdl }
254 1.1 fvdl #endif
255 1.1 fvdl
256 1.1 fvdl return ret;
257 1.1 fvdl
258 1.1 fvdl freedata:
259 1.1 fvdl if (ret->xp_ltaddr.buf != NULL)
260 1.1 fvdl mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
261 1.1 fvdl
262 1.1 fvdl return NULL;
263 1.1 fvdl }
264 1.1 fvdl
265 1.1 fvdl static SVCXPRT *
266 1.1 fvdl makefd_xprt(fd, sendsize, recvsize)
267 1.1 fvdl int fd;
268 1.1 fvdl u_int sendsize;
269 1.1 fvdl u_int recvsize;
270 1.1 fvdl {
271 1.1 fvdl SVCXPRT *xprt;
272 1.1 fvdl struct cf_conn *cd;
273 1.7 fvdl const char *netid;
274 1.7 fvdl struct __rpc_sockinfo si;
275 1.1 fvdl
276 1.1 fvdl _DIAGASSERT(fd != -1);
277 1.1 fvdl
278 1.5 christos xprt = mem_alloc(sizeof(SVCXPRT));
279 1.5 christos if (xprt == NULL) {
280 1.7 fvdl warnx("svc_vc: makefd_xprt: out of memory");
281 1.1 fvdl goto done;
282 1.1 fvdl }
283 1.2 fvdl memset(xprt, 0, sizeof *xprt);
284 1.5 christos cd = mem_alloc(sizeof(struct cf_conn));
285 1.5 christos if (cd == NULL) {
286 1.1 fvdl warnx("svc_tcp: makefd_xprt: out of memory");
287 1.1 fvdl mem_free(xprt, sizeof(SVCXPRT));
288 1.5 christos xprt = NULL;
289 1.1 fvdl goto done;
290 1.1 fvdl }
291 1.1 fvdl cd->strm_stat = XPRT_IDLE;
292 1.1 fvdl xdrrec_create(&(cd->xdrs), sendsize, recvsize,
293 1.1 fvdl (caddr_t)(void *)xprt, read_vc, write_vc);
294 1.1 fvdl xprt->xp_p1 = (caddr_t)(void *)cd;
295 1.1 fvdl xprt->xp_verf.oa_base = cd->verf_body;
296 1.1 fvdl svc_vc_ops(xprt); /* truely deals with calls */
297 1.1 fvdl xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
298 1.1 fvdl xprt->xp_fd = fd;
299 1.8 lukem if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
300 1.7 fvdl xprt->xp_netid = strdup(netid);
301 1.7 fvdl
302 1.1 fvdl xprt_register(xprt);
303 1.1 fvdl done:
304 1.1 fvdl return (xprt);
305 1.1 fvdl }
306 1.1 fvdl
307 1.1 fvdl /*ARGSUSED*/
308 1.1 fvdl static bool_t
309 1.1 fvdl rendezvous_request(xprt, msg)
310 1.1 fvdl SVCXPRT *xprt;
311 1.1 fvdl struct rpc_msg *msg;
312 1.1 fvdl {
313 1.10 fvdl int sock, flags;
314 1.1 fvdl struct cf_rendezvous *r;
315 1.10 fvdl struct cf_conn *cd;
316 1.1 fvdl struct sockaddr_storage addr;
317 1.1 fvdl socklen_t len;
318 1.1 fvdl struct __rpc_sockinfo si;
319 1.10 fvdl SVCXPRT *newxprt;
320 1.10 fvdl fd_set cleanfds;
321 1.1 fvdl
322 1.1 fvdl _DIAGASSERT(xprt != NULL);
323 1.1 fvdl _DIAGASSERT(msg != NULL);
324 1.1 fvdl
325 1.1 fvdl r = (struct cf_rendezvous *)xprt->xp_p1;
326 1.1 fvdl again:
327 1.1 fvdl len = sizeof addr;
328 1.5 christos if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
329 1.5 christos &len)) < 0) {
330 1.1 fvdl if (errno == EINTR)
331 1.1 fvdl goto again;
332 1.10 fvdl /*
333 1.10 fvdl * Clean out the most idle file descriptor when we're
334 1.10 fvdl * running out.
335 1.10 fvdl */
336 1.10 fvdl if (errno == EMFILE || errno == ENFILE) {
337 1.10 fvdl cleanfds = svc_fdset;
338 1.10 fvdl __svc_clean_idle(&cleanfds, 0, FALSE);
339 1.10 fvdl goto again;
340 1.10 fvdl }
341 1.10 fvdl return (FALSE);
342 1.1 fvdl }
343 1.1 fvdl /*
344 1.1 fvdl * make a new transporter (re-uses xprt)
345 1.1 fvdl */
346 1.10 fvdl newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
347 1.10 fvdl newxprt->xp_rtaddr.buf = mem_alloc(len);
348 1.10 fvdl if (newxprt->xp_rtaddr.buf == NULL)
349 1.1 fvdl return (FALSE);
350 1.10 fvdl memcpy(newxprt->xp_rtaddr.buf, &addr, len);
351 1.10 fvdl newxprt->xp_rtaddr.len = len;
352 1.1 fvdl #ifdef PORTMAP
353 1.1 fvdl if (addr.ss_family == AF_INET) {
354 1.10 fvdl newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
355 1.10 fvdl newxprt->xp_addrlen = sizeof (struct sockaddr_in);
356 1.1 fvdl }
357 1.1 fvdl #endif
358 1.1 fvdl if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
359 1.1 fvdl len = 1;
360 1.1 fvdl /* XXX fvdl - is this useful? */
361 1.1 fvdl setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
362 1.1 fvdl }
363 1.10 fvdl
364 1.10 fvdl cd = (struct cf_conn *)newxprt->xp_p1;
365 1.10 fvdl
366 1.10 fvdl cd->recvsize = r->recvsize;
367 1.10 fvdl cd->sendsize = r->sendsize;
368 1.10 fvdl cd->maxrec = r->maxrec;
369 1.10 fvdl
370 1.10 fvdl if (cd->maxrec != 0) {
371 1.10 fvdl flags = fcntl(sock, F_GETFL, 0);
372 1.10 fvdl if (flags == -1)
373 1.10 fvdl return (FALSE);
374 1.10 fvdl if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
375 1.10 fvdl return (FALSE);
376 1.10 fvdl if (cd->recvsize > cd->maxrec)
377 1.10 fvdl cd->recvsize = cd->maxrec;
378 1.10 fvdl cd->nonblock = TRUE;
379 1.10 fvdl __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
380 1.10 fvdl } else
381 1.10 fvdl cd->nonblock = FALSE;
382 1.10 fvdl
383 1.10 fvdl gettimeofday(&cd->last_recv_time, NULL);
384 1.10 fvdl
385 1.1 fvdl return (FALSE); /* there is never an rpc msg to be processed */
386 1.1 fvdl }
387 1.1 fvdl
388 1.1 fvdl /*ARGSUSED*/
389 1.1 fvdl static enum xprt_stat
390 1.1 fvdl rendezvous_stat(xprt)
391 1.1 fvdl SVCXPRT *xprt;
392 1.1 fvdl {
393 1.1 fvdl
394 1.1 fvdl return (XPRT_IDLE);
395 1.1 fvdl }
396 1.1 fvdl
397 1.1 fvdl static void
398 1.1 fvdl svc_vc_destroy(xprt)
399 1.1 fvdl SVCXPRT *xprt;
400 1.1 fvdl {
401 1.10 fvdl _DIAGASSERT(xprt != NULL);
402 1.10 fvdl
403 1.10 fvdl xprt_unregister(xprt);
404 1.10 fvdl __svc_vc_dodestroy(xprt);
405 1.10 fvdl }
406 1.10 fvdl
407 1.10 fvdl static void
408 1.10 fvdl __svc_vc_dodestroy(xprt)
409 1.10 fvdl SVCXPRT *xprt;
410 1.10 fvdl {
411 1.1 fvdl struct cf_conn *cd;
412 1.1 fvdl struct cf_rendezvous *r;
413 1.1 fvdl
414 1.1 fvdl cd = (struct cf_conn *)xprt->xp_p1;
415 1.1 fvdl
416 1.1 fvdl if (xprt->xp_fd != RPC_ANYFD)
417 1.1 fvdl (void)close(xprt->xp_fd);
418 1.1 fvdl if (xprt->xp_port != 0) {
419 1.1 fvdl /* a rendezvouser socket */
420 1.1 fvdl r = (struct cf_rendezvous *)xprt->xp_p1;
421 1.1 fvdl mem_free(r, sizeof (struct cf_rendezvous));
422 1.1 fvdl xprt->xp_port = 0;
423 1.1 fvdl } else {
424 1.1 fvdl /* an actual connection socket */
425 1.1 fvdl XDR_DESTROY(&(cd->xdrs));
426 1.1 fvdl mem_free(cd, sizeof(struct cf_conn));
427 1.1 fvdl }
428 1.1 fvdl if (xprt->xp_rtaddr.buf)
429 1.1 fvdl mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
430 1.1 fvdl if (xprt->xp_ltaddr.buf)
431 1.1 fvdl mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
432 1.1 fvdl if (xprt->xp_tp)
433 1.1 fvdl free(xprt->xp_tp);
434 1.1 fvdl if (xprt->xp_netid)
435 1.1 fvdl free(xprt->xp_netid);
436 1.1 fvdl mem_free(xprt, sizeof(SVCXPRT));
437 1.1 fvdl }
438 1.1 fvdl
439 1.5 christos /*ARGSUSED*/
440 1.1 fvdl static bool_t
441 1.1 fvdl svc_vc_control(xprt, rq, in)
442 1.1 fvdl SVCXPRT *xprt;
443 1.1 fvdl const u_int rq;
444 1.1 fvdl void *in;
445 1.1 fvdl {
446 1.1 fvdl return (FALSE);
447 1.1 fvdl }
448 1.1 fvdl
449 1.10 fvdl /*ARGSUSED*/
450 1.10 fvdl static bool_t
451 1.10 fvdl svc_vc_rendezvous_control(xprt, rq, in)
452 1.10 fvdl SVCXPRT *xprt;
453 1.10 fvdl const u_int rq;
454 1.10 fvdl void *in;
455 1.10 fvdl {
456 1.10 fvdl struct cf_rendezvous *cfp;
457 1.10 fvdl
458 1.10 fvdl cfp = (struct cf_rendezvous *)xprt->xp_p1;
459 1.10 fvdl if (cfp == NULL)
460 1.10 fvdl return (FALSE);
461 1.10 fvdl switch (rq) {
462 1.10 fvdl case SVCGET_CONNMAXREC:
463 1.10 fvdl *(int *)in = cfp->maxrec;
464 1.10 fvdl break;
465 1.10 fvdl case SVCSET_CONNMAXREC:
466 1.10 fvdl cfp->maxrec = *(int *)in;
467 1.10 fvdl break;
468 1.10 fvdl default:
469 1.10 fvdl return (FALSE);
470 1.10 fvdl }
471 1.10 fvdl return (TRUE);
472 1.10 fvdl }
473 1.10 fvdl
474 1.1 fvdl /*
475 1.9 cjep * reads data from the tcp connection.
476 1.1 fvdl * any error is fatal and the connection is closed.
477 1.1 fvdl * (And a read of zero bytes is a half closed stream => error.)
478 1.1 fvdl * All read operations timeout after 35 seconds. A timeout is
479 1.1 fvdl * fatal for the connection.
480 1.1 fvdl */
481 1.1 fvdl static int
482 1.1 fvdl read_vc(xprtp, buf, len)
483 1.1 fvdl caddr_t xprtp;
484 1.1 fvdl caddr_t buf;
485 1.1 fvdl int len;
486 1.1 fvdl {
487 1.1 fvdl SVCXPRT *xprt;
488 1.1 fvdl int sock;
489 1.1 fvdl struct pollfd pollfd;
490 1.1 fvdl struct sockaddr *sa;
491 1.1 fvdl struct msghdr msg;
492 1.1 fvdl struct cmsghdr *cmp;
493 1.3 thorpej void *crmsg = NULL;
494 1.1 fvdl struct sockcred *sc;
495 1.3 thorpej socklen_t crmsgsize;
496 1.10 fvdl struct cf_conn *cfp;
497 1.13 christos static const struct timespec ts = { 35, 0 };
498 1.1 fvdl
499 1.1 fvdl xprt = (SVCXPRT *)(void *)xprtp;
500 1.1 fvdl _DIAGASSERT(xprt != NULL);
501 1.1 fvdl
502 1.1 fvdl sock = xprt->xp_fd;
503 1.1 fvdl
504 1.1 fvdl sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
505 1.1 fvdl if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) {
506 1.1 fvdl memset(&msg, 0, sizeof msg);
507 1.3 thorpej crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
508 1.3 thorpej crmsg = malloc(crmsgsize);
509 1.3 thorpej if (crmsg == NULL)
510 1.3 thorpej goto fatal_err;
511 1.3 thorpej memset(crmsg, 0, crmsgsize);
512 1.3 thorpej
513 1.3 thorpej msg.msg_control = crmsg;
514 1.3 thorpej msg.msg_controllen = crmsgsize;
515 1.1 fvdl
516 1.1 fvdl if (recvmsg(sock, &msg, 0) < 0)
517 1.1 fvdl goto fatal_err;
518 1.1 fvdl
519 1.3 thorpej if (msg.msg_controllen == 0 ||
520 1.3 thorpej (msg.msg_flags & MSG_CTRUNC) != 0)
521 1.3 thorpej goto fatal_err;
522 1.3 thorpej
523 1.1 fvdl cmp = CMSG_FIRSTHDR(&msg);
524 1.1 fvdl if (cmp->cmsg_level != SOL_SOCKET ||
525 1.1 fvdl cmp->cmsg_type != SCM_CREDS)
526 1.1 fvdl goto fatal_err;
527 1.1 fvdl
528 1.5 christos sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
529 1.1 fvdl
530 1.1 fvdl xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups));
531 1.1 fvdl if (xprt->xp_p2 == NULL)
532 1.1 fvdl goto fatal_err;
533 1.1 fvdl
534 1.1 fvdl memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups));
535 1.3 thorpej free(crmsg);
536 1.3 thorpej crmsg = NULL;
537 1.1 fvdl }
538 1.10 fvdl
539 1.10 fvdl cfp = (struct cf_conn *)xprt->xp_p1;
540 1.10 fvdl
541 1.10 fvdl if (cfp->nonblock) {
542 1.10 fvdl len = read(sock, buf, (size_t)len);
543 1.10 fvdl if (len < 0) {
544 1.10 fvdl if (errno == EAGAIN)
545 1.10 fvdl len = 0;
546 1.10 fvdl else
547 1.10 fvdl goto fatal_err;
548 1.10 fvdl }
549 1.10 fvdl if (len != 0)
550 1.10 fvdl gettimeofday(&cfp->last_recv_time, NULL);
551 1.10 fvdl return len;
552 1.10 fvdl }
553 1.10 fvdl
554 1.1 fvdl do {
555 1.1 fvdl pollfd.fd = sock;
556 1.1 fvdl pollfd.events = POLLIN;
557 1.13 christos switch (pollts(&pollfd, 1, &ts, NULL)) {
558 1.1 fvdl case -1:
559 1.1 fvdl if (errno == EINTR) {
560 1.1 fvdl continue;
561 1.1 fvdl }
562 1.1 fvdl /*FALLTHROUGH*/
563 1.1 fvdl case 0:
564 1.1 fvdl goto fatal_err;
565 1.1 fvdl
566 1.1 fvdl default:
567 1.1 fvdl break;
568 1.1 fvdl }
569 1.1 fvdl } while ((pollfd.revents & POLLIN) == 0);
570 1.1 fvdl
571 1.10 fvdl if ((len = read(sock, buf, (size_t)len)) > 0) {
572 1.10 fvdl gettimeofday(&cfp->last_recv_time, NULL);
573 1.1 fvdl return (len);
574 1.10 fvdl }
575 1.1 fvdl
576 1.1 fvdl fatal_err:
577 1.3 thorpej if (crmsg != NULL)
578 1.3 thorpej free(crmsg);
579 1.1 fvdl ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
580 1.1 fvdl return (-1);
581 1.1 fvdl }
582 1.1 fvdl
583 1.1 fvdl /*
584 1.1 fvdl * writes data to the tcp connection.
585 1.1 fvdl * Any error is fatal and the connection is closed.
586 1.1 fvdl */
587 1.1 fvdl static int
588 1.1 fvdl write_vc(xprtp, buf, len)
589 1.1 fvdl caddr_t xprtp;
590 1.1 fvdl caddr_t buf;
591 1.1 fvdl int len;
592 1.1 fvdl {
593 1.1 fvdl SVCXPRT *xprt;
594 1.1 fvdl int i, cnt;
595 1.10 fvdl struct cf_conn *cd;
596 1.10 fvdl struct timeval tv0, tv1;
597 1.1 fvdl
598 1.1 fvdl xprt = (SVCXPRT *)(void *)xprtp;
599 1.1 fvdl _DIAGASSERT(xprt != NULL);
600 1.1 fvdl
601 1.10 fvdl cd = (struct cf_conn *)xprt->xp_p1;
602 1.10 fvdl
603 1.10 fvdl if (cd->nonblock)
604 1.10 fvdl gettimeofday(&tv0, NULL);
605 1.10 fvdl
606 1.1 fvdl for (cnt = len; cnt > 0; cnt -= i, buf += i) {
607 1.1 fvdl if ((i = write(xprt->xp_fd, buf, (size_t)cnt)) < 0) {
608 1.10 fvdl if (errno != EAGAIN || !cd->nonblock) {
609 1.10 fvdl cd->strm_stat = XPRT_DIED;
610 1.10 fvdl return (-1);
611 1.10 fvdl }
612 1.10 fvdl if (cd->nonblock && i != cnt) {
613 1.10 fvdl /*
614 1.10 fvdl * For non-blocking connections, do not
615 1.10 fvdl * take more than 2 seconds writing the
616 1.10 fvdl * data out.
617 1.10 fvdl *
618 1.10 fvdl * XXX 2 is an arbitrary amount.
619 1.10 fvdl */
620 1.10 fvdl gettimeofday(&tv1, NULL);
621 1.10 fvdl if (tv1.tv_sec - tv0.tv_sec >= 2) {
622 1.10 fvdl cd->strm_stat = XPRT_DIED;
623 1.10 fvdl return (-1);
624 1.10 fvdl }
625 1.10 fvdl }
626 1.1 fvdl }
627 1.1 fvdl }
628 1.1 fvdl return (len);
629 1.1 fvdl }
630 1.1 fvdl
631 1.1 fvdl static enum xprt_stat
632 1.1 fvdl svc_vc_stat(xprt)
633 1.1 fvdl SVCXPRT *xprt;
634 1.1 fvdl {
635 1.1 fvdl struct cf_conn *cd;
636 1.1 fvdl
637 1.1 fvdl _DIAGASSERT(xprt != NULL);
638 1.1 fvdl
639 1.1 fvdl cd = (struct cf_conn *)(xprt->xp_p1);
640 1.1 fvdl
641 1.1 fvdl if (cd->strm_stat == XPRT_DIED)
642 1.1 fvdl return (XPRT_DIED);
643 1.1 fvdl if (! xdrrec_eof(&(cd->xdrs)))
644 1.1 fvdl return (XPRT_MOREREQS);
645 1.1 fvdl return (XPRT_IDLE);
646 1.1 fvdl }
647 1.1 fvdl
648 1.1 fvdl static bool_t
649 1.1 fvdl svc_vc_recv(xprt, msg)
650 1.1 fvdl SVCXPRT *xprt;
651 1.1 fvdl struct rpc_msg *msg;
652 1.1 fvdl {
653 1.1 fvdl struct cf_conn *cd;
654 1.1 fvdl XDR *xdrs;
655 1.1 fvdl
656 1.1 fvdl _DIAGASSERT(xprt != NULL);
657 1.1 fvdl _DIAGASSERT(msg != NULL);
658 1.1 fvdl
659 1.1 fvdl cd = (struct cf_conn *)(xprt->xp_p1);
660 1.1 fvdl xdrs = &(cd->xdrs);
661 1.1 fvdl
662 1.10 fvdl if (cd->nonblock) {
663 1.10 fvdl if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
664 1.10 fvdl return FALSE;
665 1.10 fvdl }
666 1.10 fvdl
667 1.1 fvdl xdrs->x_op = XDR_DECODE;
668 1.1 fvdl (void)xdrrec_skiprecord(xdrs);
669 1.10 fvdl
670 1.1 fvdl if (xdr_callmsg(xdrs, msg)) {
671 1.1 fvdl cd->x_id = msg->rm_xid;
672 1.1 fvdl return (TRUE);
673 1.1 fvdl }
674 1.1 fvdl cd->strm_stat = XPRT_DIED;
675 1.1 fvdl return (FALSE);
676 1.1 fvdl }
677 1.1 fvdl
678 1.1 fvdl static bool_t
679 1.1 fvdl svc_vc_getargs(xprt, xdr_args, args_ptr)
680 1.1 fvdl SVCXPRT *xprt;
681 1.1 fvdl xdrproc_t xdr_args;
682 1.1 fvdl caddr_t args_ptr;
683 1.1 fvdl {
684 1.1 fvdl
685 1.1 fvdl _DIAGASSERT(xprt != NULL);
686 1.1 fvdl /* args_ptr may be NULL */
687 1.1 fvdl
688 1.1 fvdl return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
689 1.1 fvdl args_ptr));
690 1.1 fvdl }
691 1.1 fvdl
692 1.1 fvdl static bool_t
693 1.1 fvdl svc_vc_freeargs(xprt, xdr_args, args_ptr)
694 1.1 fvdl SVCXPRT *xprt;
695 1.1 fvdl xdrproc_t xdr_args;
696 1.1 fvdl caddr_t args_ptr;
697 1.1 fvdl {
698 1.1 fvdl XDR *xdrs;
699 1.1 fvdl
700 1.1 fvdl _DIAGASSERT(xprt != NULL);
701 1.1 fvdl /* args_ptr may be NULL */
702 1.1 fvdl
703 1.1 fvdl xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
704 1.1 fvdl
705 1.1 fvdl xdrs->x_op = XDR_FREE;
706 1.1 fvdl return ((*xdr_args)(xdrs, args_ptr));
707 1.1 fvdl }
708 1.1 fvdl
709 1.1 fvdl static bool_t
710 1.1 fvdl svc_vc_reply(xprt, msg)
711 1.1 fvdl SVCXPRT *xprt;
712 1.1 fvdl struct rpc_msg *msg;
713 1.1 fvdl {
714 1.1 fvdl struct cf_conn *cd;
715 1.1 fvdl XDR *xdrs;
716 1.10 fvdl bool_t rstat;
717 1.1 fvdl
718 1.1 fvdl _DIAGASSERT(xprt != NULL);
719 1.1 fvdl _DIAGASSERT(msg != NULL);
720 1.1 fvdl
721 1.1 fvdl cd = (struct cf_conn *)(xprt->xp_p1);
722 1.1 fvdl xdrs = &(cd->xdrs);
723 1.1 fvdl
724 1.1 fvdl xdrs->x_op = XDR_ENCODE;
725 1.1 fvdl msg->rm_xid = cd->x_id;
726 1.10 fvdl rstat = xdr_replymsg(xdrs, msg);
727 1.1 fvdl (void)xdrrec_endofrecord(xdrs, TRUE);
728 1.10 fvdl return (rstat);
729 1.1 fvdl }
730 1.1 fvdl
731 1.1 fvdl static void
732 1.1 fvdl svc_vc_ops(xprt)
733 1.1 fvdl SVCXPRT *xprt;
734 1.1 fvdl {
735 1.1 fvdl static struct xp_ops ops;
736 1.1 fvdl static struct xp_ops2 ops2;
737 1.12 thorpej #ifdef _REENTRANT
738 1.1 fvdl extern mutex_t ops_lock;
739 1.1 fvdl #endif
740 1.1 fvdl
741 1.1 fvdl /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
742 1.1 fvdl
743 1.1 fvdl mutex_lock(&ops_lock);
744 1.1 fvdl if (ops.xp_recv == NULL) {
745 1.1 fvdl ops.xp_recv = svc_vc_recv;
746 1.1 fvdl ops.xp_stat = svc_vc_stat;
747 1.1 fvdl ops.xp_getargs = svc_vc_getargs;
748 1.1 fvdl ops.xp_reply = svc_vc_reply;
749 1.1 fvdl ops.xp_freeargs = svc_vc_freeargs;
750 1.1 fvdl ops.xp_destroy = svc_vc_destroy;
751 1.1 fvdl ops2.xp_control = svc_vc_control;
752 1.1 fvdl }
753 1.1 fvdl xprt->xp_ops = &ops;
754 1.1 fvdl xprt->xp_ops2 = &ops2;
755 1.1 fvdl mutex_unlock(&ops_lock);
756 1.1 fvdl }
757 1.1 fvdl
758 1.1 fvdl static void
759 1.1 fvdl svc_vc_rendezvous_ops(xprt)
760 1.1 fvdl SVCXPRT *xprt;
761 1.1 fvdl {
762 1.1 fvdl static struct xp_ops ops;
763 1.1 fvdl static struct xp_ops2 ops2;
764 1.12 thorpej #ifdef _REENTRANT
765 1.1 fvdl extern mutex_t ops_lock;
766 1.1 fvdl #endif
767 1.15 jmc /* XXXGCC vax compiler unhappy otherwise */
768 1.15 jmc #ifdef __vax__
769 1.15 jmc extern void abort(void);
770 1.15 jmc #endif
771 1.1 fvdl
772 1.1 fvdl mutex_lock(&ops_lock);
773 1.1 fvdl if (ops.xp_recv == NULL) {
774 1.1 fvdl ops.xp_recv = rendezvous_request;
775 1.1 fvdl ops.xp_stat = rendezvous_stat;
776 1.1 fvdl ops.xp_getargs =
777 1.1 fvdl (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
778 1.1 fvdl ops.xp_reply =
779 1.1 fvdl (bool_t (*) __P((SVCXPRT *, struct rpc_msg *)))abort;
780 1.1 fvdl ops.xp_freeargs =
781 1.14 christos (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
782 1.1 fvdl ops.xp_destroy = svc_vc_destroy;
783 1.10 fvdl ops2.xp_control = svc_vc_rendezvous_control;
784 1.1 fvdl }
785 1.1 fvdl xprt->xp_ops = &ops;
786 1.1 fvdl xprt->xp_ops2 = &ops2;
787 1.1 fvdl mutex_unlock(&ops_lock);
788 1.10 fvdl }
789 1.10 fvdl
790 1.10 fvdl /*
791 1.10 fvdl * Destroy xprts that have not have had any activity in 'timeout' seconds.
792 1.10 fvdl * If 'cleanblock' is true, blocking connections (the default) are also
793 1.10 fvdl * cleaned. If timeout is 0, the least active connection is picked.
794 1.10 fvdl */
795 1.10 fvdl bool_t
796 1.10 fvdl __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
797 1.10 fvdl {
798 1.10 fvdl int i, ncleaned;
799 1.10 fvdl SVCXPRT *xprt, *least_active;
800 1.10 fvdl struct timeval tv, tdiff, tmax;
801 1.10 fvdl struct cf_conn *cd;
802 1.10 fvdl
803 1.10 fvdl gettimeofday(&tv, NULL);
804 1.10 fvdl tmax.tv_sec = tmax.tv_usec = 0;
805 1.10 fvdl least_active = NULL;
806 1.10 fvdl rwlock_wrlock(&svc_fd_lock);
807 1.10 fvdl for (i = ncleaned = 0; i <= svc_maxfd; i++) {
808 1.10 fvdl if (FD_ISSET(i, fds)) {
809 1.10 fvdl xprt = __svc_xports[i];
810 1.10 fvdl if (xprt == NULL || xprt->xp_ops == NULL ||
811 1.10 fvdl xprt->xp_ops->xp_recv != svc_vc_recv)
812 1.10 fvdl continue;
813 1.10 fvdl cd = (struct cf_conn *)xprt->xp_p1;
814 1.10 fvdl if (!cleanblock && !cd->nonblock)
815 1.10 fvdl continue;
816 1.10 fvdl if (timeout == 0) {
817 1.10 fvdl timersub(&tv, &cd->last_recv_time, &tdiff);
818 1.10 fvdl if (timercmp(&tdiff, &tmax, >)) {
819 1.10 fvdl tmax = tdiff;
820 1.10 fvdl least_active = xprt;
821 1.10 fvdl }
822 1.10 fvdl continue;
823 1.10 fvdl }
824 1.10 fvdl if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
825 1.10 fvdl __xprt_unregister_unlocked(xprt);
826 1.10 fvdl __svc_vc_dodestroy(xprt);
827 1.10 fvdl ncleaned++;
828 1.10 fvdl }
829 1.10 fvdl }
830 1.10 fvdl }
831 1.10 fvdl if (timeout == 0 && least_active != NULL) {
832 1.10 fvdl __xprt_unregister_unlocked(least_active);
833 1.10 fvdl __svc_vc_dodestroy(least_active);
834 1.10 fvdl ncleaned++;
835 1.10 fvdl }
836 1.10 fvdl rwlock_unlock(&svc_fd_lock);
837 1.10 fvdl return ncleaned > 0 ? TRUE : FALSE;
838 1.1 fvdl }
839