Libinfo-78.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.2 1999/10/14 21:56:54 wsanchez 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 <rpc/rpc.h>
71 #include <sys/socket.h>
72 #include <errno.h>
73 extern bool_t abort();
74 extern errno;
75
76 /*
77 * Ops vector for TCP/IP based rpc service handle
78 */
79 static bool_t svctcp_recv();
80 static enum xprt_stat svctcp_stat();
81 static bool_t svctcp_getargs();
82 static bool_t svctcp_reply();
83 static bool_t svctcp_freeargs();
84 static void svctcp_destroy();
85
86 static struct xp_ops svctcp_op = {
87 svctcp_recv,
88 svctcp_stat,
89 svctcp_getargs,
90 svctcp_reply,
91 svctcp_freeargs,
92 svctcp_destroy
93 };
94
95 /*
96 * Ops vector for TCP/IP rendezvous handler
97 */
98 static bool_t rendezvous_request();
99 static enum xprt_stat rendezvous_stat();
100
101 static struct xp_ops svctcp_rendezvous_op = {
102 rendezvous_request,
103 rendezvous_stat,
104 abort,
105 abort,
106 abort,
107 svctcp_destroy
108 };
109
110 static int readtcp(), writetcp();
111 static SVCXPRT *makefd_xprt();
112
113 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
114 u_int sendsize;
115 u_int recvsize;
116 };
117
118 struct tcp_conn { /* kept in xprt->xp_p1 */
119 enum xprt_stat strm_stat;
120 u_long x_id;
121 XDR xdrs;
122 char verf_body[MAX_AUTH_BYTES];
123 };
124
125 /*
126 * Usage:
127 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
128 *
129 * Creates, registers, and returns a (rpc) tcp based transporter.
130 * Once *xprt is initialized, it is registered as a transporter
131 * see (svc.h, xprt_register). This routine returns
132 * a NULL if a problem occurred.
133 *
134 * If sock<0 then a socket is created, else sock is used.
135 * If the socket, sock is not bound to a port then svctcp_create
136 * binds it to an arbitrary port. The routine then starts a tcp
137 * listener on the socket's associated port. In any (successful) case,
138 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
139 * associated port number.
140 *
141 * Since tcp streams do buffered io similar to stdio, the caller can specify
142 * how big the send and receive buffers are via the second and third parms;
143 * 0 => use the system default.
144 */
145 SVCXPRT *
146 svctcp_create(sock, sendsize, recvsize)
147 register int sock;
148 u_int sendsize;
149 u_int recvsize;
150 {
151 bool_t madesock = FALSE;
152 register SVCXPRT *xprt;
153 register struct tcp_rendezvous *r;
154 struct sockaddr_in addr;
155 int len = sizeof(struct sockaddr_in);
156
157 if (sock == RPC_ANYSOCK) {
158 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
159 perror("svctcp_.c - udp socket creation problem");
160 return ((SVCXPRT *)NULL);
161 }
162 madesock = TRUE;
163 }
164 bzero((char *)&addr, sizeof (addr));
165 addr.sin_family = AF_INET;
166 if (bindresvport(sock, &addr)) {
167 addr.sin_port = 0;
168 (void)bind(sock, (struct sockaddr *)&addr, len);
169 }
170 if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
171 (listen(sock, 2) != 0)) {
172 perror("svctcp_.c - cannot getsockname or listen");
173 if (madesock)
174 (void)close(sock);
175 return ((SVCXPRT *)NULL);
176 }
177 r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
178 if (r == NULL) {
179 (void) fprintf(stderr, "svctcp_create: out of memory\n");
180 return (NULL);
181 }
182 r->sendsize = sendsize;
183 r->recvsize = recvsize;
184 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
185 if (xprt == NULL) {
186 (void) fprintf(stderr, "svctcp_create: out of memory\n");
187 return (NULL);
188 }
189 xprt->xp_p2 = NULL;
190 xprt->xp_p1 = (caddr_t)r;
191 xprt->xp_verf = _null_auth;
192 xprt->xp_ops = &svctcp_rendezvous_op;
193 xprt->xp_port = ntohs(addr.sin_port);
194 xprt->xp_sock = sock;
195 xprt_register(xprt);
196 return (xprt);
197 }
198
199 /*
200 * Like svtcp_create(), except the routine takes any *open* UNIX file
201 * descriptor as its first input.
202 */
203 SVCXPRT *
204 svcfd_create(fd, sendsize, recvsize)
205 int fd;
206 u_int sendsize;
207 u_int recvsize;
208 {
209
210 return (makefd_xprt(fd, sendsize, recvsize));
211 }
212
213 static SVCXPRT *
214 makefd_xprt(fd, sendsize, recvsize)
215 int fd;
216 u_int sendsize;
217 u_int recvsize;
218 {
219 register SVCXPRT *xprt;
220 register struct tcp_conn *cd;
221
222 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
223 if (xprt == (SVCXPRT *)NULL) {
224 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
225 goto done;
226 }
227 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
228 if (cd == (struct tcp_conn *)NULL) {
229 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
230 mem_free((char *) xprt, sizeof(SVCXPRT));
231 xprt = (SVCXPRT *)NULL;
232 goto done;
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; /* truely deals with calls */
242 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
243 xprt->xp_sock = fd;
244 xprt_register(xprt);
245 done:
246 return (xprt);
247 }
248
249 static bool_t
250 rendezvous_request(xprt)
251 register SVCXPRT *xprt;
252 {
253 int sock;
254 struct tcp_rendezvous *r;
255 struct sockaddr_in addr;
256 int len;
257
258 r = (struct tcp_rendezvous *)xprt->xp_p1;
259 again:
260 len = sizeof(struct sockaddr_in);
261 if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
262 &len)) < 0) {
263 if (errno == EINTR)
264 goto again;
265 return (FALSE);
266 }
267 /*
268 * make a new transporter (re-uses xprt)
269 */
270 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
271 xprt->xp_raddr = addr;
272 xprt->xp_addrlen = len;
273 return (FALSE); /* there is never an rpc msg to be processed */
274 }
275
276 static enum xprt_stat
277 rendezvous_stat()
278 {
279
280 return (XPRT_IDLE);
281 }
282
283 static void
284 svctcp_destroy(xprt)
285 register SVCXPRT *xprt;
286 {
287 register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
288
289 xprt_unregister(xprt);
290 (void)close(xprt->xp_sock);
291 if (xprt->xp_port != 0) {
292 /* a rendezvouser socket */
293 xprt->xp_port = 0;
294 } else {
295 /* an actual connection socket */
296 XDR_DESTROY(&(cd->xdrs));
297 }
298 mem_free((caddr_t)cd, sizeof(struct tcp_conn));
299 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
300 }
301
302 /*
303 * All read operations timeout after 35 seconds.
304 * A timeout is fatal for the connection.
305 */
306 static struct timeval wait_per_try = { 35, 0 };
307
308 /*
309 * reads data from the tcp conection.
310 * any error is fatal and the connection is closed.
311 * (And a read of zero bytes is a half closed stream => error.)
312 */
313 static int
314 readtcp(xprt, buf, len)
315 register SVCXPRT *xprt;
316 caddr_t buf;
317 register int len;
318 {
319 register int sock = xprt->xp_sock;
320 fd_set mask;
321 fd_set readfds;
322
323 FD_ZERO(&mask);
324 FD_SET(sock, &mask);
325 do {
326 readfds = mask;
327 if (select(sock+1, &readfds, (int*)NULL, (int*)NULL,
328 &wait_per_try) <= 0) {
329 if (errno == EINTR) {
330 continue;
331 }
332 goto fatal_err;
333 }
334 } while (!FD_ISSET(sock, &readfds));
335 if ((len = read(sock, buf, len)) > 0) {
336 return (len);
337 }
338 fatal_err:
339 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
340 return (-1);
341 }
342
343 /*
344 * writes data to the tcp connection.
345 * Any error is fatal and the connection is closed.
346 */
347 static int
348 writetcp(xprt, buf, len)
349 register SVCXPRT *xprt;
350 caddr_t buf;
351 int len;
352 {
353 register int i, cnt;
354
355 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
356 if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
357 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
358 XPRT_DIED;
359 return (-1);
360 }
361 }
362 return (len);
363 }
364
365 static enum xprt_stat
366 svctcp_stat(xprt)
367 SVCXPRT *xprt;
368 {
369 register struct tcp_conn *cd =
370 (struct tcp_conn *)(xprt->xp_p1);
371
372 if (cd->strm_stat == XPRT_DIED)
373 return (XPRT_DIED);
374 if (! xdrrec_eof(&(cd->xdrs)))
375 return (XPRT_MOREREQS);
376 return (XPRT_IDLE);
377 }
378
379 static bool_t
380 svctcp_recv(xprt, msg)
381 SVCXPRT *xprt;
382 register struct rpc_msg *msg;
383 {
384 register struct tcp_conn *cd =
385 (struct tcp_conn *)(xprt->xp_p1);
386 register XDR *xdrs = &(cd->xdrs);
387
388 xdrs->x_op = XDR_DECODE;
389 (void)xdrrec_skiprecord(xdrs);
390 if (xdr_callmsg(xdrs, msg)) {
391 cd->x_id = msg->rm_xid;
392 return (TRUE);
393 }
394 return (FALSE);
395 }
396
397 static bool_t
398 svctcp_getargs(xprt, xdr_args, args_ptr)
399 SVCXPRT *xprt;
400 xdrproc_t xdr_args;
401 caddr_t args_ptr;
402 {
403
404 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
405 }
406
407 static bool_t
408 svctcp_freeargs(xprt, xdr_args, args_ptr)
409 SVCXPRT *xprt;
410 xdrproc_t xdr_args;
411 caddr_t args_ptr;
412 {
413 register XDR *xdrs =
414 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
415
416 xdrs->x_op = XDR_FREE;
417 return ((*xdr_args)(xdrs, args_ptr));
418 }
419
420 static bool_t
421 svctcp_reply(xprt, msg)
422 SVCXPRT *xprt;
423 register struct rpc_msg *msg;
424 {
425 register struct tcp_conn *cd =
426 (struct tcp_conn *)(xprt->xp_p1);
427 register XDR *xdrs = &(cd->xdrs);
428 register bool_t stat;
429
430 xdrs->x_op = XDR_ENCODE;
431 msg->rm_xid = cd->x_id;
432 stat = xdr_replymsg(xdrs, msg);
433 (void)xdrrec_endofrecord(xdrs, TRUE);
434 return (stat);
435 }