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