source: rtems-libbsd/services/librpc/src/rpc/svc_tcp.c @ 20ec9e6

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 20ec9e6 was 20ec9e6, checked in by Joel Sherrill <joel.sherrill@…>, on 08/03/12 at 19:19:49

librpc: Initial addition

This does not currently compile. There is an include file
issue and int32_t is not defined even though stdint.h is included
before its use.

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