Update.
[glibc.git] / sunrpc / svc_tcp.c
blobe14a9ad7704a6889ba31d027ea39ddebe67f17e3
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>
53 * Ops vector for TCP/IP based rpc service handle
55 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
56 static enum xprt_stat svctcp_stat (SVCXPRT *);
57 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
58 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
59 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
60 static void svctcp_destroy (SVCXPRT *);
62 static const struct xp_ops svctcp_op =
64 svctcp_recv,
65 svctcp_stat,
66 svctcp_getargs,
67 svctcp_reply,
68 svctcp_freeargs,
69 svctcp_destroy
73 * Ops vector for TCP/IP rendezvous handler
75 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
76 static enum xprt_stat rendezvous_stat (SVCXPRT *);
78 static const struct xp_ops svctcp_rendezvous_op =
80 rendezvous_request,
81 rendezvous_stat,
82 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
83 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) abort,
84 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
85 svctcp_destroy
88 static int readtcp (char*, char *, int);
89 static int writetcp (char *, char *, int);
90 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
92 struct tcp_rendezvous
93 { /* kept in xprt->xp_p1 */
94 u_int sendsize;
95 u_int recvsize;
98 struct tcp_conn
99 { /* kept in xprt->xp_p1 */
100 enum xprt_stat strm_stat;
101 u_long x_id;
102 XDR xdrs;
103 char verf_body[MAX_AUTH_BYTES];
107 * Usage:
108 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
110 * Creates, registers, and returns a (rpc) tcp based transporter.
111 * Once *xprt is initialized, it is registered as a transporter
112 * see (svc.h, xprt_register). This routine returns
113 * a NULL if a problem occurred.
115 * If sock<0 then a socket is created, else sock is used.
116 * If the socket, sock is not bound to a port then svctcp_create
117 * binds it to an arbitrary port. The routine then starts a tcp
118 * listener on the socket's associated port. In any (successful) case,
119 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
120 * associated port number.
122 * Since tcp streams do buffered io similar to stdio, the caller can specify
123 * how big the send and receive buffers are via the second and third parms;
124 * 0 => use the system default.
126 SVCXPRT *
127 svctcp_create (int sock, u_int sendsize, u_int recvsize)
129 bool_t madesock = FALSE;
130 SVCXPRT *xprt;
131 struct tcp_rendezvous *r;
132 struct sockaddr_in addr;
133 size_t len = sizeof (struct sockaddr_in);
135 if (sock == RPC_ANYSOCK)
137 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
139 perror (_("svctcp_.c - udp socket creation problem"));
140 return (SVCXPRT *) NULL;
142 madesock = TRUE;
144 bzero ((char *) &addr, sizeof (addr));
145 addr.sin_family = AF_INET;
146 if (bindresvport (sock, &addr))
148 addr.sin_port = 0;
149 (void) bind (sock, (struct sockaddr *) &addr, len);
151 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
152 (listen (sock, 2) != 0))
154 perror (_("svctcp_.c - cannot getsockname or listen"));
155 if (madesock)
156 (void) close (sock);
157 return (SVCXPRT *) NULL;
159 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
160 if (r == NULL)
162 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
163 return NULL;
165 r->sendsize = sendsize;
166 r->recvsize = recvsize;
167 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
168 if (xprt == NULL)
170 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
171 return NULL;
173 xprt->xp_p2 = NULL;
174 xprt->xp_p1 = (caddr_t) r;
175 xprt->xp_verf = _null_auth;
176 xprt->xp_ops = &svctcp_rendezvous_op;
177 xprt->xp_port = ntohs (addr.sin_port);
178 xprt->xp_sock = sock;
179 xprt_register (xprt);
180 return xprt;
184 * Like svtcp_create(), except the routine takes any *open* UNIX file
185 * descriptor as its first input.
187 SVCXPRT *
188 svcfd_create (int fd, u_int sendsize, u_int recvsize)
190 return makefd_xprt (fd, sendsize, recvsize);
193 static SVCXPRT *
194 internal_function
195 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
197 SVCXPRT *xprt;
198 struct tcp_conn *cd;
200 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
201 if (xprt == (SVCXPRT *) NULL)
203 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
204 goto done;
206 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
207 if (cd == (struct tcp_conn *) NULL)
209 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
210 mem_free ((char *) xprt, sizeof (SVCXPRT));
211 xprt = (SVCXPRT *) NULL;
212 goto done;
214 cd->strm_stat = XPRT_IDLE;
215 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
216 (caddr_t) xprt, readtcp, writetcp);
217 xprt->xp_p2 = NULL;
218 xprt->xp_p1 = (caddr_t) cd;
219 xprt->xp_verf.oa_base = cd->verf_body;
220 xprt->xp_addrlen = 0;
221 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
222 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
223 xprt->xp_sock = fd;
224 xprt_register (xprt);
225 done:
226 return xprt;
229 static bool_t
230 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
232 int sock;
233 struct tcp_rendezvous *r;
234 struct sockaddr_in addr;
235 size_t len;
237 r = (struct tcp_rendezvous *) xprt->xp_p1;
238 again:
239 len = sizeof (struct sockaddr_in);
240 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr,
241 &len)) < 0)
243 if (errno == EINTR)
244 goto again;
245 return FALSE;
248 * make a new transporter (re-uses xprt)
250 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
251 xprt->xp_raddr = addr;
252 xprt->xp_addrlen = len;
253 return FALSE; /* there is never an rpc msg to be processed */
256 static enum xprt_stat
257 rendezvous_stat (SVCXPRT *xprt)
259 return XPRT_IDLE;
262 static void
263 svctcp_destroy (SVCXPRT *xprt)
265 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
267 xprt_unregister (xprt);
268 (void) close (xprt->xp_sock);
269 if (xprt->xp_port != 0)
271 /* a rendezvouser socket */
272 xprt->xp_port = 0;
274 else
276 /* an actual connection socket */
277 XDR_DESTROY (&(cd->xdrs));
279 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
280 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
284 * All read operations timeout after 35 seconds.
285 * A timeout is fatal for the connection.
287 static struct timeval wait_per_try =
288 {35, 0};
291 * reads data from the tcp connection.
292 * any error is fatal and the connection is closed.
293 * (And a read of zero bytes is a half closed stream => error.)
295 static int
296 readtcp (char *xprtptr, char *buf, int len)
298 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
299 int sock = xprt->xp_sock;
300 #ifdef FD_SETSIZE
301 fd_set mask;
302 fd_set readfds;
304 FD_ZERO (&mask);
305 FD_SET (sock, &mask);
306 #else
307 int mask = 1 << sock;
308 int readfds;
309 #endif /* def FD_SETSIZE */
312 struct timeval timeout = wait_per_try;
313 readfds = mask;
314 if (select (_rpc_dtablesize (), &readfds, (fd_set *) NULL,
315 (fd_set *) NULL, &timeout) <= 0)
317 if (errno == EINTR)
319 continue;
321 goto fatal_err;
323 #ifdef FD_SETSIZE
325 while (!FD_ISSET (sock, &readfds));
326 #else
328 while (readfds != mask);
329 #endif /* def FD_SETSIZE */
330 if ((len = read (sock, buf, len)) > 0)
332 return len;
334 fatal_err:
335 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
336 return -1;
340 * writes data to the tcp connection.
341 * Any error is fatal and the connection is closed.
343 static int
344 writetcp (char *xprtptr, char * buf, int len)
346 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
347 int i, cnt;
349 for (cnt = len; cnt > 0; cnt -= i, buf += i)
351 if ((i = write (xprt->xp_sock, buf, cnt)) < 0)
353 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat =
354 XPRT_DIED;
355 return (-1);
358 return (len);
361 static enum xprt_stat
362 svctcp_stat (SVCXPRT *xprt)
364 struct tcp_conn *cd =
365 (struct tcp_conn *) (xprt->xp_p1);
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);
374 static bool_t
375 svctcp_recv (xprt, msg)
376 SVCXPRT *xprt;
377 struct rpc_msg *msg;
379 struct tcp_conn *cd =
380 (struct tcp_conn *) (xprt->xp_p1);
381 XDR *xdrs = &(cd->xdrs);
383 xdrs->x_op = XDR_DECODE;
384 (void) xdrrec_skiprecord (xdrs);
385 if (xdr_callmsg (xdrs, msg))
387 cd->x_id = msg->rm_xid;
388 return (TRUE);
390 return (FALSE);
393 static bool_t
394 svctcp_getargs (xprt, xdr_args, args_ptr)
395 SVCXPRT *xprt;
396 xdrproc_t xdr_args;
397 caddr_t args_ptr;
400 return ((*xdr_args) (&(((struct tcp_conn *) (xprt->xp_p1))->xdrs), args_ptr));
403 static bool_t
404 svctcp_freeargs (xprt, xdr_args, args_ptr)
405 SVCXPRT *xprt;
406 xdrproc_t xdr_args;
407 caddr_t args_ptr;
409 XDR *xdrs =
410 &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
412 xdrs->x_op = XDR_FREE;
413 return ((*xdr_args) (xdrs, args_ptr));
416 static bool_t
417 svctcp_reply (xprt, msg)
418 SVCXPRT *xprt;
419 struct rpc_msg *msg;
421 struct tcp_conn *cd =
422 (struct tcp_conn *) (xprt->xp_p1);
423 XDR *xdrs = &(cd->xdrs);
424 bool_t stat;
426 xdrs->x_op = XDR_ENCODE;
427 msg->rm_xid = cd->x_id;
428 stat = xdr_replymsg (xdrs, msg);
429 (void) xdrrec_endofrecord (xdrs, TRUE);
430 return (stat);