Update.
[glibc.git] / sunrpc / svc_tcp.c
blob0c2ce9650ad7bb9af39d974b1965362272fb9a13
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32 #endif
35 * svc_tcp.c, Server side for TCP/IP based RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * Actually implements two flavors of transporter -
40 * a tcp rendezvouser (a listener and connection establisher)
41 * and a record/tcp stream.
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <rpc/rpc.h>
48 #include <sys/socket.h>
49 #include <errno.h>
50 #include <stdlib.h>
52 #ifdef USE_IN_LIBIO
53 # include <libio/iolibio.h>
54 # define fputs(s, f) _IO_fputs (s, f)
55 #endif
58 * Ops vector for TCP/IP based rpc service handle
60 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
61 static enum xprt_stat svctcp_stat (SVCXPRT *);
62 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
63 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
64 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
65 static void svctcp_destroy (SVCXPRT *);
67 static const struct xp_ops svctcp_op =
69 svctcp_recv,
70 svctcp_stat,
71 svctcp_getargs,
72 svctcp_reply,
73 svctcp_freeargs,
74 svctcp_destroy
78 * Ops vector for TCP/IP rendezvous handler
80 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
81 static enum xprt_stat rendezvous_stat (SVCXPRT *);
83 static const struct xp_ops svctcp_rendezvous_op =
85 rendezvous_request,
86 rendezvous_stat,
87 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
88 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) abort,
89 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
90 svctcp_destroy
93 static int readtcp (char*, char *, int);
94 static int writetcp (char *, char *, int);
95 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
97 struct tcp_rendezvous
98 { /* kept in xprt->xp_p1 */
99 u_int sendsize;
100 u_int recvsize;
103 struct tcp_conn
104 { /* kept in xprt->xp_p1 */
105 enum xprt_stat strm_stat;
106 u_long x_id;
107 XDR xdrs;
108 char verf_body[MAX_AUTH_BYTES];
112 * Usage:
113 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
115 * Creates, registers, and returns a (rpc) tcp based transporter.
116 * Once *xprt is initialized, it is registered as a transporter
117 * see (svc.h, xprt_register). This routine returns
118 * a NULL if a problem occurred.
120 * If sock<0 then a socket is created, else sock is used.
121 * If the socket, sock is not bound to a port then svctcp_create
122 * binds it to an arbitrary port. The routine then starts a tcp
123 * listener on the socket's associated port. In any (successful) case,
124 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
125 * associated port number.
127 * Since tcp streams do buffered io similar to stdio, the caller can specify
128 * how big the send and receive buffers are via the second and third parms;
129 * 0 => use the system default.
131 SVCXPRT *
132 svctcp_create (int sock, u_int sendsize, u_int recvsize)
134 bool_t madesock = FALSE;
135 SVCXPRT *xprt;
136 struct tcp_rendezvous *r;
137 struct sockaddr_in addr;
138 socklen_t len = sizeof (struct sockaddr_in);
140 if (sock == RPC_ANYSOCK)
142 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
144 perror (_("svc_tcp.c - tcp socket creation problem"));
145 return (SVCXPRT *) NULL;
147 madesock = TRUE;
149 __bzero ((char *) &addr, sizeof (addr));
150 addr.sin_family = AF_INET;
151 if (bindresvport (sock, &addr))
153 addr.sin_port = 0;
154 (void) bind (sock, (struct sockaddr *) &addr, len);
156 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
157 (listen (sock, 2) != 0))
159 perror (_("svc_tcp.c - cannot getsockname or listen"));
160 if (madesock)
161 (void) __close (sock);
162 return (SVCXPRT *) NULL;
164 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
165 if (r == NULL)
167 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
168 return NULL;
170 r->sendsize = sendsize;
171 r->recvsize = recvsize;
172 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
173 if (xprt == NULL)
175 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
176 return NULL;
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;
189 * Like svtcp_create(), except the routine takes any *open* UNIX file
190 * descriptor as its first input.
192 SVCXPRT *
193 svcfd_create (int fd, u_int sendsize, u_int recvsize)
195 return makefd_xprt (fd, sendsize, recvsize);
198 static SVCXPRT *
199 internal_function
200 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
202 SVCXPRT *xprt;
203 struct tcp_conn *cd;
205 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
206 if (xprt == (SVCXPRT *) NULL)
208 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
209 goto done;
211 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
212 if (cd == (struct tcp_conn *) NULL)
214 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
215 mem_free ((char *) xprt, sizeof (SVCXPRT));
216 xprt = (SVCXPRT *) NULL;
217 goto done;
219 cd->strm_stat = XPRT_IDLE;
220 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
221 (caddr_t) xprt, readtcp, writetcp);
222 xprt->xp_p2 = NULL;
223 xprt->xp_p1 = (caddr_t) cd;
224 xprt->xp_verf.oa_base = cd->verf_body;
225 xprt->xp_addrlen = 0;
226 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
227 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
228 xprt->xp_sock = fd;
229 xprt_register (xprt);
230 done:
231 return xprt;
234 static bool_t
235 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
237 int sock;
238 struct tcp_rendezvous *r;
239 struct sockaddr_in addr;
240 socklen_t len;
242 r = (struct tcp_rendezvous *) xprt->xp_p1;
243 again:
244 len = sizeof (struct sockaddr_in);
245 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
247 if (errno == EINTR)
248 goto again;
249 return FALSE;
252 * make a new transporter (re-uses xprt)
254 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
255 xprt->xp_raddr = addr;
256 xprt->xp_addrlen = len;
257 return FALSE; /* there is never an rpc msg to be processed */
260 static enum xprt_stat
261 rendezvous_stat (SVCXPRT *xprt)
263 return XPRT_IDLE;
266 static void
267 svctcp_destroy (SVCXPRT *xprt)
269 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
271 xprt_unregister (xprt);
272 (void) __close (xprt->xp_sock);
273 if (xprt->xp_port != 0)
275 /* a rendezvouser socket */
276 xprt->xp_port = 0;
278 else
280 /* an actual connection socket */
281 XDR_DESTROY (&(cd->xdrs));
283 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
284 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
288 * All read operations timeout after 35 seconds.
289 * A timeout is fatal for the connection.
291 static struct timeval wait_per_try = {35, 0};
294 * reads data from the tcp connection.
295 * any error is fatal and the connection is closed.
296 * (And a read of zero bytes is a half closed stream => error.)
298 * Note: we have to be careful here not to allow ourselves to become
299 * blocked too long in this routine. While we're waiting for data from one
300 * client, another client may be trying to connect. To avoid this situation,
301 * some code from svc_run() is transplanted here: the select() loop checks
302 * all RPC descriptors including the one we want and calls svc_getreqset2()
303 * to handle new requests if any are detected.
305 static int
306 readtcp (char *xprtptr, char *buf, int len)
308 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
309 int sock = xprt->xp_sock;
310 #ifdef FD_SETSIZE
311 fd_set readfds;
312 #else
313 int mask = 1 << sock;
314 int readfds;
315 #endif /* def FD_SETSIZE */
316 while (1)
318 struct timeval timeout = wait_per_try;
319 readfds = svc_fdset;
320 #ifdef FD_SETSIZE
321 FD_SET (sock, &readfds);
322 #else
323 readfds |= (1 << sock);
324 #endif /* def FD_SETSIZE */
325 if (__select (_rpc_dtablesize (), &readfds, (fd_set *) NULL,
326 (fd_set *) NULL, &timeout) <= 0)
328 if (errno == EINTR)
329 continue;
330 goto fatal_err;
333 #ifdef FD_SETSIZE
334 if (FD_ISSET (sock, &readfds))
335 #else
336 if (readfds == mask)
337 #endif /* def FD_SETSIZE */
338 break;
340 svc_getreqset (&readfds);
343 if ((len = __read (sock, buf, len)) > 0)
344 return len;
346 fatal_err:
347 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
348 return -1;
352 * writes data to the tcp connection.
353 * Any error is fatal and the connection is closed.
355 static int
356 writetcp (char *xprtptr, char * buf, int len)
358 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
359 int i, cnt;
361 for (cnt = len; cnt > 0; cnt -= i, buf += i)
363 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
365 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat =
366 XPRT_DIED;
367 return (-1);
370 return (len);
373 static enum xprt_stat
374 svctcp_stat (SVCXPRT *xprt)
376 struct tcp_conn *cd =
377 (struct tcp_conn *) (xprt->xp_p1);
379 if (cd->strm_stat == XPRT_DIED)
380 return (XPRT_DIED);
381 if (!xdrrec_eof (&(cd->xdrs)))
382 return (XPRT_MOREREQS);
383 return (XPRT_IDLE);
386 static bool_t
387 svctcp_recv (xprt, msg)
388 SVCXPRT *xprt;
389 struct rpc_msg *msg;
391 struct tcp_conn *cd =
392 (struct tcp_conn *) (xprt->xp_p1);
393 XDR *xdrs = &(cd->xdrs);
395 xdrs->x_op = XDR_DECODE;
396 (void) xdrrec_skiprecord (xdrs);
397 if (xdr_callmsg (xdrs, msg))
399 cd->x_id = msg->rm_xid;
400 return (TRUE);
402 cd->strm_stat = XPRT_DIED; /* XXXX */
403 return (FALSE);
406 static bool_t
407 svctcp_getargs (xprt, xdr_args, args_ptr)
408 SVCXPRT *xprt;
409 xdrproc_t xdr_args;
410 caddr_t args_ptr;
413 return ((*xdr_args) (&(((struct tcp_conn *) (xprt->xp_p1))->xdrs), args_ptr));
416 static bool_t
417 svctcp_freeargs (xprt, xdr_args, args_ptr)
418 SVCXPRT *xprt;
419 xdrproc_t xdr_args;
420 caddr_t args_ptr;
422 XDR *xdrs =
423 &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
425 xdrs->x_op = XDR_FREE;
426 return ((*xdr_args) (xdrs, args_ptr));
429 static bool_t
430 svctcp_reply (xprt, msg)
431 SVCXPRT *xprt;
432 struct rpc_msg *msg;
434 struct tcp_conn *cd =
435 (struct tcp_conn *) (xprt->xp_p1);
436 XDR *xdrs = &(cd->xdrs);
437 bool_t stat;
439 xdrs->x_op = XDR_ENCODE;
440 msg->rm_xid = cd->x_id;
441 stat = xdr_replymsg (xdrs, msg);
442 (void) xdrrec_endofrecord (xdrs, TRUE);
443 return (stat);