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