]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */ |
2 | /* | |
3 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for | |
4 | * unrestricted use provided that this legend is included on all tape | |
5 | * media and as a part of the software program in whole or part. Users | |
6 | * may copy or modify Sun RPC without charge, but are not authorized | |
7 | * to license or distribute it to anyone else except as part of a product or | |
8 | * program developed by the user. | |
c4029823 | 9 | * |
28f540f4 RM |
10 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
11 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR | |
12 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. | |
c4029823 | 13 | * |
28f540f4 RM |
14 | * Sun RPC is provided with no support and without any obligation on the |
15 | * part of Sun Microsystems, Inc. to assist in its use, correction, | |
16 | * modification or enhancement. | |
c4029823 | 17 | * |
28f540f4 RM |
18 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
19 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC | |
20 | * OR ANY PART THEREOF. | |
c4029823 | 21 | * |
28f540f4 RM |
22 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
23 | * or profits or other special, indirect and consequential damages, even if | |
24 | * Sun has been advised of the possibility of such damages. | |
c4029823 | 25 | * |
28f540f4 RM |
26 | * Sun Microsystems, Inc. |
27 | * 2550 Garcia Avenue | |
28 | * Mountain View, California 94043 | |
29 | */ | |
30 | #if !defined(lint) && defined(SCCSIDS) | |
31 | static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro"; | |
32 | #endif | |
33 | ||
34 | /* | |
35 | * clnt_udp.c, Implements a UDP/IP based, client side RPC. | |
36 | * | |
37 | * Copyright (C) 1984, Sun Microsystems, Inc. | |
38 | */ | |
39 | ||
40 | #include <stdio.h> | |
e7fd8a39 | 41 | #include <unistd.h> |
28f540f4 | 42 | #include <rpc/rpc.h> |
e7fd8a39 UD |
43 | #include <rpc/xdr.h> |
44 | #include <rpc/clnt.h> | |
28f540f4 RM |
45 | #include <sys/socket.h> |
46 | #include <sys/ioctl.h> | |
47 | #include <netdb.h> | |
48 | #include <errno.h> | |
49 | #include <rpc/pmap_clnt.h> | |
50 | ||
e7fd8a39 | 51 | extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *); |
28f540f4 RM |
52 | |
53 | /* | |
54 | * UDP bases client side rpc operations | |
55 | */ | |
e7fd8a39 UD |
56 | static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t, |
57 | xdrproc_t, caddr_t, struct timeval); | |
58 | static void clntudp_abort (void); | |
59 | static void clntudp_geterr (CLIENT *, struct rpc_err *); | |
60 | static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t); | |
61 | static bool_t clntudp_control (CLIENT *, int, char *); | |
62 | static void clntudp_destroy (CLIENT *); | |
28f540f4 | 63 | |
e7fd8a39 UD |
64 | static struct clnt_ops udp_ops = |
65 | { | |
66 | clntudp_call, | |
67 | clntudp_abort, | |
68 | clntudp_geterr, | |
69 | clntudp_freeres, | |
70 | clntudp_destroy, | |
71 | clntudp_control | |
28f540f4 RM |
72 | }; |
73 | ||
c4029823 | 74 | /* |
28f540f4 RM |
75 | * Private data kept per client handle |
76 | */ | |
e7fd8a39 UD |
77 | struct cu_data |
78 | { | |
79 | int cu_sock; | |
80 | bool_t cu_closeit; | |
81 | struct sockaddr_in cu_raddr; | |
82 | int cu_rlen; | |
83 | struct timeval cu_wait; | |
84 | struct timeval cu_total; | |
85 | struct rpc_err cu_error; | |
86 | XDR cu_outxdrs; | |
87 | u_int cu_xdrpos; | |
88 | u_int cu_sendsz; | |
89 | char *cu_outbuf; | |
90 | u_int cu_recvsz; | |
91 | char cu_inbuf[1]; | |
92 | }; | |
28f540f4 RM |
93 | |
94 | /* | |
95 | * Create a UDP based client handle. | |
96 | * If *sockp<0, *sockp is set to a newly created UPD socket. | |
97 | * If raddr->sin_port is 0 a binder on the remote machine | |
98 | * is consulted for the correct port number. | |
99 | * NB: It is the clients responsibility to close *sockp. | |
100 | * NB: The rpch->cl_auth is initialized to null authentication. | |
101 | * Caller may wish to set this something more useful. | |
102 | * | |
103 | * wait is the amount of time used between retransmitting a call if | |
6d52618b | 104 | * no response has been heard; retransmission occurs until the actual |
28f540f4 RM |
105 | * rpc call times out. |
106 | * | |
107 | * sendsz and recvsz are the maximum allowable packet sizes that can be | |
108 | * sent and received. | |
109 | */ | |
110 | CLIENT * | |
e7fd8a39 UD |
111 | clntudp_bufcreate (raddr, program, version, wait, sockp, sendsz, recvsz) |
112 | struct sockaddr_in *raddr; | |
113 | u_long program; | |
114 | u_long version; | |
115 | struct timeval wait; | |
116 | int *sockp; | |
117 | u_int sendsz; | |
118 | u_int recvsz; | |
28f540f4 | 119 | { |
e7fd8a39 UD |
120 | CLIENT *cl; |
121 | struct cu_data *cu = NULL; | |
122 | struct timeval now; | |
123 | struct rpc_msg call_msg; | |
28f540f4 | 124 | |
e7fd8a39 UD |
125 | cl = (CLIENT *) mem_alloc (sizeof (CLIENT)); |
126 | if (cl == NULL) | |
127 | { | |
128 | (void) fprintf (stderr, _("clntudp_create: out of memory\n")); | |
129 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; | |
130 | rpc_createerr.cf_error.re_errno = errno; | |
131 | goto fooy; | |
132 | } | |
133 | sendsz = ((sendsz + 3) / 4) * 4; | |
134 | recvsz = ((recvsz + 3) / 4) * 4; | |
135 | cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz); | |
136 | if (cu == NULL) | |
137 | { | |
138 | (void) fprintf (stderr, _("clntudp_create: out of memory\n")); | |
139 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; | |
140 | rpc_createerr.cf_error.re_errno = errno; | |
141 | goto fooy; | |
142 | } | |
143 | cu->cu_outbuf = &cu->cu_inbuf[recvsz]; | |
28f540f4 | 144 | |
e7fd8a39 UD |
145 | (void) gettimeofday (&now, (struct timezone *) 0); |
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 | } | |
156 | cl->cl_ops = &udp_ops; | |
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; | |
165 | call_msg.rm_xid = getpid () ^ now.tv_sec ^ now.tv_usec; | |
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; | |
170 | xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf, | |
171 | sendsz, XDR_ENCODE); | |
172 | if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg)) | |
173 | { | |
174 | goto fooy; | |
175 | } | |
176 | cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs)); | |
177 | if (*sockp < 0) | |
178 | { | |
179 | int dontblock = 1; | |
28f540f4 | 180 | |
e7fd8a39 UD |
181 | *sockp = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
182 | if (*sockp < 0) | |
183 | { | |
184 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; | |
185 | rpc_createerr.cf_error.re_errno = errno; | |
186 | goto fooy; | |
28f540f4 | 187 | } |
e7fd8a39 UD |
188 | /* attempt to bind to prov port */ |
189 | (void) bindresvport (*sockp, (struct sockaddr_in *) 0); | |
190 | /* the sockets rpc controls are non-blocking */ | |
191 | (void) ioctl (*sockp, FIONBIO, (char *) &dontblock); | |
192 | cu->cu_closeit = TRUE; | |
193 | } | |
194 | else | |
195 | { | |
196 | cu->cu_closeit = FALSE; | |
197 | } | |
198 | cu->cu_sock = *sockp; | |
199 | cl->cl_auth = authnone_create (); | |
200 | return cl; | |
28f540f4 | 201 | fooy: |
e7fd8a39 UD |
202 | if (cu) |
203 | mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz); | |
204 | if (cl) | |
205 | mem_free ((caddr_t) cl, sizeof (CLIENT)); | |
206 | return (CLIENT *) NULL; | |
28f540f4 RM |
207 | } |
208 | ||
209 | CLIENT * | |
e7fd8a39 UD |
210 | clntudp_create (raddr, program, version, wait, sockp) |
211 | struct sockaddr_in *raddr; | |
212 | u_long program; | |
213 | u_long version; | |
214 | struct timeval wait; | |
215 | int *sockp; | |
28f540f4 RM |
216 | { |
217 | ||
e7fd8a39 UD |
218 | return clntudp_bufcreate (raddr, program, version, wait, sockp, |
219 | UDPMSGSIZE, UDPMSGSIZE); | |
28f540f4 RM |
220 | } |
221 | ||
c4029823 | 222 | static enum clnt_stat |
e7fd8a39 UD |
223 | clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout) |
224 | CLIENT *cl; /* client handle */ | |
225 | u_long proc; /* procedure number */ | |
226 | xdrproc_t xargs; /* xdr routine for args */ | |
227 | caddr_t argsp; /* pointer to args */ | |
228 | xdrproc_t xresults; /* xdr routine for results */ | |
229 | caddr_t resultsp; /* pointer to results */ | |
230 | struct timeval utimeout; /* seconds to wait before giving up */ | |
28f540f4 | 231 | { |
e7fd8a39 UD |
232 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
233 | XDR *xdrs; | |
234 | int outlen; | |
235 | int inlen; | |
236 | int fromlen; | |
28f540f4 | 237 | #ifdef FD_SETSIZE |
e7fd8a39 UD |
238 | fd_set readfds; |
239 | fd_set mask; | |
28f540f4 | 240 | #else |
e7fd8a39 UD |
241 | int readfds; |
242 | int mask; | |
28f540f4 | 243 | #endif /* def FD_SETSIZE */ |
e7fd8a39 UD |
244 | struct sockaddr_in from; |
245 | struct rpc_msg reply_msg; | |
246 | XDR reply_xdrs; | |
247 | struct timeval time_waited; | |
248 | bool_t ok; | |
249 | int nrefreshes = 2; /* number of times to refresh cred */ | |
250 | struct timeval timeout; | |
28f540f4 | 251 | |
e7fd8a39 UD |
252 | if (cu->cu_total.tv_usec == -1) |
253 | { | |
254 | timeout = utimeout; /* use supplied timeout */ | |
255 | } | |
256 | else | |
257 | { | |
258 | timeout = cu->cu_total; /* use default timeout */ | |
259 | } | |
28f540f4 | 260 | |
e7fd8a39 UD |
261 | time_waited.tv_sec = 0; |
262 | time_waited.tv_usec = 0; | |
28f540f4 | 263 | call_again: |
e7fd8a39 UD |
264 | xdrs = &(cu->cu_outxdrs); |
265 | xdrs->x_op = XDR_ENCODE; | |
266 | XDR_SETPOS (xdrs, cu->cu_xdrpos); | |
267 | /* | |
268 | * the transaction is the first thing in the out buffer | |
269 | */ | |
270 | (*(u_short *) (cu->cu_outbuf))++; | |
271 | if ((!XDR_PUTLONG (xdrs, (long *) &proc)) || | |
272 | (!AUTH_MARSHALL (cl->cl_auth, xdrs)) || | |
273 | (!(*xargs) (xdrs, argsp))) | |
274 | return (cu->cu_error.re_status = RPC_CANTENCODEARGS); | |
275 | outlen = (int) XDR_GETPOS (xdrs); | |
28f540f4 RM |
276 | |
277 | send_again: | |
e7fd8a39 UD |
278 | if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0, |
279 | (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen) | |
280 | != outlen) | |
281 | { | |
282 | cu->cu_error.re_errno = errno; | |
283 | return (cu->cu_error.re_status = RPC_CANTSEND); | |
284 | } | |
28f540f4 | 285 | |
e7fd8a39 UD |
286 | /* |
287 | * Hack to provide rpc-based message passing | |
288 | */ | |
289 | if (timeout.tv_sec == 0 && timeout.tv_usec == 0) | |
290 | { | |
291 | return (cu->cu_error.re_status = RPC_TIMEDOUT); | |
292 | } | |
293 | /* | |
294 | * sub-optimal code appears here because we have | |
295 | * some clock time to spare while the packets are in flight. | |
296 | * (We assume that this is actually only executed once.) | |
297 | */ | |
298 | reply_msg.acpted_rply.ar_verf = _null_auth; | |
299 | reply_msg.acpted_rply.ar_results.where = resultsp; | |
300 | reply_msg.acpted_rply.ar_results.proc = xresults; | |
28f540f4 | 301 | #ifdef FD_SETSIZE |
e7fd8a39 UD |
302 | FD_ZERO (&mask); |
303 | FD_SET (cu->cu_sock, &mask); | |
28f540f4 | 304 | #else |
e7fd8a39 | 305 | mask = 1 << cu->cu_sock; |
28f540f4 | 306 | #endif /* def FD_SETSIZE */ |
e7fd8a39 UD |
307 | for (;;) |
308 | { | |
309 | struct timeval timeout = cu->cu_wait; | |
310 | readfds = mask; | |
311 | switch (select (_rpc_dtablesize (), &readfds, (fd_set*) NULL, | |
312 | (fd_set*) NULL, &timeout)) | |
313 | { | |
28f540f4 | 314 | |
e7fd8a39 UD |
315 | case 0: |
316 | time_waited.tv_sec += cu->cu_wait.tv_sec; | |
317 | time_waited.tv_usec += cu->cu_wait.tv_usec; | |
318 | while (time_waited.tv_usec >= 1000000) | |
319 | { | |
320 | time_waited.tv_sec++; | |
321 | time_waited.tv_usec -= 1000000; | |
322 | } | |
323 | if ((time_waited.tv_sec < timeout.tv_sec) || | |
324 | ((time_waited.tv_sec == timeout.tv_sec) && | |
325 | (time_waited.tv_usec < timeout.tv_usec))) | |
326 | goto send_again; | |
327 | return (cu->cu_error.re_status = RPC_TIMEDOUT); | |
28f540f4 | 328 | |
e7fd8a39 UD |
329 | /* |
330 | * buggy in other cases because time_waited is not being | |
331 | * updated. | |
332 | */ | |
333 | case -1: | |
334 | if (errno == EINTR) | |
335 | continue; | |
336 | cu->cu_error.re_errno = errno; | |
337 | return (cu->cu_error.re_status = RPC_CANTRECV); | |
28f540f4 | 338 | } |
e7fd8a39 UD |
339 | do |
340 | { | |
341 | fromlen = sizeof (struct sockaddr); | |
342 | inlen = recvfrom (cu->cu_sock, cu->cu_inbuf, | |
343 | (int) cu->cu_recvsz, 0, | |
344 | (struct sockaddr *) &from, &fromlen); | |
345 | } | |
346 | while (inlen < 0 && errno == EINTR); | |
347 | if (inlen < 0) | |
348 | { | |
349 | if (errno == EWOULDBLOCK) | |
350 | continue; | |
351 | cu->cu_error.re_errno = errno; | |
352 | return (cu->cu_error.re_status = RPC_CANTRECV); | |
28f540f4 | 353 | } |
e7fd8a39 UD |
354 | if (inlen < 4) |
355 | continue; | |
356 | /* see if reply transaction id matches sent id */ | |
357 | if (*((u_int32_t *) (cu->cu_inbuf)) != *((u_int32_t *) (cu->cu_outbuf))) | |
358 | continue; | |
359 | /* we now assume we have the proper reply */ | |
360 | break; | |
361 | } | |
362 | ||
363 | /* | |
364 | * now decode and validate the response | |
365 | */ | |
366 | xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE); | |
367 | ok = xdr_replymsg (&reply_xdrs, &reply_msg); | |
368 | /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ | |
369 | if (ok) | |
370 | { | |
371 | _seterr_reply (&reply_msg, &(cu->cu_error)); | |
372 | if (cu->cu_error.re_status == RPC_SUCCESS) | |
373 | { | |
374 | if (!AUTH_VALIDATE (cl->cl_auth, | |
375 | &reply_msg.acpted_rply.ar_verf)) | |
376 | { | |
377 | cu->cu_error.re_status = RPC_AUTHERROR; | |
378 | cu->cu_error.re_why = AUTH_INVALIDRESP; | |
379 | } | |
380 | if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) | |
381 | { | |
382 | xdrs->x_op = XDR_FREE; | |
383 | (void) xdr_opaque_auth (xdrs, | |
384 | &(reply_msg.acpted_rply.ar_verf)); | |
385 | } | |
386 | } /* end successful completion */ | |
387 | else | |
388 | { | |
389 | /* maybe our credentials need to be refreshed ... */ | |
390 | if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth)) | |
391 | { | |
392 | nrefreshes--; | |
393 | goto call_again; | |
394 | } | |
395 | } /* end of unsuccessful completion */ | |
396 | } /* end of valid reply message */ | |
397 | else | |
398 | { | |
399 | cu->cu_error.re_status = RPC_CANTDECODERES; | |
400 | } | |
401 | return cu->cu_error.re_status; | |
28f540f4 RM |
402 | } |
403 | ||
404 | static void | |
e7fd8a39 | 405 | clntudp_geterr (CLIENT *cl, struct rpc_err *errp) |
28f540f4 | 406 | { |
e7fd8a39 | 407 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
28f540f4 | 408 | |
e7fd8a39 | 409 | *errp = cu->cu_error; |
28f540f4 RM |
410 | } |
411 | ||
412 | ||
413 | static bool_t | |
e7fd8a39 | 414 | clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr) |
28f540f4 | 415 | { |
e7fd8a39 UD |
416 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
417 | XDR *xdrs = &(cu->cu_outxdrs); | |
28f540f4 | 418 | |
e7fd8a39 UD |
419 | xdrs->x_op = XDR_FREE; |
420 | return (*xdr_res) (xdrs, res_ptr); | |
28f540f4 RM |
421 | } |
422 | ||
c4029823 | 423 | static void |
e7fd8a39 | 424 | clntudp_abort (void) |
28f540f4 RM |
425 | { |
426 | } | |
427 | ||
428 | static bool_t | |
e7fd8a39 | 429 | clntudp_control (CLIENT *cl, int request, char *info) |
28f540f4 | 430 | { |
e7fd8a39 | 431 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
28f540f4 | 432 | |
e7fd8a39 UD |
433 | switch (request) |
434 | { | |
435 | case CLSET_TIMEOUT: | |
436 | cu->cu_total = *(struct timeval *) info; | |
437 | break; | |
438 | case CLGET_TIMEOUT: | |
439 | *(struct timeval *) info = cu->cu_total; | |
440 | break; | |
441 | case CLSET_RETRY_TIMEOUT: | |
442 | cu->cu_wait = *(struct timeval *) info; | |
443 | break; | |
444 | case CLGET_RETRY_TIMEOUT: | |
445 | *(struct timeval *) info = cu->cu_wait; | |
446 | break; | |
447 | case CLGET_SERVER_ADDR: | |
448 | *(struct sockaddr_in *) info = cu->cu_raddr; | |
449 | break; | |
450 | default: | |
451 | return FALSE; | |
452 | } | |
453 | return TRUE; | |
28f540f4 | 454 | } |
c4029823 | 455 | |
28f540f4 | 456 | static void |
e7fd8a39 | 457 | clntudp_destroy (CLIENT *cl) |
28f540f4 | 458 | { |
e7fd8a39 | 459 | struct cu_data *cu = (struct cu_data *) cl->cl_private; |
28f540f4 | 460 | |
e7fd8a39 UD |
461 | if (cu->cu_closeit) |
462 | { | |
463 | (void) close (cu->cu_sock); | |
464 | } | |
465 | XDR_DESTROY (&(cu->cu_outxdrs)); | |
466 | mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); | |
467 | mem_free ((caddr_t) cl, sizeof (CLIENT)); | |
28f540f4 | 468 | } |