2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)clnt_unix.c 1.37 87/10/05 Copyr 1984 Sun Micro
30 * @(#)clnt_unix.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/clnt_unix.c,v 1.5 2000/01/27 23:06:37 jasone Exp $
32 * $DragonFly: src/lib/libc/rpc/clnt_unix.c,v 1.4 2005/01/31 22:29:38 dillon Exp $
36 * clnt_unix.c, Implements a AF_UNIX based, client side RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * AF_UNIX based RPC supports 'batched calls'.
41 * A sequence of calls may be batched-up in a send buffer. The rpc call
42 * return immediately to the client even though the call was not necessarily
43 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
44 * the rpc timeout value is zero (see clnt.h, rpc).
46 * Clients should NOT casually batch calls that in fact return results; that is,
47 * the server side should be aware that a call is batched and not produce any
48 * return message. Batched calls that produce many result messages can
49 * deadlock (netlock) the client and the server....
51 * Now go hang yourself.
54 #include "namespace.h"
61 #include <sys/socket.h>
65 #include <rpc/pmap_clnt.h>
66 #include "un-namespace.h"
68 #define MCALL_MSG_SIZE 24
70 static int readunix();
71 static int writeunix();
73 static enum clnt_stat clntunix_call();
74 static void clntunix_abort();
75 static void clntunix_geterr();
76 static bool_t clntunix_freeres();
77 static bool_t clntunix_control();
78 static void clntunix_destroy();
80 static struct clnt_ops unix_ops = {
92 struct timeval ct_wait;
93 bool_t ct_waitset; /* wait set by clnt_control? */
94 struct sockaddr_un ct_addr;
95 struct rpc_err ct_error;
96 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
97 u_int ct_mpos; /* pos after marshal */
102 * Create a client handle for a unix/ip connection.
103 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
104 * connected to raddr. If *sockp non-negative then
105 * raddr is ignored. The rpc/unix package does buffering
106 * similar to stdio, so the client must pick send and receive buffer sizes,];
107 * 0 => use the default.
108 * If raddr->sin_port is 0, then a binder on the remote machine is
109 * consulted for the right port number.
110 * NB: *sockp is copied into a private area.
111 * NB: It is the clients responsibility to close *sockp.
112 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
113 * something more useful.
116 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
117 struct sockaddr_un *raddr;
125 struct ct_data *ct = NULL;
127 struct rpc_msg call_msg;
128 static u_int32_t disrupt;
132 disrupt = (u_int32_t)(long)raddr;
134 h = (CLIENT *)mem_alloc(sizeof(*h));
136 (void)fprintf(stderr, "clntunix_create: out of memory\n");
137 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
138 rpc_createerr.cf_error.re_errno = errno;
141 ct = (struct ct_data *)mem_alloc(sizeof(*ct));
143 (void)fprintf(stderr, "clntunix_create: out of memory\n");
144 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
145 rpc_createerr.cf_error.re_errno = errno;
150 * If no socket given, open one
153 *sockp = _socket(AF_UNIX, SOCK_STREAM, 0);
154 len = strlen(raddr->sun_path) + sizeof(raddr->sun_family) +
155 sizeof(raddr->sun_len) + 1;
156 raddr->sun_len = len;
158 || (_connect(*sockp, (struct sockaddr *)raddr, len) < 0)) {
159 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
160 rpc_createerr.cf_error.re_errno = errno;
162 (void)_close(*sockp);
165 ct->ct_closeit = TRUE;
167 ct->ct_closeit = FALSE;
171 * Set up private data struct
173 ct->ct_sock = *sockp;
174 ct->ct_wait.tv_usec = 0;
175 ct->ct_waitset = FALSE;
176 ct->ct_addr = *raddr;
179 * Initialize call message
181 (void)gettimeofday(&now, (struct timezone *)0);
182 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
183 call_msg.rm_direction = CALL;
184 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
185 call_msg.rm_call.cb_prog = prog;
186 call_msg.rm_call.cb_vers = vers;
189 * pre-serialize the static part of the call msg and stash it away
191 xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
193 if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
194 if (ct->ct_closeit) {
195 (void)_close(*sockp);
199 ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
200 XDR_DESTROY(&(ct->ct_xdrs));
203 * Create a client handle which uses xdrrec for serialization
204 * and authnone for authentication.
206 xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
207 (caddr_t)ct, readunix, writeunix);
208 h->cl_ops = &unix_ops;
209 h->cl_private = (caddr_t) ct;
210 h->cl_auth = authnone_create();
215 * Something goofed, free stuff and barf
218 mem_free((caddr_t)ct, sizeof(struct ct_data));
220 mem_free((caddr_t)h, sizeof(CLIENT));
221 return ((CLIENT *)NULL);
224 static enum clnt_stat
225 clntunix_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
230 xdrproc_t xdr_results;
232 struct timeval timeout;
234 struct ct_data *ct = (struct ct_data *) h->cl_private;
235 XDR *xdrs = &(ct->ct_xdrs);
236 struct rpc_msg reply_msg;
238 u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */
242 if (!ct->ct_waitset) {
243 ct->ct_wait = timeout;
247 (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
248 && timeout.tv_usec == 0) ? FALSE : TRUE;
251 xdrs->x_op = XDR_ENCODE;
252 ct->ct_error.re_status = RPC_SUCCESS;
253 x_id = ntohl(--(*msg_x_id));
254 if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
255 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
256 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
257 (! (*xdr_args)(xdrs, args_ptr))) {
258 if (ct->ct_error.re_status == RPC_SUCCESS)
259 ct->ct_error.re_status = RPC_CANTENCODEARGS;
260 (void)xdrrec_endofrecord(xdrs, TRUE);
261 return (ct->ct_error.re_status);
263 if (! xdrrec_endofrecord(xdrs, shipnow))
264 return (ct->ct_error.re_status = RPC_CANTSEND);
266 return (RPC_SUCCESS);
268 * Hack to provide rpc-based message passing
270 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
271 return(ct->ct_error.re_status = RPC_TIMEDOUT);
276 * Keep receiving until we get a valid transaction id
278 xdrs->x_op = XDR_DECODE;
280 reply_msg.acpted_rply.ar_verf = _null_auth;
281 reply_msg.acpted_rply.ar_results.where = NULL;
282 reply_msg.acpted_rply.ar_results.proc = xdr_void;
283 if (! xdrrec_skiprecord(xdrs))
284 return (ct->ct_error.re_status);
285 /* now decode and validate the response header */
286 if (! xdr_replymsg(xdrs, &reply_msg)) {
287 if (ct->ct_error.re_status == RPC_SUCCESS)
289 return (ct->ct_error.re_status);
291 if (reply_msg.rm_xid == x_id)
298 _seterr_reply(&reply_msg, &(ct->ct_error));
299 if (ct->ct_error.re_status == RPC_SUCCESS) {
300 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
301 ct->ct_error.re_status = RPC_AUTHERROR;
302 ct->ct_error.re_why = AUTH_INVALIDRESP;
303 } else if (! (*xdr_results)(xdrs, results_ptr)) {
304 if (ct->ct_error.re_status == RPC_SUCCESS)
305 ct->ct_error.re_status = RPC_CANTDECODERES;
307 /* free verifier ... */
308 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
309 xdrs->x_op = XDR_FREE;
310 (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
312 } /* end successful completion */
314 /* maybe our credentials need to be refreshed ... */
315 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
317 } /* end of unsuccessful completion */
318 return (ct->ct_error.re_status);
322 clntunix_geterr(h, errp)
324 struct rpc_err *errp;
327 (struct ct_data *) h->cl_private;
329 *errp = ct->ct_error;
333 clntunix_freeres(cl, xdr_res, res_ptr)
338 struct ct_data *ct = (struct ct_data *)cl->cl_private;
339 XDR *xdrs = &(ct->ct_xdrs);
341 xdrs->x_op = XDR_FREE;
342 return ((*xdr_res)(xdrs, res_ptr));
352 clntunix_control(cl, request, info)
357 struct ct_data *ct = (struct ct_data *)cl->cl_private;
363 ct->ct_closeit = TRUE;
365 case CLSET_FD_NCLOSE:
366 ct->ct_closeit = FALSE;
371 tv = (struct timeval *)info;
372 ct->ct_wait.tv_sec = tv->tv_sec;
373 ct->ct_wait.tv_usec = tv->tv_usec;
374 ct->ct_waitset = TRUE;
379 *(struct timeval *)info = ct->ct_wait;
381 case CLGET_SERVER_ADDR:
384 *(struct sockaddr_un *)info = ct->ct_addr;
389 *(int *)info = ct->ct_sock;
393 * use the knowledge that xid is the
394 * first element in the call structure *.
395 * This will get the xid of the PREVIOUS call
399 *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall);
402 /* This will set the xid of the NEXT call */
405 *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1);
406 /* decrement by 1 as clntunix_call() increments once */
409 * This RELIES on the information that, in the call body,
410 * the version number field is the fifth field from the
411 * begining of the RPC header. MUST be changed if the
412 * call_struct is changed
416 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
417 4 * BYTES_PER_XDR_UNIT));
422 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
423 = htonl(*(u_long *)info);
427 * This RELIES on the information that, in the call body,
428 * the program number field is the field from the
429 * begining of the RPC header. MUST be changed if the
430 * call_struct is changed
434 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
435 3 * BYTES_PER_XDR_UNIT));
440 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
441 = htonl(*(u_long *)info);
443 case CLGET_LOCAL_ADDR:
444 len = sizeof(struct sockaddr);
445 if (_getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0)
448 case CLGET_RETRY_TIMEOUT:
449 case CLSET_RETRY_TIMEOUT:
452 case CLSET_PUSH_TIMOD:
453 case CLSET_POP_TIMOD:
466 (struct ct_data *) h->cl_private;
468 if (ct->ct_closeit) {
469 (void)_close(ct->ct_sock);
471 XDR_DESTROY(&(ct->ct_xdrs));
472 mem_free((caddr_t)ct, sizeof(struct ct_data));
473 mem_free((caddr_t)h, sizeof(CLIENT));
477 * _read() and _write() are replaced with _recvmsg()/_sendmsg() so that
478 * we can pass ancillary control data. In this case, the data constists
479 * of credential information which the kernel will fill in for us.
480 * XXX: This code is specific to FreeBSD and will not work on other
481 * platforms without the requisite kernel modifications.
485 struct cmsgcred cmcred;
488 static int __msgread(sock, buf, cnt)
497 bzero((char *)&cm, sizeof(cm));
498 iov[0].iov_base = buf;
499 iov[0].iov_len = cnt;
505 msg.msg_control = (caddr_t)&cm;
506 msg.msg_controllen = sizeof(struct cmessage);
509 return(_recvmsg(sock, &msg, 0));
512 static int __msgwrite(sock, buf, cnt)
521 bzero((char *)&cm, sizeof(cm));
522 iov[0].iov_base = buf;
523 iov[0].iov_len = cnt;
525 cm.cmsg.cmsg_type = SCM_CREDS;
526 cm.cmsg.cmsg_level = SOL_SOCKET;
527 cm.cmsg.cmsg_len = sizeof(struct cmessage);
533 msg.msg_control = (caddr_t)&cm;
534 msg.msg_controllen = sizeof(struct cmessage);
537 return(_sendmsg(sock, &msg, 0));
541 * Interface between xdr serializer and unix connection.
542 * Behaves like the system calls, read & write, but keeps some error state
543 * around for the rpc level.
546 readunix(ct, buf, len)
551 fd_set *fds, readfds;
552 struct timeval start, after, duration, delta, tmp, tv;
558 if (ct->ct_sock + 1 > FD_SETSIZE) {
559 int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask);
560 fds = (fd_set *)malloc(bytes);
563 memset(fds, 0, bytes);
569 gettimeofday(&start, NULL);
572 /* XXX we know the other bits are still clear */
573 FD_SET(ct->ct_sock, fds);
574 tv = delta; /* in case select writes back */
575 r = _select(ct->ct_sock+1, fds, NULL, NULL, &tv);
578 gettimeofday(&after, NULL);
579 timersub(&start, &after, &duration);
580 timersub(&delta, &duration, &tmp);
582 if (delta.tv_sec < 0 || !timerisset(&delta))
589 ct->ct_error.re_status = RPC_TIMEDOUT;
597 ct->ct_error.re_status = RPC_CANTRECV;
598 ct->ct_error.re_errno = save_errno;
603 switch (len = __msgread(ct->ct_sock, buf, len)) {
607 ct->ct_error.re_errno = ECONNRESET;
608 ct->ct_error.re_status = RPC_CANTRECV;
609 len = -1; /* it's really an error */
613 ct->ct_error.re_errno = errno;
614 ct->ct_error.re_status = RPC_CANTRECV;
621 writeunix(ct, buf, len)
628 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
629 if ((i = __msgwrite(ct->ct_sock, buf, cnt)) == -1) {
630 ct->ct_error.re_errno = errno;
631 ct->ct_error.re_status = RPC_CANTSEND;