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