Correct data for isw* functions.
[glibc/history.git] / sunrpc / svc_tcp.c
blob278e16fc33aa6d009bc647218aca5150ac1396e3
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 <rpc/rpc.h>
46 #include <sys/socket.h>
47 #include <errno.h>
48 #define abort ((bool_t (*) ()) abort)
49 #ifndef errno
50 extern errno;
51 #endif
54 * Ops vector for TCP/IP based rpc service handle
56 static bool_t svctcp_recv();
57 static enum xprt_stat svctcp_stat();
58 static bool_t svctcp_getargs();
59 static bool_t svctcp_reply();
60 static bool_t svctcp_freeargs();
61 static void svctcp_destroy();
63 static struct xp_ops svctcp_op = {
64 svctcp_recv,
65 svctcp_stat,
66 svctcp_getargs,
67 svctcp_reply,
68 svctcp_freeargs,
69 svctcp_destroy
73 * Ops vector for TCP/IP rendezvous handler
75 static bool_t rendezvous_request();
76 static enum xprt_stat rendezvous_stat();
78 static struct xp_ops svctcp_rendezvous_op = {
79 rendezvous_request,
80 rendezvous_stat,
81 abort,
82 abort,
83 abort,
84 svctcp_destroy
87 static int readtcp(), writetcp();
88 static SVCXPRT *makefd_xprt();
90 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
91 u_int sendsize;
92 u_int recvsize;
95 struct tcp_conn { /* kept in xprt->xp_p1 */
96 enum xprt_stat strm_stat;
97 u_long x_id;
98 XDR xdrs;
99 char verf_body[MAX_AUTH_BYTES];
103 * Usage:
104 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
106 * Creates, registers, and returns a (rpc) tcp based transporter.
107 * Once *xprt is initialized, it is registered as a transporter
108 * see (svc.h, xprt_register). This routine returns
109 * a NULL if a problem occurred.
111 * If sock<0 then a socket is created, else sock is used.
112 * If the socket, sock is not bound to a port then svctcp_create
113 * binds it to an arbitrary port. The routine then starts a tcp
114 * listener on the socket's associated port. In any (successful) case,
115 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
116 * associated port number.
118 * Since tcp streams do buffered io similar to stdio, the caller can specify
119 * how big the send and receive buffers are via the second and third parms;
120 * 0 => use the system default.
122 SVCXPRT *
123 svctcp_create(sock, sendsize, recvsize)
124 register int sock;
125 u_int sendsize;
126 u_int recvsize;
128 bool_t madesock = FALSE;
129 register SVCXPRT *xprt;
130 register struct tcp_rendezvous *r;
131 struct sockaddr_in addr;
132 int len = sizeof(struct sockaddr_in);
134 if (sock == RPC_ANYSOCK) {
135 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
136 perror(_("svctcp_.c - udp socket creation problem"));
137 return ((SVCXPRT *)NULL);
139 madesock = TRUE;
141 bzero((char *)&addr, sizeof (addr));
142 addr.sin_family = AF_INET;
143 if (bindresvport(sock, &addr)) {
144 addr.sin_port = 0;
145 (void)bind(sock, (struct sockaddr *)&addr, len);
147 if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
148 (listen(sock, 2) != 0)) {
149 perror(_("svctcp_.c - cannot getsockname or listen"));
150 if (madesock)
151 (void)close(sock);
152 return ((SVCXPRT *)NULL);
154 r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
155 if (r == NULL) {
156 (void) fprintf(stderr, _("svctcp_create: out of memory\n"));
157 return (NULL);
159 r->sendsize = sendsize;
160 r->recvsize = recvsize;
161 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
162 if (xprt == NULL) {
163 (void) fprintf(stderr, _("svctcp_create: out of memory\n"));
164 return (NULL);
166 xprt->xp_p2 = NULL;
167 xprt->xp_p1 = (caddr_t)r;
168 xprt->xp_verf = _null_auth;
169 xprt->xp_ops = &svctcp_rendezvous_op;
170 xprt->xp_port = ntohs(addr.sin_port);
171 xprt->xp_sock = sock;
172 xprt_register(xprt);
173 return (xprt);
177 * Like svtcp_create(), except the routine takes any *open* UNIX file
178 * descriptor as its first input.
180 SVCXPRT *
181 svcfd_create(fd, sendsize, recvsize)
182 int fd;
183 u_int sendsize;
184 u_int recvsize;
187 return (makefd_xprt(fd, sendsize, recvsize));
190 static SVCXPRT *
191 makefd_xprt(fd, sendsize, recvsize)
192 int fd;
193 u_int sendsize;
194 u_int recvsize;
196 register SVCXPRT *xprt;
197 register struct tcp_conn *cd;
199 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
200 if (xprt == (SVCXPRT *)NULL) {
201 (void) fprintf(stderr, _("svc_tcp: makefd_xprt: out of memory\n"));
202 goto done;
204 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
205 if (cd == (struct tcp_conn *)NULL) {
206 (void) fprintf(stderr, _("svc_tcp: makefd_xprt: out of memory\n"));
207 mem_free((char *) xprt, sizeof(SVCXPRT));
208 xprt = (SVCXPRT *)NULL;
209 goto done;
211 cd->strm_stat = XPRT_IDLE;
212 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
213 (caddr_t)xprt, readtcp, writetcp);
214 xprt->xp_p2 = NULL;
215 xprt->xp_p1 = (caddr_t)cd;
216 xprt->xp_verf.oa_base = cd->verf_body;
217 xprt->xp_addrlen = 0;
218 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
219 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
220 xprt->xp_sock = fd;
221 xprt_register(xprt);
222 done:
223 return (xprt);
226 static bool_t
227 rendezvous_request(xprt)
228 register SVCXPRT *xprt;
230 int sock;
231 struct tcp_rendezvous *r;
232 struct sockaddr_in addr;
233 int len;
235 r = (struct tcp_rendezvous *)xprt->xp_p1;
236 again:
237 len = sizeof(struct sockaddr_in);
238 if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
239 &len)) < 0) {
240 if (errno == EINTR)
241 goto again;
242 return (FALSE);
245 * make a new transporter (re-uses xprt)
247 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
248 xprt->xp_raddr = addr;
249 xprt->xp_addrlen = len;
250 return (FALSE); /* there is never an rpc msg to be processed */
253 static enum xprt_stat
254 rendezvous_stat()
257 return (XPRT_IDLE);
260 static void
261 svctcp_destroy(xprt)
262 register SVCXPRT *xprt;
264 register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
266 xprt_unregister(xprt);
267 (void)close(xprt->xp_sock);
268 if (xprt->xp_port != 0) {
269 /* a rendezvouser socket */
270 xprt->xp_port = 0;
271 } else {
272 /* an actual connection socket */
273 XDR_DESTROY(&(cd->xdrs));
275 mem_free((caddr_t)cd, sizeof(struct tcp_conn));
276 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
280 * All read operations timeout after 35 seconds.
281 * A timeout is fatal for the connection.
283 static struct timeval wait_per_try = { 35, 0 };
286 * reads data from the tcp connection.
287 * any error is fatal and the connection is closed.
288 * (And a read of zero bytes is a half closed stream => error.)
290 static int
291 readtcp(xprt, buf, len)
292 register SVCXPRT *xprt;
293 caddr_t buf;
294 register int len;
296 register int sock = xprt->xp_sock;
297 #ifdef FD_SETSIZE
298 fd_set mask;
299 fd_set readfds;
301 FD_ZERO(&mask);
302 FD_SET(sock, &mask);
303 #else
304 register int mask = 1 << sock;
305 int readfds;
306 #endif /* def FD_SETSIZE */
307 do {
308 struct timeval timeout = wait_per_try;
309 readfds = mask;
310 if (select(_rpc_dtablesize(), &readfds, (int*)NULL, (int*)NULL,
311 &timeout) <= 0) {
312 if (errno == EINTR) {
313 continue;
315 goto fatal_err;
317 #ifdef FD_SETSIZE
318 } while (!FD_ISSET(sock, &readfds));
319 #else
320 } while (readfds != mask);
321 #endif /* def FD_SETSIZE */
322 if ((len = read(sock, buf, len)) > 0) {
323 return (len);
325 fatal_err:
326 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
327 return (-1);
331 * writes data to the tcp connection.
332 * Any error is fatal and the connection is closed.
334 static int
335 writetcp(xprt, buf, len)
336 register SVCXPRT *xprt;
337 caddr_t buf;
338 int len;
340 register int i, cnt;
342 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
343 if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
344 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
345 XPRT_DIED;
346 return (-1);
349 return (len);
352 static enum xprt_stat
353 svctcp_stat(xprt)
354 SVCXPRT *xprt;
356 register 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(xprt, msg)
368 SVCXPRT *xprt;
369 register struct rpc_msg *msg;
371 register struct tcp_conn *cd =
372 (struct tcp_conn *)(xprt->xp_p1);
373 register XDR *xdrs = &(cd->xdrs);
375 xdrs->x_op = XDR_DECODE;
376 (void)xdrrec_skiprecord(xdrs);
377 if (xdr_callmsg(xdrs, msg)) {
378 cd->x_id = msg->rm_xid;
379 return (TRUE);
381 return (FALSE);
384 static bool_t
385 svctcp_getargs(xprt, xdr_args, args_ptr)
386 SVCXPRT *xprt;
387 xdrproc_t xdr_args;
388 caddr_t args_ptr;
391 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
394 static bool_t
395 svctcp_freeargs(xprt, xdr_args, args_ptr)
396 SVCXPRT *xprt;
397 xdrproc_t xdr_args;
398 caddr_t args_ptr;
400 register XDR *xdrs =
401 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
403 xdrs->x_op = XDR_FREE;
404 return ((*xdr_args)(xdrs, args_ptr));
407 static bool_t
408 svctcp_reply(xprt, msg)
409 SVCXPRT *xprt;
410 register struct rpc_msg *msg;
412 register struct tcp_conn *cd =
413 (struct tcp_conn *)(xprt->xp_p1);
414 register XDR *xdrs = &(cd->xdrs);
415 register bool_t stat;
417 xdrs->x_op = XDR_ENCODE;
418 msg->rm_xid = cd->x_id;
419 stat = xdr_replymsg(xdrs, msg);
420 (void)xdrrec_endofrecord(xdrs, TRUE);
421 return (stat);