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