]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* |
a7ab6ec8 | 2 | * clnt_udp.c, Implements a UDP/IP based, client side RPC. |
c4029823 | 3 | * |
a7ab6ec8 | 4 | * Copyright (c) 2010, Oracle America, Inc. |
c4029823 | 5 | * |
a7ab6ec8 UD |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are | |
8 | * met: | |
cb636bb2 | 9 | * |
a7ab6ec8 UD |
10 | * * Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * * Redistributions in binary form must reproduce the above | |
13 | * copyright notice, this list of conditions and the following | |
14 | * disclaimer in the documentation and/or other materials | |
15 | * provided with the distribution. | |
16 | * * Neither the name of the "Oracle America, Inc." nor the names of its | |
17 | * contributors may be used to endorse or promote products derived | |
18 | * from this software without specific prior written permission. | |
28f540f4 | 19 | * |
a7ab6ec8 UD |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
24 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
27 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28f540f4 RM |
32 | */ |
33 | ||
34 | #include <stdio.h> | |
e7fd8a39 | 35 | #include <unistd.h> |
4360eafd | 36 | #include <libintl.h> |
28f540f4 | 37 | #include <rpc/rpc.h> |
e7fd8a39 UD |
38 | #include <rpc/xdr.h> |
39 | #include <rpc/clnt.h> | |
099a6fbd | 40 | #include <sys/poll.h> |
28f540f4 RM |
41 | #include <sys/socket.h> |
42 | #include <sys/ioctl.h> | |
43 | #include <netdb.h> | |
44 | #include <errno.h> | |
45 | #include <rpc/pmap_clnt.h> | |
ea48e2c4 | 46 | #include <net/if.h> |
412c954a | 47 | #include <ifaddrs.h> |
3ce1f295 | 48 | #include <wchar.h> |
8ccf22f9 | 49 | #include <fcntl.h> |
28f540f4 | 50 | |
b1eab230 UD |
51 | #ifdef IP_RECVERR |
52 | #include <errqueue.h> | |
53 | #include <sys/uio.h> | |
54 | #endif | |
55 | ||
8ccf22f9 UD |
56 | #include <kernel-features.h> |
57 | ||
090ca000 | 58 | extern u_long _create_xid (void); |
28f540f4 RM |
59 | |
60 | /* | |
61 | * UDP bases client side rpc operations | |
62 | */ | |
e7fd8a39 UD |
63 | static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t, |
64 | xdrproc_t, caddr_t, struct timeval); | |
65 | static void clntudp_abort (void); | |
66 | static void clntudp_geterr (CLIENT *, struct rpc_err *); | |
67 | static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t); | |
68 | static bool_t clntudp_control (CLIENT *, int, char *); | |
69 | static void clntudp_destroy (CLIENT *); | |
28f540f4 | 70 | |
31d7b14c | 71 | static const struct clnt_ops udp_ops = |
e7fd8a39 UD |
72 | { |
73 | clntudp_call, | |
74 | clntudp_abort, | |
75 | clntudp_geterr, | |
76 | clntudp_freeres, | |
77 | clntudp_destroy, | |
78 | clntudp_control | |
28f540f4 RM |
79 | }; |
80 | ||
c4029823 | 81 | /* |
28f540f4 RM |
82 | * Private data kept per client handle |
83 | */ | |
e7fd8a39 UD |
84 | struct cu_data |
85 | { | |
86 | int cu_sock; | |
87 | bool_t cu_closeit; | |
88 | struct sockaddr_in cu_raddr; | |
89 | int cu_rlen; | |
90 | struct timeval cu_wait; | |
91 | struct timeval cu_total; | |
92 | struct rpc_err cu_error; | |
93 | XDR cu_outxdrs; | |
94 | u_int cu_xdrpos; | |
95 | u_int cu_sendsz; | |
96 | char *cu_outbuf; | |
97 | u_int cu_recvsz; | |
98 | char cu_inbuf[1]; | |
99 | }; | |
28f540f4 RM |
100 | |
101 | /* | |
102 | * Create a UDP based client handle. | |
103 | * If *sockp<0, *sockp is set to a newly created UPD socket. | |
104 | * If raddr->sin_port is 0 a binder on the remote machine | |
105 | * is consulted for the correct port number. | |
106 | * NB: It is the clients responsibility to close *sockp. | |
107 | * NB: The rpch->cl_auth is initialized to null authentication. | |
108 | * Caller may wish to set this something more useful. | |
109 | * | |
110 | * wait is the amount of time used between retransmitting a call if | |
6d52618b | 111 | * no response has been heard; retransmission occurs until the actual |
28f540f4 RM |
112 | * rpc call times out. |
113 | * | |
114 | * sendsz and recvsz are the maximum allowable packet sizes that can be | |
115 | * sent and received. | |
116 | */ | |
117 | CLIENT * | |
8ccf22f9 UD |
118 | __libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, |
119 | u_long version, struct timeval wait, int *sockp, | |
120 | u_int sendsz, u_int recvsz, int flags) | |
28f540f4 | 121 | { |
e7fd8a39 UD |
122 | CLIENT *cl; |
123 | struct cu_data *cu = NULL; | |
e7fd8a39 | 124 | struct rpc_msg call_msg; |
28f540f4 | 125 | |
e7fd8a39 | 126 | cl = (CLIENT *) mem_alloc (sizeof (CLIENT)); |
e7fd8a39 UD |
127 | sendsz = ((sendsz + 3) / 4) * 4; |
128 | recvsz = ((recvsz + 3) / 4) * 4; | |
129 | cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz); | |
51028f34 | 130 | if (cl == NULL || cu == NULL) |
e7fd8a39 | 131 | { |
543cf8a9 | 132 | struct rpc_createerr *ce = &get_rpc_createerr (); |
1d20f7f8 UD |
133 | (void) __fxprintf (NULL, "%s: %s", |
134 | "clntudp_create", _("out of memory\n")); | |
543cf8a9 | 135 | ce->cf_stat = RPC_SYSTEMERROR; |
51028f34 | 136 | ce->cf_error.re_errno = ENOMEM; |
e7fd8a39 UD |
137 | goto fooy; |
138 | } | |
139 | cu->cu_outbuf = &cu->cu_inbuf[recvsz]; | |
28f540f4 | 140 | |
e7fd8a39 UD |
141 | if (raddr->sin_port == 0) |
142 | { | |
143 | u_short port; | |
144 | if ((port = | |
145 | pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0) | |
146 | { | |
147 | goto fooy; | |
28f540f4 | 148 | } |
e7fd8a39 UD |
149 | raddr->sin_port = htons (port); |
150 | } | |
31d7b14c | 151 | cl->cl_ops = (struct clnt_ops *) &udp_ops; |
e7fd8a39 UD |
152 | cl->cl_private = (caddr_t) cu; |
153 | cu->cu_raddr = *raddr; | |
154 | cu->cu_rlen = sizeof (cu->cu_raddr); | |
155 | cu->cu_wait = wait; | |
156 | cu->cu_total.tv_sec = -1; | |
157 | cu->cu_total.tv_usec = -1; | |
158 | cu->cu_sendsz = sendsz; | |
159 | cu->cu_recvsz = recvsz; | |
090ca000 | 160 | call_msg.rm_xid = _create_xid (); |
e7fd8a39 UD |
161 | call_msg.rm_direction = CALL; |
162 | call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; | |
163 | call_msg.rm_call.cb_prog = program; | |
164 | call_msg.rm_call.cb_vers = version; | |
7b57bfe5 UD |
165 | xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE); |
166 | if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg)) | |
e7fd8a39 UD |
167 | { |
168 | goto fooy; | |
169 | } | |
170 | cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs)); | |
171 | if (*sockp < 0) | |
172 | { | |
8ccf22f9 UD |
173 | #ifdef SOCK_NONBLOCK |
174 | # ifndef __ASSUME_SOCK_CLOEXEC | |
175 | if (__have_sock_cloexec >= 0) | |
176 | # endif | |
177 | { | |
178 | *sockp = __socket (AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|flags, | |
179 | IPPROTO_UDP); | |
180 | # ifndef __ASSUME_SOCK_CLOEXEC | |
181 | if (__have_sock_cloexec == 0) | |
182 | __have_sock_cloexec = *sockp >= 0 || errno != EINVAL ? 1 : -1; | |
183 | # endif | |
184 | } | |
185 | #endif | |
186 | #ifndef __ASSUME_SOCK_CLOEXEC | |
187 | # ifdef SOCK_CLOEXEC | |
188 | if (__have_sock_cloexec < 0) | |
189 | # endif | |
190 | { | |
191 | *sockp = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
192 | # ifdef SOCK_CLOEXEC | |
193 | if (flags & SOCK_CLOEXEC) | |
ff0bf753 | 194 | __fcntl (*sockp, F_SETFD, FD_CLOEXEC); |
8ccf22f9 UD |
195 | # endif |
196 | } | |
197 | #endif | |
198 | if (__builtin_expect (*sockp < 0, 0)) | |
e7fd8a39 | 199 | { |
543cf8a9 UD |
200 | struct rpc_createerr *ce = &get_rpc_createerr (); |
201 | ce->cf_stat = RPC_SYSTEMERROR; | |
202 | ce->cf_error.re_errno = errno; | |
e7fd8a39 | 203 | goto fooy; |
28f540f4 | 204 | } |
e7fd8a39 | 205 | /* attempt to bind to prov port */ |
a585ba22 | 206 | (void) bindresvport (*sockp, (struct sockaddr_in *) 0); |
8ccf22f9 UD |
207 | #ifndef __ASSUME_SOCK_CLOEXEC |
208 | # ifdef SOCK_CLOEXEC | |
209 | if (__have_sock_cloexec < 0) | |
210 | # endif | |
5615eaf2 RM |
211 | { |
212 | /* the sockets rpc controls are non-blocking */ | |
213 | int dontblock = 1; | |
214 | (void) __ioctl (*sockp, FIONBIO, (char *) &dontblock); | |
215 | } | |
8ccf22f9 | 216 | #endif |
b1eab230 UD |
217 | #ifdef IP_RECVERR |
218 | { | |
219 | int on = 1; | |
b2bffca2 | 220 | __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on)); |
b1eab230 UD |
221 | } |
222 | #endif | |
e7fd8a39 UD |
223 | cu->cu_closeit = TRUE; |
224 | } | |
225 | else | |
226 | { | |
227 | cu->cu_closeit = FALSE; | |
228 | } | |
229 | cu->cu_sock = *sockp; | |
7b57bfe5 | 230 | cl->cl_auth = authnone_create (); |
e7fd8a39 | 231 | return cl; |
28f540f4 | 232 | fooy: |
e7fd8a39 UD |
233 | if (cu) |
234 | mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz); | |
235 | if (cl) | |
236 | mem_free ((caddr_t) cl, sizeof (CLIENT)); | |
237 | return (CLIENT *) NULL; | |
28f540f4 | 238 | } |
7b57bfe5 UD |
239 | #ifdef EXPORT_RPC_SYMBOLS |
240 | libc_hidden_def (__libc_clntudp_bufcreate) | |
241 | #else | |
021db4be | 242 | libc_hidden_nolink_sunrpc (__libc_clntudp_bufcreate, GLIBC_PRIVATE) |
7b57bfe5 | 243 | #endif |
8ccf22f9 UD |
244 | |
245 | CLIENT * | |
246 | clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version, | |
247 | struct timeval wait, int *sockp, u_int sendsz, | |
248 | u_int recvsz) | |
249 | { | |
7b57bfe5 UD |
250 | return __libc_clntudp_bufcreate (raddr, program, version, wait, |
251 | sockp, sendsz, recvsz, 0); | |
8ccf22f9 | 252 | } |
021db4be | 253 | libc_hidden_nolink_sunrpc (clntudp_bufcreate, GLIBC_2_0) |
28f540f4 RM |
254 | |
255 | CLIENT * | |
e7fd8a39 UD |
256 | clntudp_create (raddr, program, version, wait, sockp) |
257 | struct sockaddr_in *raddr; | |
258 | u_long program; | |
259 | u_long version; | |
260 | struct timeval wait; | |
261 | int *sockp; | |
28f540f4 | 262 | { |
7b57bfe5 UD |
263 | return __libc_clntudp_bufcreate (raddr, program, version, wait, |
264 | sockp, UDPMSGSIZE, UDPMSGSIZE, 0); | |
28f540f4 | 265 | } |
7b57bfe5 UD |
266 | #ifdef EXPORT_RPC_SYMBOLS |
267 | libc_hidden_def (clntudp_create) | |
268 | #else | |
021db4be | 269 | libc_hidden_nolink_sunrpc (clntudp_create, GLIBC_2_0) |
7b57bfe5 | 270 | #endif |
28f540f4 | 271 | |
ea48e2c4 UD |
272 | static int |
273 | is_network_up (int sock) | |
274 | { | |
412c954a | 275 | struct ifaddrs *ifa; |
ea48e2c4 | 276 | |
412c954a UD |
277 | if (getifaddrs (&ifa) != 0) |
278 | return 0; | |
279 | ||
280 | struct ifaddrs *run = ifa; | |
281 | while (run != NULL) | |
ea48e2c4 | 282 | { |
412c954a | 283 | if ((run->ifa_flags & IFF_UP) != 0 |
f7e7a396 | 284 | && run->ifa_addr != NULL |
412c954a UD |
285 | && run->ifa_addr->sa_family == AF_INET) |
286 | break; | |
ea48e2c4 | 287 | |
412c954a | 288 | run = run->ifa_next; |
ea48e2c4 | 289 | } |
412c954a UD |
290 | |
291 | freeifaddrs (ifa); | |
292 | ||
293 | return run != NULL; | |
ea48e2c4 UD |
294 | } |
295 | ||
c4029823 | 296 | static enum clnt_stat |
e7fd8a39 UD |
297 | clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout) |
298 | CLIENT *cl; /* client handle */ | |
299 | u_long proc; /* procedure number */ | |
300 | xdrproc_t xargs; /* xdr routine for args */ | |
301 | caddr_t argsp; /* pointer to args */ | |
302 | xdrproc_t xresults; /* xdr routine for results */ | |
303 | caddr_t resultsp; /* pointer to results */ | |
304 | struct timeval utimeout; /* seconds to wait before giving up */ | |
28f540f4 | 305 | { |
e7fd8a39 UD |
306 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
307 | XDR *xdrs; | |
1522c368 | 308 | int outlen = 0; |
e7fd8a39 | 309 | int inlen; |
41df5ed4 | 310 | socklen_t fromlen; |
099a6fbd UD |
311 | struct pollfd fd; |
312 | int milliseconds = (cu->cu_wait.tv_sec * 1000) + | |
313 | (cu->cu_wait.tv_usec / 1000); | |
e7fd8a39 UD |
314 | struct sockaddr_in from; |
315 | struct rpc_msg reply_msg; | |
316 | XDR reply_xdrs; | |
317 | struct timeval time_waited; | |
318 | bool_t ok; | |
319 | int nrefreshes = 2; /* number of times to refresh cred */ | |
320 | struct timeval timeout; | |
ea48e2c4 | 321 | int anyup; /* any network interface up */ |
28f540f4 | 322 | |
e7fd8a39 UD |
323 | if (cu->cu_total.tv_usec == -1) |
324 | { | |
325 | timeout = utimeout; /* use supplied timeout */ | |
326 | } | |
327 | else | |
328 | { | |
329 | timeout = cu->cu_total; /* use default timeout */ | |
330 | } | |
28f540f4 | 331 | |
e7fd8a39 UD |
332 | time_waited.tv_sec = 0; |
333 | time_waited.tv_usec = 0; | |
28f540f4 | 334 | call_again: |
e7fd8a39 | 335 | xdrs = &(cu->cu_outxdrs); |
60c96635 UD |
336 | if (xargs == NULL) |
337 | goto get_reply; | |
e7fd8a39 UD |
338 | xdrs->x_op = XDR_ENCODE; |
339 | XDR_SETPOS (xdrs, cu->cu_xdrpos); | |
340 | /* | |
341 | * the transaction is the first thing in the out buffer | |
342 | */ | |
d6f6ffa1 | 343 | (*(uint32_t *) (cu->cu_outbuf))++; |
e7fd8a39 UD |
344 | if ((!XDR_PUTLONG (xdrs, (long *) &proc)) || |
345 | (!AUTH_MARSHALL (cl->cl_auth, xdrs)) || | |
346 | (!(*xargs) (xdrs, argsp))) | |
347 | return (cu->cu_error.re_status = RPC_CANTENCODEARGS); | |
348 | outlen = (int) XDR_GETPOS (xdrs); | |
28f540f4 RM |
349 | |
350 | send_again: | |
b2bffca2 UD |
351 | if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0, |
352 | (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen) | |
e7fd8a39 UD |
353 | != outlen) |
354 | { | |
355 | cu->cu_error.re_errno = errno; | |
356 | return (cu->cu_error.re_status = RPC_CANTSEND); | |
357 | } | |
28f540f4 | 358 | |
e7fd8a39 UD |
359 | /* |
360 | * Hack to provide rpc-based message passing | |
361 | */ | |
362 | if (timeout.tv_sec == 0 && timeout.tv_usec == 0) | |
363 | { | |
364 | return (cu->cu_error.re_status = RPC_TIMEDOUT); | |
365 | } | |
60c96635 | 366 | get_reply: |
e7fd8a39 UD |
367 | /* |
368 | * sub-optimal code appears here because we have | |
369 | * some clock time to spare while the packets are in flight. | |
370 | * (We assume that this is actually only executed once.) | |
371 | */ | |
372 | reply_msg.acpted_rply.ar_verf = _null_auth; | |
373 | reply_msg.acpted_rply.ar_results.where = resultsp; | |
374 | reply_msg.acpted_rply.ar_results.proc = xresults; | |
099a6fbd UD |
375 | fd.fd = cu->cu_sock; |
376 | fd.events = POLLIN; | |
c556351f | 377 | anyup = 0; |
e7fd8a39 UD |
378 | for (;;) |
379 | { | |
ea48e2c4 | 380 | switch (__poll (&fd, 1, milliseconds)) |
e7fd8a39 | 381 | { |
28f540f4 | 382 | |
e7fd8a39 | 383 | case 0: |
ea48e2c4 UD |
384 | if (anyup == 0) |
385 | { | |
386 | anyup = is_network_up (cu->cu_sock); | |
387 | if (!anyup) | |
388 | return (cu->cu_error.re_status = RPC_CANTRECV); | |
389 | } | |
390 | ||
e7fd8a39 UD |
391 | time_waited.tv_sec += cu->cu_wait.tv_sec; |
392 | time_waited.tv_usec += cu->cu_wait.tv_usec; | |
393 | while (time_waited.tv_usec >= 1000000) | |
394 | { | |
395 | time_waited.tv_sec++; | |
396 | time_waited.tv_usec -= 1000000; | |
397 | } | |
398 | if ((time_waited.tv_sec < timeout.tv_sec) || | |
399 | ((time_waited.tv_sec == timeout.tv_sec) && | |
400 | (time_waited.tv_usec < timeout.tv_usec))) | |
401 | goto send_again; | |
402 | return (cu->cu_error.re_status = RPC_TIMEDOUT); | |
28f540f4 | 403 | |
e7fd8a39 UD |
404 | /* |
405 | * buggy in other cases because time_waited is not being | |
406 | * updated. | |
407 | */ | |
408 | case -1: | |
409 | if (errno == EINTR) | |
410 | continue; | |
411 | cu->cu_error.re_errno = errno; | |
412 | return (cu->cu_error.re_status = RPC_CANTRECV); | |
28f540f4 | 413 | } |
b1eab230 UD |
414 | #ifdef IP_RECVERR |
415 | if (fd.revents & POLLERR) | |
416 | { | |
417 | struct msghdr msg; | |
418 | struct cmsghdr *cmsg; | |
419 | struct sock_extended_err *e; | |
420 | struct sockaddr_in err_addr; | |
421 | struct iovec iov; | |
422 | char *cbuf = (char *) alloca (outlen + 256); | |
423 | int ret; | |
424 | ||
425 | iov.iov_base = cbuf + 256; | |
426 | iov.iov_len = outlen; | |
427 | msg.msg_name = (void *) &err_addr; | |
428 | msg.msg_namelen = sizeof (err_addr); | |
429 | msg.msg_iov = &iov; | |
430 | msg.msg_iovlen = 1; | |
431 | msg.msg_flags = 0; | |
432 | msg.msg_control = cbuf; | |
433 | msg.msg_controllen = 256; | |
b2bffca2 | 434 | ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE); |
b1eab230 UD |
435 | if (ret >= 0 |
436 | && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0 | |
437 | && (msg.msg_flags & MSG_ERRQUEUE) | |
438 | && ((msg.msg_namelen == 0 | |
439 | && ret >= 12) | |
440 | || (msg.msg_namelen == sizeof (err_addr) | |
441 | && err_addr.sin_family == AF_INET | |
442 | && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr, | |
443 | sizeof (err_addr.sin_addr)) == 0 | |
444 | && err_addr.sin_port == cu->cu_raddr.sin_port))) | |
445 | for (cmsg = CMSG_FIRSTHDR (&msg); cmsg; | |
446 | cmsg = CMSG_NXTHDR (&msg, cmsg)) | |
447 | if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) | |
448 | { | |
449 | e = (struct sock_extended_err *) CMSG_DATA(cmsg); | |
450 | cu->cu_error.re_errno = e->ee_errno; | |
451 | return (cu->cu_error.re_status = RPC_CANTRECV); | |
452 | } | |
453 | } | |
454 | #endif | |
e7fd8a39 UD |
455 | do |
456 | { | |
457 | fromlen = sizeof (struct sockaddr); | |
b2bffca2 | 458 | inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf, |
7e9f348f | 459 | (int) cu->cu_recvsz, MSG_DONTWAIT, |
b2bffca2 | 460 | (struct sockaddr *) &from, &fromlen); |
e7fd8a39 UD |
461 | } |
462 | while (inlen < 0 && errno == EINTR); | |
463 | if (inlen < 0) | |
464 | { | |
465 | if (errno == EWOULDBLOCK) | |
466 | continue; | |
467 | cu->cu_error.re_errno = errno; | |
468 | return (cu->cu_error.re_status = RPC_CANTRECV); | |
28f540f4 | 469 | } |
e7fd8a39 UD |
470 | if (inlen < 4) |
471 | continue; | |
4c66860c UD |
472 | |
473 | /* see if reply transaction id matches sent id. | |
7b57bfe5 | 474 | Don't do this if we only wait for a replay */ |
4c66860c | 475 | if (xargs != NULL |
4efbd5cb | 476 | && memcmp (cu->cu_inbuf, cu->cu_outbuf, sizeof (u_int32_t)) != 0) |
e7fd8a39 UD |
477 | continue; |
478 | /* we now assume we have the proper reply */ | |
479 | break; | |
480 | } | |
481 | ||
482 | /* | |
483 | * now decode and validate the response | |
484 | */ | |
7b57bfe5 UD |
485 | xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE); |
486 | ok = xdr_replymsg (&reply_xdrs, &reply_msg); | |
e7fd8a39 UD |
487 | /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ |
488 | if (ok) | |
489 | { | |
490 | _seterr_reply (&reply_msg, &(cu->cu_error)); | |
491 | if (cu->cu_error.re_status == RPC_SUCCESS) | |
492 | { | |
493 | if (!AUTH_VALIDATE (cl->cl_auth, | |
494 | &reply_msg.acpted_rply.ar_verf)) | |
495 | { | |
496 | cu->cu_error.re_status = RPC_AUTHERROR; | |
497 | cu->cu_error.re_why = AUTH_INVALIDRESP; | |
498 | } | |
499 | if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) | |
500 | { | |
501 | xdrs->x_op = XDR_FREE; | |
7b57bfe5 | 502 | (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf)); |
e7fd8a39 UD |
503 | } |
504 | } /* end successful completion */ | |
505 | else | |
506 | { | |
507 | /* maybe our credentials need to be refreshed ... */ | |
508 | if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth)) | |
509 | { | |
510 | nrefreshes--; | |
511 | goto call_again; | |
512 | } | |
513 | } /* end of unsuccessful completion */ | |
514 | } /* end of valid reply message */ | |
515 | else | |
516 | { | |
517 | cu->cu_error.re_status = RPC_CANTDECODERES; | |
518 | } | |
519 | return cu->cu_error.re_status; | |
28f540f4 RM |
520 | } |
521 | ||
522 | static void | |
e7fd8a39 | 523 | clntudp_geterr (CLIENT *cl, struct rpc_err *errp) |
28f540f4 | 524 | { |
e7fd8a39 | 525 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
28f540f4 | 526 | |
e7fd8a39 | 527 | *errp = cu->cu_error; |
28f540f4 RM |
528 | } |
529 | ||
530 | ||
531 | static bool_t | |
e7fd8a39 | 532 | clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr) |
28f540f4 | 533 | { |
e7fd8a39 UD |
534 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
535 | XDR *xdrs = &(cu->cu_outxdrs); | |
28f540f4 | 536 | |
e7fd8a39 UD |
537 | xdrs->x_op = XDR_FREE; |
538 | return (*xdr_res) (xdrs, res_ptr); | |
28f540f4 RM |
539 | } |
540 | ||
c4029823 | 541 | static void |
e7fd8a39 | 542 | clntudp_abort (void) |
28f540f4 RM |
543 | { |
544 | } | |
545 | ||
546 | static bool_t | |
e7fd8a39 | 547 | clntudp_control (CLIENT *cl, int request, char *info) |
28f540f4 | 548 | { |
e7fd8a39 | 549 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
28f540f4 | 550 | |
e7fd8a39 UD |
551 | switch (request) |
552 | { | |
26a60f90 UD |
553 | case CLSET_FD_CLOSE: |
554 | cu->cu_closeit = TRUE; | |
555 | break; | |
556 | case CLSET_FD_NCLOSE: | |
557 | cu->cu_closeit = FALSE; | |
558 | break; | |
e7fd8a39 UD |
559 | case CLSET_TIMEOUT: |
560 | cu->cu_total = *(struct timeval *) info; | |
561 | break; | |
562 | case CLGET_TIMEOUT: | |
563 | *(struct timeval *) info = cu->cu_total; | |
564 | break; | |
565 | case CLSET_RETRY_TIMEOUT: | |
566 | cu->cu_wait = *(struct timeval *) info; | |
567 | break; | |
568 | case CLGET_RETRY_TIMEOUT: | |
569 | *(struct timeval *) info = cu->cu_wait; | |
570 | break; | |
571 | case CLGET_SERVER_ADDR: | |
572 | *(struct sockaddr_in *) info = cu->cu_raddr; | |
573 | break; | |
26a60f90 UD |
574 | case CLGET_FD: |
575 | *(int *)info = cu->cu_sock; | |
576 | break; | |
577 | case CLGET_XID: | |
578 | /* | |
579 | * use the knowledge that xid is the | |
580 | * first element in the call structure *. | |
581 | * This will get the xid of the PREVIOUS call | |
582 | */ | |
583 | *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf); | |
584 | break; | |
585 | case CLSET_XID: | |
586 | /* This will set the xid of the NEXT call */ | |
587 | *(u_long *)cu->cu_outbuf = htonl(*(u_long *)info - 1); | |
588 | /* decrement by 1 as clntudp_call() increments once */ | |
f0ccf6ea | 589 | break; |
26a60f90 UD |
590 | case CLGET_VERS: |
591 | /* | |
592 | * This RELIES on the information that, in the call body, | |
593 | * the version number field is the fifth field from the | |
594 | * begining of the RPC header. MUST be changed if the | |
595 | * call_struct is changed | |
596 | */ | |
597 | *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf + | |
598 | 4 * BYTES_PER_XDR_UNIT)); | |
599 | break; | |
600 | case CLSET_VERS: | |
601 | *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT) | |
602 | = htonl(*(u_long *)info); | |
603 | break; | |
604 | case CLGET_PROG: | |
605 | /* | |
606 | * This RELIES on the information that, in the call body, | |
607 | * the program number field is the field from the | |
608 | * begining of the RPC header. MUST be changed if the | |
609 | * call_struct is changed | |
610 | */ | |
611 | *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf + | |
612 | 3 * BYTES_PER_XDR_UNIT)); | |
613 | break; | |
614 | case CLSET_PROG: | |
615 | *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT) | |
616 | = htonl(*(u_long *)info); | |
617 | break; | |
618 | /* The following are only possible with TI-RPC */ | |
619 | case CLGET_SVC_ADDR: | |
620 | case CLSET_SVC_ADDR: | |
621 | case CLSET_PUSH_TIMOD: | |
622 | case CLSET_POP_TIMOD: | |
e7fd8a39 UD |
623 | default: |
624 | return FALSE; | |
625 | } | |
626 | return TRUE; | |
28f540f4 | 627 | } |
c4029823 | 628 | |
28f540f4 | 629 | static void |
e7fd8a39 | 630 | clntudp_destroy (CLIENT *cl) |
28f540f4 | 631 | { |
e7fd8a39 | 632 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
28f540f4 | 633 | |
e7fd8a39 UD |
634 | if (cu->cu_closeit) |
635 | { | |
50304ef0 | 636 | (void) __close (cu->cu_sock); |
e7fd8a39 UD |
637 | } |
638 | XDR_DESTROY (&(cu->cu_outxdrs)); | |
639 | mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); | |
640 | mem_free ((caddr_t) cl, sizeof (CLIENT)); | |
28f540f4 | 641 | } |