m68k: add sotruss-lib.c
[glibc.git] / sunrpc / svc_tcp.c
blobeb615494d88675c5bf22438ad1a5ee8130e2e435
1 /*
2 * svc_tcp.c, Server side for TCP/IP based RPC.
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Actually implements two flavors of transporter -
34 * a tcp rendezvouser (a listener and connection establisher)
35 * and a record/tcp stream.
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <libintl.h>
42 #include <rpc/rpc.h>
43 #include <sys/socket.h>
44 #include <sys/poll.h>
45 #include <errno.h>
46 #include <stdlib.h>
48 #include <wchar.h>
49 #include <libio/iolibio.h>
52 * Ops vector for TCP/IP based rpc service handle
54 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
55 static enum xprt_stat svctcp_stat (SVCXPRT *);
56 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
57 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
58 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
59 static void svctcp_destroy (SVCXPRT *);
61 static const struct xp_ops svctcp_op =
63 svctcp_recv,
64 svctcp_stat,
65 svctcp_getargs,
66 svctcp_reply,
67 svctcp_freeargs,
68 svctcp_destroy
72 * Ops vector for TCP/IP rendezvous handler
74 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
75 static enum xprt_stat rendezvous_stat (SVCXPRT *);
76 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
78 /* This function makes sure abort() relocation goes through PLT
79 and thus can be lazy bound. */
80 static void
81 svctcp_rendezvous_abort (void)
83 abort ();
86 static const struct xp_ops svctcp_rendezvous_op =
88 rendezvous_request,
89 rendezvous_stat,
90 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
91 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
92 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
93 svctcp_destroy
96 static int readtcp (char*, char *, int);
97 static int writetcp (char *, char *, int);
98 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
100 struct tcp_rendezvous
101 { /* kept in xprt->xp_p1 */
102 u_int sendsize;
103 u_int recvsize;
106 struct tcp_conn
107 { /* kept in xprt->xp_p1 */
108 enum xprt_stat strm_stat;
109 u_long x_id;
110 XDR xdrs;
111 char verf_body[MAX_AUTH_BYTES];
115 * Usage:
116 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
118 * Creates, registers, and returns a (rpc) tcp based transporter.
119 * Once *xprt is initialized, it is registered as a transporter
120 * see (svc.h, xprt_register). This routine returns
121 * a NULL if a problem occurred.
123 * If sock<0 then a socket is created, else sock is used.
124 * If the socket, sock is not bound to a port then svctcp_create
125 * binds it to an arbitrary port. The routine then starts a tcp
126 * listener on the socket's associated port. In any (successful) case,
127 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
128 * associated port number.
130 * Since tcp streams do buffered io similar to stdio, the caller can specify
131 * how big the send and receive buffers are via the second and third parms;
132 * 0 => use the system default.
134 SVCXPRT *
135 svctcp_create (int sock, u_int sendsize, u_int recvsize)
137 bool_t madesock = FALSE;
138 SVCXPRT *xprt;
139 struct tcp_rendezvous *r;
140 struct sockaddr_in addr;
141 socklen_t len = sizeof (struct sockaddr_in);
143 if (sock == RPC_ANYSOCK)
145 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
147 perror (_("svc_tcp.c - tcp socket creation problem"));
148 return (SVCXPRT *) NULL;
150 madesock = TRUE;
152 __bzero ((char *) &addr, sizeof (addr));
153 addr.sin_family = AF_INET;
154 if (bindresvport (sock, &addr))
156 addr.sin_port = 0;
157 (void) __bind (sock, (struct sockaddr *) &addr, len);
159 if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
160 (__listen (sock, SOMAXCONN) != 0))
162 perror (_("svc_tcp.c - cannot getsockname or listen"));
163 if (madesock)
164 (void) __close (sock);
165 return (SVCXPRT *) NULL;
167 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
168 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
169 if (r == NULL || xprt == NULL)
171 (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
172 mem_free (r, sizeof (*r));
173 mem_free (xprt, sizeof (SVCXPRT));
174 return NULL;
176 r->sendsize = sendsize;
177 r->recvsize = recvsize;
178 xprt->xp_p2 = NULL;
179 xprt->xp_p1 = (caddr_t) r;
180 xprt->xp_verf = _null_auth;
181 xprt->xp_ops = &svctcp_rendezvous_op;
182 xprt->xp_port = ntohs (addr.sin_port);
183 xprt->xp_sock = sock;
184 xprt_register (xprt);
185 return xprt;
187 #ifdef EXPORT_RPC_SYMBOLS
188 libc_hidden_def (svctcp_create)
189 #else
190 libc_hidden_nolink_sunrpc (svctcp_create, GLIBC_2_0)
191 #endif
194 * Like svtcp_create(), except the routine takes any *open* UNIX file
195 * descriptor as its first input.
197 SVCXPRT *
198 svcfd_create (int fd, u_int sendsize, u_int recvsize)
200 return makefd_xprt (fd, sendsize, recvsize);
202 libc_hidden_nolink_sunrpc (svcfd_create, GLIBC_2_0)
204 static SVCXPRT *
205 internal_function
206 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
208 SVCXPRT *xprt;
209 struct tcp_conn *cd;
211 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
212 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
213 if (xprt == (SVCXPRT *) NULL || cd == NULL)
215 (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt",
216 _("out of memory\n"));
217 mem_free (xprt, sizeof (SVCXPRT));
218 mem_free (cd, sizeof (struct tcp_conn));
219 return NULL;
221 cd->strm_stat = XPRT_IDLE;
222 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
223 (caddr_t) xprt, readtcp, writetcp);
224 xprt->xp_p2 = NULL;
225 xprt->xp_p1 = (caddr_t) cd;
226 xprt->xp_verf.oa_base = cd->verf_body;
227 xprt->xp_addrlen = 0;
228 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
229 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
230 xprt->xp_sock = fd;
231 xprt_register (xprt);
232 return xprt;
235 static bool_t
236 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
238 int sock;
239 struct tcp_rendezvous *r;
240 struct sockaddr_in addr;
241 socklen_t len;
243 r = (struct tcp_rendezvous *) xprt->xp_p1;
244 again:
245 len = sizeof (struct sockaddr_in);
246 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
248 if (errno == EINTR)
249 goto again;
250 return FALSE;
253 * make a new transporter (re-uses xprt)
255 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
256 memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
257 xprt->xp_addrlen = len;
258 return FALSE; /* there is never an rpc msg to be processed */
261 static enum xprt_stat
262 rendezvous_stat (SVCXPRT *xprt)
264 return XPRT_IDLE;
267 static void
268 svctcp_destroy (SVCXPRT *xprt)
270 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
272 xprt_unregister (xprt);
273 (void) __close (xprt->xp_sock);
274 if (xprt->xp_port != 0)
276 /* a rendezvouser socket */
277 xprt->xp_port = 0;
279 else
281 /* an actual connection socket */
282 XDR_DESTROY (&(cd->xdrs));
284 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
285 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
290 * reads data from the tcp connection.
291 * any error is fatal and the connection is closed.
292 * (And a read of zero bytes is a half closed stream => error.)
294 static int
295 readtcp (char *xprtptr, char *buf, int len)
297 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
298 int sock = xprt->xp_sock;
299 int milliseconds = 35 * 1000;
300 struct pollfd pollfd;
304 pollfd.fd = sock;
305 pollfd.events = POLLIN;
306 switch (__poll (&pollfd, 1, milliseconds))
308 case -1:
309 if (errno == EINTR)
310 continue;
311 /*FALLTHROUGH*/
312 case 0:
313 goto fatal_err;
314 default:
315 if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
316 || (pollfd.revents & POLLNVAL))
317 goto fatal_err;
318 break;
321 while ((pollfd.revents & POLLIN) == 0);
323 if ((len = __read (sock, buf, len)) > 0)
324 return len;
326 fatal_err:
327 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
328 return -1;
332 * writes data to the tcp connection.
333 * Any error is fatal and the connection is closed.
335 static int
336 writetcp (char *xprtptr, char * buf, int len)
338 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
339 int i, cnt;
341 for (cnt = len; cnt > 0; cnt -= i, buf += i)
343 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
345 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
346 return -1;
349 return len;
352 static enum xprt_stat
353 svctcp_stat (SVCXPRT *xprt)
355 struct tcp_conn *cd =
356 (struct tcp_conn *) (xprt->xp_p1);
358 if (cd->strm_stat == XPRT_DIED)
359 return XPRT_DIED;
360 if (!xdrrec_eof (&(cd->xdrs)))
361 return XPRT_MOREREQS;
362 return XPRT_IDLE;
365 static bool_t
366 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
368 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
369 XDR *xdrs = &(cd->xdrs);
371 xdrs->x_op = XDR_DECODE;
372 (void) xdrrec_skiprecord (xdrs);
373 if (xdr_callmsg (xdrs, msg))
375 cd->x_id = msg->rm_xid;
376 return TRUE;
378 cd->strm_stat = XPRT_DIED; /* XXXX */
379 return FALSE;
382 static bool_t
383 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
385 return ((*xdr_args) (&(((struct tcp_conn *)
386 (xprt->xp_p1))->xdrs), args_ptr));
389 static bool_t
390 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
392 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
394 xdrs->x_op = XDR_FREE;
395 return ((*xdr_args) (xdrs, args_ptr));
398 static bool_t
399 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
401 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
402 XDR *xdrs = &(cd->xdrs);
403 bool_t stat;
405 xdrs->x_op = XDR_ENCODE;
406 msg->rm_xid = cd->x_id;
407 stat = xdr_replymsg (xdrs, msg);
408 (void) xdrrec_endofrecord (xdrs, TRUE);
409 return stat;