OSDN Git Service

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