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