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/libcr/rpc/Attic/clnt_unix.c,v 1.3 2004/10/25 19:38:25 drhodus 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.
60 #include <sys/socket.h>
64 #include <rpc/pmap_clnt.h>
66 #define MCALL_MSG_SIZE 24
68 static int readunix();
69 static int writeunix();
71 static enum clnt_stat clntunix_call();
72 static void clntunix_abort();
73 static void clntunix_geterr();
74 static bool_t clntunix_freeres();
75 static bool_t clntunix_control();
76 static void clntunix_destroy();
78 static struct clnt_ops unix_ops = {
90 struct timeval ct_wait;
91 bool_t ct_waitset; /* wait set by clnt_control? */
92 struct sockaddr_un ct_addr;
93 struct rpc_err ct_error;
94 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
95 u_int ct_mpos; /* pos after marshal */
100 * Create a client handle for a unix/ip connection.
101 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
102 * connected to raddr. If *sockp non-negative then
103 * raddr is ignored. The rpc/unix package does buffering
104 * similar to stdio, so the client must pick send and receive buffer sizes,];
105 * 0 => use the default.
106 * If raddr->sin_port is 0, then a binder on the remote machine is
107 * consulted for the right port number.
108 * NB: *sockp is copied into a private area.
109 * NB: It is the clients responsibility to close *sockp.
110 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
111 * something more useful.
114 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
115 struct sockaddr_un *raddr;
123 struct ct_data *ct = NULL;
125 struct rpc_msg call_msg;
126 static u_int32_t disrupt;
130 disrupt = (u_int32_t)(long)raddr;
132 h = (CLIENT *)mem_alloc(sizeof(*h));
134 (void)fprintf(stderr, "clntunix_create: out of memory\n");
135 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
136 rpc_createerr.cf_error.re_errno = errno;
139 ct = (struct ct_data *)mem_alloc(sizeof(*ct));
141 (void)fprintf(stderr, "clntunix_create: out of memory\n");
142 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
143 rpc_createerr.cf_error.re_errno = errno;
148 * If no socket given, open one
151 *sockp = socket(AF_UNIX, SOCK_STREAM, 0);
152 len = strlen(raddr->sun_path) + sizeof(raddr->sun_family) +
153 sizeof(raddr->sun_len) + 1;
154 raddr->sun_len = len;
156 || (connect(*sockp, (struct sockaddr *)raddr, len) < 0)) {
157 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
158 rpc_createerr.cf_error.re_errno = errno;
160 (void)_close(*sockp);
163 ct->ct_closeit = TRUE;
165 ct->ct_closeit = FALSE;
169 * Set up private data struct
171 ct->ct_sock = *sockp;
172 ct->ct_wait.tv_usec = 0;
173 ct->ct_waitset = FALSE;
174 ct->ct_addr = *raddr;
177 * Initialize call message
179 (void)gettimeofday(&now, (struct timezone *)0);
180 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
181 call_msg.rm_direction = CALL;
182 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
183 call_msg.rm_call.cb_prog = prog;
184 call_msg.rm_call.cb_vers = vers;
187 * pre-serialize the static part of the call msg and stash it away
189 xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
191 if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
192 if (ct->ct_closeit) {
193 (void)_close(*sockp);
197 ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
198 XDR_DESTROY(&(ct->ct_xdrs));
201 * Create a client handle which uses xdrrec for serialization
202 * and authnone for authentication.
204 xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
205 (caddr_t)ct, readunix, writeunix);
206 h->cl_ops = &unix_ops;
207 h->cl_private = (caddr_t) ct;
208 h->cl_auth = authnone_create();
213 * Something goofed, free stuff and barf
216 mem_free((caddr_t)ct, sizeof(struct ct_data));
218 mem_free((caddr_t)h, sizeof(CLIENT));
219 return ((CLIENT *)NULL);
222 static enum clnt_stat
223 clntunix_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
228 xdrproc_t xdr_results;
230 struct timeval timeout;
232 struct ct_data *ct = (struct ct_data *) h->cl_private;
233 XDR *xdrs = &(ct->ct_xdrs);
234 struct rpc_msg reply_msg;
236 u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */
240 if (!ct->ct_waitset) {
241 ct->ct_wait = timeout;
245 (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
246 && timeout.tv_usec == 0) ? FALSE : TRUE;
249 xdrs->x_op = XDR_ENCODE;
250 ct->ct_error.re_status = RPC_SUCCESS;
251 x_id = ntohl(--(*msg_x_id));
252 if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
253 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
254 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
255 (! (*xdr_args)(xdrs, args_ptr))) {
256 if (ct->ct_error.re_status == RPC_SUCCESS)
257 ct->ct_error.re_status = RPC_CANTENCODEARGS;
258 (void)xdrrec_endofrecord(xdrs, TRUE);
259 return (ct->ct_error.re_status);
261 if (! xdrrec_endofrecord(xdrs, shipnow))
262 return (ct->ct_error.re_status = RPC_CANTSEND);
264 return (RPC_SUCCESS);
266 * Hack to provide rpc-based message passing
268 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
269 return(ct->ct_error.re_status = RPC_TIMEDOUT);
274 * Keep receiving until we get a valid transaction id
276 xdrs->x_op = XDR_DECODE;
278 reply_msg.acpted_rply.ar_verf = _null_auth;
279 reply_msg.acpted_rply.ar_results.where = NULL;
280 reply_msg.acpted_rply.ar_results.proc = xdr_void;
281 if (! xdrrec_skiprecord(xdrs))
282 return (ct->ct_error.re_status);
283 /* now decode and validate the response header */
284 if (! xdr_replymsg(xdrs, &reply_msg)) {
285 if (ct->ct_error.re_status == RPC_SUCCESS)
287 return (ct->ct_error.re_status);
289 if (reply_msg.rm_xid == x_id)
296 _seterr_reply(&reply_msg, &(ct->ct_error));
297 if (ct->ct_error.re_status == RPC_SUCCESS) {
298 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
299 ct->ct_error.re_status = RPC_AUTHERROR;
300 ct->ct_error.re_why = AUTH_INVALIDRESP;
301 } else if (! (*xdr_results)(xdrs, results_ptr)) {
302 if (ct->ct_error.re_status == RPC_SUCCESS)
303 ct->ct_error.re_status = RPC_CANTDECODERES;
305 /* free verifier ... */
306 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
307 xdrs->x_op = XDR_FREE;
308 (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
310 } /* end successful completion */
312 /* maybe our credentials need to be refreshed ... */
313 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
315 } /* end of unsuccessful completion */
316 return (ct->ct_error.re_status);
320 clntunix_geterr(h, errp)
322 struct rpc_err *errp;
325 (struct ct_data *) h->cl_private;
327 *errp = ct->ct_error;
331 clntunix_freeres(cl, xdr_res, res_ptr)
336 struct ct_data *ct = (struct ct_data *)cl->cl_private;
337 XDR *xdrs = &(ct->ct_xdrs);
339 xdrs->x_op = XDR_FREE;
340 return ((*xdr_res)(xdrs, res_ptr));
350 clntunix_control(cl, request, info)
355 struct ct_data *ct = (struct ct_data *)cl->cl_private;
361 ct->ct_closeit = TRUE;
363 case CLSET_FD_NCLOSE:
364 ct->ct_closeit = FALSE;
369 tv = (struct timeval *)info;
370 ct->ct_wait.tv_sec = tv->tv_sec;
371 ct->ct_wait.tv_usec = tv->tv_usec;
372 ct->ct_waitset = TRUE;
377 *(struct timeval *)info = ct->ct_wait;
379 case CLGET_SERVER_ADDR:
382 *(struct sockaddr_un *)info = ct->ct_addr;
387 *(int *)info = ct->ct_sock;
391 * use the knowledge that xid is the
392 * first element in the call structure *.
393 * This will get the xid of the PREVIOUS call
397 *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall);
400 /* This will set the xid of the NEXT call */
403 *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1);
404 /* decrement by 1 as clntunix_call() increments once */
407 * This RELIES on the information that, in the call body,
408 * the version number field is the fifth field from the
409 * begining of the RPC header. MUST be changed if the
410 * call_struct is changed
414 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
415 4 * BYTES_PER_XDR_UNIT));
420 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
421 = htonl(*(u_long *)info);
425 * This RELIES on the information that, in the call body,
426 * the program number field is the field from the
427 * begining of the RPC header. MUST be changed if the
428 * call_struct is changed
432 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
433 3 * BYTES_PER_XDR_UNIT));
438 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
439 = htonl(*(u_long *)info);
441 case CLGET_LOCAL_ADDR:
442 len = sizeof(struct sockaddr);
443 if (getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0)
446 case CLGET_RETRY_TIMEOUT:
447 case CLSET_RETRY_TIMEOUT:
450 case CLSET_PUSH_TIMOD:
451 case CLSET_POP_TIMOD:
464 (struct ct_data *) h->cl_private;
466 if (ct->ct_closeit) {
467 (void)_close(ct->ct_sock);
469 XDR_DESTROY(&(ct->ct_xdrs));
470 mem_free((caddr_t)ct, sizeof(struct ct_data));
471 mem_free((caddr_t)h, sizeof(CLIENT));
475 * read() and write() are replaced with recvmsg()/sendmsg() so that
476 * we can pass ancillary control data. In this case, the data constists
477 * of credential information which the kernel will fill in for us.
478 * XXX: This code is specific to FreeBSD and will not work on other
479 * platforms without the requisite kernel modifications.
483 struct cmsgcred cmcred;
486 static int __msgread(sock, buf, cnt)
495 bzero((char *)&cm, sizeof(cm));
496 iov[0].iov_base = buf;
497 iov[0].iov_len = cnt;
503 msg.msg_control = (caddr_t)&cm;
504 msg.msg_controllen = sizeof(struct cmessage);
507 return(recvmsg(sock, &msg, 0));
510 static int __msgwrite(sock, buf, cnt)
519 bzero((char *)&cm, sizeof(cm));
520 iov[0].iov_base = buf;
521 iov[0].iov_len = cnt;
523 cm.cmsg.cmsg_type = SCM_CREDS;
524 cm.cmsg.cmsg_level = SOL_SOCKET;
525 cm.cmsg.cmsg_len = sizeof(struct cmessage);
531 msg.msg_control = (caddr_t)&cm;
532 msg.msg_controllen = sizeof(struct cmessage);
535 return(sendmsg(sock, &msg, 0));
539 * Interface between xdr serializer and unix connection.
540 * Behaves like the system calls, read & write, but keeps some error state
541 * around for the rpc level.
544 readunix(ct, buf, len)
549 fd_set *fds, readfds;
550 struct timeval start, after, duration, delta, tmp, tv;
556 if (ct->ct_sock + 1 > FD_SETSIZE) {
557 int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask);
558 fds = (fd_set *)malloc(bytes);
561 memset(fds, 0, bytes);
567 gettimeofday(&start, NULL);
570 /* XXX we know the other bits are still clear */
571 FD_SET(ct->ct_sock, fds);
572 tv = delta; /* in case select writes back */
573 r = select(ct->ct_sock+1, fds, NULL, NULL, &tv);
576 gettimeofday(&after, NULL);
577 timersub(&start, &after, &duration);
578 timersub(&delta, &duration, &tmp);
580 if (delta.tv_sec < 0 || !timerisset(&delta))
587 ct->ct_error.re_status = RPC_TIMEDOUT;
595 ct->ct_error.re_status = RPC_CANTRECV;
596 ct->ct_error.re_errno = save_errno;
601 switch (len = __msgread(ct->ct_sock, buf, len)) {
605 ct->ct_error.re_errno = ECONNRESET;
606 ct->ct_error.re_status = RPC_CANTRECV;
607 len = -1; /* it's really an error */
611 ct->ct_error.re_errno = errno;
612 ct->ct_error.re_status = RPC_CANTRECV;
619 writeunix(ct, buf, len)
626 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
627 if ((i = __msgwrite(ct->ct_sock, buf, cnt)) == -1) {
628 ct->ct_error.re_errno = errno;
629 ct->ct_error.re_status = RPC_CANTSEND;