Update.
[glibc.git] / sunrpc / svc_tcp.c
blob06c05a640d4113870a769b3c75160277a4f4acd8
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 *);
84 static void svctcp_rendezvous_abort (void);
86 /* This function makes sure abort() relocation goes through PLT
87 and thus can be lazy bound. */
88 static void
89 svctcp_rendezvous_abort (void)
91 abort ();
94 static const struct xp_ops svctcp_rendezvous_op =
96 rendezvous_request,
97 rendezvous_stat,
98 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
99 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
100 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
101 svctcp_destroy
104 static int readtcp (char*, char *, int);
105 static int writetcp (char *, char *, int);
106 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
108 struct tcp_rendezvous
109 { /* kept in xprt->xp_p1 */
110 u_int sendsize;
111 u_int recvsize;
114 struct tcp_conn
115 { /* kept in xprt->xp_p1 */
116 enum xprt_stat strm_stat;
117 u_long x_id;
118 XDR xdrs;
119 char verf_body[MAX_AUTH_BYTES];
123 * Usage:
124 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
126 * Creates, registers, and returns a (rpc) tcp based transporter.
127 * Once *xprt is initialized, it is registered as a transporter
128 * see (svc.h, xprt_register). This routine returns
129 * a NULL if a problem occurred.
131 * If sock<0 then a socket is created, else sock is used.
132 * If the socket, sock is not bound to a port then svctcp_create
133 * binds it to an arbitrary port. The routine then starts a tcp
134 * listener on the socket's associated port. In any (successful) case,
135 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
136 * associated port number.
138 * Since tcp streams do buffered io similar to stdio, the caller can specify
139 * how big the send and receive buffers are via the second and third parms;
140 * 0 => use the system default.
142 SVCXPRT *
143 svctcp_create (int sock, u_int sendsize, u_int recvsize)
145 bool_t madesock = FALSE;
146 SVCXPRT *xprt;
147 struct tcp_rendezvous *r;
148 struct sockaddr_in addr;
149 socklen_t len = sizeof (struct sockaddr_in);
151 if (sock == RPC_ANYSOCK)
153 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
155 perror (_("svc_tcp.c - tcp socket creation problem"));
156 return (SVCXPRT *) NULL;
158 madesock = TRUE;
160 __bzero ((char *) &addr, sizeof (addr));
161 addr.sin_family = AF_INET;
162 if (bindresvport (sock, &addr))
164 addr.sin_port = 0;
165 (void) bind (sock, (struct sockaddr *) &addr, len);
167 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
168 (listen (sock, 2) != 0))
170 perror (_("svc_tcp.c - cannot getsockname or listen"));
171 if (madesock)
172 (void) __close (sock);
173 return (SVCXPRT *) NULL;
175 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
176 if (r == NULL)
178 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
179 return NULL;
181 r->sendsize = sendsize;
182 r->recvsize = recvsize;
183 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
184 if (xprt == NULL)
186 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
187 return NULL;
189 xprt->xp_p2 = NULL;
190 xprt->xp_p1 = (caddr_t) r;
191 xprt->xp_verf = _null_auth;
192 xprt->xp_ops = &svctcp_rendezvous_op;
193 xprt->xp_port = ntohs (addr.sin_port);
194 xprt->xp_sock = sock;
195 xprt_register (xprt);
196 return xprt;
200 * Like svtcp_create(), except the routine takes any *open* UNIX file
201 * descriptor as its first input.
203 SVCXPRT *
204 svcfd_create (int fd, u_int sendsize, u_int recvsize)
206 return makefd_xprt (fd, sendsize, recvsize);
209 static SVCXPRT *
210 internal_function
211 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
213 SVCXPRT *xprt;
214 struct tcp_conn *cd;
216 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
217 if (xprt == (SVCXPRT *) NULL)
219 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
220 goto done;
222 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
223 if (cd == (struct tcp_conn *) NULL)
225 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
226 mem_free ((char *) xprt, sizeof (SVCXPRT));
227 xprt = (SVCXPRT *) NULL;
228 goto done;
230 cd->strm_stat = XPRT_IDLE;
231 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
232 (caddr_t) xprt, readtcp, writetcp);
233 xprt->xp_p2 = NULL;
234 xprt->xp_p1 = (caddr_t) cd;
235 xprt->xp_verf.oa_base = cd->verf_body;
236 xprt->xp_addrlen = 0;
237 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
238 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
239 xprt->xp_sock = fd;
240 xprt_register (xprt);
241 done:
242 return xprt;
245 static bool_t
246 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
248 int sock;
249 struct tcp_rendezvous *r;
250 struct sockaddr_in addr;
251 socklen_t len;
253 r = (struct tcp_rendezvous *) xprt->xp_p1;
254 again:
255 len = sizeof (struct sockaddr_in);
256 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
258 if (errno == EINTR)
259 goto again;
260 return FALSE;
263 * make a new transporter (re-uses xprt)
265 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
266 memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
267 xprt->xp_addrlen = len;
268 return FALSE; /* there is never an rpc msg to be processed */
271 static enum xprt_stat
272 rendezvous_stat (SVCXPRT *xprt)
274 return XPRT_IDLE;
277 static void
278 svctcp_destroy (SVCXPRT *xprt)
280 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
282 xprt_unregister (xprt);
283 (void) __close (xprt->xp_sock);
284 if (xprt->xp_port != 0)
286 /* a rendezvouser socket */
287 xprt->xp_port = 0;
289 else
291 /* an actual connection socket */
292 XDR_DESTROY (&(cd->xdrs));
294 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
295 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
300 * reads data from the tcp connection.
301 * any error is fatal and the connection is closed.
302 * (And a read of zero bytes is a half closed stream => error.)
304 static int
305 readtcp (char *xprtptr, char *buf, int len)
307 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
308 int sock = xprt->xp_sock;
309 int milliseconds = 35 * 1000;
310 struct pollfd pollfd;
314 pollfd.fd = sock;
315 pollfd.events = POLLIN;
316 switch (__poll (&pollfd, 1, milliseconds))
318 case -1:
319 if (errno == EINTR)
320 continue;
321 /*FALLTHROUGH*/
322 case 0:
323 goto fatal_err;
324 default:
325 if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
326 || (pollfd.revents & POLLNVAL))
327 goto fatal_err;
328 break;
331 while ((pollfd.revents & POLLIN) == 0);
333 if ((len = __read (sock, buf, len)) > 0)
334 return len;
336 fatal_err:
337 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
338 return -1;
342 * writes data to the tcp connection.
343 * Any error is fatal and the connection is closed.
345 static int
346 writetcp (char *xprtptr, char * buf, int len)
348 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
349 int i, cnt;
351 for (cnt = len; cnt > 0; cnt -= i, buf += i)
353 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
355 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
356 return -1;
359 return len;
362 static enum xprt_stat
363 svctcp_stat (SVCXPRT *xprt)
365 struct tcp_conn *cd =
366 (struct tcp_conn *) (xprt->xp_p1);
368 if (cd->strm_stat == XPRT_DIED)
369 return XPRT_DIED;
370 if (!xdrrec_eof (&(cd->xdrs)))
371 return XPRT_MOREREQS;
372 return XPRT_IDLE;
375 static bool_t
376 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
378 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
379 XDR *xdrs = &(cd->xdrs);
381 xdrs->x_op = XDR_DECODE;
382 (void) xdrrec_skiprecord (xdrs);
383 if (xdr_callmsg (xdrs, msg))
385 cd->x_id = msg->rm_xid;
386 return TRUE;
388 cd->strm_stat = XPRT_DIED; /* XXXX */
389 return FALSE;
392 static bool_t
393 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
395 return ((*xdr_args) (&(((struct tcp_conn *)
396 (xprt->xp_p1))->xdrs), args_ptr));
399 static bool_t
400 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
402 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
404 xdrs->x_op = XDR_FREE;
405 return ((*xdr_args) (xdrs, args_ptr));
408 static bool_t
409 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
411 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
412 XDR *xdrs = &(cd->xdrs);
413 bool_t stat;
415 xdrs->x_op = XDR_ENCODE;
416 msg->rm_xid = cd->x_id;
417 stat = xdr_replymsg (xdrs, msg);
418 (void) xdrrec_endofrecord (xdrs, TRUE);
419 return stat;