1 /* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI"
37 static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * Implements a connectionless client side RPC.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/mutex.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
61 #include <rpc/rpc_com.h>
64 #ifdef _FREEFALL_CONFIG
66 * Disable RPC exponential back-off for FreeBSD.org systems.
68 #define RPC_MAX_BACKOFF 1 /* second */
70 #define RPC_MAX_BACKOFF 30 /* seconds */
73 static bool_t time_not_ok(struct timeval *);
74 static enum clnt_stat clnt_dg_call(CLIENT *, struct rpc_callextra *,
75 rpcproc_t, xdrproc_t, void *, xdrproc_t, void *, struct timeval);
76 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
77 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
78 static void clnt_dg_abort(CLIENT *);
79 static bool_t clnt_dg_control(CLIENT *, u_int, void *);
80 static void clnt_dg_destroy(CLIENT *);
81 static void clnt_dg_soupcall(struct socket *so, void *arg, int waitflag);
83 static struct clnt_ops clnt_dg_ops = {
84 .cl_call = clnt_dg_call,
85 .cl_abort = clnt_dg_abort,
86 .cl_geterr = clnt_dg_geterr,
87 .cl_freeres = clnt_dg_freeres,
88 .cl_destroy = clnt_dg_destroy,
89 .cl_control = clnt_dg_control
92 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
95 * A pending RPC request which awaits a reply. Requests which have
96 * received their reply will have cr_xid set to zero and cr_mrep to
97 * the mbuf chain of the reply.
100 TAILQ_ENTRY(cu_request) cr_link;
101 CLIENT *cr_client; /* owner */
102 uint32_t cr_xid; /* XID of request */
103 struct mbuf *cr_mrep; /* reply received by upcall */
104 int cr_error; /* any error from upcall */
107 TAILQ_HEAD(cu_request_list, cu_request);
109 #define MCALL_MSG_SIZE 24
112 * This structure is pointed to by the socket's so_upcallarg
113 * member. It is separate from the client private data to facilitate
114 * multiple clients sharing the same socket. The cs_lock mutex is used
115 * to protect all fields of this structure, the socket's receive
116 * buffer SOCKBUF_LOCK is used to ensure that exactly one of these
117 * structures is installed on the socket.
121 int cs_refs; /* Count of clients */
122 struct cu_request_list cs_pending; /* Requests awaiting replies */
127 * Private data kept per client handle
130 int cu_threads; /* # threads in clnt_vc_call */
131 bool_t cu_closing; /* TRUE if we are destroying */
132 struct socket *cu_socket; /* connection socket */
133 bool_t cu_closeit; /* opened by library */
134 struct sockaddr_storage cu_raddr; /* remote address */
136 struct timeval cu_wait; /* retransmit interval */
137 struct timeval cu_total; /* total time for the call */
138 struct rpc_err cu_error;
140 char cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
142 size_t cu_sendsz; /* send size */
143 size_t cu_recvsz; /* recv size */
145 int cu_connect; /* Use connect(). */
146 int cu_connected; /* Have done connect(). */
147 const char *cu_waitchan;
152 * Connection less client creation returns with client handle parameters.
153 * Default options are set, which the user can change using clnt_control().
154 * fd should be open and bound.
155 * NB: The rpch->cl_auth is initialized to null authentication.
156 * Caller may wish to set this something more useful.
158 * sendsz and recvsz are the maximum allowable packet sizes that can be
159 * sent and received. Normally they are the same, but they can be
160 * changed to improve the program efficiency and buffer allocation.
161 * If they are 0, use the transport default.
163 * If svcaddr is NULL, returns NULL.
168 struct sockaddr *svcaddr, /* servers address */
169 rpcprog_t program, /* program number */
170 rpcvers_t version, /* version number */
171 size_t sendsz, /* buffer recv size */
172 size_t recvsz) /* buffer send size */
174 CLIENT *cl = NULL; /* client handle */
175 struct cu_data *cu = NULL; /* private data */
176 struct cu_socket *cs = NULL;
178 struct rpc_msg call_msg;
179 struct __rpc_sockinfo si;
182 if (svcaddr == NULL) {
183 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
187 if (!__rpc_socket2sockinfo(so, &si)) {
188 rpc_createerr.cf_stat = RPC_TLIERROR;
189 rpc_createerr.cf_error.re_errno = 0;
194 * Find the receive and the send size
196 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
197 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
198 if ((sendsz == 0) || (recvsz == 0)) {
199 rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
200 rpc_createerr.cf_error.re_errno = 0;
204 cl = mem_alloc(sizeof (CLIENT));
207 * Should be multiple of 4 for XDR.
209 sendsz = ((sendsz + 3) / 4) * 4;
210 recvsz = ((recvsz + 3) / 4) * 4;
211 cu = mem_alloc(sizeof (*cu));
213 cu->cu_closing = FALSE;
214 (void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len);
215 cu->cu_rlen = svcaddr->sa_len;
216 /* Other values can also be set through clnt_control() */
217 cu->cu_wait.tv_sec = 3; /* heuristically chosen */
218 cu->cu_wait.tv_usec = 0;
219 cu->cu_total.tv_sec = -1;
220 cu->cu_total.tv_usec = -1;
221 cu->cu_sendsz = sendsz;
222 cu->cu_recvsz = recvsz;
223 cu->cu_async = FALSE;
224 cu->cu_connect = FALSE;
225 cu->cu_connected = FALSE;
226 cu->cu_waitchan = "rpcrecv";
228 (void) getmicrotime(&now);
229 cu->cu_xid = __RPC_GETXID(&now);
230 call_msg.rm_xid = cu->cu_xid;
231 call_msg.rm_call.cb_prog = program;
232 call_msg.rm_call.cb_vers = version;
233 xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE);
234 if (! xdr_callhdr(&xdrs, &call_msg)) {
235 rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
236 rpc_createerr.cf_error.re_errno = 0;
239 cu->cu_mcalllen = XDR_GETPOS(&xdrs);;
242 * By default, closeit is always FALSE. It is users responsibility
243 * to do a close on it, else the user may use clnt_control
244 * to let clnt_destroy do it for him/her.
246 cu->cu_closeit = FALSE;
248 soreserve(so, 256*1024, 256*1024);
250 SOCKBUF_LOCK(&so->so_rcv);
253 if (so->so_upcall != clnt_dg_soupcall) {
254 SOCKBUF_UNLOCK(&so->so_rcv);
255 printf("clnt_dg_create(): socket already has an incompatible upcall\n");
258 cs = (struct cu_socket *) so->so_upcallarg;
259 mtx_lock(&cs->cs_lock);
261 mtx_unlock(&cs->cs_lock);
264 * We are the first on this socket - allocate the
265 * structure and install it in the socket.
267 SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
268 cs = mem_alloc(sizeof(*cs));
269 SOCKBUF_LOCK(&cu->cu_socket->so_rcv);
272 * We have lost a race with some other client.
274 mem_free(cs, sizeof(*cs));
277 mtx_init(&cs->cs_lock, "cs->cs_lock", NULL, MTX_DEF);
279 TAILQ_INIT(&cs->cs_pending);
280 so->so_upcallarg = cs;
281 so->so_upcall = clnt_dg_soupcall;
282 so->so_rcv.sb_flags |= SB_UPCALL;
284 SOCKBUF_UNLOCK(&so->so_rcv);
287 cl->cl_ops = &clnt_dg_ops;
288 cl->cl_private = (caddr_t)(void *)cu;
289 cl->cl_auth = authnone_create();
295 mem_free(cl, sizeof (CLIENT));
297 mem_free(cu, sizeof (*cu));
302 static enum clnt_stat
304 CLIENT *cl, /* client handle */
305 struct rpc_callextra *ext, /* call metadata */
306 rpcproc_t proc, /* procedure number */
307 xdrproc_t xargs, /* xdr routine for args */
308 void *argsp, /* pointer to args */
309 xdrproc_t xresults, /* xdr routine for results */
310 void *resultsp, /* pointer to results */
311 struct timeval utimeout) /* seconds to wait before giving up */
313 struct cu_data *cu = (struct cu_data *)cl->cl_private;
314 struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg;
317 struct rpc_msg reply_msg;
319 int retrans; /* number of re-transmits so far */
320 int nrefreshes = 2; /* number of times to refresh cred */
324 int next_sendtime, starttime, time_waited, tv;
328 struct mbuf *mreq = NULL;
329 struct cu_request *cr;
332 cr = malloc(sizeof(struct cu_request), M_RPC, M_WAITOK);
334 mtx_lock(&cs->cs_lock);
336 if (cu->cu_closing) {
337 mtx_unlock(&cs->cs_lock);
339 return (RPC_CANTSEND);
352 if (cu->cu_total.tv_usec == -1) {
353 tvp = &utimeout; /* use supplied timeout */
355 tvp = &cu->cu_total; /* use default timeout */
357 if (tvp->tv_sec || tvp->tv_usec)
358 timeout = tvtohz(tvp);
362 if (cu->cu_connect && !cu->cu_connected) {
363 mtx_unlock(&cs->cs_lock);
364 error = soconnect(cu->cu_socket,
365 (struct sockaddr *)&cu->cu_raddr, curthread);
366 mtx_lock(&cs->cs_lock);
368 cu->cu_error.re_errno = error;
369 cu->cu_error.re_status = RPC_CANTSEND;
372 cu->cu_connected = 1;
374 if (cu->cu_connected) {
378 sa = (struct sockaddr *)&cu->cu_raddr;
383 retransmit_time = next_sendtime = tvtohz(&cu->cu_wait);
388 mtx_assert(&cs->cs_lock, MA_OWNED);
394 mtx_unlock(&cs->cs_lock);
396 MGETHDR(mreq, M_WAIT, MT_DATA);
397 MCLGET(mreq, M_WAIT);
399 m_append(mreq, cu->cu_mcalllen, cu->cu_mcallc);
402 * The XID is the first thing in the request.
404 *mtod(mreq, uint32_t *) = htonl(xid);
406 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
408 if (cu->cu_async == TRUE && xargs == NULL)
411 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
412 (! AUTH_MARSHALL(auth, &xdrs)) ||
413 (! (*xargs)(&xdrs, argsp))) {
414 cu->cu_error.re_status = RPC_CANTENCODEARGS;
415 mtx_lock(&cs->cs_lock);
421 mtx_lock(&cs->cs_lock);
422 TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link);
423 mtx_unlock(&cs->cs_lock);
426 * sosend consumes mreq.
428 error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread);
432 * sub-optimal code appears here because we have
433 * some clock time to spare while the packets are in flight.
434 * (We assume that this is actually only executed once.)
436 reply_msg.acpted_rply.ar_verf = _null_auth;
437 reply_msg.acpted_rply.ar_results.where = resultsp;
438 reply_msg.acpted_rply.ar_results.proc = xresults;
440 mtx_lock(&cs->cs_lock);
442 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
443 cu->cu_error.re_errno = error;
444 cu->cu_error.re_status = RPC_CANTSEND;
449 * Check to see if we got an upcall while waiting for the
453 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
454 cu->cu_error.re_errno = cr->cr_error;
455 cu->cu_error.re_status = RPC_CANTRECV;
459 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
464 * Hack to provide rpc-based message passing
467 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
468 cu->cu_error.re_status = RPC_TIMEDOUT;
474 /* Decide how long to wait. */
475 if (next_sendtime < timeout)
485 error = msleep(cr, &cs->cs_lock,
486 cu->cu_waitflag, cu->cu_waitchan, tv);
491 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
495 * We were woken up by the upcall. If the
496 * upcall had a receive error, report that,
497 * otherwise we have a reply.
500 cu->cu_error.re_errno = cr->cr_error;
501 cu->cu_error.re_status = RPC_CANTRECV;
508 * The sleep returned an error so our request is still
509 * on the list. If we got EWOULDBLOCK, we may want to
510 * re-send the request.
512 if (error != EWOULDBLOCK) {
513 cu->cu_error.re_errno = error;
515 cu->cu_error.re_status = RPC_INTR;
517 cu->cu_error.re_status = RPC_CANTRECV;
521 time_waited = ticks - starttime;
523 /* Check for timeout. */
524 if (time_waited > timeout) {
525 cu->cu_error.re_errno = EWOULDBLOCK;
526 cu->cu_error.re_status = RPC_TIMEDOUT;
530 /* Retransmit if necessary. */
531 if (time_waited >= next_sendtime) {
532 if (ext && ext->rc_feedback) {
533 mtx_unlock(&cs->cs_lock);
535 ext->rc_feedback(FEEDBACK_REXMIT1,
536 proc, ext->rc_feedback_arg);
538 ext->rc_feedback(FEEDBACK_REXMIT2,
539 proc, ext->rc_feedback_arg);
540 mtx_lock(&cs->cs_lock);
542 if (cu->cu_closing) {
543 cu->cu_error.re_errno = ESHUTDOWN;
544 cu->cu_error.re_status = RPC_CANTRECV;
548 /* update retransmit_time */
549 if (retransmit_time < RPC_MAX_BACKOFF * hz)
550 retransmit_time = 2 * retransmit_time;
551 next_sendtime += retransmit_time;
554 TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link);
559 * Now decode and validate the response. We need to drop the
560 * lock since xdr_replymsg may end up sleeping in malloc.
562 mtx_unlock(&cs->cs_lock);
564 if (ext && ext->rc_feedback)
565 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
567 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
568 ok = xdr_replymsg(&xdrs, &reply_msg);
572 mtx_lock(&cs->cs_lock);
575 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
576 (reply_msg.acpted_rply.ar_stat == SUCCESS))
577 cu->cu_error.re_status = RPC_SUCCESS;
579 _seterr_reply(&reply_msg, &(cu->cu_error));
581 if (cu->cu_error.re_status == RPC_SUCCESS) {
582 if (! AUTH_VALIDATE(cl->cl_auth,
583 &reply_msg.acpted_rply.ar_verf)) {
584 cu->cu_error.re_status = RPC_AUTHERROR;
585 cu->cu_error.re_why = AUTH_INVALIDRESP;
587 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
588 xdrs.x_op = XDR_FREE;
589 (void) xdr_opaque_auth(&xdrs,
590 &(reply_msg.acpted_rply.ar_verf));
592 } /* end successful completion */
594 * If unsuccesful AND error is an authentication error
595 * then refresh credentials and try again, else break
597 else if (cu->cu_error.re_status == RPC_AUTHERROR)
598 /* maybe our credentials need to be refreshed ... */
599 if (nrefreshes > 0 &&
600 AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
604 /* end of unsuccessful completion */
605 } /* end of valid reply message */
607 cu->cu_error.re_status = RPC_CANTDECODERES;
611 mtx_assert(&cs->cs_lock, MA_OWNED);
616 m_freem(cr->cr_mrep);
622 mtx_unlock(&cs->cs_lock);
626 return (cu->cu_error.re_status);
630 clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
632 struct cu_data *cu = (struct cu_data *)cl->cl_private;
634 *errp = cu->cu_error;
638 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
643 xdrs.x_op = XDR_FREE;
644 dummy = (*xdr_res)(&xdrs, res_ptr);
651 clnt_dg_abort(CLIENT *h)
656 clnt_dg_control(CLIENT *cl, u_int request, void *info)
658 struct cu_data *cu = (struct cu_data *)cl->cl_private;
659 struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg;
660 struct sockaddr *addr;
662 mtx_lock(&cs->cs_lock);
666 cu->cu_closeit = TRUE;
667 mtx_unlock(&cs->cs_lock);
669 case CLSET_FD_NCLOSE:
670 cu->cu_closeit = FALSE;
671 mtx_unlock(&cs->cs_lock);
675 /* for other requests which use info */
677 mtx_unlock(&cs->cs_lock);
682 if (time_not_ok((struct timeval *)info)) {
683 mtx_unlock(&cs->cs_lock);
686 cu->cu_total = *(struct timeval *)info;
689 *(struct timeval *)info = cu->cu_total;
691 case CLSET_RETRY_TIMEOUT:
692 if (time_not_ok((struct timeval *)info)) {
693 mtx_unlock(&cs->cs_lock);
696 cu->cu_wait = *(struct timeval *)info;
698 case CLGET_RETRY_TIMEOUT:
699 *(struct timeval *)info = cu->cu_wait;
703 * Slightly different semantics to userland - we use
704 * sockaddr instead of netbuf.
706 memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len);
708 case CLSET_SVC_ADDR: /* set to new address */
709 addr = (struct sockaddr *)info;
710 (void) memcpy(&cu->cu_raddr, addr, addr->sa_len);
713 *(uint32_t *)info = cu->cu_xid;
717 /* This will set the xid of the NEXT call */
718 /* decrement by 1 as clnt_dg_call() increments once */
719 cu->cu_xid = *(uint32_t *)info - 1;
724 * This RELIES on the information that, in the call body,
725 * the version number field is the fifth field from the
726 * begining of the RPC header. MUST be changed if the
727 * call_struct is changed
730 ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
731 4 * BYTES_PER_XDR_UNIT));
735 *(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT)
736 = htonl(*(uint32_t *)info);
741 * This RELIES on the information that, in the call body,
742 * the program number field is the fourth field from the
743 * begining of the RPC header. MUST be changed if the
744 * call_struct is changed
747 ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
748 3 * BYTES_PER_XDR_UNIT));
752 *(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT)
753 = htonl(*(uint32_t *)info);
756 cu->cu_async = *(int *)info;
759 cu->cu_connect = *(int *)info;
762 cu->cu_waitchan = *(const char **)info;
765 *(const char **) info = cu->cu_waitchan;
767 case CLSET_INTERRUPTIBLE:
769 cu->cu_waitflag = PCATCH;
773 case CLGET_INTERRUPTIBLE:
775 *(int *) info = TRUE;
777 *(int *) info = FALSE;
780 mtx_unlock(&cs->cs_lock);
783 mtx_unlock(&cs->cs_lock);
788 clnt_dg_destroy(CLIENT *cl)
790 struct cu_data *cu = (struct cu_data *)cl->cl_private;
791 struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg;
792 struct cu_request *cr;
793 struct socket *so = NULL;
794 bool_t lastsocketref;
796 mtx_lock(&cs->cs_lock);
799 * Abort any pending requests and wait until everyone
800 * has finished with clnt_vc_call.
802 cu->cu_closing = TRUE;
803 TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
804 if (cr->cr_client == cl) {
806 cr->cr_error = ESHUTDOWN;
811 while (cu->cu_threads)
812 msleep(cu, &cs->cs_lock, 0, "rpcclose", 0);
815 if (cs->cs_refs == 0) {
816 mtx_destroy(&cs->cs_lock);
817 SOCKBUF_LOCK(&cu->cu_socket->so_rcv);
818 cu->cu_socket->so_upcallarg = NULL;
819 cu->cu_socket->so_upcall = NULL;
820 cu->cu_socket->so_rcv.sb_flags &= ~SB_UPCALL;
821 SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
822 mem_free(cs, sizeof(*cs));
823 lastsocketref = TRUE;
825 mtx_unlock(&cs->cs_lock);
826 lastsocketref = FALSE;
829 if (cu->cu_closeit && lastsocketref) {
831 cu->cu_socket = NULL;
837 if (cl->cl_netid && cl->cl_netid[0])
838 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
839 if (cl->cl_tp && cl->cl_tp[0])
840 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
841 mem_free(cu, sizeof (*cu));
842 mem_free(cl, sizeof (CLIENT));
846 * Make sure that the time is not garbage. -1 value is allowed.
849 time_not_ok(struct timeval *t)
851 return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
852 t->tv_usec < -1 || t->tv_usec > 1000000);
856 clnt_dg_soupcall(struct socket *so, void *arg, int waitflag)
858 struct cu_socket *cs = (struct cu_socket *) arg;
861 struct mbuf *control;
862 struct cu_request *cr;
863 int error, rcvflag, foundreq;
866 uio.uio_resid = 1000000000;
867 uio.uio_td = curthread;
871 rcvflag = MSG_DONTWAIT;
872 error = soreceive(so, NULL, &uio, &m, &control, &rcvflag);
876 if (error == EWOULDBLOCK)
880 * If there was an error, wake up all pending
884 mtx_lock(&cs->cs_lock);
885 TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
887 cr->cr_error = error;
890 mtx_unlock(&cs->cs_lock);
895 * The XID is in the first uint32_t of the reply.
897 m = m_pullup(m, sizeof(xid));
900 * Should never happen.
904 xid = ntohl(*mtod(m, uint32_t *));
907 * Attempt to match this reply with a pending request.
909 mtx_lock(&cs->cs_lock);
911 TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
912 if (cr->cr_xid == xid) {
914 * This one matches. We leave the
915 * reply mbuf in cr->cr_mrep. Set the
916 * XID to zero so that we will ignore
917 * any duplicated replies that arrive
918 * before clnt_dg_call removes it from
929 mtx_unlock(&cs->cs_lock);
932 * If we didn't find the matching request, just drop
933 * it - its probably a repeated reply.