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