1 | /* |
---|
2 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
3 | * unrestricted use provided that this legend is included on all tape |
---|
4 | * media and as a part of the software program in whole or part. Users |
---|
5 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
6 | * to license or distribute it to anyone else except as part of a product or |
---|
7 | * program developed by the user. |
---|
8 | * |
---|
9 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
10 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
11 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
12 | * |
---|
13 | * Sun RPC is provided with no support and without any obligation on the |
---|
14 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
15 | * modification or enhancement. |
---|
16 | * |
---|
17 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
18 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
19 | * OR ANY PART THEREOF. |
---|
20 | * |
---|
21 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
22 | * or profits or other special, indirect and consequential damages, even if |
---|
23 | * Sun has been advised of the possibility of such damages. |
---|
24 | * |
---|
25 | * Sun Microsystems, Inc. |
---|
26 | * 2550 Garcia Avenue |
---|
27 | * Mountain View, California 94043 |
---|
28 | */ |
---|
29 | |
---|
30 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
31 | /*static char *sccsid = "from: @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/ |
---|
32 | /*static char *sccsid = "from: @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/ |
---|
33 | static char *rcsid = "$FreeBSD: src/lib/libc/rpc/svc_tcp.c,v 1.18 2000/01/27 23:06:41 jasone Exp $"; |
---|
34 | #endif |
---|
35 | |
---|
36 | /* |
---|
37 | * svc_tcp.c, Server side for TCP/IP based RPC. |
---|
38 | * |
---|
39 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
40 | * |
---|
41 | * Actually implements two flavors of transporter - |
---|
42 | * a tcp rendezvouser (a listner and connection establisher) |
---|
43 | * and a record/tcp stream. |
---|
44 | */ |
---|
45 | |
---|
46 | #include <stdio.h> |
---|
47 | #include <stdlib.h> |
---|
48 | #include <unistd.h> |
---|
49 | #include <string.h> |
---|
50 | #include <rpc/rpc.h> |
---|
51 | #include <sys/socket.h> |
---|
52 | #include <sys/ioctl.h> |
---|
53 | #include <errno.h> |
---|
54 | #include <sys/select.h> |
---|
55 | |
---|
56 | /* |
---|
57 | * Ops vector for TCP/IP based rpc service handle |
---|
58 | */ |
---|
59 | static bool_t svctcp_recv(); |
---|
60 | static enum xprt_stat svctcp_stat(); |
---|
61 | static bool_t svctcp_getargs(); |
---|
62 | static bool_t svctcp_reply(); |
---|
63 | static bool_t svctcp_freeargs(); |
---|
64 | static void svctcp_destroy(); |
---|
65 | |
---|
66 | static struct xp_ops svctcp_op = { |
---|
67 | svctcp_recv, |
---|
68 | svctcp_stat, |
---|
69 | svctcp_getargs, |
---|
70 | svctcp_reply, |
---|
71 | svctcp_freeargs, |
---|
72 | svctcp_destroy |
---|
73 | }; |
---|
74 | |
---|
75 | /* |
---|
76 | * Ops vector for TCP/IP rendezvous handler |
---|
77 | */ |
---|
78 | static bool_t rendezvous_request(); |
---|
79 | static enum xprt_stat rendezvous_stat(); |
---|
80 | |
---|
81 | static struct xp_ops svctcp_rendezvous_op = { |
---|
82 | rendezvous_request, |
---|
83 | rendezvous_stat, |
---|
84 | (bool_t (*)())abort, |
---|
85 | (bool_t (*)())abort, |
---|
86 | (bool_t (*)())abort, |
---|
87 | svctcp_destroy |
---|
88 | }; |
---|
89 | |
---|
90 | static int readtcp(), writetcp(); |
---|
91 | static SVCXPRT *makefd_xprt(); |
---|
92 | |
---|
93 | struct tcp_rendezvous { /* kept in xprt->xp_p1 */ |
---|
94 | u_int sendsize; |
---|
95 | u_int recvsize; |
---|
96 | }; |
---|
97 | |
---|
98 | struct tcp_conn { /* kept in xprt->xp_p1 */ |
---|
99 | enum xprt_stat strm_stat; |
---|
100 | u_long x_id; |
---|
101 | XDR xdrs; |
---|
102 | char verf_body[MAX_AUTH_BYTES]; |
---|
103 | }; |
---|
104 | |
---|
105 | /* |
---|
106 | * Usage: |
---|
107 | * xprt = svctcp_create(sock, send_buf_size, recv_buf_size); |
---|
108 | * |
---|
109 | * Creates, registers, and returns a (rpc) tcp based transporter. |
---|
110 | * Once *xprt is initialized, it is registered as a transporter |
---|
111 | * see (svc.h, xprt_register). This routine returns |
---|
112 | * a NULL if a problem occurred. |
---|
113 | * |
---|
114 | * If sock<0 then a socket is created, else sock is used. |
---|
115 | * If the socket, sock is not bound to a port then svctcp_create |
---|
116 | * binds it to an arbitrary port. The routine then starts a tcp |
---|
117 | * listener on the socket's associated port. In any (successful) case, |
---|
118 | * xprt->xp_sock is the registered socket number and xprt->xp_port is the |
---|
119 | * associated port number. |
---|
120 | * |
---|
121 | * Since tcp streams do buffered io similar to stdio, the caller can specify |
---|
122 | * how big the send and receive buffers are via the second and third parms; |
---|
123 | * 0 => use the system default. |
---|
124 | */ |
---|
125 | SVCXPRT * |
---|
126 | svctcp_create(sock, sendsize, recvsize) |
---|
127 | register int sock; |
---|
128 | u_int sendsize; |
---|
129 | u_int recvsize; |
---|
130 | { |
---|
131 | bool_t madesock = FALSE; |
---|
132 | register SVCXPRT *xprt; |
---|
133 | register struct tcp_rendezvous *r; |
---|
134 | struct sockaddr_in addr; |
---|
135 | int len = sizeof(struct sockaddr_in); |
---|
136 | int on; |
---|
137 | |
---|
138 | if (sock == RPC_ANYSOCK) { |
---|
139 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
---|
140 | perror("svctcp_.c - udp socket creation problem"); |
---|
141 | return ((SVCXPRT *)NULL); |
---|
142 | } |
---|
143 | madesock = TRUE; |
---|
144 | } |
---|
145 | on = 1; |
---|
146 | if (ioctl(sock, FIONBIO, &on) < 0) { |
---|
147 | perror("svc_tcp.c - cannot turn on non-blocking mode"); |
---|
148 | if (madesock) |
---|
149 | (void)_RPC_close(sock); |
---|
150 | return ((SVCXPRT *)NULL); |
---|
151 | } |
---|
152 | memset(&addr, 0, sizeof (addr)); |
---|
153 | addr.sin_len = sizeof(struct sockaddr_in); |
---|
154 | addr.sin_family = AF_INET; |
---|
155 | if (bindresvport(sock, &addr)) { |
---|
156 | addr.sin_port = 0; |
---|
157 | (void)bind(sock, (struct sockaddr *)&addr, len); |
---|
158 | } |
---|
159 | if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) || |
---|
160 | (listen(sock, 2) != 0)) { |
---|
161 | perror("svctcp_.c - cannot getsockname or listen"); |
---|
162 | if (madesock) |
---|
163 | (void)_RPC_close(sock); |
---|
164 | return ((SVCXPRT *)NULL); |
---|
165 | } |
---|
166 | r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r)); |
---|
167 | if (r == NULL) { |
---|
168 | (void) fprintf(stderr, "svctcp_create: out of memory\n"); |
---|
169 | return (NULL); |
---|
170 | } |
---|
171 | r->sendsize = sendsize; |
---|
172 | r->recvsize = recvsize; |
---|
173 | xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); |
---|
174 | if (xprt == NULL) { |
---|
175 | (void) fprintf(stderr, "svctcp_create: out of memory\n"); |
---|
176 | return (NULL); |
---|
177 | } |
---|
178 | xprt->xp_p2 = NULL; |
---|
179 | xprt->xp_p1 = (caddr_t)r; |
---|
180 | xprt->xp_verf = _null_auth; |
---|
181 | xprt->xp_ops = &svctcp_rendezvous_op; |
---|
182 | xprt->xp_port = ntohs(addr.sin_port); |
---|
183 | xprt->xp_sock = sock; |
---|
184 | xprt_register(xprt); |
---|
185 | return (xprt); |
---|
186 | } |
---|
187 | |
---|
188 | /* |
---|
189 | * Like svtcp_create(), except the routine takes any *open* UNIX file |
---|
190 | * descriptor as its first input. |
---|
191 | */ |
---|
192 | SVCXPRT * |
---|
193 | svcfd_create(fd, sendsize, recvsize) |
---|
194 | int fd; |
---|
195 | u_int sendsize; |
---|
196 | u_int recvsize; |
---|
197 | { |
---|
198 | |
---|
199 | return (makefd_xprt(fd, sendsize, recvsize)); |
---|
200 | } |
---|
201 | |
---|
202 | static SVCXPRT * |
---|
203 | makefd_xprt(fd, sendsize, recvsize) |
---|
204 | int fd; |
---|
205 | u_int sendsize; |
---|
206 | u_int recvsize; |
---|
207 | { |
---|
208 | register SVCXPRT *xprt; |
---|
209 | register struct tcp_conn *cd; |
---|
210 | |
---|
211 | xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); |
---|
212 | if (xprt == (SVCXPRT *)NULL) { |
---|
213 | (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); |
---|
214 | goto done; |
---|
215 | } |
---|
216 | cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn)); |
---|
217 | if (cd == (struct tcp_conn *)NULL) { |
---|
218 | (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); |
---|
219 | mem_free((char *) xprt, sizeof(SVCXPRT)); |
---|
220 | xprt = (SVCXPRT *)NULL; |
---|
221 | goto done; |
---|
222 | } |
---|
223 | cd->strm_stat = XPRT_IDLE; |
---|
224 | xdrrec_create(&(cd->xdrs), sendsize, recvsize, |
---|
225 | (caddr_t)xprt, readtcp, writetcp); |
---|
226 | xprt->xp_p2 = NULL; |
---|
227 | xprt->xp_p1 = (caddr_t)cd; |
---|
228 | xprt->xp_verf.oa_base = cd->verf_body; |
---|
229 | xprt->xp_addrlen = 0; |
---|
230 | xprt->xp_ops = &svctcp_op; /* truely deals with calls */ |
---|
231 | xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ |
---|
232 | xprt->xp_sock = fd; |
---|
233 | xprt_register(xprt); |
---|
234 | done: |
---|
235 | return (xprt); |
---|
236 | } |
---|
237 | |
---|
238 | static bool_t |
---|
239 | rendezvous_request(xprt) |
---|
240 | register SVCXPRT *xprt; |
---|
241 | { |
---|
242 | int sock; |
---|
243 | struct tcp_rendezvous *r; |
---|
244 | struct sockaddr_in addr; |
---|
245 | int len; |
---|
246 | int off; |
---|
247 | |
---|
248 | r = (struct tcp_rendezvous *)xprt->xp_p1; |
---|
249 | again: |
---|
250 | len = sizeof(struct sockaddr_in); |
---|
251 | if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr, |
---|
252 | &len)) < 0) { |
---|
253 | if (errno == EINTR) |
---|
254 | goto again; |
---|
255 | return (FALSE); |
---|
256 | } |
---|
257 | /* |
---|
258 | * Guard against FTP bounce attacks. |
---|
259 | */ |
---|
260 | if (addr.sin_port == htons(20)) { |
---|
261 | _RPC_close(sock); |
---|
262 | return (FALSE); |
---|
263 | } |
---|
264 | /* |
---|
265 | * The listening socket is in FIONBIO mode and we inherit it. |
---|
266 | */ |
---|
267 | off = 0; |
---|
268 | if (ioctl(sock, FIONBIO, &off) < 0) { |
---|
269 | _RPC_close(sock); |
---|
270 | return (FALSE); |
---|
271 | } |
---|
272 | /* |
---|
273 | * make a new transporter (re-uses xprt) |
---|
274 | */ |
---|
275 | xprt = makefd_xprt(sock, r->sendsize, r->recvsize); |
---|
276 | xprt->xp_raddr = addr; |
---|
277 | xprt->xp_addrlen = len; |
---|
278 | return (FALSE); /* there is never an rpc msg to be processed */ |
---|
279 | } |
---|
280 | |
---|
281 | static enum xprt_stat |
---|
282 | rendezvous_stat() |
---|
283 | { |
---|
284 | |
---|
285 | return (XPRT_IDLE); |
---|
286 | } |
---|
287 | |
---|
288 | static void |
---|
289 | svctcp_destroy(xprt) |
---|
290 | register SVCXPRT *xprt; |
---|
291 | { |
---|
292 | register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1; |
---|
293 | |
---|
294 | xprt_unregister(xprt); |
---|
295 | (void)_RPC_close(xprt->xp_sock); |
---|
296 | if (xprt->xp_port != 0) { |
---|
297 | /* a rendezvouser socket */ |
---|
298 | xprt->xp_port = 0; |
---|
299 | } else { |
---|
300 | /* an actual connection socket */ |
---|
301 | XDR_DESTROY(&(cd->xdrs)); |
---|
302 | } |
---|
303 | mem_free((caddr_t)cd, sizeof(struct tcp_conn)); |
---|
304 | mem_free((caddr_t)xprt, sizeof(SVCXPRT)); |
---|
305 | } |
---|
306 | |
---|
307 | /* |
---|
308 | * All read operations timeout after 35 seconds. |
---|
309 | * A timeout is fatal for the connection. |
---|
310 | */ |
---|
311 | static struct timeval wait_per_try = { 35, 0 }; |
---|
312 | |
---|
313 | /* |
---|
314 | * reads data from the tcp conection. |
---|
315 | * any error is fatal and the connection is closed. |
---|
316 | * (And a read of zero bytes is a half closed stream => error.) |
---|
317 | * |
---|
318 | * Note: we have to be careful here not to allow ourselves to become |
---|
319 | * blocked too long in this routine. While we're waiting for data from one |
---|
320 | * client, another client may be trying to connect. To avoid this situation, |
---|
321 | * some code from svc_run() is transplanted here: the select() loop checks |
---|
322 | * all RPC descriptors including the one we want and calls svc_getreqset2() |
---|
323 | * to handle new requests if any are detected. |
---|
324 | */ |
---|
325 | static int |
---|
326 | readtcp(xprt, buf, len) |
---|
327 | register SVCXPRT *xprt; |
---|
328 | caddr_t buf; |
---|
329 | register int len; |
---|
330 | { |
---|
331 | register int sock = xprt->xp_sock; |
---|
332 | struct timeval start, delta, tv; |
---|
333 | struct timeval tmp1, tmp2; |
---|
334 | fd_set *fds; |
---|
335 | |
---|
336 | delta = wait_per_try; |
---|
337 | fds = NULL; |
---|
338 | gettimeofday(&start, NULL); |
---|
339 | do { |
---|
340 | int bytes = sizeof (fd_set); |
---|
341 | if (fds != NULL) |
---|
342 | free(fds); |
---|
343 | fds = (fd_set *)malloc(bytes); |
---|
344 | if (fds == NULL) |
---|
345 | goto fatal_err; |
---|
346 | memcpy(fds, __svc_fdset, bytes); |
---|
347 | |
---|
348 | /* XXX we know the other bits are still clear */ |
---|
349 | FD_SET(sock, fds); |
---|
350 | tv = delta; /* in case select() implements writeback */ |
---|
351 | switch (select(svc_maxfd + 1, fds, NULL, NULL, &tv)) { |
---|
352 | case -1: |
---|
353 | if (errno != EINTR) |
---|
354 | goto fatal_err; |
---|
355 | gettimeofday(&tmp1, NULL); |
---|
356 | timersub(&tmp1, &start, &tmp2); |
---|
357 | timersub(&wait_per_try, &tmp2, &tmp1); |
---|
358 | if (tmp1.tv_sec < 0 || !timerisset(&tmp1)) |
---|
359 | goto fatal_err; |
---|
360 | delta = tmp1; |
---|
361 | continue; |
---|
362 | case 0: |
---|
363 | goto fatal_err; |
---|
364 | default: |
---|
365 | if (!FD_ISSET(sock, fds)) { |
---|
366 | svc_getreqset2(fds, svc_maxfd + 1); |
---|
367 | gettimeofday(&tmp1, NULL); |
---|
368 | timersub(&tmp1, &start, &tmp2); |
---|
369 | timersub(&wait_per_try, &tmp2, &tmp1); |
---|
370 | if (tmp1.tv_sec < 0 || !timerisset(&tmp1)) |
---|
371 | goto fatal_err; |
---|
372 | delta = tmp1; |
---|
373 | continue; |
---|
374 | } |
---|
375 | } |
---|
376 | } while (!FD_ISSET(sock, fds)); |
---|
377 | if ((len = _RPC_read(sock, buf, len)) > 0) { |
---|
378 | if (fds != NULL) |
---|
379 | free(fds); |
---|
380 | return (len); |
---|
381 | } |
---|
382 | fatal_err: |
---|
383 | ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; |
---|
384 | if (fds != NULL) |
---|
385 | free(fds); |
---|
386 | return (-1); |
---|
387 | } |
---|
388 | |
---|
389 | /* |
---|
390 | * writes data to the tcp connection. |
---|
391 | * Any error is fatal and the connection is closed. |
---|
392 | */ |
---|
393 | static int |
---|
394 | writetcp(xprt, buf, len) |
---|
395 | register SVCXPRT *xprt; |
---|
396 | caddr_t buf; |
---|
397 | int len; |
---|
398 | { |
---|
399 | register int i, cnt; |
---|
400 | |
---|
401 | for (cnt = len; cnt > 0; cnt -= i, buf += i) { |
---|
402 | if ((i = _RPC_write(xprt->xp_sock, buf, cnt)) < 0) { |
---|
403 | ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = |
---|
404 | XPRT_DIED; |
---|
405 | return (-1); |
---|
406 | } |
---|
407 | } |
---|
408 | return (len); |
---|
409 | } |
---|
410 | |
---|
411 | static enum xprt_stat |
---|
412 | svctcp_stat(xprt) |
---|
413 | SVCXPRT *xprt; |
---|
414 | { |
---|
415 | register struct tcp_conn *cd = |
---|
416 | (struct tcp_conn *)(xprt->xp_p1); |
---|
417 | |
---|
418 | if (cd->strm_stat == XPRT_DIED) |
---|
419 | return (XPRT_DIED); |
---|
420 | if (! xdrrec_eof(&(cd->xdrs))) |
---|
421 | return (XPRT_MOREREQS); |
---|
422 | return (XPRT_IDLE); |
---|
423 | } |
---|
424 | |
---|
425 | static bool_t |
---|
426 | svctcp_recv(xprt, msg) |
---|
427 | SVCXPRT *xprt; |
---|
428 | register struct rpc_msg *msg; |
---|
429 | { |
---|
430 | register struct tcp_conn *cd = |
---|
431 | (struct tcp_conn *)(xprt->xp_p1); |
---|
432 | register XDR *xdrs = &(cd->xdrs); |
---|
433 | |
---|
434 | xdrs->x_op = XDR_DECODE; |
---|
435 | (void)xdrrec_skiprecord(xdrs); |
---|
436 | if (xdr_callmsg(xdrs, msg)) { |
---|
437 | cd->x_id = msg->rm_xid; |
---|
438 | return (TRUE); |
---|
439 | } |
---|
440 | cd->strm_stat = XPRT_DIED; /* XXXX */ |
---|
441 | return (FALSE); |
---|
442 | } |
---|
443 | |
---|
444 | static bool_t |
---|
445 | svctcp_getargs(xprt, xdr_args, args_ptr) |
---|
446 | SVCXPRT *xprt; |
---|
447 | xdrproc_t xdr_args; |
---|
448 | caddr_t args_ptr; |
---|
449 | { |
---|
450 | |
---|
451 | return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr)); |
---|
452 | } |
---|
453 | |
---|
454 | static bool_t |
---|
455 | svctcp_freeargs(xprt, xdr_args, args_ptr) |
---|
456 | SVCXPRT *xprt; |
---|
457 | xdrproc_t xdr_args; |
---|
458 | caddr_t args_ptr; |
---|
459 | { |
---|
460 | register XDR *xdrs = |
---|
461 | &(((struct tcp_conn *)(xprt->xp_p1))->xdrs); |
---|
462 | |
---|
463 | xdrs->x_op = XDR_FREE; |
---|
464 | return ((*xdr_args)(xdrs, args_ptr)); |
---|
465 | } |
---|
466 | |
---|
467 | static bool_t |
---|
468 | svctcp_reply(xprt, msg) |
---|
469 | SVCXPRT *xprt; |
---|
470 | register struct rpc_msg *msg; |
---|
471 | { |
---|
472 | register struct tcp_conn *cd = |
---|
473 | (struct tcp_conn *)(xprt->xp_p1); |
---|
474 | register XDR *xdrs = &(cd->xdrs); |
---|
475 | register bool_t stat; |
---|
476 | |
---|
477 | xdrs->x_op = XDR_ENCODE; |
---|
478 | msg->rm_xid = cd->x_id; |
---|
479 | stat = xdr_replymsg(xdrs, msg); |
---|
480 | (void)xdrrec_endofrecord(xdrs, TRUE); |
---|
481 | return (stat); |
---|
482 | } |
---|