]> sourceware.org Git - glibc.git/blame - sunrpc/svc_tcp.c
* elf/dblloadmod1.c: Add prototype to avoid warning.
[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>
4360eafd 47#include <libintl.h>
28f540f4
RM
48#include <rpc/rpc.h>
49#include <sys/socket.h>
090ca000 50#include <sys/poll.h>
28f540f4 51#include <errno.h>
e7fd8a39 52#include <stdlib.h>
28f540f4 53
50304ef0 54#ifdef USE_IN_LIBIO
51028f34 55# include <wchar.h>
50304ef0
UD
56# include <libio/iolibio.h>
57# define fputs(s, f) _IO_fputs (s, f)
58#endif
59
28f540f4
RM
60/*
61 * Ops vector for TCP/IP based rpc service handle
62 */
e7fd8a39
UD
63static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
64static enum xprt_stat svctcp_stat (SVCXPRT *);
65static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
66static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
67static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
68static void svctcp_destroy (SVCXPRT *);
69
70static const struct xp_ops svctcp_op =
71{
72 svctcp_recv,
73 svctcp_stat,
74 svctcp_getargs,
75 svctcp_reply,
76 svctcp_freeargs,
77 svctcp_destroy
28f540f4
RM
78};
79
80/*
81 * Ops vector for TCP/IP rendezvous handler
82 */
e7fd8a39
UD
83static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
84static enum xprt_stat rendezvous_stat (SVCXPRT *);
a49d0179 85static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
4b9afc43
UD
86
87/* This function makes sure abort() relocation goes through PLT
88 and thus can be lazy bound. */
89static void
90svctcp_rendezvous_abort (void)
91{
92 abort ();
93};
e7fd8a39
UD
94
95static const struct xp_ops svctcp_rendezvous_op =
96{
97 rendezvous_request,
98 rendezvous_stat,
4b9afc43
UD
99 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
100 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
101 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
e7fd8a39 102 svctcp_destroy
28f540f4
RM
103};
104
e7fd8a39
UD
105static int readtcp (char*, char *, int);
106static int writetcp (char *, char *, int);
dfd2257a 107static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
28f540f4 108
e7fd8a39
UD
109struct tcp_rendezvous
110 { /* kept in xprt->xp_p1 */
111 u_int sendsize;
112 u_int recvsize;
113 };
28f540f4 114
e7fd8a39
UD
115struct tcp_conn
116 { /* kept in xprt->xp_p1 */
117 enum xprt_stat strm_stat;
118 u_long x_id;
119 XDR xdrs;
120 char verf_body[MAX_AUTH_BYTES];
121 };
28f540f4
RM
122
123/*
124 * Usage:
e7fd8a39 125 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
28f540f4
RM
126 *
127 * Creates, registers, and returns a (rpc) tcp based transporter.
128 * Once *xprt is initialized, it is registered as a transporter
129 * see (svc.h, xprt_register). This routine returns
130 * a NULL if a problem occurred.
131 *
132 * If sock<0 then a socket is created, else sock is used.
133 * If the socket, sock is not bound to a port then svctcp_create
134 * binds it to an arbitrary port. The routine then starts a tcp
135 * listener on the socket's associated port. In any (successful) case,
136 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
137 * associated port number.
138 *
139 * Since tcp streams do buffered io similar to stdio, the caller can specify
140 * how big the send and receive buffers are via the second and third parms;
141 * 0 => use the system default.
142 */
143SVCXPRT *
e7fd8a39 144svctcp_create (int sock, u_int sendsize, u_int recvsize)
28f540f4 145{
e7fd8a39
UD
146 bool_t madesock = FALSE;
147 SVCXPRT *xprt;
148 struct tcp_rendezvous *r;
149 struct sockaddr_in addr;
41df5ed4 150 socklen_t len = sizeof (struct sockaddr_in);
e7fd8a39
UD
151
152 if (sock == RPC_ANYSOCK)
153 {
50304ef0 154 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
e7fd8a39 155 {
e852e889 156 perror (_("svc_tcp.c - tcp socket creation problem"));
e7fd8a39 157 return (SVCXPRT *) NULL;
28f540f4 158 }
e7fd8a39
UD
159 madesock = TRUE;
160 }
50304ef0 161 __bzero ((char *) &addr, sizeof (addr));
e7fd8a39
UD
162 addr.sin_family = AF_INET;
163 if (bindresvport (sock, &addr))
164 {
165 addr.sin_port = 0;
166 (void) bind (sock, (struct sockaddr *) &addr, len);
167 }
168 if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
169 (listen (sock, 2) != 0))
170 {
e852e889 171 perror (_("svc_tcp.c - cannot getsockname or listen"));
e7fd8a39 172 if (madesock)
50304ef0 173 (void) __close (sock);
e7fd8a39
UD
174 return (SVCXPRT *) NULL;
175 }
176 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
51028f34
UD
177 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
178 if (r == NULL || xprt == NULL)
e7fd8a39 179 {
51028f34
UD
180#ifdef USE_IN_LIBIO
181 if (_IO_fwide (stderr, 0) > 0)
182 (void) __fwprintf (stderr, L"%s", _("svctcp_create: out of memory\n"));
183 else
184#endif
185 (void) fputs (_("svctcp_create: out of memory\n"), stderr);
2e3e5db6
UD
186 mem_free (r, sizeof (*r));
187 mem_free (xprt, sizeof (SVCXPRT));
e7fd8a39
UD
188 return NULL;
189 }
190 r->sendsize = sendsize;
191 r->recvsize = recvsize;
e7fd8a39
UD
192 xprt->xp_p2 = NULL;
193 xprt->xp_p1 = (caddr_t) r;
194 xprt->xp_verf = _null_auth;
195 xprt->xp_ops = &svctcp_rendezvous_op;
196 xprt->xp_port = ntohs (addr.sin_port);
197 xprt->xp_sock = sock;
198 xprt_register (xprt);
199 return xprt;
28f540f4
RM
200}
201
202/*
203 * Like svtcp_create(), except the routine takes any *open* UNIX file
204 * descriptor as its first input.
205 */
206SVCXPRT *
e7fd8a39 207svcfd_create (int fd, u_int sendsize, u_int recvsize)
28f540f4 208{
e7fd8a39 209 return makefd_xprt (fd, sendsize, recvsize);
28f540f4
RM
210}
211
212static SVCXPRT *
dfd2257a 213internal_function
e7fd8a39 214makefd_xprt (int fd, u_int sendsize, u_int recvsize)
28f540f4 215{
e7fd8a39
UD
216 SVCXPRT *xprt;
217 struct tcp_conn *cd;
218
219 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
e7fd8a39 220 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
51028f34 221 if (xprt == (SVCXPRT *) NULL || cd == NULL)
e7fd8a39 222 {
51028f34
UD
223#ifdef USE_IN_LIBIO
224 if (_IO_fwide (stderr, 0) > 0)
225 (void) __fwprintf (stderr, L"%s",
226 _("svc_tcp: makefd_xprt: out of memory\n"));
227 else
228#endif
229 (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
2e3e5db6
UD
230 mem_free (xprt, sizeof (SVCXPRT));
231 mem_free (cd, sizeof (struct tcp_conn));
51028f34 232 return NULL;
e7fd8a39
UD
233 }
234 cd->strm_stat = XPRT_IDLE;
235 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
236 (caddr_t) xprt, readtcp, writetcp);
237 xprt->xp_p2 = NULL;
238 xprt->xp_p1 = (caddr_t) cd;
239 xprt->xp_verf.oa_base = cd->verf_body;
240 xprt->xp_addrlen = 0;
241 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
242 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
243 xprt->xp_sock = fd;
244 xprt_register (xprt);
e7fd8a39 245 return xprt;
28f540f4
RM
246}
247
248static bool_t
e7fd8a39 249rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
28f540f4 250{
e7fd8a39
UD
251 int sock;
252 struct tcp_rendezvous *r;
253 struct sockaddr_in addr;
41df5ed4 254 socklen_t len;
e7fd8a39
UD
255
256 r = (struct tcp_rendezvous *) xprt->xp_p1;
257again:
258 len = sizeof (struct sockaddr_in);
50304ef0 259 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
e7fd8a39
UD
260 {
261 if (errno == EINTR)
262 goto again;
263 return FALSE;
264 }
265 /*
266 * make a new transporter (re-uses xprt)
267 */
268 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
c1301d9a 269 memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
e7fd8a39
UD
270 xprt->xp_addrlen = len;
271 return FALSE; /* there is never an rpc msg to be processed */
28f540f4
RM
272}
273
274static enum xprt_stat
e7fd8a39 275rendezvous_stat (SVCXPRT *xprt)
28f540f4 276{
e7fd8a39 277 return XPRT_IDLE;
28f540f4
RM
278}
279
280static void
e7fd8a39 281svctcp_destroy (SVCXPRT *xprt)
28f540f4 282{
e7fd8a39
UD
283 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
284
285 xprt_unregister (xprt);
50304ef0 286 (void) __close (xprt->xp_sock);
e7fd8a39
UD
287 if (xprt->xp_port != 0)
288 {
289 /* a rendezvouser socket */
290 xprt->xp_port = 0;
291 }
292 else
293 {
294 /* an actual connection socket */
295 XDR_DESTROY (&(cd->xdrs));
296 }
297 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
298 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
28f540f4
RM
299}
300
28f540f4
RM
301
302/*
6d52618b 303 * reads data from the tcp connection.
28f540f4
RM
304 * any error is fatal and the connection is closed.
305 * (And a read of zero bytes is a half closed stream => error.)
306 */
307static int
e7fd8a39 308readtcp (char *xprtptr, char *buf, int len)
28f540f4 309{
e7fd8a39
UD
310 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
311 int sock = xprt->xp_sock;
090ca000
UD
312 int milliseconds = 35 * 1000;
313 struct pollfd pollfd;
314
315 do
e7fd8a39 316 {
090ca000
UD
317 pollfd.fd = sock;
318 pollfd.events = POLLIN;
319 switch (__poll (&pollfd, 1, milliseconds))
e7fd8a39 320 {
090ca000 321 case -1:
e7fd8a39 322 if (errno == EINTR)
fed8f7f7 323 continue;
090ca000
UD
324 /*FALLTHROUGH*/
325 case 0:
e7fd8a39 326 goto fatal_err;
090ca000 327 default:
7d1de115
UD
328 if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
329 || (pollfd.revents & POLLNVAL))
330 goto fatal_err;
090ca000 331 break;
7d1de115 332 }
e7fd8a39 333 }
090ca000 334 while ((pollfd.revents & POLLIN) == 0);
fed8f7f7 335
50304ef0 336 if ((len = __read (sock, buf, len)) > 0)
fed8f7f7
UD
337 return len;
338
090ca000 339 fatal_err:
e7fd8a39
UD
340 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
341 return -1;
28f540f4
RM
342}
343
344/*
345 * writes data to the tcp connection.
346 * Any error is fatal and the connection is closed.
347 */
348static int
e7fd8a39 349writetcp (char *xprtptr, char * buf, int len)
28f540f4 350{
e7fd8a39
UD
351 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
352 int i, cnt;
353
354 for (cnt = len; cnt > 0; cnt -= i, buf += i)
355 {
50304ef0 356 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
e7fd8a39 357 {
090ca000
UD
358 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
359 return -1;
28f540f4 360 }
e7fd8a39 361 }
090ca000 362 return len;
28f540f4
RM
363}
364
365static enum xprt_stat
e7fd8a39 366svctcp_stat (SVCXPRT *xprt)
28f540f4 367{
e7fd8a39
UD
368 struct tcp_conn *cd =
369 (struct tcp_conn *) (xprt->xp_p1);
370
371 if (cd->strm_stat == XPRT_DIED)
090ca000 372 return XPRT_DIED;
e7fd8a39 373 if (!xdrrec_eof (&(cd->xdrs)))
090ca000
UD
374 return XPRT_MOREREQS;
375 return XPRT_IDLE;
28f540f4
RM
376}
377
378static bool_t
090ca000 379svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
28f540f4 380{
090ca000 381 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
e7fd8a39
UD
382 XDR *xdrs = &(cd->xdrs);
383
384 xdrs->x_op = XDR_DECODE;
385 (void) xdrrec_skiprecord (xdrs);
386 if (xdr_callmsg (xdrs, msg))
387 {
388 cd->x_id = msg->rm_xid;
090ca000 389 return TRUE;
e7fd8a39 390 }
41aa20c2 391 cd->strm_stat = XPRT_DIED; /* XXXX */
090ca000 392 return FALSE;
28f540f4
RM
393}
394
395static bool_t
090ca000 396svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
28f540f4 397{
090ca000
UD
398 return ((*xdr_args) (&(((struct tcp_conn *)
399 (xprt->xp_p1))->xdrs), args_ptr));
28f540f4
RM
400}
401
402static bool_t
090ca000 403svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
28f540f4 404{
090ca000 405 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
28f540f4 406
e7fd8a39
UD
407 xdrs->x_op = XDR_FREE;
408 return ((*xdr_args) (xdrs, args_ptr));
28f540f4
RM
409}
410
411static bool_t
090ca000 412svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
28f540f4 413{
090ca000 414 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
e7fd8a39
UD
415 XDR *xdrs = &(cd->xdrs);
416 bool_t stat;
417
418 xdrs->x_op = XDR_ENCODE;
419 msg->rm_xid = cd->x_id;
420 stat = xdr_replymsg (xdrs, msg);
421 (void) xdrrec_endofrecord (xdrs, TRUE);
090ca000 422 return stat;
28f540f4 423}
This page took 0.235247 seconds and 5 git commands to generate.