]> git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/clnt_tcp.c
Libinfo-173.tar.gz
[apple/libinfo.git] / rpc.subproj / clnt_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: @(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";*/
56 /*static char *sccsid = "from: @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";*/
57 static char *rcsid = "$Id: clnt_tcp.c,v 1.4 2002/03/15 22:07:48 majka Exp $";
58 #endif
59
60 /*
61 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
62 *
63 * Copyright (C) 1984, Sun Microsystems, Inc.
64 *
65 * TCP based RPC supports 'batched calls'.
66 * A sequence of calls may be batched-up in a send buffer. The rpc call
67 * return immediately to the client even though the call was not necessarily
68 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
69 * the rpc timeout value is zero (see clnt.h, rpc).
70 *
71 * Clients should NOT casually batch calls that in fact return results; that is,
72 * the server side should be aware that a call is batched and not produce any
73 * return message. Batched calls that produce many result messages can
74 * deadlock (netlock) the client and the server....
75 *
76 * Now go hang yourself.
77 */
78
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <rpc/rpc.h>
84 #include <sys/socket.h>
85 #include <sys/fcntl.h>
86 #include <netdb.h>
87 #include <errno.h>
88 #include <rpc/pmap_clnt.h>
89
90 #define MCALL_MSG_SIZE 24
91
92 extern int errno;
93
94 extern int bindresvport();
95 extern bool_t xdr_opaque_auth();
96
97 static int readtcp();
98 static int writetcp();
99
100 static enum clnt_stat clnttcp_call();
101 static void clnttcp_abort();
102 static void clnttcp_geterr();
103 static bool_t clnttcp_freeres();
104 static bool_t clnttcp_control();
105 static void clnttcp_destroy();
106
107 static struct clnt_ops tcp_ops = {
108 clnttcp_call,
109 clnttcp_abort,
110 clnttcp_geterr,
111 clnttcp_freeres,
112 clnttcp_destroy,
113 clnttcp_control
114 };
115
116 struct ct_data {
117 int ct_sock;
118 bool_t ct_closeit;
119 struct timeval ct_wait;
120 bool_t ct_waitset; /* wait set by clnt_control? */
121 struct sockaddr_in ct_addr;
122 struct rpc_err ct_error;
123 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
124 u_int ct_mpos; /* pos after marshal */
125 XDR ct_xdrs;
126 };
127
128 /*
129 * Create a client handle for a tcp/ip connection.
130 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
131 * connected to raddr. If *sockp non-negative then
132 * raddr is ignored. The rpc/tcp package does buffering
133 * similar to stdio, so the client must pick send and receive buffer sizes,];
134 * 0 => use the default.
135 * If raddr->sin_port is 0, then a binder on the remote machine is
136 * consulted for the right port number.
137 * NB: *sockp is copied into a private area.
138 * NB: It is the clients responsibility to close *sockp.
139 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
140 * something more useful.
141 */
142 CLIENT *
143 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
144 struct sockaddr_in *raddr;
145 u_long prog;
146 u_long vers;
147 register int *sockp;
148 u_int sendsz;
149 u_int recvsz;
150 {
151 CLIENT *h;
152 register struct ct_data *ct = NULL;
153 struct timeval now;
154 struct rpc_msg call_msg;
155 int rfd;
156
157 h = (CLIENT *)mem_alloc(sizeof(*h));
158 if (h == NULL) {
159 (void)fprintf(stderr, "clnttcp_create: out of memory\n");
160 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
161 rpc_createerr.cf_error.re_errno = errno;
162 goto fooy;
163 }
164 ct = (struct ct_data *)mem_alloc(sizeof(*ct));
165 if (ct == NULL) {
166 (void)fprintf(stderr, "clnttcp_create: out of memory\n");
167 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
168 rpc_createerr.cf_error.re_errno = errno;
169 goto fooy;
170 }
171
172 /*
173 * If no port number given ask the pmap for one
174 */
175 if (raddr->sin_port == 0) {
176 u_short port;
177 if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
178 mem_free((caddr_t)ct, sizeof(struct ct_data));
179 mem_free((caddr_t)h, sizeof(CLIENT));
180 return ((CLIENT *)NULL);
181 }
182 raddr->sin_port = htons(port);
183 }
184
185 /*
186 * If no socket given, open one
187 */
188 if (*sockp < 0) {
189 *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
190 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
191 if ((*sockp < 0)
192 || (connect(*sockp, (struct sockaddr *)raddr,
193 sizeof(*raddr)) < 0)) {
194 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
195 rpc_createerr.cf_error.re_errno = errno;
196 (void)close(*sockp);
197 goto fooy;
198 }
199 ct->ct_closeit = TRUE;
200 } else {
201 ct->ct_closeit = FALSE;
202 }
203
204 /*
205 * Set up private data struct
206 */
207 ct->ct_sock = *sockp;
208 ct->ct_wait.tv_usec = 0;
209 ct->ct_waitset = FALSE;
210 ct->ct_addr = *raddr;
211
212 /*
213 * Initialize call message
214 */
215 rfd = open("/dev/random", O_RDONLY, 0);
216 if ((rfd < 0) || (read(rfd, &call_msg.rm_xid, sizeof(call_msg.rm_xid)) != sizeof(call_msg.rm_xid)))
217 {
218 gettimeofday(&now, (struct timezone *)0);
219 call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
220 }
221 if (rfd > 0) close(rfd);
222
223 call_msg.rm_direction = CALL;
224 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
225 call_msg.rm_call.cb_prog = prog;
226 call_msg.rm_call.cb_vers = vers;
227
228 /*
229 * pre-serialize the staic part of the call msg and stash it away
230 */
231 xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
232 XDR_ENCODE);
233 if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
234 if (ct->ct_closeit) {
235 (void)close(*sockp);
236 }
237 goto fooy;
238 }
239 ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
240 XDR_DESTROY(&(ct->ct_xdrs));
241
242 /*
243 * Create a client handle which uses xdrrec for serialization
244 * and authnone for authentication.
245 */
246 xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
247 (caddr_t)ct, readtcp, writetcp);
248 h->cl_ops = &tcp_ops;
249 h->cl_private = (caddr_t) ct;
250 h->cl_auth = authnone_create();
251 return (h);
252
253 fooy:
254 /*
255 * Something goofed, free stuff and barf
256 */
257 mem_free((caddr_t)ct, sizeof(struct ct_data));
258 mem_free((caddr_t)h, sizeof(CLIENT));
259 return ((CLIENT *)NULL);
260 }
261
262 static enum clnt_stat
263 clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
264 register CLIENT *h;
265 u_long proc;
266 xdrproc_t xdr_args;
267 caddr_t args_ptr;
268 xdrproc_t xdr_results;
269 caddr_t results_ptr;
270 struct timeval timeout;
271 {
272 register struct ct_data *ct = (struct ct_data *) h->cl_private;
273 register XDR *xdrs = &(ct->ct_xdrs);
274 struct rpc_msg reply_msg;
275 u_long x_id;
276 u_long *msg_x_id = (u_long *)(ct->ct_mcall); /* yuk */
277 register bool_t shipnow;
278 int refreshes = 2;
279
280 if (!ct->ct_waitset) {
281 ct->ct_wait = timeout;
282 }
283
284 shipnow =
285 (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
286 && timeout.tv_usec == 0) ? FALSE : TRUE;
287
288 call_again:
289 xdrs->x_op = XDR_ENCODE;
290 ct->ct_error.re_status = RPC_SUCCESS;
291 x_id = ntohl(--(*msg_x_id));
292 if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
293 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
294 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
295 (! (*xdr_args)(xdrs, args_ptr))) {
296 if (ct->ct_error.re_status == RPC_SUCCESS)
297 ct->ct_error.re_status = RPC_CANTENCODEARGS;
298 (void)xdrrec_endofrecord(xdrs, TRUE);
299 return (ct->ct_error.re_status);
300 }
301 if (! xdrrec_endofrecord(xdrs, shipnow))
302 return (ct->ct_error.re_status = RPC_CANTSEND);
303 if (! shipnow)
304 return (RPC_SUCCESS);
305 /*
306 * Hack to provide rpc-based message passing
307 */
308 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
309 return(ct->ct_error.re_status = RPC_TIMEDOUT);
310 }
311
312
313 /*
314 * Keep receiving until we get a valid transaction id
315 */
316 xdrs->x_op = XDR_DECODE;
317 while (TRUE) {
318 reply_msg.acpted_rply.ar_verf = _null_auth;
319 reply_msg.acpted_rply.ar_results.where = NULL;
320 reply_msg.acpted_rply.ar_results.proc = xdr_void;
321 if (! xdrrec_skiprecord(xdrs))
322 return (ct->ct_error.re_status);
323 /* now decode and validate the response header */
324 if (! xdr_replymsg(xdrs, &reply_msg)) {
325 if (ct->ct_error.re_status == RPC_SUCCESS)
326 continue;
327 return (ct->ct_error.re_status);
328 }
329 if (reply_msg.rm_xid == x_id)
330 break;
331 }
332
333 /*
334 * process header
335 */
336 _seterr_reply(&reply_msg, &(ct->ct_error));
337 if (ct->ct_error.re_status == RPC_SUCCESS) {
338 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
339 ct->ct_error.re_status = RPC_AUTHERROR;
340 ct->ct_error.re_why = AUTH_INVALIDRESP;
341 } else if (! (*xdr_results)(xdrs, results_ptr)) {
342 if (ct->ct_error.re_status == RPC_SUCCESS)
343 ct->ct_error.re_status = RPC_CANTDECODERES;
344 }
345 /* free verifier ... */
346 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
347 xdrs->x_op = XDR_FREE;
348 (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
349 }
350 } /* end successful completion */
351 else {
352 /* maybe our credentials need to be refreshed ... */
353 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
354 goto call_again;
355 } /* end of unsuccessful completion */
356 return (ct->ct_error.re_status);
357 }
358
359 static void
360 clnttcp_geterr(h, errp)
361 CLIENT *h;
362 struct rpc_err *errp;
363 {
364 register struct ct_data *ct =
365 (struct ct_data *) h->cl_private;
366
367 *errp = ct->ct_error;
368 }
369
370 static bool_t
371 clnttcp_freeres(cl, xdr_res, res_ptr)
372 CLIENT *cl;
373 xdrproc_t xdr_res;
374 caddr_t res_ptr;
375 {
376 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
377 register XDR *xdrs = &(ct->ct_xdrs);
378
379 xdrs->x_op = XDR_FREE;
380 return ((*xdr_res)(xdrs, res_ptr));
381 }
382
383 static void
384 clnttcp_abort()
385 {
386 }
387
388 static bool_t
389 clnttcp_control(cl, request, info)
390 CLIENT *cl;
391 int request;
392 char *info;
393 {
394 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
395
396 switch (request) {
397 case CLSET_TIMEOUT:
398 ct->ct_wait = *(struct timeval *)info;
399 ct->ct_waitset = TRUE;
400 break;
401 case CLGET_TIMEOUT:
402 *(struct timeval *)info = ct->ct_wait;
403 break;
404 case CLGET_SERVER_ADDR:
405 *(struct sockaddr_in *)info = ct->ct_addr;
406 break;
407 default:
408 return (FALSE);
409 }
410 return (TRUE);
411 }
412
413
414 static void
415 clnttcp_destroy(h)
416 CLIENT *h;
417 {
418 register struct ct_data *ct =
419 (struct ct_data *) h->cl_private;
420
421 if (ct->ct_closeit) {
422 (void)close(ct->ct_sock);
423 }
424 XDR_DESTROY(&(ct->ct_xdrs));
425 mem_free((caddr_t)ct, sizeof(struct ct_data));
426 mem_free((caddr_t)h, sizeof(CLIENT));
427 }
428
429 /*
430 * Interface between xdr serializer and tcp connection.
431 * Behaves like the system calls, read & write, but keeps some error state
432 * around for the rpc level.
433 */
434 static int
435 readtcp(ct, buf, len)
436 register struct ct_data *ct;
437 caddr_t buf;
438 register int len;
439 {
440 fd_set mask;
441 fd_set readfds;
442
443 if (len == 0)
444 return (0);
445 FD_ZERO(&mask);
446 FD_SET(ct->ct_sock, &mask);
447 while (TRUE) {
448 readfds = mask;
449 switch (select(ct->ct_sock+1, &readfds, NULL, NULL,
450 &(ct->ct_wait))) {
451 case 0:
452 ct->ct_error.re_status = RPC_TIMEDOUT;
453 return (-1);
454
455 case -1:
456 if (errno == EINTR)
457 continue;
458 ct->ct_error.re_status = RPC_CANTRECV;
459 ct->ct_error.re_errno = errno;
460 return (-1);
461 }
462 break;
463 }
464 switch (len = read(ct->ct_sock, buf, len)) {
465
466 case 0:
467 /* premature eof */
468 ct->ct_error.re_errno = ECONNRESET;
469 ct->ct_error.re_status = RPC_CANTRECV;
470 len = -1; /* it's really an error */
471 break;
472
473 case -1:
474 ct->ct_error.re_errno = errno;
475 ct->ct_error.re_status = RPC_CANTRECV;
476 break;
477 }
478 return (len);
479 }
480
481 static int
482 writetcp(ct, buf, len)
483 struct ct_data *ct;
484 caddr_t buf;
485 int len;
486 {
487 register int i, cnt;
488
489 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
490 if ((i = write(ct->ct_sock, buf, cnt)) == -1) {
491 ct->ct_error.re_errno = errno;
492 ct->ct_error.re_status = RPC_CANTSEND;
493 return (-1);
494 }
495 }
496 return (len);
497 }