svc_vc.c revision 1.4.4.2 1 1.4.4.2 minoura /* $NetBSD: svc_vc.c,v 1.4.4.2 2000/06/23 16:17:52 minoura Exp $ */
2 1.4.4.2 minoura
3 1.4.4.2 minoura /*
4 1.4.4.2 minoura * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.4.4.2 minoura * unrestricted use provided that this legend is included on all tape
6 1.4.4.2 minoura * media and as a part of the software program in whole or part. Users
7 1.4.4.2 minoura * may copy or modify Sun RPC without charge, but are not authorized
8 1.4.4.2 minoura * to license or distribute it to anyone else except as part of a product or
9 1.4.4.2 minoura * program developed by the user.
10 1.4.4.2 minoura *
11 1.4.4.2 minoura * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.4.4.2 minoura * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.4.4.2 minoura * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.4.4.2 minoura *
15 1.4.4.2 minoura * Sun RPC is provided with no support and without any obligation on the
16 1.4.4.2 minoura * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.4.4.2 minoura * modification or enhancement.
18 1.4.4.2 minoura *
19 1.4.4.2 minoura * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.4.4.2 minoura * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.4.4.2 minoura * OR ANY PART THEREOF.
22 1.4.4.2 minoura *
23 1.4.4.2 minoura * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.4.4.2 minoura * or profits or other special, indirect and consequential damages, even if
25 1.4.4.2 minoura * Sun has been advised of the possibility of such damages.
26 1.4.4.2 minoura *
27 1.4.4.2 minoura * Sun Microsystems, Inc.
28 1.4.4.2 minoura * 2550 Garcia Avenue
29 1.4.4.2 minoura * Mountain View, California 94043
30 1.4.4.2 minoura */
31 1.4.4.2 minoura
32 1.4.4.2 minoura #include <sys/cdefs.h>
33 1.4.4.2 minoura #if defined(LIBC_SCCS) && !defined(lint)
34 1.4.4.2 minoura #if 0
35 1.4.4.2 minoura static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
36 1.4.4.2 minoura static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
37 1.4.4.2 minoura #else
38 1.4.4.2 minoura __RCSID("$NetBSD: svc_vc.c,v 1.4.4.2 2000/06/23 16:17:52 minoura Exp $");
39 1.4.4.2 minoura #endif
40 1.4.4.2 minoura #endif
41 1.4.4.2 minoura
42 1.4.4.2 minoura /*
43 1.4.4.2 minoura * svc_vc.c, Server side for Connection Oriented based RPC.
44 1.4.4.2 minoura *
45 1.4.4.2 minoura * Actually implements two flavors of transporter -
46 1.4.4.2 minoura * a tcp rendezvouser (a listner and connection establisher)
47 1.4.4.2 minoura * and a record/tcp stream.
48 1.4.4.2 minoura */
49 1.4.4.2 minoura
50 1.4.4.2 minoura #include "namespace.h"
51 1.4.4.2 minoura #include "reentrant.h"
52 1.4.4.2 minoura #include <sys/types.h>
53 1.4.4.2 minoura #include <sys/param.h>
54 1.4.4.2 minoura #include <sys/poll.h>
55 1.4.4.2 minoura #include <sys/socket.h>
56 1.4.4.2 minoura #include <sys/un.h>
57 1.4.4.2 minoura #include <netinet/in.h>
58 1.4.4.2 minoura #include <netinet/tcp.h>
59 1.4.4.2 minoura
60 1.4.4.2 minoura #include <assert.h>
61 1.4.4.2 minoura #include <err.h>
62 1.4.4.2 minoura #include <errno.h>
63 1.4.4.2 minoura #include <stdio.h>
64 1.4.4.2 minoura #include <stdlib.h>
65 1.4.4.2 minoura #include <string.h>
66 1.4.4.2 minoura #include <unistd.h>
67 1.4.4.2 minoura
68 1.4.4.2 minoura #include <rpc/rpc.h>
69 1.4.4.2 minoura
70 1.4.4.2 minoura #include "rpc_com.h"
71 1.4.4.2 minoura
72 1.4.4.2 minoura #ifdef __weak_alias
73 1.4.4.2 minoura __weak_alias(svc_fd_create,_svc_fd_create)
74 1.4.4.2 minoura __weak_alias(svc_vc_create,_svc_vc_create)
75 1.4.4.2 minoura #endif
76 1.4.4.2 minoura
77 1.4.4.2 minoura static SVCXPRT *makefd_xprt __P((int, u_int, u_int));
78 1.4.4.2 minoura static bool_t rendezvous_request __P((SVCXPRT *, struct rpc_msg *));
79 1.4.4.2 minoura static enum xprt_stat rendezvous_stat __P((SVCXPRT *));
80 1.4.4.2 minoura static void svc_vc_destroy __P((SVCXPRT *));
81 1.4.4.2 minoura static int read_vc __P((caddr_t, caddr_t, int));
82 1.4.4.2 minoura static int write_vc __P((caddr_t, caddr_t, int));
83 1.4.4.2 minoura static enum xprt_stat svc_vc_stat __P((SVCXPRT *));
84 1.4.4.2 minoura static bool_t svc_vc_recv __P((SVCXPRT *, struct rpc_msg *));
85 1.4.4.2 minoura static bool_t svc_vc_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
86 1.4.4.2 minoura static bool_t svc_vc_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
87 1.4.4.2 minoura static bool_t svc_vc_reply __P((SVCXPRT *, struct rpc_msg *));
88 1.4.4.2 minoura static void svc_vc_rendezvous_ops __P((SVCXPRT *));
89 1.4.4.2 minoura static void svc_vc_ops __P((SVCXPRT *));
90 1.4.4.2 minoura static bool_t svc_vc_control __P((SVCXPRT *xprt, const u_int rq, void *in));
91 1.4.4.2 minoura
92 1.4.4.2 minoura struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
93 1.4.4.2 minoura u_int sendsize;
94 1.4.4.2 minoura u_int recvsize;
95 1.4.4.2 minoura };
96 1.4.4.2 minoura
97 1.4.4.2 minoura struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
98 1.4.4.2 minoura enum xprt_stat strm_stat;
99 1.4.4.2 minoura u_int32_t x_id;
100 1.4.4.2 minoura XDR xdrs;
101 1.4.4.2 minoura char verf_body[MAX_AUTH_BYTES];
102 1.4.4.2 minoura };
103 1.4.4.2 minoura
104 1.4.4.2 minoura /*
105 1.4.4.2 minoura * Usage:
106 1.4.4.2 minoura * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
107 1.4.4.2 minoura *
108 1.4.4.2 minoura * Creates, registers, and returns a (rpc) tcp based transporter.
109 1.4.4.2 minoura * Once *xprt is initialized, it is registered as a transporter
110 1.4.4.2 minoura * see (svc.h, xprt_register). This routine returns
111 1.4.4.2 minoura * a NULL if a problem occurred.
112 1.4.4.2 minoura *
113 1.4.4.2 minoura * The filedescriptor passed in is expected to refer to a bound, but
114 1.4.4.2 minoura * not yet connected socket.
115 1.4.4.2 minoura *
116 1.4.4.2 minoura * Since streams do buffered io similar to stdio, the caller can specify
117 1.4.4.2 minoura * how big the send and receive buffers are via the second and third parms;
118 1.4.4.2 minoura * 0 => use the system default.
119 1.4.4.2 minoura */
120 1.4.4.2 minoura SVCXPRT *
121 1.4.4.2 minoura svc_vc_create(fd, sendsize, recvsize)
122 1.4.4.2 minoura int fd;
123 1.4.4.2 minoura u_int sendsize;
124 1.4.4.2 minoura u_int recvsize;
125 1.4.4.2 minoura {
126 1.4.4.2 minoura SVCXPRT *xprt;
127 1.4.4.2 minoura struct cf_rendezvous *r = NULL;
128 1.4.4.2 minoura struct __rpc_sockinfo si;
129 1.4.4.2 minoura struct sockaddr_storage sslocal;
130 1.4.4.2 minoura socklen_t slen;
131 1.4.4.2 minoura int one = 1;
132 1.4.4.2 minoura
133 1.4.4.2 minoura r = (struct cf_rendezvous *)mem_alloc(sizeof(*r));
134 1.4.4.2 minoura if (r == NULL) {
135 1.4.4.2 minoura warnx("svc_vc_create: out of memory");
136 1.4.4.2 minoura goto cleanup_svc_vc_create;
137 1.4.4.2 minoura }
138 1.4.4.2 minoura if (!__rpc_fd2sockinfo(fd, &si))
139 1.4.4.2 minoura return NULL;
140 1.4.4.2 minoura r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, sendsize);
141 1.4.4.2 minoura r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, recvsize);
142 1.4.4.2 minoura xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
143 1.4.4.2 minoura if (xprt == NULL) {
144 1.4.4.2 minoura warnx("svc_vc_create: out of memory");
145 1.4.4.2 minoura goto cleanup_svc_vc_create;
146 1.4.4.2 minoura }
147 1.4.4.2 minoura xprt->xp_tp = NULL;
148 1.4.4.2 minoura xprt->xp_p1 = (caddr_t)(void *)r;
149 1.4.4.2 minoura xprt->xp_p2 = NULL;
150 1.4.4.2 minoura xprt->xp_p3 = NULL;
151 1.4.4.2 minoura xprt->xp_verf = _null_auth;
152 1.4.4.2 minoura svc_vc_rendezvous_ops(xprt);
153 1.4.4.2 minoura xprt->xp_port = -1; /* It is the rendezvouser */
154 1.4.4.2 minoura xprt->xp_fd = fd;
155 1.4.4.2 minoura
156 1.4.4.2 minoura slen = sizeof (struct sockaddr_storage);
157 1.4.4.2 minoura if (getsockname(fd, (struct sockaddr *)&sslocal, &slen) < 0) {
158 1.4.4.2 minoura warnx("svc_vc_create: could not retrieve local addr");
159 1.4.4.2 minoura goto cleanup_svc_vc_create;
160 1.4.4.2 minoura }
161 1.4.4.2 minoura
162 1.4.4.2 minoura /*
163 1.4.4.2 minoura * We want to be able to check credentials on local sockets.
164 1.4.4.2 minoura */
165 1.4.4.2 minoura if (sslocal.ss_family == AF_LOCAL)
166 1.4.4.2 minoura if (setsockopt(fd, 0, LOCAL_CREDS, &one, sizeof one) < 0)
167 1.4.4.2 minoura goto cleanup_svc_vc_create;
168 1.4.4.2 minoura
169 1.4.4.2 minoura xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
170 1.4.4.2 minoura xprt->xp_ltaddr.buf = mem_alloc(sslocal.ss_len);
171 1.4.4.2 minoura if (xprt->xp_ltaddr.buf == NULL) {
172 1.4.4.2 minoura warnx("svc_vc_create: no mem for local addr");
173 1.4.4.2 minoura goto cleanup_svc_vc_create;
174 1.4.4.2 minoura }
175 1.4.4.2 minoura memcpy(xprt->xp_ltaddr.buf, &sslocal, sslocal.ss_len);
176 1.4.4.2 minoura
177 1.4.4.2 minoura xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
178 1.4.4.2 minoura xprt_register(xprt);
179 1.4.4.2 minoura return (xprt);
180 1.4.4.2 minoura cleanup_svc_vc_create:
181 1.4.4.2 minoura if (r != NULL)
182 1.4.4.2 minoura mem_free(r, sizeof(*r));
183 1.4.4.2 minoura return ((SVCXPRT *)NULL);
184 1.4.4.2 minoura }
185 1.4.4.2 minoura
186 1.4.4.2 minoura /*
187 1.4.4.2 minoura * Like svtcp_create(), except the routine takes any *open* UNIX file
188 1.4.4.2 minoura * descriptor as its first input.
189 1.4.4.2 minoura */
190 1.4.4.2 minoura SVCXPRT *
191 1.4.4.2 minoura svc_fd_create(fd, sendsize, recvsize)
192 1.4.4.2 minoura int fd;
193 1.4.4.2 minoura u_int sendsize;
194 1.4.4.2 minoura u_int recvsize;
195 1.4.4.2 minoura {
196 1.4.4.2 minoura struct sockaddr_storage ss;
197 1.4.4.2 minoura socklen_t slen;
198 1.4.4.2 minoura SVCXPRT *ret;
199 1.4.4.2 minoura
200 1.4.4.2 minoura _DIAGASSERT(fd != -1);
201 1.4.4.2 minoura
202 1.4.4.2 minoura ret = makefd_xprt(fd, sendsize, recvsize);
203 1.4.4.2 minoura if (ret == NULL)
204 1.4.4.2 minoura return NULL;
205 1.4.4.2 minoura
206 1.4.4.2 minoura slen = sizeof (struct sockaddr_storage);
207 1.4.4.2 minoura if (getsockname(fd, (struct sockaddr *)&ss, &slen) < 0) {
208 1.4.4.2 minoura warnx("svc_fd_create: could not retrieve local addr");
209 1.4.4.2 minoura goto freedata;
210 1.4.4.2 minoura }
211 1.4.4.2 minoura ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
212 1.4.4.2 minoura ret->xp_ltaddr.buf = mem_alloc(ss.ss_len);
213 1.4.4.2 minoura if (ret->xp_ltaddr.buf == NULL) {
214 1.4.4.2 minoura warnx("svc_fd_create: no mem for local addr");
215 1.4.4.2 minoura goto freedata;
216 1.4.4.2 minoura }
217 1.4.4.2 minoura memcpy(ret->xp_ltaddr.buf, &ss, ss.ss_len);
218 1.4.4.2 minoura
219 1.4.4.2 minoura slen = sizeof (struct sockaddr_storage);
220 1.4.4.2 minoura if (getpeername(fd, (struct sockaddr *)&ss, &slen) < 0) {
221 1.4.4.2 minoura warnx("svc_fd_create: could not retrieve remote addr");
222 1.4.4.2 minoura goto freedata;
223 1.4.4.2 minoura }
224 1.4.4.2 minoura ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
225 1.4.4.2 minoura ret->xp_rtaddr.buf = mem_alloc(ss.ss_len);
226 1.4.4.2 minoura if (ret->xp_rtaddr.buf == NULL) {
227 1.4.4.2 minoura warnx("svc_fd_create: no mem for local addr");
228 1.4.4.2 minoura goto freedata;
229 1.4.4.2 minoura }
230 1.4.4.2 minoura memcpy(ret->xp_rtaddr.buf, &ss, ss.ss_len);
231 1.4.4.2 minoura #ifdef PORTMAP
232 1.4.4.2 minoura if (ss.ss_family == AF_INET) {
233 1.4.4.2 minoura ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
234 1.4.4.2 minoura ret->xp_addrlen = sizeof (struct sockaddr_in);
235 1.4.4.2 minoura }
236 1.4.4.2 minoura #endif
237 1.4.4.2 minoura
238 1.4.4.2 minoura return ret;
239 1.4.4.2 minoura
240 1.4.4.2 minoura freedata:
241 1.4.4.2 minoura if (ret->xp_ltaddr.buf != NULL)
242 1.4.4.2 minoura mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
243 1.4.4.2 minoura
244 1.4.4.2 minoura return NULL;
245 1.4.4.2 minoura }
246 1.4.4.2 minoura
247 1.4.4.2 minoura static SVCXPRT *
248 1.4.4.2 minoura makefd_xprt(fd, sendsize, recvsize)
249 1.4.4.2 minoura int fd;
250 1.4.4.2 minoura u_int sendsize;
251 1.4.4.2 minoura u_int recvsize;
252 1.4.4.2 minoura {
253 1.4.4.2 minoura SVCXPRT *xprt;
254 1.4.4.2 minoura struct cf_conn *cd;
255 1.4.4.2 minoura
256 1.4.4.2 minoura _DIAGASSERT(fd != -1);
257 1.4.4.2 minoura
258 1.4.4.2 minoura xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
259 1.4.4.2 minoura if (xprt == (SVCXPRT *)NULL) {
260 1.4.4.2 minoura warnx("svc_tcp: makefd_xprt: out of memory");
261 1.4.4.2 minoura goto done;
262 1.4.4.2 minoura }
263 1.4.4.2 minoura memset(xprt, 0, sizeof *xprt);
264 1.4.4.2 minoura cd = (struct cf_conn *)mem_alloc(sizeof(struct cf_conn));
265 1.4.4.2 minoura if (cd == (struct cf_conn *)NULL) {
266 1.4.4.2 minoura warnx("svc_tcp: makefd_xprt: out of memory");
267 1.4.4.2 minoura mem_free(xprt, sizeof(SVCXPRT));
268 1.4.4.2 minoura xprt = (SVCXPRT *)NULL;
269 1.4.4.2 minoura goto done;
270 1.4.4.2 minoura }
271 1.4.4.2 minoura cd->strm_stat = XPRT_IDLE;
272 1.4.4.2 minoura xdrrec_create(&(cd->xdrs), sendsize, recvsize,
273 1.4.4.2 minoura (caddr_t)(void *)xprt, read_vc, write_vc);
274 1.4.4.2 minoura xprt->xp_p1 = (caddr_t)(void *)cd;
275 1.4.4.2 minoura xprt->xp_verf.oa_base = cd->verf_body;
276 1.4.4.2 minoura svc_vc_ops(xprt); /* truely deals with calls */
277 1.4.4.2 minoura xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
278 1.4.4.2 minoura xprt->xp_fd = fd;
279 1.4.4.2 minoura xprt_register(xprt);
280 1.4.4.2 minoura done:
281 1.4.4.2 minoura return (xprt);
282 1.4.4.2 minoura }
283 1.4.4.2 minoura
284 1.4.4.2 minoura /*ARGSUSED*/
285 1.4.4.2 minoura static bool_t
286 1.4.4.2 minoura rendezvous_request(xprt, msg)
287 1.4.4.2 minoura SVCXPRT *xprt;
288 1.4.4.2 minoura struct rpc_msg *msg;
289 1.4.4.2 minoura {
290 1.4.4.2 minoura int sock;
291 1.4.4.2 minoura struct cf_rendezvous *r;
292 1.4.4.2 minoura struct sockaddr_storage addr;
293 1.4.4.2 minoura socklen_t len;
294 1.4.4.2 minoura struct __rpc_sockinfo si;
295 1.4.4.2 minoura
296 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
297 1.4.4.2 minoura _DIAGASSERT(msg != NULL);
298 1.4.4.2 minoura
299 1.4.4.2 minoura r = (struct cf_rendezvous *)xprt->xp_p1;
300 1.4.4.2 minoura again:
301 1.4.4.2 minoura len = sizeof addr;
302 1.4.4.2 minoura if ((sock = accept(xprt->xp_fd, (struct sockaddr *)&addr, &len)) < 0) {
303 1.4.4.2 minoura if (errno == EINTR)
304 1.4.4.2 minoura goto again;
305 1.4.4.2 minoura return (FALSE);
306 1.4.4.2 minoura }
307 1.4.4.2 minoura /*
308 1.4.4.2 minoura * make a new transporter (re-uses xprt)
309 1.4.4.2 minoura */
310 1.4.4.2 minoura xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
311 1.4.4.2 minoura xprt->xp_rtaddr.buf = mem_alloc(len);
312 1.4.4.2 minoura if (xprt->xp_rtaddr.buf == NULL)
313 1.4.4.2 minoura return (FALSE);
314 1.4.4.2 minoura memcpy(xprt->xp_rtaddr.buf, &addr, len);
315 1.4.4.2 minoura xprt->xp_rtaddr.len = len;
316 1.4.4.2 minoura #ifdef PORTMAP
317 1.4.4.2 minoura if (addr.ss_family == AF_INET) {
318 1.4.4.2 minoura xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
319 1.4.4.2 minoura xprt->xp_addrlen = sizeof (struct sockaddr_in);
320 1.4.4.2 minoura }
321 1.4.4.2 minoura #endif
322 1.4.4.2 minoura if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
323 1.4.4.2 minoura len = 1;
324 1.4.4.2 minoura /* XXX fvdl - is this useful? */
325 1.4.4.2 minoura setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
326 1.4.4.2 minoura }
327 1.4.4.2 minoura return (FALSE); /* there is never an rpc msg to be processed */
328 1.4.4.2 minoura }
329 1.4.4.2 minoura
330 1.4.4.2 minoura /*ARGSUSED*/
331 1.4.4.2 minoura static enum xprt_stat
332 1.4.4.2 minoura rendezvous_stat(xprt)
333 1.4.4.2 minoura SVCXPRT *xprt;
334 1.4.4.2 minoura {
335 1.4.4.2 minoura
336 1.4.4.2 minoura return (XPRT_IDLE);
337 1.4.4.2 minoura }
338 1.4.4.2 minoura
339 1.4.4.2 minoura static void
340 1.4.4.2 minoura svc_vc_destroy(xprt)
341 1.4.4.2 minoura SVCXPRT *xprt;
342 1.4.4.2 minoura {
343 1.4.4.2 minoura struct cf_conn *cd;
344 1.4.4.2 minoura struct cf_rendezvous *r;
345 1.4.4.2 minoura
346 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
347 1.4.4.2 minoura
348 1.4.4.2 minoura cd = (struct cf_conn *)xprt->xp_p1;
349 1.4.4.2 minoura
350 1.4.4.2 minoura xprt_unregister(xprt);
351 1.4.4.2 minoura if (xprt->xp_fd != RPC_ANYFD)
352 1.4.4.2 minoura (void)close(xprt->xp_fd);
353 1.4.4.2 minoura if (xprt->xp_port != 0) {
354 1.4.4.2 minoura /* a rendezvouser socket */
355 1.4.4.2 minoura r = (struct cf_rendezvous *)xprt->xp_p1;
356 1.4.4.2 minoura mem_free(r, sizeof (struct cf_rendezvous));
357 1.4.4.2 minoura xprt->xp_port = 0;
358 1.4.4.2 minoura } else {
359 1.4.4.2 minoura /* an actual connection socket */
360 1.4.4.2 minoura XDR_DESTROY(&(cd->xdrs));
361 1.4.4.2 minoura mem_free(cd, sizeof(struct cf_conn));
362 1.4.4.2 minoura }
363 1.4.4.2 minoura if (xprt->xp_rtaddr.buf)
364 1.4.4.2 minoura mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
365 1.4.4.2 minoura if (xprt->xp_ltaddr.buf)
366 1.4.4.2 minoura mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
367 1.4.4.2 minoura if (xprt->xp_tp)
368 1.4.4.2 minoura free(xprt->xp_tp);
369 1.4.4.2 minoura if (xprt->xp_netid)
370 1.4.4.2 minoura free(xprt->xp_netid);
371 1.4.4.2 minoura mem_free(xprt, sizeof(SVCXPRT));
372 1.4.4.2 minoura }
373 1.4.4.2 minoura
374 1.4.4.2 minoura static bool_t
375 1.4.4.2 minoura svc_vc_control(xprt, rq, in)
376 1.4.4.2 minoura SVCXPRT *xprt;
377 1.4.4.2 minoura const u_int rq;
378 1.4.4.2 minoura void *in;
379 1.4.4.2 minoura {
380 1.4.4.2 minoura return (FALSE);
381 1.4.4.2 minoura }
382 1.4.4.2 minoura
383 1.4.4.2 minoura /*
384 1.4.4.2 minoura * reads data from the tcp conection.
385 1.4.4.2 minoura * any error is fatal and the connection is closed.
386 1.4.4.2 minoura * (And a read of zero bytes is a half closed stream => error.)
387 1.4.4.2 minoura * All read operations timeout after 35 seconds. A timeout is
388 1.4.4.2 minoura * fatal for the connection.
389 1.4.4.2 minoura */
390 1.4.4.2 minoura static int
391 1.4.4.2 minoura read_vc(xprtp, buf, len)
392 1.4.4.2 minoura caddr_t xprtp;
393 1.4.4.2 minoura caddr_t buf;
394 1.4.4.2 minoura int len;
395 1.4.4.2 minoura {
396 1.4.4.2 minoura SVCXPRT *xprt;
397 1.4.4.2 minoura int sock;
398 1.4.4.2 minoura int milliseconds = 35 * 1000;
399 1.4.4.2 minoura struct pollfd pollfd;
400 1.4.4.2 minoura struct sockaddr *sa;
401 1.4.4.2 minoura struct msghdr msg;
402 1.4.4.2 minoura struct cmsghdr *cmp;
403 1.4.4.2 minoura void *crmsg = NULL;
404 1.4.4.2 minoura struct sockcred *sc;
405 1.4.4.2 minoura socklen_t crmsgsize;
406 1.4.4.2 minoura
407 1.4.4.2 minoura xprt = (SVCXPRT *)(void *)xprtp;
408 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
409 1.4.4.2 minoura
410 1.4.4.2 minoura sock = xprt->xp_fd;
411 1.4.4.2 minoura
412 1.4.4.2 minoura sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
413 1.4.4.2 minoura if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) {
414 1.4.4.2 minoura memset(&msg, 0, sizeof msg);
415 1.4.4.2 minoura crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
416 1.4.4.2 minoura crmsg = malloc(crmsgsize);
417 1.4.4.2 minoura if (crmsg == NULL)
418 1.4.4.2 minoura goto fatal_err;
419 1.4.4.2 minoura memset(crmsg, 0, crmsgsize);
420 1.4.4.2 minoura
421 1.4.4.2 minoura msg.msg_control = crmsg;
422 1.4.4.2 minoura msg.msg_controllen = crmsgsize;
423 1.4.4.2 minoura
424 1.4.4.2 minoura if (recvmsg(sock, &msg, 0) < 0)
425 1.4.4.2 minoura goto fatal_err;
426 1.4.4.2 minoura
427 1.4.4.2 minoura if (msg.msg_controllen == 0 ||
428 1.4.4.2 minoura (msg.msg_flags & MSG_CTRUNC) != 0)
429 1.4.4.2 minoura goto fatal_err;
430 1.4.4.2 minoura
431 1.4.4.2 minoura cmp = CMSG_FIRSTHDR(&msg);
432 1.4.4.2 minoura if (cmp->cmsg_level != SOL_SOCKET ||
433 1.4.4.2 minoura cmp->cmsg_type != SCM_CREDS)
434 1.4.4.2 minoura goto fatal_err;
435 1.4.4.2 minoura
436 1.4.4.2 minoura sc = (struct sockcred *)CMSG_DATA(cmp);
437 1.4.4.2 minoura
438 1.4.4.2 minoura xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups));
439 1.4.4.2 minoura if (xprt->xp_p2 == NULL)
440 1.4.4.2 minoura goto fatal_err;
441 1.4.4.2 minoura
442 1.4.4.2 minoura memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups));
443 1.4.4.2 minoura free(crmsg);
444 1.4.4.2 minoura crmsg = NULL;
445 1.4.4.2 minoura }
446 1.4.4.2 minoura
447 1.4.4.2 minoura do {
448 1.4.4.2 minoura pollfd.fd = sock;
449 1.4.4.2 minoura pollfd.events = POLLIN;
450 1.4.4.2 minoura switch (poll(&pollfd, 1, milliseconds)) {
451 1.4.4.2 minoura case -1:
452 1.4.4.2 minoura if (errno == EINTR) {
453 1.4.4.2 minoura continue;
454 1.4.4.2 minoura }
455 1.4.4.2 minoura /*FALLTHROUGH*/
456 1.4.4.2 minoura case 0:
457 1.4.4.2 minoura goto fatal_err;
458 1.4.4.2 minoura
459 1.4.4.2 minoura default:
460 1.4.4.2 minoura break;
461 1.4.4.2 minoura }
462 1.4.4.2 minoura } while ((pollfd.revents & POLLIN) == 0);
463 1.4.4.2 minoura
464 1.4.4.2 minoura if ((len = read(sock, buf, (size_t)len)) > 0)
465 1.4.4.2 minoura return (len);
466 1.4.4.2 minoura
467 1.4.4.2 minoura fatal_err:
468 1.4.4.2 minoura if (crmsg != NULL)
469 1.4.4.2 minoura free(crmsg);
470 1.4.4.2 minoura ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
471 1.4.4.2 minoura return (-1);
472 1.4.4.2 minoura }
473 1.4.4.2 minoura
474 1.4.4.2 minoura /*
475 1.4.4.2 minoura * writes data to the tcp connection.
476 1.4.4.2 minoura * Any error is fatal and the connection is closed.
477 1.4.4.2 minoura */
478 1.4.4.2 minoura static int
479 1.4.4.2 minoura write_vc(xprtp, buf, len)
480 1.4.4.2 minoura caddr_t xprtp;
481 1.4.4.2 minoura caddr_t buf;
482 1.4.4.2 minoura int len;
483 1.4.4.2 minoura {
484 1.4.4.2 minoura SVCXPRT *xprt;
485 1.4.4.2 minoura int i, cnt;
486 1.4.4.2 minoura
487 1.4.4.2 minoura xprt = (SVCXPRT *)(void *)xprtp;
488 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
489 1.4.4.2 minoura
490 1.4.4.2 minoura for (cnt = len; cnt > 0; cnt -= i, buf += i) {
491 1.4.4.2 minoura if ((i = write(xprt->xp_fd, buf, (size_t)cnt)) < 0) {
492 1.4.4.2 minoura ((struct cf_conn *)(xprt->xp_p1))->strm_stat =
493 1.4.4.2 minoura XPRT_DIED;
494 1.4.4.2 minoura return (-1);
495 1.4.4.2 minoura }
496 1.4.4.2 minoura }
497 1.4.4.2 minoura return (len);
498 1.4.4.2 minoura }
499 1.4.4.2 minoura
500 1.4.4.2 minoura static enum xprt_stat
501 1.4.4.2 minoura svc_vc_stat(xprt)
502 1.4.4.2 minoura SVCXPRT *xprt;
503 1.4.4.2 minoura {
504 1.4.4.2 minoura struct cf_conn *cd;
505 1.4.4.2 minoura
506 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
507 1.4.4.2 minoura
508 1.4.4.2 minoura cd = (struct cf_conn *)(xprt->xp_p1);
509 1.4.4.2 minoura
510 1.4.4.2 minoura if (cd->strm_stat == XPRT_DIED)
511 1.4.4.2 minoura return (XPRT_DIED);
512 1.4.4.2 minoura if (! xdrrec_eof(&(cd->xdrs)))
513 1.4.4.2 minoura return (XPRT_MOREREQS);
514 1.4.4.2 minoura return (XPRT_IDLE);
515 1.4.4.2 minoura }
516 1.4.4.2 minoura
517 1.4.4.2 minoura static bool_t
518 1.4.4.2 minoura svc_vc_recv(xprt, msg)
519 1.4.4.2 minoura SVCXPRT *xprt;
520 1.4.4.2 minoura struct rpc_msg *msg;
521 1.4.4.2 minoura {
522 1.4.4.2 minoura struct cf_conn *cd;
523 1.4.4.2 minoura XDR *xdrs;
524 1.4.4.2 minoura
525 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
526 1.4.4.2 minoura _DIAGASSERT(msg != NULL);
527 1.4.4.2 minoura
528 1.4.4.2 minoura cd = (struct cf_conn *)(xprt->xp_p1);
529 1.4.4.2 minoura xdrs = &(cd->xdrs);
530 1.4.4.2 minoura
531 1.4.4.2 minoura xdrs->x_op = XDR_DECODE;
532 1.4.4.2 minoura (void)xdrrec_skiprecord(xdrs);
533 1.4.4.2 minoura if (xdr_callmsg(xdrs, msg)) {
534 1.4.4.2 minoura cd->x_id = msg->rm_xid;
535 1.4.4.2 minoura return (TRUE);
536 1.4.4.2 minoura }
537 1.4.4.2 minoura cd->strm_stat = XPRT_DIED;
538 1.4.4.2 minoura return (FALSE);
539 1.4.4.2 minoura }
540 1.4.4.2 minoura
541 1.4.4.2 minoura static bool_t
542 1.4.4.2 minoura svc_vc_getargs(xprt, xdr_args, args_ptr)
543 1.4.4.2 minoura SVCXPRT *xprt;
544 1.4.4.2 minoura xdrproc_t xdr_args;
545 1.4.4.2 minoura caddr_t args_ptr;
546 1.4.4.2 minoura {
547 1.4.4.2 minoura
548 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
549 1.4.4.2 minoura /* args_ptr may be NULL */
550 1.4.4.2 minoura
551 1.4.4.2 minoura return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
552 1.4.4.2 minoura args_ptr));
553 1.4.4.2 minoura }
554 1.4.4.2 minoura
555 1.4.4.2 minoura static bool_t
556 1.4.4.2 minoura svc_vc_freeargs(xprt, xdr_args, args_ptr)
557 1.4.4.2 minoura SVCXPRT *xprt;
558 1.4.4.2 minoura xdrproc_t xdr_args;
559 1.4.4.2 minoura caddr_t args_ptr;
560 1.4.4.2 minoura {
561 1.4.4.2 minoura XDR *xdrs;
562 1.4.4.2 minoura
563 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
564 1.4.4.2 minoura /* args_ptr may be NULL */
565 1.4.4.2 minoura
566 1.4.4.2 minoura xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
567 1.4.4.2 minoura
568 1.4.4.2 minoura xdrs->x_op = XDR_FREE;
569 1.4.4.2 minoura return ((*xdr_args)(xdrs, args_ptr));
570 1.4.4.2 minoura }
571 1.4.4.2 minoura
572 1.4.4.2 minoura static bool_t
573 1.4.4.2 minoura svc_vc_reply(xprt, msg)
574 1.4.4.2 minoura SVCXPRT *xprt;
575 1.4.4.2 minoura struct rpc_msg *msg;
576 1.4.4.2 minoura {
577 1.4.4.2 minoura struct cf_conn *cd;
578 1.4.4.2 minoura XDR *xdrs;
579 1.4.4.2 minoura bool_t stat;
580 1.4.4.2 minoura
581 1.4.4.2 minoura _DIAGASSERT(xprt != NULL);
582 1.4.4.2 minoura _DIAGASSERT(msg != NULL);
583 1.4.4.2 minoura
584 1.4.4.2 minoura cd = (struct cf_conn *)(xprt->xp_p1);
585 1.4.4.2 minoura xdrs = &(cd->xdrs);
586 1.4.4.2 minoura
587 1.4.4.2 minoura xdrs->x_op = XDR_ENCODE;
588 1.4.4.2 minoura msg->rm_xid = cd->x_id;
589 1.4.4.2 minoura stat = xdr_replymsg(xdrs, msg);
590 1.4.4.2 minoura (void)xdrrec_endofrecord(xdrs, TRUE);
591 1.4.4.2 minoura return (stat);
592 1.4.4.2 minoura }
593 1.4.4.2 minoura
594 1.4.4.2 minoura static void
595 1.4.4.2 minoura svc_vc_ops(xprt)
596 1.4.4.2 minoura SVCXPRT *xprt;
597 1.4.4.2 minoura {
598 1.4.4.2 minoura static struct xp_ops ops;
599 1.4.4.2 minoura static struct xp_ops2 ops2;
600 1.4.4.2 minoura #ifdef __REENT
601 1.4.4.2 minoura extern mutex_t ops_lock;
602 1.4.4.2 minoura #endif
603 1.4.4.2 minoura
604 1.4.4.2 minoura /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
605 1.4.4.2 minoura
606 1.4.4.2 minoura mutex_lock(&ops_lock);
607 1.4.4.2 minoura if (ops.xp_recv == NULL) {
608 1.4.4.2 minoura ops.xp_recv = svc_vc_recv;
609 1.4.4.2 minoura ops.xp_stat = svc_vc_stat;
610 1.4.4.2 minoura ops.xp_getargs = svc_vc_getargs;
611 1.4.4.2 minoura ops.xp_reply = svc_vc_reply;
612 1.4.4.2 minoura ops.xp_freeargs = svc_vc_freeargs;
613 1.4.4.2 minoura ops.xp_destroy = svc_vc_destroy;
614 1.4.4.2 minoura ops2.xp_control = svc_vc_control;
615 1.4.4.2 minoura }
616 1.4.4.2 minoura xprt->xp_ops = &ops;
617 1.4.4.2 minoura xprt->xp_ops2 = &ops2;
618 1.4.4.2 minoura mutex_unlock(&ops_lock);
619 1.4.4.2 minoura }
620 1.4.4.2 minoura
621 1.4.4.2 minoura static void
622 1.4.4.2 minoura svc_vc_rendezvous_ops(xprt)
623 1.4.4.2 minoura SVCXPRT *xprt;
624 1.4.4.2 minoura {
625 1.4.4.2 minoura static struct xp_ops ops;
626 1.4.4.2 minoura static struct xp_ops2 ops2;
627 1.4.4.2 minoura #ifdef __REENT
628 1.4.4.2 minoura extern mutex_t ops_lock;
629 1.4.4.2 minoura #endif
630 1.4.4.2 minoura
631 1.4.4.2 minoura mutex_lock(&ops_lock);
632 1.4.4.2 minoura if (ops.xp_recv == NULL) {
633 1.4.4.2 minoura ops.xp_recv = rendezvous_request;
634 1.4.4.2 minoura ops.xp_stat = rendezvous_stat;
635 1.4.4.2 minoura ops.xp_getargs =
636 1.4.4.2 minoura (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
637 1.4.4.2 minoura ops.xp_reply =
638 1.4.4.2 minoura (bool_t (*) __P((SVCXPRT *, struct rpc_msg *)))abort;
639 1.4.4.2 minoura ops.xp_freeargs =
640 1.4.4.2 minoura (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort,
641 1.4.4.2 minoura ops.xp_destroy = svc_vc_destroy;
642 1.4.4.2 minoura ops2.xp_control = svc_vc_control;
643 1.4.4.2 minoura }
644 1.4.4.2 minoura xprt->xp_ops = &ops;
645 1.4.4.2 minoura xprt->xp_ops2 = &ops2;
646 1.4.4.2 minoura mutex_unlock(&ops_lock);
647 1.4.4.2 minoura }
648