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