]> sourceware.org Git - glibc.git/blame - sunrpc/svc_tcp.c
Update.
[glibc.git] / sunrpc / svc_tcp.c
CommitLineData
28f540f4
RM
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.
cbd3dceb 9 *
28f540f4
RM
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.
cbd3dceb 13 *
28f540f4
RM
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.
cbd3dceb 17 *
28f540f4
RM
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.
cbd3dceb 21 *
28f540f4
RM
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.
cbd3dceb 25 *
28f540f4
RM
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30#if !defined(lint) && defined(SCCSIDS)
31static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32#endif
33
34/*
cbd3dceb 35 * svc_tcp.c, Server side for TCP/IP based RPC.
28f540f4
RM
36 *
37 * Copyright (C) 1984, Sun Microsystems, Inc.
38 *
39 * Actually implements two flavors of transporter -
6d52618b 40 * a tcp rendezvouser (a listener and connection establisher)
28f540f4
RM
41 * and a record/tcp stream.
42 */
43
44#include <stdio.h>
e7fd8a39
UD
45#include <unistd.h>
46#include <string.h>
28f540f4
RM
47#include <rpc/rpc.h>
48#include <sys/socket.h>
49#include <errno.h>
e7fd8a39 50#include <stdlib.h>
28f540f4
RM
51
52/*
53 * Ops vector for TCP/IP based rpc service handle
54 */
e7fd8a39
UD
55static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
56static enum xprt_stat svctcp_stat (SVCXPRT *);
57static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
58static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
59static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
60static void svctcp_destroy (SVCXPRT *);
61
62static const struct xp_ops svctcp_op =
63{
64 svctcp_recv,
65 svctcp_stat,
66 svctcp_getargs,
67 svctcp_reply,
68 svctcp_freeargs,
69 svctcp_destroy
28f540f4
RM
70};
71
72/*
73 * Ops vector for TCP/IP rendezvous handler
74 */
e7fd8a39
UD
75static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
76static enum xprt_stat rendezvous_stat (SVCXPRT *);
77
78static const struct xp_ops svctcp_rendezvous_op =
79{
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
28f540f4
RM
86};
87
e7fd8a39
UD
88static int readtcp (char*, char *, int);
89static int writetcp (char *, char *, int);
dfd2257a 90static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
28f540f4 91
e7fd8a39
UD
92struct tcp_rendezvous
93 { /* kept in xprt->xp_p1 */
94 u_int sendsize;
95 u_int recvsize;
96 };
28f540f4 97
e7fd8a39
UD
98struct 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];
104 };
28f540f4
RM
105
106/*
107 * Usage:
e7fd8a39 108 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
28f540f4
RM
109 *
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.
114 *
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.
121 *
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.
125 */
126SVCXPRT *
e7fd8a39 127svctcp_create (int sock, u_int sendsize, u_int recvsize)
28f540f4 128{
e7fd8a39
UD
129 bool_t madesock = FALSE;
130 SVCXPRT *xprt;
131 struct tcp_rendezvous *r;
132 struct sockaddr_in addr;
f671aeab 133 size_t len = sizeof (struct sockaddr_in);
e7fd8a39
UD
134
135 if (sock == RPC_ANYSOCK)
136 {
137 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
138 {
139 perror (_("svctcp_.c - udp socket creation problem"));
140 return (SVCXPRT *) NULL;
28f540f4 141 }
e7fd8a39
UD
142 madesock = TRUE;
143 }
144 bzero ((char *) &addr, sizeof (addr));
145 addr.sin_family = AF_INET;
146 if (bindresvport (sock, &addr))
147 {
148 addr.sin_port = 0;
149 (void) bind (sock, (struct sockaddr *) &addr, len);
150 }
151 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
152 (listen (sock, 2) != 0))
153 {
154 perror (_("svctcp_.c - cannot getsockname or listen"));
155 if (madesock)
156 (void) close (sock);
157 return (SVCXPRT *) NULL;
158 }
159 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
160 if (r == NULL)
161 {
162 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
163 return NULL;
164 }
165 r->sendsize = sendsize;
166 r->recvsize = recvsize;
167 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
168 if (xprt == NULL)
169 {
170 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
171 return NULL;
172 }
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;
28f540f4
RM
181}
182
183/*
184 * Like svtcp_create(), except the routine takes any *open* UNIX file
185 * descriptor as its first input.
186 */
187SVCXPRT *
e7fd8a39 188svcfd_create (int fd, u_int sendsize, u_int recvsize)
28f540f4 189{
e7fd8a39 190 return makefd_xprt (fd, sendsize, recvsize);
28f540f4
RM
191}
192
193static SVCXPRT *
dfd2257a 194internal_function
e7fd8a39 195makefd_xprt (int fd, u_int sendsize, u_int recvsize)
28f540f4 196{
e7fd8a39
UD
197 SVCXPRT *xprt;
198 struct tcp_conn *cd;
199
200 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
201 if (xprt == (SVCXPRT *) NULL)
202 {
203 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
204 goto done;
205 }
206 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
207 if (cd == (struct tcp_conn *) NULL)
208 {
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;
213 }
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);
225done:
226 return xprt;
28f540f4
RM
227}
228
229static bool_t
e7fd8a39 230rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
28f540f4 231{
e7fd8a39
UD
232 int sock;
233 struct tcp_rendezvous *r;
234 struct sockaddr_in addr;
f671aeab 235 size_t len;
e7fd8a39
UD
236
237 r = (struct tcp_rendezvous *) xprt->xp_p1;
238again:
239 len = sizeof (struct sockaddr_in);
240 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr,
241 &len)) < 0)
242 {
243 if (errno == EINTR)
244 goto again;
245 return FALSE;
246 }
247 /*
248 * make a new transporter (re-uses xprt)
249 */
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 */
28f540f4
RM
254}
255
256static enum xprt_stat
e7fd8a39 257rendezvous_stat (SVCXPRT *xprt)
28f540f4 258{
e7fd8a39 259 return XPRT_IDLE;
28f540f4
RM
260}
261
262static void
e7fd8a39 263svctcp_destroy (SVCXPRT *xprt)
28f540f4 264{
e7fd8a39
UD
265 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
266
267 xprt_unregister (xprt);
268 (void) close (xprt->xp_sock);
269 if (xprt->xp_port != 0)
270 {
271 /* a rendezvouser socket */
272 xprt->xp_port = 0;
273 }
274 else
275 {
276 /* an actual connection socket */
277 XDR_DESTROY (&(cd->xdrs));
278 }
279 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
280 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
28f540f4
RM
281}
282
283/*
284 * All read operations timeout after 35 seconds.
285 * A timeout is fatal for the connection.
286 */
e7fd8a39
UD
287static struct timeval wait_per_try =
288{35, 0};
28f540f4
RM
289
290/*
6d52618b 291 * reads data from the tcp connection.
28f540f4
RM
292 * any error is fatal and the connection is closed.
293 * (And a read of zero bytes is a half closed stream => error.)
294 */
295static int
e7fd8a39 296readtcp (char *xprtptr, char *buf, int len)
28f540f4 297{
e7fd8a39
UD
298 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
299 int sock = xprt->xp_sock;
28f540f4 300#ifdef FD_SETSIZE
e7fd8a39
UD
301 fd_set mask;
302 fd_set readfds;
28f540f4 303
e7fd8a39
UD
304 FD_ZERO (&mask);
305 FD_SET (sock, &mask);
28f540f4 306#else
e7fd8a39
UD
307 int mask = 1 << sock;
308 int readfds;
28f540f4 309#endif /* def FD_SETSIZE */
e7fd8a39
UD
310 do
311 {
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)
316 {
317 if (errno == EINTR)
318 {
319 continue;
320 }
321 goto fatal_err;
322 }
28f540f4 323#ifdef FD_SETSIZE
e7fd8a39
UD
324 }
325 while (!FD_ISSET (sock, &readfds));
28f540f4 326#else
e7fd8a39
UD
327 }
328 while (readfds != mask);
28f540f4 329#endif /* def FD_SETSIZE */
e7fd8a39
UD
330 if ((len = read (sock, buf, len)) > 0)
331 {
332 return len;
333 }
28f540f4 334fatal_err:
e7fd8a39
UD
335 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
336 return -1;
28f540f4
RM
337}
338
339/*
340 * writes data to the tcp connection.
341 * Any error is fatal and the connection is closed.
342 */
343static int
e7fd8a39 344writetcp (char *xprtptr, char * buf, int len)
28f540f4 345{
e7fd8a39
UD
346 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
347 int i, cnt;
348
349 for (cnt = len; cnt > 0; cnt -= i, buf += i)
350 {
351 if ((i = write (xprt->xp_sock, buf, cnt)) < 0)
352 {
353 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat =
354 XPRT_DIED;
355 return (-1);
28f540f4 356 }
e7fd8a39
UD
357 }
358 return (len);
28f540f4
RM
359}
360
361static enum xprt_stat
e7fd8a39 362svctcp_stat (SVCXPRT *xprt)
28f540f4 363{
e7fd8a39
UD
364 struct tcp_conn *cd =
365 (struct tcp_conn *) (xprt->xp_p1);
366
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);
28f540f4
RM
372}
373
374static bool_t
e7fd8a39
UD
375svctcp_recv (xprt, msg)
376 SVCXPRT *xprt;
377 struct rpc_msg *msg;
28f540f4 378{
e7fd8a39
UD
379 struct tcp_conn *cd =
380 (struct tcp_conn *) (xprt->xp_p1);
381 XDR *xdrs = &(cd->xdrs);
382
383 xdrs->x_op = XDR_DECODE;
384 (void) xdrrec_skiprecord (xdrs);
385 if (xdr_callmsg (xdrs, msg))
386 {
387 cd->x_id = msg->rm_xid;
388 return (TRUE);
389 }
390 return (FALSE);
28f540f4
RM
391}
392
393static bool_t
e7fd8a39
UD
394svctcp_getargs (xprt, xdr_args, args_ptr)
395 SVCXPRT *xprt;
396 xdrproc_t xdr_args;
397 caddr_t args_ptr;
28f540f4
RM
398{
399
e7fd8a39 400 return ((*xdr_args) (&(((struct tcp_conn *) (xprt->xp_p1))->xdrs), args_ptr));
28f540f4
RM
401}
402
403static bool_t
e7fd8a39
UD
404svctcp_freeargs (xprt, xdr_args, args_ptr)
405 SVCXPRT *xprt;
406 xdrproc_t xdr_args;
407 caddr_t args_ptr;
28f540f4 408{
e7fd8a39
UD
409 XDR *xdrs =
410 &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
28f540f4 411
e7fd8a39
UD
412 xdrs->x_op = XDR_FREE;
413 return ((*xdr_args) (xdrs, args_ptr));
28f540f4
RM
414}
415
416static bool_t
e7fd8a39
UD
417svctcp_reply (xprt, msg)
418 SVCXPRT *xprt;
419 struct rpc_msg *msg;
28f540f4 420{
e7fd8a39
UD
421 struct tcp_conn *cd =
422 (struct tcp_conn *) (xprt->xp_p1);
423 XDR *xdrs = &(cd->xdrs);
424 bool_t stat;
425
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);
28f540f4 431}
This page took 0.110537 seconds and 5 git commands to generate.