source: rtems/cpukit/librpc/src/rpc/svc_tcp.c @ 12c21c8

4.104.114.95
Last change on this file since 12c21c8 was 12c21c8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/01/08 at 14:29:11

Add missing prototypes.

  • Property mode set to 100644
File size: 12.1 KB
Line 
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";*/
33static 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 */
59static bool_t           svctcp_recv(SVCXPRT *xprt, struct rpc_msg *msg);
60static enum xprt_stat   svctcp_stat(SVCXPRT *xprt);
61static bool_t           svctcp_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr);
62static bool_t           svctcp_reply(SVCXPRT *xprt, struct rpc_msg *msg);
63static bool_t           svctcp_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr);
64static void             svctcp_destroy(SVCXPRT *xprt);
65
66static 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 */
78static bool_t           rendezvous_request(SVCXPRT *xprt, struct rpc_msg *msg);
79static enum xprt_stat   rendezvous_stat(SVCXPRT *xprt);
80
81static struct xp_ops svctcp_rendezvous_op = {
82        rendezvous_request,
83        rendezvous_stat,
84        (bool_t (*)(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr))abort,
85        (bool_t (*)(SVCXPRT *xprt, struct rpc_msg *msg))abort,
86        (bool_t (*)(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr))abort,
87        svctcp_destroy
88};
89
90static int readtcp(), writetcp();
91static SVCXPRT *makefd_xprt();
92
93struct tcp_rendezvous { /* kept in xprt->xp_p1 */
94        u_int sendsize;
95        u_int recvsize;
96};
97
98struct 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 */
125SVCXPRT *
126svctcp_create(
127        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        socklen_t 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 */
192SVCXPRT *
193svcfd_create(
194        int fd,
195        u_int sendsize,
196        u_int recvsize)
197{
198
199        return (makefd_xprt(fd, sendsize, recvsize));
200}
201
202static SVCXPRT *
203makefd_xprt(
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
238static bool_t
239rendezvous_request(
240        SVCXPRT *xprt,
241        struct rpc_msg *msg)
242{
243        int sock;
244        struct tcp_rendezvous *r;
245        struct sockaddr_in addr;
246        socklen_t len;
247        int off;
248
249        r = (struct tcp_rendezvous *)xprt->xp_p1;
250    again:
251        len = sizeof(struct sockaddr_in);
252        if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
253            &len)) < 0) {
254                if (errno == EINTR)
255                        goto again;
256               return (FALSE);
257        }
258        /*
259         * Guard against FTP bounce attacks.
260         */
261        if (addr.sin_port == htons(20)) {
262                _RPC_close(sock);
263                return (FALSE);
264        }
265        /*
266         * The listening socket is in FIONBIO mode and we inherit it.
267         */
268        off = 0;
269        if (ioctl(sock, FIONBIO, &off) < 0) {
270                _RPC_close(sock);
271                return (FALSE);
272        }
273        /*
274         * make a new transporter (re-uses xprt)
275         */
276        xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
277        xprt->xp_raddr = addr;
278        xprt->xp_addrlen = len;
279        return (FALSE); /* there is never an rpc msg to be processed */
280}
281
282static enum xprt_stat
283rendezvous_stat(SVCXPRT *xprt)
284{
285
286        return (XPRT_IDLE);
287}
288
289static void
290svctcp_destroy(
291        SVCXPRT *xprt)
292{
293        register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
294
295        xprt_unregister(xprt);
296        (void)_RPC_close(xprt->xp_sock);
297        if (xprt->xp_port != 0) {
298                /* a rendezvouser socket */
299                xprt->xp_port = 0;
300        } else {
301                /* an actual connection socket */
302                XDR_DESTROY(&(cd->xdrs));
303        }
304        mem_free((caddr_t)cd, sizeof(struct tcp_conn));
305        mem_free((caddr_t)xprt, sizeof(SVCXPRT));
306}
307
308/*
309 * All read operations timeout after 35 seconds.
310 * A timeout is fatal for the connection.
311 */
312static struct timeval wait_per_try = { 35, 0 };
313
314/*
315 * reads data from the tcp conection.
316 * any error is fatal and the connection is closed.
317 * (And a read of zero bytes is a half closed stream => error.)
318 *
319 * Note: we have to be careful here not to allow ourselves to become
320 * blocked too long in this routine. While we're waiting for data from one
321 * client, another client may be trying to connect. To avoid this situation,
322 * some code from svc_run() is transplanted here: the select() loop checks
323 * all RPC descriptors including the one we want and calls svc_getreqset2()
324 * to handle new requests if any are detected.
325 */
326static int
327readtcp(
328        SVCXPRT *xprt,
329        caddr_t buf,
330        int len)
331{
332        register int sock = xprt->xp_sock;
333        struct timeval start, delta, tv;
334        struct timeval tmp1, tmp2;
335        fd_set *fds;
336
337        delta = wait_per_try;
338        fds = NULL;
339        gettimeofday(&start, NULL);
340        do {
341                int bytes = sizeof (fd_set);
342                if (fds != NULL)
343                        free(fds);
344                fds = (fd_set *)malloc(bytes);
345                if (fds == NULL)
346                        goto fatal_err;
347                memcpy(fds, __svc_fdset, bytes);
348
349                /* XXX we know the other bits are still clear */
350                FD_SET(sock, fds);
351                tv = delta;     /* in case select() implements writeback */
352                switch (select(svc_maxfd + 1, fds, NULL, NULL, &tv)) {
353                case -1:
354                        if (errno != EINTR)
355                                goto fatal_err;
356                        gettimeofday(&tmp1, NULL);
357                        timersub(&tmp1, &start, &tmp2);
358                        timersub(&wait_per_try, &tmp2, &tmp1);
359                        if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
360                                goto fatal_err;
361                        delta = tmp1;
362                        continue;
363                case 0:
364                        goto fatal_err;
365                default:
366                        if (!FD_ISSET(sock, fds)) {
367                                svc_getreqset2(fds, svc_maxfd + 1);
368                                gettimeofday(&tmp1, NULL);
369                                timersub(&tmp1, &start, &tmp2);
370                                timersub(&wait_per_try, &tmp2, &tmp1);
371                                if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
372                                        goto fatal_err;
373                                delta = tmp1;
374                                continue;
375                        }
376                }
377        } while (!FD_ISSET(sock, fds));
378        if ((len = _RPC_read(sock, buf, len)) > 0) {
379                if (fds != NULL)
380                        free(fds);
381                return (len);
382        }
383fatal_err:
384        ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
385        if (fds != NULL)
386                free(fds);
387        return (-1);
388}
389
390/*
391 * writes data to the tcp connection.
392 * Any error is fatal and the connection is closed.
393 */
394static int
395writetcp(
396        SVCXPRT *xprt,
397        caddr_t buf,
398        int len)
399{
400        register int i, cnt;
401
402        for (cnt = len; cnt > 0; cnt -= i, buf += i) {
403                if ((i = _RPC_write(xprt->xp_sock, buf, cnt)) < 0) {
404                        ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
405                            XPRT_DIED;
406                        return (-1);
407                }
408        }
409        return (len);
410}
411
412static enum xprt_stat
413svctcp_stat(
414        SVCXPRT *xprt)
415{
416        register struct tcp_conn *cd =
417            (struct tcp_conn *)(xprt->xp_p1);
418
419        if (cd->strm_stat == XPRT_DIED)
420                return (XPRT_DIED);
421        if (! xdrrec_eof(&(cd->xdrs)))
422                return (XPRT_MOREREQS);
423        return (XPRT_IDLE);
424}
425
426static bool_t
427svctcp_recv(
428        SVCXPRT *xprt,
429        struct rpc_msg *msg)
430{
431        register struct tcp_conn *cd =
432            (struct tcp_conn *)(xprt->xp_p1);
433        register XDR *xdrs = &(cd->xdrs);
434
435        xdrs->x_op = XDR_DECODE;
436        (void)xdrrec_skiprecord(xdrs);
437        if (xdr_callmsg(xdrs, msg)) {
438                cd->x_id = msg->rm_xid;
439                return (TRUE);
440        }
441        cd->strm_stat = XPRT_DIED;      /* XXXX */
442        return (FALSE);
443}
444
445static bool_t
446svctcp_getargs(
447        SVCXPRT *xprt,
448        xdrproc_t xdr_args,
449        caddr_t args_ptr)
450{
451
452        return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
453}
454
455static bool_t
456svctcp_freeargs(
457        SVCXPRT *xprt,
458        xdrproc_t xdr_args,
459        caddr_t args_ptr)
460{
461        register XDR *xdrs =
462            &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
463
464        xdrs->x_op = XDR_FREE;
465        return ((*xdr_args)(xdrs, args_ptr));
466}
467
468static bool_t
469svctcp_reply(
470        SVCXPRT *xprt,
471        struct rpc_msg *msg)
472{
473        register struct tcp_conn *cd =
474            (struct tcp_conn *)(xprt->xp_p1);
475        register XDR *xdrs = &(cd->xdrs);
476        register bool_t stat;
477
478        xdrs->x_op = XDR_ENCODE;
479        msg->rm_xid = cd->x_id;
480        stat = xdr_replymsg(xdrs, msg);
481        (void)xdrrec_endofrecord(xdrs, TRUE);
482        return (stat);
483}
Note: See TracBrowser for help on using the repository browser.