Update.
[glibc.git] / sunrpc / svc_tcp.c
blobcd5cecf0f7a219899779d72b1bfaa4cd3a0bea68
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 <sys/poll.h>
50 #include <errno.h>
51 #include <stdlib.h>
53 #ifdef USE_IN_LIBIO
54 # include <libio/iolibio.h>
55 # define fputs(s, f) _IO_fputs (s, f)
56 #endif
59 * Ops vector for TCP/IP based rpc service handle
61 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
62 static enum xprt_stat svctcp_stat (SVCXPRT *);
63 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
64 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
65 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
66 static void svctcp_destroy (SVCXPRT *);
68 static const struct xp_ops svctcp_op =
70 svctcp_recv,
71 svctcp_stat,
72 svctcp_getargs,
73 svctcp_reply,
74 svctcp_freeargs,
75 svctcp_destroy
79 * Ops vector for TCP/IP rendezvous handler
81 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
82 static enum xprt_stat rendezvous_stat (SVCXPRT *);
84 static const struct xp_ops svctcp_rendezvous_op =
86 rendezvous_request,
87 rendezvous_stat,
88 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
89 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) abort,
90 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) abort,
91 svctcp_destroy
94 static int readtcp (char*, char *, int);
95 static int writetcp (char *, char *, int);
96 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
98 struct tcp_rendezvous
99 { /* kept in xprt->xp_p1 */
100 u_int sendsize;
101 u_int recvsize;
104 struct tcp_conn
105 { /* kept in xprt->xp_p1 */
106 enum xprt_stat strm_stat;
107 u_long x_id;
108 XDR xdrs;
109 char verf_body[MAX_AUTH_BYTES];
113 * Usage:
114 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
116 * Creates, registers, and returns a (rpc) tcp based transporter.
117 * Once *xprt is initialized, it is registered as a transporter
118 * see (svc.h, xprt_register). This routine returns
119 * a NULL if a problem occurred.
121 * If sock<0 then a socket is created, else sock is used.
122 * If the socket, sock is not bound to a port then svctcp_create
123 * binds it to an arbitrary port. The routine then starts a tcp
124 * listener on the socket's associated port. In any (successful) case,
125 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
126 * associated port number.
128 * Since tcp streams do buffered io similar to stdio, the caller can specify
129 * how big the send and receive buffers are via the second and third parms;
130 * 0 => use the system default.
132 SVCXPRT *
133 svctcp_create (int sock, u_int sendsize, u_int recvsize)
135 bool_t madesock = FALSE;
136 SVCXPRT *xprt;
137 struct tcp_rendezvous *r;
138 struct sockaddr_in addr;
139 socklen_t len = sizeof (struct sockaddr_in);
141 if (sock == RPC_ANYSOCK)
143 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
145 perror (_("svc_tcp.c - tcp socket creation problem"));
146 return (SVCXPRT *) NULL;
148 madesock = TRUE;
150 __bzero ((char *) &addr, sizeof (addr));
151 addr.sin_family = AF_INET;
152 if (bindresvport (sock, &addr))
154 addr.sin_port = 0;
155 (void) bind (sock, (struct sockaddr *) &addr, len);
157 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
158 (listen (sock, 2) != 0))
160 perror (_("svc_tcp.c - cannot getsockname or listen"));
161 if (madesock)
162 (void) __close (sock);
163 return (SVCXPRT *) NULL;
165 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
166 if (r == NULL)
168 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
169 return NULL;
171 r->sendsize = sendsize;
172 r->recvsize = recvsize;
173 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
174 if (xprt == NULL)
176 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
177 return NULL;
179 xprt->xp_p2 = NULL;
180 xprt->xp_p1 = (caddr_t) r;
181 xprt->xp_verf = _null_auth;
182 xprt->xp_ops = &svctcp_rendezvous_op;
183 xprt->xp_port = ntohs (addr.sin_port);
184 xprt->xp_sock = sock;
185 xprt_register (xprt);
186 return xprt;
190 * Like svtcp_create(), except the routine takes any *open* UNIX file
191 * descriptor as its first input.
193 SVCXPRT *
194 svcfd_create (int fd, u_int sendsize, u_int recvsize)
196 return makefd_xprt (fd, sendsize, recvsize);
199 static SVCXPRT *
200 internal_function
201 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
203 SVCXPRT *xprt;
204 struct tcp_conn *cd;
206 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
207 if (xprt == (SVCXPRT *) NULL)
209 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
210 goto done;
212 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
213 if (cd == (struct tcp_conn *) NULL)
215 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
216 mem_free ((char *) xprt, sizeof (SVCXPRT));
217 xprt = (SVCXPRT *) NULL;
218 goto done;
220 cd->strm_stat = XPRT_IDLE;
221 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
222 (caddr_t) xprt, readtcp, writetcp);
223 xprt->xp_p2 = NULL;
224 xprt->xp_p1 = (caddr_t) cd;
225 xprt->xp_verf.oa_base = cd->verf_body;
226 xprt->xp_addrlen = 0;
227 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
228 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
229 xprt->xp_sock = fd;
230 xprt_register (xprt);
231 done:
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 xprt->xp_raddr = 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 break;
318 while ((pollfd.revents & POLLIN) == 0);
320 if ((len = __read (sock, buf, len)) > 0)
321 return len;
323 fatal_err:
324 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
325 return -1;
329 * writes data to the tcp connection.
330 * Any error is fatal and the connection is closed.
332 static int
333 writetcp (char *xprtptr, char * buf, int len)
335 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
336 int i, cnt;
338 for (cnt = len; cnt > 0; cnt -= i, buf += i)
340 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
342 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
343 return -1;
346 return len;
349 static enum xprt_stat
350 svctcp_stat (SVCXPRT *xprt)
352 struct tcp_conn *cd =
353 (struct tcp_conn *) (xprt->xp_p1);
355 if (cd->strm_stat == XPRT_DIED)
356 return XPRT_DIED;
357 if (!xdrrec_eof (&(cd->xdrs)))
358 return XPRT_MOREREQS;
359 return XPRT_IDLE;
362 static bool_t
363 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
365 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
366 XDR *xdrs = &(cd->xdrs);
368 xdrs->x_op = XDR_DECODE;
369 (void) xdrrec_skiprecord (xdrs);
370 if (xdr_callmsg (xdrs, msg))
372 cd->x_id = msg->rm_xid;
373 return TRUE;
375 cd->strm_stat = XPRT_DIED; /* XXXX */
376 return FALSE;
379 static bool_t
380 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
382 return ((*xdr_args) (&(((struct tcp_conn *)
383 (xprt->xp_p1))->xdrs), args_ptr));
386 static bool_t
387 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
389 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
391 xdrs->x_op = XDR_FREE;
392 return ((*xdr_args) (xdrs, args_ptr));
395 static bool_t
396 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
398 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
399 XDR *xdrs = &(cd->xdrs);
400 bool_t stat;
402 xdrs->x_op = XDR_ENCODE;
403 msg->rm_xid = cd->x_id;
404 stat = xdr_replymsg (xdrs, msg);
405 (void) xdrrec_endofrecord (xdrs, TRUE);
406 return stat;