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
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)clnt_unix.c 1.37 87/10/05 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)clnt_unix.c 2.2 88/08/01 4.0 RPCSRC";*/
33 static char *rcsid = "$FreeBSD: src/lib/libc/rpc/clnt_unix.c,v 1.5 2000/01/27 23:06:37 jasone Exp $";
37 * clnt_unix.c, Implements a AF_UNIX based, client side RPC.
39 * Copyright (C) 1984, Sun Microsystems, Inc.
41 * AF_UNIX based RPC supports 'batched calls'.
42 * A sequence of calls may be batched-up in a send buffer. The rpc call
43 * return immediately to the client even though the call was not necessarily
44 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
45 * the rpc timeout value is zero (see clnt.h, rpc).
47 * Clients should NOT casually batch calls that in fact return results; that is,
48 * the server side should be aware that a call is batched and not produce any
49 * return message. Batched calls that produce many result messages can
50 * deadlock (netlock) the client and the server....
52 * Now go hang yourself.
61 #include <sys/socket.h>
65 #include <rpc/pmap_clnt.h>
67 #define MCALL_MSG_SIZE 24
69 static int readunix();
70 static int writeunix();
72 static enum clnt_stat clntunix_call();
73 static void clntunix_abort();
74 static void clntunix_geterr();
75 static bool_t clntunix_freeres();
76 static bool_t clntunix_control();
77 static void clntunix_destroy();
79 static struct clnt_ops unix_ops = {
91 struct timeval ct_wait;
92 bool_t ct_waitset; /* wait set by clnt_control? */
93 struct sockaddr_un ct_addr;
94 struct rpc_err ct_error;
95 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
96 u_int ct_mpos; /* pos after marshal */
101 * Create a client handle for a unix/ip connection.
102 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
103 * connected to raddr. If *sockp non-negative then
104 * raddr is ignored. The rpc/unix package does buffering
105 * similar to stdio, so the client must pick send and receive buffer sizes,];
106 * 0 => use the default.
107 * If raddr->sin_port is 0, then a binder on the remote machine is
108 * consulted for the right port number.
109 * NB: *sockp is copied into a private area.
110 * NB: It is the clients responsibility to close *sockp.
111 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
112 * something more useful.
115 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
116 struct sockaddr_un *raddr;
124 register struct ct_data *ct = NULL;
126 struct rpc_msg call_msg;
127 static u_int32_t disrupt;
131 disrupt = (u_int32_t)(long)raddr;
133 h = (CLIENT *)mem_alloc(sizeof(*h));
135 (void)fprintf(stderr, "clntunix_create: out of memory\n");
136 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
137 rpc_createerr.cf_error.re_errno = errno;
140 ct = (struct ct_data *)mem_alloc(sizeof(*ct));
142 (void)fprintf(stderr, "clntunix_create: out of memory\n");
143 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
144 rpc_createerr.cf_error.re_errno = errno;
149 * If no socket given, open one
152 *sockp = socket(AF_UNIX, SOCK_STREAM, 0);
153 len = strlen(raddr->sun_path) + sizeof(raddr->sun_family) +
154 sizeof(raddr->sun_len) + 1;
155 raddr->sun_len = len;
157 || (connect(*sockp, (struct sockaddr *)raddr, len) < 0)) {
158 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
159 rpc_createerr.cf_error.re_errno = errno;
161 (void)_close(*sockp);
164 ct->ct_closeit = TRUE;
166 ct->ct_closeit = FALSE;
170 * Set up private data struct
172 ct->ct_sock = *sockp;
173 ct->ct_wait.tv_usec = 0;
174 ct->ct_waitset = FALSE;
175 ct->ct_addr = *raddr;
178 * Initialize call message
180 (void)gettimeofday(&now, (struct timezone *)0);
181 call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
182 call_msg.rm_direction = CALL;
183 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
184 call_msg.rm_call.cb_prog = prog;
185 call_msg.rm_call.cb_vers = vers;
188 * pre-serialize the static part of the call msg and stash it away
190 xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
192 if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
193 if (ct->ct_closeit) {
194 (void)_close(*sockp);
198 ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
199 XDR_DESTROY(&(ct->ct_xdrs));
202 * Create a client handle which uses xdrrec for serialization
203 * and authnone for authentication.
205 xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
206 (caddr_t)ct, readunix, writeunix);
207 h->cl_ops = &unix_ops;
208 h->cl_private = (caddr_t) ct;
209 h->cl_auth = authnone_create();
214 * Something goofed, free stuff and barf
217 mem_free((caddr_t)ct, sizeof(struct ct_data));
219 mem_free((caddr_t)h, sizeof(CLIENT));
220 return ((CLIENT *)NULL);
223 static enum clnt_stat
224 clntunix_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
229 xdrproc_t xdr_results;
231 struct timeval timeout;
233 register struct ct_data *ct = (struct ct_data *) h->cl_private;
234 register XDR *xdrs = &(ct->ct_xdrs);
235 struct rpc_msg reply_msg;
237 u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */
238 register bool_t shipnow;
241 if (!ct->ct_waitset) {
242 ct->ct_wait = timeout;
246 (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
247 && timeout.tv_usec == 0) ? FALSE : TRUE;
250 xdrs->x_op = XDR_ENCODE;
251 ct->ct_error.re_status = RPC_SUCCESS;
252 x_id = ntohl(--(*msg_x_id));
253 if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
254 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
255 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
256 (! (*xdr_args)(xdrs, args_ptr))) {
257 if (ct->ct_error.re_status == RPC_SUCCESS)
258 ct->ct_error.re_status = RPC_CANTENCODEARGS;
259 (void)xdrrec_endofrecord(xdrs, TRUE);
260 return (ct->ct_error.re_status);
262 if (! xdrrec_endofrecord(xdrs, shipnow))
263 return (ct->ct_error.re_status = RPC_CANTSEND);
265 return (RPC_SUCCESS);
267 * Hack to provide rpc-based message passing
269 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
270 return(ct->ct_error.re_status = RPC_TIMEDOUT);
275 * Keep receiving until we get a valid transaction id
277 xdrs->x_op = XDR_DECODE;
279 reply_msg.acpted_rply.ar_verf = _null_auth;
280 reply_msg.acpted_rply.ar_results.where = NULL;
281 reply_msg.acpted_rply.ar_results.proc = xdr_void;
282 if (! xdrrec_skiprecord(xdrs))
283 return (ct->ct_error.re_status);
284 /* now decode and validate the response header */
285 if (! xdr_replymsg(xdrs, &reply_msg)) {
286 if (ct->ct_error.re_status == RPC_SUCCESS)
288 return (ct->ct_error.re_status);
290 if (reply_msg.rm_xid == x_id)
297 _seterr_reply(&reply_msg, &(ct->ct_error));
298 if (ct->ct_error.re_status == RPC_SUCCESS) {
299 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
300 ct->ct_error.re_status = RPC_AUTHERROR;
301 ct->ct_error.re_why = AUTH_INVALIDRESP;
302 } else if (! (*xdr_results)(xdrs, results_ptr)) {
303 if (ct->ct_error.re_status == RPC_SUCCESS)
304 ct->ct_error.re_status = RPC_CANTDECODERES;
306 /* free verifier ... */
307 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
308 xdrs->x_op = XDR_FREE;
309 (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
311 } /* end successful completion */
313 /* maybe our credentials need to be refreshed ... */
314 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
316 } /* end of unsuccessful completion */
317 return (ct->ct_error.re_status);
321 clntunix_geterr(h, errp)
323 struct rpc_err *errp;
325 register struct ct_data *ct =
326 (struct ct_data *) h->cl_private;
328 *errp = ct->ct_error;
332 clntunix_freeres(cl, xdr_res, res_ptr)
337 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
338 register XDR *xdrs = &(ct->ct_xdrs);
340 xdrs->x_op = XDR_FREE;
341 return ((*xdr_res)(xdrs, res_ptr));
351 clntunix_control(cl, request, info)
356 register struct ct_data *ct = (struct ct_data *)cl->cl_private;
357 register struct timeval *tv;
362 ct->ct_closeit = TRUE;
364 case CLSET_FD_NCLOSE:
365 ct->ct_closeit = FALSE;
370 tv = (struct timeval *)info;
371 ct->ct_wait.tv_sec = tv->tv_sec;
372 ct->ct_wait.tv_usec = tv->tv_usec;
373 ct->ct_waitset = TRUE;
378 *(struct timeval *)info = ct->ct_wait;
380 case CLGET_SERVER_ADDR:
383 *(struct sockaddr_un *)info = ct->ct_addr;
388 *(int *)info = ct->ct_sock;
392 * use the knowledge that xid is the
393 * first element in the call structure *.
394 * This will get the xid of the PREVIOUS call
398 *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall);
401 /* This will set the xid of the NEXT call */
404 *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1);
405 /* decrement by 1 as clntunix_call() increments once */
408 * This RELIES on the information that, in the call body,
409 * the version number field is the fifth field from the
410 * begining of the RPC header. MUST be changed if the
411 * call_struct is changed
415 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
416 4 * BYTES_PER_XDR_UNIT));
421 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
422 = htonl(*(u_long *)info);
426 * This RELIES on the information that, in the call body,
427 * the program number field is the field from the
428 * begining of the RPC header. MUST be changed if the
429 * call_struct is changed
433 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
434 3 * BYTES_PER_XDR_UNIT));
439 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
440 = htonl(*(u_long *)info);
442 case CLGET_LOCAL_ADDR:
443 len = sizeof(struct sockaddr);
444 if (getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0)
447 case CLGET_RETRY_TIMEOUT:
448 case CLSET_RETRY_TIMEOUT:
451 case CLSET_PUSH_TIMOD:
452 case CLSET_POP_TIMOD:
464 register struct ct_data *ct =
465 (struct ct_data *) h->cl_private;
467 if (ct->ct_closeit) {
468 (void)_close(ct->ct_sock);
470 XDR_DESTROY(&(ct->ct_xdrs));
471 mem_free((caddr_t)ct, sizeof(struct ct_data));
472 mem_free((caddr_t)h, sizeof(CLIENT));
476 * read() and write() are replaced with recvmsg()/sendmsg() so that
477 * we can pass ancillary control data. In this case, the data constists
478 * of credential information which the kernel will fill in for us.
479 * XXX: This code is specific to FreeBSD and will not work on other
480 * platforms without the requisite kernel modifications.
484 struct cmsgcred cmcred;
487 static int __msgread(sock, buf, cnt)
496 bzero((char *)&cm, sizeof(cm));
497 iov[0].iov_base = buf;
498 iov[0].iov_len = cnt;
504 msg.msg_control = (caddr_t)&cm;
505 msg.msg_controllen = sizeof(struct cmessage);
508 return(recvmsg(sock, &msg, 0));
511 static int __msgwrite(sock, buf, cnt)
520 bzero((char *)&cm, sizeof(cm));
521 iov[0].iov_base = buf;
522 iov[0].iov_len = cnt;
524 cm.cmsg.cmsg_type = SCM_CREDS;
525 cm.cmsg.cmsg_level = SOL_SOCKET;
526 cm.cmsg.cmsg_len = sizeof(struct cmessage);
532 msg.msg_control = (caddr_t)&cm;
533 msg.msg_controllen = sizeof(struct cmessage);
536 return(sendmsg(sock, &msg, 0));
540 * Interface between xdr serializer and unix connection.
541 * Behaves like the system calls, read & write, but keeps some error state
542 * around for the rpc level.
545 readunix(ct, buf, len)
546 register struct ct_data *ct;
550 fd_set *fds, readfds;
551 struct timeval start, after, duration, delta, tmp, tv;
557 if (ct->ct_sock + 1 > FD_SETSIZE) {
558 int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask);
559 fds = (fd_set *)malloc(bytes);
562 memset(fds, 0, bytes);
568 gettimeofday(&start, NULL);
571 /* XXX we know the other bits are still clear */
572 FD_SET(ct->ct_sock, fds);
573 tv = delta; /* in case select writes back */
574 r = select(ct->ct_sock+1, fds, NULL, NULL, &tv);
577 gettimeofday(&after, NULL);
578 timersub(&start, &after, &duration);
579 timersub(&delta, &duration, &tmp);
581 if (delta.tv_sec < 0 || !timerisset(&delta))
588 ct->ct_error.re_status = RPC_TIMEDOUT;
596 ct->ct_error.re_status = RPC_CANTRECV;
597 ct->ct_error.re_errno = save_errno;
602 switch (len = __msgread(ct->ct_sock, buf, len)) {
606 ct->ct_error.re_errno = ECONNRESET;
607 ct->ct_error.re_status = RPC_CANTRECV;
608 len = -1; /* it's really an error */
612 ct->ct_error.re_errno = errno;
613 ct->ct_error.re_status = RPC_CANTRECV;
620 writeunix(ct, buf, len)
627 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
628 if ((i = __msgwrite(ct->ct_sock, buf, cnt)) == -1) {
629 ct->ct_error.re_errno = errno;
630 ct->ct_error.re_status = RPC_CANTSEND;