Libinfo-173.tar.gz
[apple/libinfo.git] / rpc.subproj / svc_tcp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
32 *
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 *
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
40 *
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
44 *
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
53
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/
56 /*static char *sccsid = "from: @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/
57 static char *rcsid = "$Id: svc_tcp.c,v 1.5 2003/06/23 17:24:59 majka Exp $";
58 #endif
59
60 /*
61 * svc_tcp.c, Server side for TCP/IP based RPC.
62 *
63 * Copyright (C) 1984, Sun Microsystems, Inc.
64 *
65 * Actually implements two flavors of transporter -
66 * a tcp rendezvouser (a listner and connection establisher)
67 * and a record/tcp stream.
68 */
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <rpc/rpc.h>
75 #include <sys/socket.h>
76 #include <errno.h>
77
78 #define max(a, b) (((a) > (b)) ? (a) : (b))
79
80 extern int bindresvport();
81
82 /*
83 * Ops vector for TCP/IP based rpc service handle
84 */
85 static bool_t svctcp_recv();
86 static enum xprt_stat svctcp_stat();
87 static bool_t svctcp_getargs();
88 static bool_t svctcp_reply();
89 static bool_t svctcp_freeargs();
90 static void svctcp_destroy();
91
92 static struct xp_ops svctcp_op = {
93 svctcp_recv,
94 svctcp_stat,
95 svctcp_getargs,
96 svctcp_reply,
97 svctcp_freeargs,
98 svctcp_destroy
99 };
100
101 /*
102 * Ops vector for TCP/IP rendezvous handler
103 */
104 static bool_t rendezvous_abort();
105 static bool_t rendezvous_request();
106 static enum xprt_stat rendezvous_stat();
107
108 static struct xp_ops svctcp_rendezvous_op = {
109 rendezvous_request,
110 rendezvous_stat,
111 rendezvous_abort,
112 rendezvous_abort,
113 rendezvous_abort,
114 svctcp_destroy
115 };
116
117 static int readtcp(), writetcp();
118 static SVCXPRT *makefd_xprt();
119
120 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
121 u_int sendsize;
122 u_int recvsize;
123 };
124
125 struct tcp_conn { /* kept in xprt->xp_p1 */
126 enum xprt_stat strm_stat;
127 u_long x_id;
128 XDR xdrs;
129 char verf_body[MAX_AUTH_BYTES];
130 };
131
132 /*
133 * Usage:
134 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
135 *
136 * Creates, registers, and returns a (rpc) tcp based transporter.
137 * Once *xprt is initialized, it is registered as a transporter
138 * see (svc.h, xprt_register). This routine returns
139 * a NULL if a problem occurred.
140 *
141 * If sock<0 then a socket is created, else sock is used.
142 * If the socket, sock is not bound to a port then svctcp_create
143 * binds it to an arbitrary port. The routine then starts a tcp
144 * listener on the socket's associated port. In any (successful) case,
145 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
146 * associated port number.
147 *
148 * Since tcp streams do buffered io similar to stdio, the caller can specify
149 * how big the send and receive buffers are via the second and third parms;
150 * 0 => use the system default.
151 */
152 SVCXPRT *
153 svctcp_create(sock, sendsize, recvsize)
154 register int sock;
155 u_int sendsize;
156 u_int recvsize;
157 {
158 bool_t madesock = FALSE;
159 register SVCXPRT *xprt;
160 register struct tcp_rendezvous *r;
161 struct sockaddr_in addr;
162 int len = sizeof(struct sockaddr_in);
163
164 if (sock == RPC_ANYSOCK) {
165 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
166 perror("svctcp_.c - udp socket creation problem");
167 return ((SVCXPRT *)NULL);
168 }
169 madesock = TRUE;
170 }
171 bzero((char *)&addr, sizeof (addr));
172 addr.sin_family = AF_INET;
173 if (bindresvport(sock, &addr)) {
174 addr.sin_port = 0;
175 (void)bind(sock, (struct sockaddr *)&addr, len);
176 }
177 if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
178 (listen(sock, 2) != 0)) {
179 perror("svctcp_.c - cannot getsockname or listen");
180 if (madesock)
181 (void)close(sock);
182 return ((SVCXPRT *)NULL);
183 }
184 r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
185 if (r == NULL) {
186 (void) fprintf(stderr, "svctcp_create: out of memory\n");
187 return (NULL);
188 }
189 r->sendsize = sendsize;
190 r->recvsize = recvsize;
191 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
192 if (xprt == NULL) {
193 (void) fprintf(stderr, "svctcp_create: out of memory\n");
194 return (NULL);
195 }
196 xprt->xp_p2 = NULL;
197 xprt->xp_p1 = (caddr_t)r;
198 xprt->xp_verf = _null_auth;
199 xprt->xp_ops = &svctcp_rendezvous_op;
200 xprt->xp_port = ntohs(addr.sin_port);
201 xprt->xp_sock = sock;
202 xprt_register(xprt);
203 return (xprt);
204 }
205
206 /*
207 * Like svtcp_create(), except the routine takes any *open* UNIX file
208 * descriptor as its first input.
209 */
210 SVCXPRT *
211 svcfd_create(fd, sendsize, recvsize)
212 int fd;
213 u_int sendsize;
214 u_int recvsize;
215 {
216
217 return (makefd_xprt(fd, sendsize, recvsize));
218 }
219
220 static SVCXPRT *
221 makefd_xprt(fd, sendsize, recvsize)
222 int fd;
223 u_int sendsize;
224 u_int recvsize;
225 {
226 register SVCXPRT *xprt;
227 register struct tcp_conn *cd;
228
229 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
230 if (xprt == (SVCXPRT *)NULL) {
231 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
232 goto done;
233 }
234 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
235 if (cd == (struct tcp_conn *)NULL) {
236 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
237 mem_free((char *) xprt, sizeof(SVCXPRT));
238 xprt = (SVCXPRT *)NULL;
239 goto done;
240 }
241 cd->strm_stat = XPRT_IDLE;
242 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
243 (caddr_t)xprt, readtcp, writetcp);
244 xprt->xp_p2 = NULL;
245 xprt->xp_p1 = (caddr_t)cd;
246 xprt->xp_verf.oa_base = cd->verf_body;
247 xprt->xp_addrlen = 0;
248 xprt->xp_ops = &svctcp_op; /* truely deals with calls */
249 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
250 xprt->xp_sock = fd;
251 xprt_register(xprt);
252 done:
253 return (xprt);
254 }
255
256 static bool_t
257 rendezvous_abort()
258 {
259 abort();
260 return (FALSE);
261 }
262
263 static bool_t
264 rendezvous_request(xprt)
265 register SVCXPRT *xprt;
266 {
267 int sock;
268 struct tcp_rendezvous *r;
269 struct sockaddr_in addr;
270 int len;
271
272 r = (struct tcp_rendezvous *)xprt->xp_p1;
273 again:
274 len = sizeof(struct sockaddr_in);
275 if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
276 &len)) < 0) {
277 if (errno == EINTR)
278 goto again;
279 return (FALSE);
280 }
281 /*
282 * make a new transporter (re-uses xprt)
283 */
284 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
285 xprt->xp_raddr = addr;
286 xprt->xp_addrlen = len;
287 return (FALSE); /* there is never an rpc msg to be processed */
288 }
289
290 static enum xprt_stat
291 rendezvous_stat()
292 {
293
294 return (XPRT_IDLE);
295 }
296
297 static void
298 svctcp_destroy(xprt)
299 register SVCXPRT *xprt;
300 {
301 register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
302
303 xprt_unregister(xprt);
304 (void)close(xprt->xp_sock);
305 if (xprt->xp_port != 0) {
306 /* a rendezvouser socket */
307 xprt->xp_port = 0;
308 } else {
309 /* an actual connection socket */
310 XDR_DESTROY(&(cd->xdrs));
311 }
312 mem_free((caddr_t)cd, sizeof(struct tcp_conn));
313 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
314 }
315
316 /*
317 * All read operations timeout after 35 seconds.
318 * A timeout is fatal for the connection.
319 */
320 static struct timeval wait_per_try = { 35, 0 };
321
322 extern int svc_maxfd;
323
324 /*
325 * reads data from the tcp conection.
326 * any error is fatal and the connection is closed.
327 * (And a read of zero bytes is a half closed stream => error.)
328 */
329 static int
330 readtcp(xprt, buf, len)
331 register SVCXPRT *xprt;
332 caddr_t buf;
333 register int len;
334 {
335 register int sock = xprt->xp_sock;
336 fd_set readfds;
337 bool_t ready = FALSE;
338
339 do
340 {
341 FD_COPY(&svc_fdset, &readfds);
342 FD_SET(sock, &readfds);
343 if (select(max(svc_maxfd, sock) + 1, &readfds, NULL, NULL, &wait_per_try) <= 0)
344 {
345 if (errno == EINTR) continue;
346 goto fatal_err;
347 }
348 else if (FD_ISSET(sock, &readfds))
349 {
350 ready = TRUE;
351 }
352 else
353 {
354 svc_getreqset(&readfds);
355 }
356
357 } while (!ready);
358
359 if ((len = read(sock, buf, len)) > 0) return len;
360
361 fatal_err:
362 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
363 return -1;
364 }
365
366 /*
367 * writes data to the tcp connection.
368 * Any error is fatal and the connection is closed.
369 */
370 static int
371 writetcp(xprt, buf, len)
372 register SVCXPRT *xprt;
373 caddr_t buf;
374 int len;
375 {
376 register int i, cnt;
377
378 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
379 if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
380 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
381 XPRT_DIED;
382 return (-1);
383 }
384 }
385 return (len);
386 }
387
388 static enum xprt_stat
389 svctcp_stat(xprt)
390 SVCXPRT *xprt;
391 {
392 register struct tcp_conn *cd =
393 (struct tcp_conn *)(xprt->xp_p1);
394
395 if (cd->strm_stat == XPRT_DIED)
396 return (XPRT_DIED);
397 if (! xdrrec_eof(&(cd->xdrs)))
398 return (XPRT_MOREREQS);
399 return (XPRT_IDLE);
400 }
401
402 static bool_t
403 svctcp_recv(xprt, msg)
404 SVCXPRT *xprt;
405 register struct rpc_msg *msg;
406 {
407 register struct tcp_conn *cd =
408 (struct tcp_conn *)(xprt->xp_p1);
409 register XDR *xdrs = &(cd->xdrs);
410
411 xdrs->x_op = XDR_DECODE;
412 (void)xdrrec_skiprecord(xdrs);
413 if (xdr_callmsg(xdrs, msg)) {
414 cd->x_id = msg->rm_xid;
415 return (TRUE);
416 }
417 return (FALSE);
418 }
419
420 static bool_t
421 svctcp_getargs(xprt, xdr_args, args_ptr)
422 SVCXPRT *xprt;
423 xdrproc_t xdr_args;
424 caddr_t args_ptr;
425 {
426
427 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
428 }
429
430 static bool_t
431 svctcp_freeargs(xprt, xdr_args, args_ptr)
432 SVCXPRT *xprt;
433 xdrproc_t xdr_args;
434 caddr_t args_ptr;
435 {
436 register XDR *xdrs =
437 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
438
439 xdrs->x_op = XDR_FREE;
440 return ((*xdr_args)(xdrs, args_ptr));
441 }
442
443 static bool_t
444 svctcp_reply(xprt, msg)
445 SVCXPRT *xprt;
446 register struct rpc_msg *msg;
447 {
448 register struct tcp_conn *cd =
449 (struct tcp_conn *)(xprt->xp_p1);
450 register XDR *xdrs = &(cd->xdrs);
451 register bool_t stat;
452
453 xdrs->x_op = XDR_ENCODE;
454 msg->rm_xid = cd->x_id;
455 stat = xdr_replymsg(xdrs, msg);
456 (void)xdrrec_endofrecord(xdrs, TRUE);
457 return (stat);
458 }