OSDN Git Service

Implement hidden poll, switch user to hidden *printf/*scanf/poll
[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 xdrrec_create __xdrrec_create
45 #define xdrrec_endofrecord __xdrrec_endofrecord
46 #define xdrrec_skiprecord __xdrrec_skiprecord
47 #define xdrrec_eof __xdrrec_eof
48 #define xdr_callmsg __xdr_callmsg
49 #define xdr_replymsg __xdr_replymsg
50 #define xprt_register __xprt_register
51 #define xprt_unregister __xprt_unregister
52 #define getsockname __getsockname
53 #define bind __bind
54 #define bindresvport __bindresvport
55 #define poll __poll
56
57 #define __FORCE_GLIBC
58 #define _GNU_SOURCE
59 #include <features.h>
60
61 #include <stdio.h>
62 #include <unistd.h>
63 #include <string.h>
64 #include <rpc/rpc.h>
65 #include <sys/socket.h>
66 #include <sys/poll.h>
67 #include <errno.h>
68 #include <stdlib.h>
69
70 #ifdef USE_IN_LIBIO
71 # include <wchar.h>
72 # include <libio/iolibio.h>
73 # define fputs(s, f) _IO_fputs (s, f)
74 #endif
75
76 /*
77  * Ops vector for TCP/IP based rpc service handle
78  */
79 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
80 static enum xprt_stat svctcp_stat (SVCXPRT *);
81 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
82 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
83 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
84 static void svctcp_destroy (SVCXPRT *);
85
86 static const struct xp_ops svctcp_op =
87 {
88   svctcp_recv,
89   svctcp_stat,
90   svctcp_getargs,
91   svctcp_reply,
92   svctcp_freeargs,
93   svctcp_destroy
94 };
95
96 /*
97  * Ops vector for TCP/IP rendezvous handler
98  */
99 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
100 static enum xprt_stat rendezvous_stat (SVCXPRT *);
101 static void svctcp_rendezvous_abort (void);
102
103 /* This function makes sure abort() relocation goes through PLT
104    and thus can be lazy bound.  */
105 static void
106 svctcp_rendezvous_abort (void)
107 {
108   abort ();
109 };
110
111 static const struct xp_ops svctcp_rendezvous_op =
112 {
113   rendezvous_request,
114   rendezvous_stat,
115   (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
116   (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
117   (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
118   svctcp_destroy
119 };
120
121 static int readtcp (char*, char *, int);
122 static int writetcp (char *, char *, int);
123 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
124
125 struct tcp_rendezvous
126   {                             /* kept in xprt->xp_p1 */
127     u_int sendsize;
128     u_int recvsize;
129   };
130
131 struct tcp_conn
132   {                             /* kept in xprt->xp_p1 */
133     enum xprt_stat strm_stat;
134     u_long x_id;
135     XDR xdrs;
136     char verf_body[MAX_AUTH_BYTES];
137   };
138
139 /*
140  * Usage:
141  *      xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
142  *
143  * Creates, registers, and returns a (rpc) tcp based transporter.
144  * Once *xprt is initialized, it is registered as a transporter
145  * see (svc.h, xprt_register).  This routine returns
146  * a NULL if a problem occurred.
147  *
148  * If sock<0 then a socket is created, else sock is used.
149  * If the socket, sock is not bound to a port then svctcp_create
150  * binds it to an arbitrary port.  The routine then starts a tcp
151  * listener on the socket's associated port.  In any (successful) case,
152  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
153  * associated port number.
154  *
155  * Since tcp streams do buffered io similar to stdio, the caller can specify
156  * how big the send and receive buffers are via the second and third parms;
157  * 0 => use the system default.
158  */
159 SVCXPRT *
160 svctcp_create (int sock, u_int sendsize, u_int recvsize)
161 {
162   bool_t madesock = FALSE;
163   SVCXPRT *xprt;
164   struct tcp_rendezvous *r;
165   struct sockaddr_in addr;
166   socklen_t len = sizeof (struct sockaddr_in);
167
168   if (sock == RPC_ANYSOCK)
169     {
170       if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
171         {
172           __perror (_("svc_tcp.c - tcp socket creation problem"));
173           return (SVCXPRT *) NULL;
174         }
175       madesock = TRUE;
176     }
177   __memset ((char *) &addr, 0, sizeof (addr));
178   addr.sin_family = AF_INET;
179   if (bindresvport (sock, &addr))
180     {
181       addr.sin_port = 0;
182       (void) bind (sock, (struct sockaddr *) &addr, len);
183     }
184   if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
185       (listen (sock, 2) != 0))
186     {
187       __perror (_("svc_tcp.c - cannot getsockname or listen"));
188       if (madesock)
189         (void) __close (sock);
190       return (SVCXPRT *) NULL;
191     }
192   r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
193   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
194   if (r == NULL || xprt == NULL)
195     {
196 #ifdef USE_IN_LIBIO
197       if (_IO_fwide (stderr, 0) > 0)
198         (void) __fwprintf (stderr, L"%s", _("svctcp_create: out of memory\n"));
199       else
200 #endif
201         (void) fputs (_("svctcp_create: out of memory\n"), stderr);
202       mem_free (r, sizeof (*r));
203       mem_free (xprt, sizeof (SVCXPRT));
204       return NULL;
205     }
206   r->sendsize = sendsize;
207   r->recvsize = recvsize;
208   xprt->xp_p2 = NULL;
209   xprt->xp_p1 = (caddr_t) r;
210   xprt->xp_verf = _null_auth;
211   xprt->xp_ops = &svctcp_rendezvous_op;
212   xprt->xp_port = ntohs (addr.sin_port);
213   xprt->xp_sock = sock;
214   xprt_register (xprt);
215   return xprt;
216 }
217
218 /*
219  * Like svtcp_create(), except the routine takes any *open* UNIX file
220  * descriptor as its first input.
221  */
222 SVCXPRT *
223 svcfd_create (int fd, u_int sendsize, u_int recvsize)
224 {
225   return makefd_xprt (fd, sendsize, recvsize);
226 }
227
228 static SVCXPRT *
229 internal_function
230 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
231 {
232   SVCXPRT *xprt;
233   struct tcp_conn *cd;
234
235   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
236   cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
237   if (xprt == (SVCXPRT *) NULL || cd == NULL)
238     {
239 #ifdef USE_IN_LIBIO
240       if (_IO_fwide (stderr, 0) > 0)
241         (void) __fwprintf (stderr, L"%s",
242                            _("svc_tcp: makefd_xprt: out of memory\n"));
243       else
244 #endif
245         (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
246       mem_free (xprt, sizeof (SVCXPRT));
247       mem_free (cd, sizeof (struct tcp_conn));
248       return NULL;
249     }
250   cd->strm_stat = XPRT_IDLE;
251   xdrrec_create (&(cd->xdrs), sendsize, recvsize,
252                  (caddr_t) xprt, readtcp, writetcp);
253   xprt->xp_p2 = NULL;
254   xprt->xp_p1 = (caddr_t) cd;
255   xprt->xp_verf.oa_base = cd->verf_body;
256   xprt->xp_addrlen = 0;
257   xprt->xp_ops = &svctcp_op;    /* truly deals with calls */
258   xprt->xp_port = 0;            /* this is a connection, not a rendezvouser */
259   xprt->xp_sock = fd;
260   xprt_register (xprt);
261   return xprt;
262 }
263
264 static bool_t
265 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
266 {
267   int sock;
268   struct tcp_rendezvous *r;
269   struct sockaddr_in addr;
270   socklen_t len;
271
272   r = (struct tcp_rendezvous *) xprt->xp_p1;
273 again:
274   len = sizeof (struct sockaddr_in);
275   if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
276     {
277       if (errno == EINTR)
278         goto again;
279       return FALSE;
280     }
281   /*
282    * make a new transporter (re-uses xprt)
283    */
284   xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
285   __memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
286   xprt->xp_addrlen = len;
287   return FALSE;         /* there is never an rpc msg to be processed */
288 }
289
290 static enum xprt_stat
291 rendezvous_stat (SVCXPRT *xprt)
292 {
293   return XPRT_IDLE;
294 }
295
296 static void
297 svctcp_destroy (SVCXPRT *xprt)
298 {
299   struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
300
301   xprt_unregister (xprt);
302   (void) __close (xprt->xp_sock);
303   if (xprt->xp_port != 0)
304     {
305       /* a rendezvouser socket */
306       xprt->xp_port = 0;
307     }
308   else
309     {
310       /* an actual connection socket */
311       XDR_DESTROY (&(cd->xdrs));
312     }
313   mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
314   mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
315 }
316
317
318 /*
319  * reads data from the tcp connection.
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 static int
324 readtcp (char *xprtptr, char *buf, int len)
325 {
326   SVCXPRT *xprt = (SVCXPRT *)xprtptr;
327   int sock = xprt->xp_sock;
328   int milliseconds = 35 * 1000;
329   struct pollfd pollfd;
330
331   do
332     {
333       pollfd.fd = sock;
334       pollfd.events = POLLIN;
335       switch (poll (&pollfd, 1, milliseconds))
336         {
337         case -1:
338           if (errno == EINTR)
339             continue;
340           /*FALLTHROUGH*/
341         case 0:
342           goto fatal_err;
343         default:
344           if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
345               || (pollfd.revents & POLLNVAL))
346             goto fatal_err;
347           break;
348         }
349     }
350   while ((pollfd.revents & POLLIN) == 0);
351
352   if ((len = __read (sock, buf, len)) > 0)
353     return len;
354
355  fatal_err:
356   ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
357   return -1;
358 }
359
360 /*
361  * writes data to the tcp connection.
362  * Any error is fatal and the connection is closed.
363  */
364 static int
365 writetcp (char *xprtptr, char * buf, int len)
366 {
367   SVCXPRT *xprt = (SVCXPRT *)xprtptr;
368   int i, cnt;
369
370   for (cnt = len; cnt > 0; cnt -= i, buf += i)
371     {
372       if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
373         {
374           ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
375           return -1;
376         }
377     }
378   return len;
379 }
380
381 static enum xprt_stat
382 svctcp_stat (SVCXPRT *xprt)
383 {
384   struct tcp_conn *cd =
385   (struct tcp_conn *) (xprt->xp_p1);
386
387   if (cd->strm_stat == XPRT_DIED)
388     return XPRT_DIED;
389   if (!xdrrec_eof (&(cd->xdrs)))
390     return XPRT_MOREREQS;
391   return XPRT_IDLE;
392 }
393
394 static bool_t
395 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
396 {
397   struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
398   XDR *xdrs = &(cd->xdrs);
399
400   xdrs->x_op = XDR_DECODE;
401   (void) xdrrec_skiprecord (xdrs);
402   if (xdr_callmsg (xdrs, msg))
403     {
404       cd->x_id = msg->rm_xid;
405       return TRUE;
406     }
407   cd->strm_stat = XPRT_DIED;    /* XXXX */
408   return FALSE;
409 }
410
411 static bool_t
412 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
413 {
414   return ((*xdr_args) (&(((struct tcp_conn *)
415                           (xprt->xp_p1))->xdrs), args_ptr));
416 }
417
418 static bool_t
419 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
420 {
421   XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
422
423   xdrs->x_op = XDR_FREE;
424   return ((*xdr_args) (xdrs, args_ptr));
425 }
426
427 static bool_t
428 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
429 {
430   struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
431   XDR *xdrs = &(cd->xdrs);
432   bool_t stat;
433
434   xdrs->x_op = XDR_ENCODE;
435   msg->rm_xid = cd->x_id;
436   stat = xdr_replymsg (xdrs, msg);
437   (void) xdrrec_endofrecord (xdrs, TRUE);
438   return stat;
439 }