OSDN Git Service

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