Update.
[glibc.git] / sunrpc / svc_tcp.c
blob94bfb33dcf05328072d0bcab7f4eb3b91c92137f
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 <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>
54 #ifdef USE_IN_LIBIO
55 # include <libio/iolibio.h>
56 # define fputs(s, f) _IO_fputs (s, f)
57 #endif
60 * Ops vector for TCP/IP based rpc service handle
62 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
63 static enum xprt_stat svctcp_stat (SVCXPRT *);
64 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
65 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
66 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
67 static void svctcp_destroy (SVCXPRT *);
69 static const struct xp_ops svctcp_op =
71 svctcp_recv,
72 svctcp_stat,
73 svctcp_getargs,
74 svctcp_reply,
75 svctcp_freeargs,
76 svctcp_destroy
80 * Ops vector for TCP/IP rendezvous handler
82 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
83 static enum xprt_stat rendezvous_stat (SVCXPRT *);
85 static const struct xp_ops svctcp_rendezvous_op =
87 rendezvous_request,
88 rendezvous_stat,
89 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
90 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) abort,
91 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
92 svctcp_destroy
95 static int readtcp (char*, char *, int);
96 static int writetcp (char *, char *, int);
97 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
99 struct tcp_rendezvous
100 { /* kept in xprt->xp_p1 */
101 u_int sendsize;
102 u_int recvsize;
105 struct tcp_conn
106 { /* kept in xprt->xp_p1 */
107 enum xprt_stat strm_stat;
108 u_long x_id;
109 XDR xdrs;
110 char verf_body[MAX_AUTH_BYTES];
114 * Usage:
115 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
117 * Creates, registers, and returns a (rpc) tcp based transporter.
118 * Once *xprt is initialized, it is registered as a transporter
119 * see (svc.h, xprt_register). This routine returns
120 * a NULL if a problem occurred.
122 * If sock<0 then a socket is created, else sock is used.
123 * If the socket, sock is not bound to a port then svctcp_create
124 * binds it to an arbitrary port. The routine then starts a tcp
125 * listener on the socket's associated port. In any (successful) case,
126 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
127 * associated port number.
129 * Since tcp streams do buffered io similar to stdio, the caller can specify
130 * how big the send and receive buffers are via the second and third parms;
131 * 0 => use the system default.
133 SVCXPRT *
134 svctcp_create (int sock, u_int sendsize, u_int recvsize)
136 bool_t madesock = FALSE;
137 SVCXPRT *xprt;
138 struct tcp_rendezvous *r;
139 struct sockaddr_in addr;
140 socklen_t len = sizeof (struct sockaddr_in);
142 if (sock == RPC_ANYSOCK)
144 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
146 perror (_("svc_tcp.c - tcp socket creation problem"));
147 return (SVCXPRT *) NULL;
149 madesock = TRUE;
151 __bzero ((char *) &addr, sizeof (addr));
152 addr.sin_family = AF_INET;
153 if (bindresvport (sock, &addr))
155 addr.sin_port = 0;
156 (void) bind (sock, (struct sockaddr *) &addr, len);
158 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
159 (listen (sock, 2) != 0))
161 perror (_("svc_tcp.c - cannot getsockname or listen"));
162 if (madesock)
163 (void) __close (sock);
164 return (SVCXPRT *) NULL;
166 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
167 if (r == NULL)
169 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
170 return NULL;
172 r->sendsize = sendsize;
173 r->recvsize = recvsize;
174 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
175 if (xprt == NULL)
177 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
178 return NULL;
180 xprt->xp_p2 = NULL;
181 xprt->xp_p1 = (caddr_t) r;
182 xprt->xp_verf = _null_auth;
183 xprt->xp_ops = &svctcp_rendezvous_op;
184 xprt->xp_port = ntohs (addr.sin_port);
185 xprt->xp_sock = sock;
186 xprt_register (xprt);
187 return xprt;
191 * Like svtcp_create(), except the routine takes any *open* UNIX file
192 * descriptor as its first input.
194 SVCXPRT *
195 svcfd_create (int fd, u_int sendsize, u_int recvsize)
197 return makefd_xprt (fd, sendsize, recvsize);
200 static SVCXPRT *
201 internal_function
202 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
204 SVCXPRT *xprt;
205 struct tcp_conn *cd;
207 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
208 if (xprt == (SVCXPRT *) NULL)
210 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
211 goto done;
213 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
214 if (cd == (struct tcp_conn *) NULL)
216 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
217 mem_free ((char *) xprt, sizeof (SVCXPRT));
218 xprt = (SVCXPRT *) NULL;
219 goto done;
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 done:
233 return xprt;
236 static bool_t
237 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
239 int sock;
240 struct tcp_rendezvous *r;
241 struct sockaddr_in addr;
242 socklen_t len;
244 r = (struct tcp_rendezvous *) xprt->xp_p1;
245 again:
246 len = sizeof (struct sockaddr_in);
247 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
249 if (errno == EINTR)
250 goto again;
251 return FALSE;
254 * make a new transporter (re-uses xprt)
256 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
257 memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
258 xprt->xp_addrlen = len;
259 return FALSE; /* there is never an rpc msg to be processed */
262 static enum xprt_stat
263 rendezvous_stat (SVCXPRT *xprt)
265 return XPRT_IDLE;
268 static void
269 svctcp_destroy (SVCXPRT *xprt)
271 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
273 xprt_unregister (xprt);
274 (void) __close (xprt->xp_sock);
275 if (xprt->xp_port != 0)
277 /* a rendezvouser socket */
278 xprt->xp_port = 0;
280 else
282 /* an actual connection socket */
283 XDR_DESTROY (&(cd->xdrs));
285 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
286 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
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 int milliseconds = 35 * 1000;
301 struct pollfd pollfd;
305 pollfd.fd = sock;
306 pollfd.events = POLLIN;
307 switch (__poll (&pollfd, 1, milliseconds))
309 case -1:
310 if (errno == EINTR)
311 continue;
312 /*FALLTHROUGH*/
313 case 0:
314 goto fatal_err;
315 default:
316 if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
317 || (pollfd.revents & POLLNVAL))
318 goto fatal_err;
319 break;
322 while ((pollfd.revents & POLLIN) == 0);
324 if ((len = __read (sock, buf, len)) > 0)
325 return len;
327 fatal_err:
328 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
329 return -1;
333 * writes data to the tcp connection.
334 * Any error is fatal and the connection is closed.
336 static int
337 writetcp (char *xprtptr, char * buf, int len)
339 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
340 int i, cnt;
342 for (cnt = len; cnt > 0; cnt -= i, buf += i)
344 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
346 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
347 return -1;
350 return len;
353 static enum xprt_stat
354 svctcp_stat (SVCXPRT *xprt)
356 struct tcp_conn *cd =
357 (struct tcp_conn *) (xprt->xp_p1);
359 if (cd->strm_stat == XPRT_DIED)
360 return XPRT_DIED;
361 if (!xdrrec_eof (&(cd->xdrs)))
362 return XPRT_MOREREQS;
363 return XPRT_IDLE;
366 static bool_t
367 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
369 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
370 XDR *xdrs = &(cd->xdrs);
372 xdrs->x_op = XDR_DECODE;
373 (void) xdrrec_skiprecord (xdrs);
374 if (xdr_callmsg (xdrs, msg))
376 cd->x_id = msg->rm_xid;
377 return TRUE;
379 cd->strm_stat = XPRT_DIED; /* XXXX */
380 return FALSE;
383 static bool_t
384 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
386 return ((*xdr_args) (&(((struct tcp_conn *)
387 (xprt->xp_p1))->xdrs), args_ptr));
390 static bool_t
391 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
393 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
395 xdrs->x_op = XDR_FREE;
396 return ((*xdr_args) (xdrs, args_ptr));
399 static bool_t
400 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
402 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
403 XDR *xdrs = &(cd->xdrs);
404 bool_t stat;
406 xdrs->x_op = XDR_ENCODE;
407 msg->rm_xid = cd->x_id;
408 stat = xdr_replymsg (xdrs, msg);
409 (void) xdrrec_endofrecord (xdrs, TRUE);
410 return stat;