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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since d652c3b was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

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