1 /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
35 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
42 #include <gssrpc/rpc.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
46 #include <sys/filio.h>
51 #include <gssrpc/pmap_clnt.h>
52 #include <port-sockets.h>
55 #ifndef GETSOCKNAME_ARG3_TYPE
56 #define GETSOCKNAME_ARG3_TYPE int
60 * UDP bases client side rpc operations
62 static enum clnt_stat clntudp_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
63 xdrproc_t, void *, struct timeval);
64 static void clntudp_abort(CLIENT *);
65 static void clntudp_geterr(CLIENT *, struct rpc_err *);
66 static bool_t clntudp_freeres(CLIENT *, xdrproc_t, void *);
67 static bool_t clntudp_control(CLIENT *, int, void *);
68 static void clntudp_destroy(CLIENT *);
70 static struct clnt_ops udp_ops = {
80 * Private data kept per client handle
85 struct sockaddr_in cu_raddr;
87 struct sockaddr_in cu_laddr;
88 GETSOCKNAME_ARG3_TYPE cu_llen;
89 struct timeval cu_wait;
90 struct timeval cu_total;
91 struct rpc_err cu_error;
101 * Create a UDP based client handle.
102 * If *sockp<0, *sockp is set to a newly created UPD socket.
103 * If raddr->sin_port is 0 a binder on the remote machine
104 * is consulted for the correct port number.
105 * NB: It is the clients responsibility to close *sockp.
106 * NB: The rpch->cl_auth is initialized to null authentication.
107 * Caller may wish to set this something more useful.
109 * wait is the amount of time used between retransmitting a call if
110 * no response has been heard; retransmition occurs until the actual
111 * rpc call times out.
113 * sendsz and recvsz are the maximum allowable packet sizes that can be
118 struct sockaddr_in *raddr,
127 register struct cu_data *cu = 0;
129 struct rpc_msg call_msg;
131 cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
133 (void) fprintf(stderr, "clntudp_create: out of memory\n");
134 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
135 rpc_createerr.cf_error.re_errno = errno;
138 sendsz = ((sendsz + 3) / 4) * 4;
139 recvsz = ((recvsz + 3) / 4) * 4;
140 cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
142 (void) fprintf(stderr, "clntudp_create: out of memory\n");
143 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
144 rpc_createerr.cf_error.re_errno = errno;
147 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
149 (void)gettimeofday(&now, (struct timezone *)0);
150 if (raddr->sin_port == 0) {
153 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
156 raddr->sin_port = htons(port);
158 cl->cl_ops = &udp_ops;
159 cl->cl_private = (caddr_t)cu;
160 cu->cu_raddr = *raddr;
161 cu->cu_rlen = sizeof (cu->cu_raddr);
163 cu->cu_total.tv_sec = -1;
164 cu->cu_total.tv_usec = -1;
165 cu->cu_sendsz = sendsz;
166 cu->cu_recvsz = recvsz;
167 call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
168 call_msg.rm_direction = CALL;
169 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
170 call_msg.rm_call.cb_prog = program;
171 call_msg.rm_call.cb_vers = version;
172 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
174 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
177 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
181 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
183 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
184 rpc_createerr.cf_error.re_errno = errno;
187 /* attempt to bind to prov port */
188 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
189 /* the sockets rpc controls are non-blocking */
190 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
191 cu->cu_closeit = TRUE;
193 cu->cu_closeit = FALSE;
195 if (connect(*sockp, (struct sockaddr *)raddr, sizeof(*raddr)) < 0)
197 cu->cu_llen = sizeof(cu->cu_laddr);
198 if (getsockname(*sockp, (struct sockaddr *)&cu->cu_laddr, &cu->cu_llen) < 0)
201 cu->cu_sock = *sockp;
202 cl->cl_auth = authnone_create();
206 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
208 mem_free((caddr_t)cl, sizeof(CLIENT));
209 return ((CLIENT *)NULL);
214 struct sockaddr_in *raddr,
221 return(clntudp_bufcreate(raddr, program, version, wait, sockp,
222 UDPMSGSIZE, UDPMSGSIZE));
225 static enum clnt_stat
227 register CLIENT *cl, /* client handle */
228 rpcproc_t proc, /* procedure number */
229 xdrproc_t xargs, /* xdr routine for args */
230 void * argsp, /* pointer to args */
231 xdrproc_t xresults, /* xdr routine for results */
232 void * resultsp, /* pointer to results */
233 struct timeval utimeout /* seconds to wait before
237 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
240 register ssize_t inlen;
241 GETSOCKNAME_ARG3_TYPE fromlen; /* Assumes recvfrom uses same type */
248 #endif /* def FD_SETSIZE */
249 struct sockaddr_in from;
250 struct rpc_msg reply_msg;
252 struct timeval time_waited, seltimeout;
254 int nrefreshes = 2; /* number of times to refresh cred */
255 struct timeval timeout;
258 if (cu->cu_total.tv_usec == -1) {
259 timeout = utimeout; /* use supplied timeout */
261 timeout = cu->cu_total; /* use default timeout */
264 time_waited.tv_sec = 0;
265 time_waited.tv_usec = 0;
267 xdrs = &(cu->cu_outxdrs);
268 xdrs->x_op = XDR_ENCODE;
269 XDR_SETPOS(xdrs, cu->cu_xdrpos);
271 * the transaction is the first thing in the out buffer
273 (*(uint32_t *)(void *)(cu->cu_outbuf))++;
274 if ((! XDR_PUTLONG(xdrs, &procl)) ||
275 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
276 (! AUTH_WRAP(cl->cl_auth, xdrs, xargs, argsp)))
277 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
278 outlen = (int)XDR_GETPOS(xdrs);
281 if (send(cu->cu_sock, cu->cu_outbuf, (u_int)outlen, 0) != outlen) {
282 cu->cu_error.re_errno = errno;
283 return (cu->cu_error.re_status = RPC_CANTSEND);
287 * Hack to provide rpc-based message passing
289 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
290 return (cu->cu_error.re_status = RPC_TIMEDOUT);
293 * sub-optimal code appears here because we have
294 * some clock time to spare while the packets are in flight.
295 * (We assume that this is actually only executed once.)
297 reply_msg.acpted_rply.ar_verf = gssrpc__null_auth;
298 reply_msg.acpted_rply.ar_results.where = NULL;
299 reply_msg.acpted_rply.ar_results.proc = xdr_void;
302 FD_SET(cu->cu_sock, &mask);
304 mask = 1 << cu->cu_sock;
305 #endif /* def FD_SETSIZE */
308 seltimeout = cu->cu_wait;
309 switch (select(gssrpc__rpc_dtablesize(), &readfds, (fd_set *)NULL,
310 (fd_set *)NULL, &seltimeout)) {
313 time_waited.tv_sec += cu->cu_wait.tv_sec;
314 time_waited.tv_usec += cu->cu_wait.tv_usec;
315 while (time_waited.tv_usec >= 1000000) {
316 time_waited.tv_sec++;
317 time_waited.tv_usec -= 1000000;
319 if ((time_waited.tv_sec < timeout.tv_sec) ||
320 ((time_waited.tv_sec == timeout.tv_sec) &&
321 (time_waited.tv_usec < timeout.tv_usec)))
323 return (cu->cu_error.re_status = RPC_TIMEDOUT);
326 * buggy in other cases because time_waited is not being
332 cu->cu_error.re_errno = errno;
333 return (cu->cu_error.re_status = RPC_CANTRECV);
336 fromlen = sizeof(struct sockaddr);
337 inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
339 (struct sockaddr *)&from, &fromlen);
340 } while (inlen < 0 && errno == EINTR);
342 if (errno == EWOULDBLOCK)
344 cu->cu_error.re_errno = errno;
345 return (cu->cu_error.re_status = RPC_CANTRECV);
347 if ((size_t)inlen < sizeof(uint32_t))
349 /* see if reply transaction id matches sent id */
350 if (*((uint32_t *)(void *)(cu->cu_inbuf)) !=
351 *((uint32_t *)(void *)(cu->cu_outbuf)))
353 /* we now assume we have the proper reply */
358 * now decode and validate the response
360 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
361 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
362 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
364 gssrpc__seterr_reply(&reply_msg, &(cu->cu_error));
365 if (cu->cu_error.re_status == RPC_SUCCESS) {
366 if (! AUTH_VALIDATE(cl->cl_auth,
367 &reply_msg.acpted_rply.ar_verf)) {
368 cu->cu_error.re_status = RPC_AUTHERROR;
369 cu->cu_error.re_why = AUTH_INVALIDRESP;
370 } else if (! AUTH_UNWRAP(cl->cl_auth, &reply_xdrs,
371 xresults, resultsp)) {
372 if (cu->cu_error.re_status == RPC_SUCCESS)
373 cu->cu_error.re_status = RPC_CANTDECODERES;
375 } /* end successful completion */
377 /* maybe our credentials need to be refreshed ... */
378 if (nrefreshes > 0 &&
379 AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
383 } /* end of unsuccessful completion */
385 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
386 (reply_msg.acpted_rply.ar_verf.oa_base != NULL)) {
387 xdrs->x_op = XDR_FREE;
388 (void)xdr_opaque_auth(xdrs,
389 &(reply_msg.acpted_rply.ar_verf));
391 } /* end of valid reply message */
394 * It's possible for xdr_replymsg() to fail partway
395 * through its attempt to decode the result from the
396 * server. If this happens, it will leave the reply
397 * structure partially populated with dynamically
398 * allocated memory. (This can happen if someone uses
399 * clntudp_bufcreate() to create a CLIENT handle and
400 * specifies a receive buffer size that is too small.)
401 * This memory must be free()ed to avoid a leak.
403 enum xdr_op op = reply_xdrs.x_op;
404 reply_xdrs.x_op = XDR_FREE;
405 xdr_replymsg(&reply_xdrs, &reply_msg);
406 reply_xdrs.x_op = op;
407 cu->cu_error.re_status = RPC_CANTDECODERES;
409 return (cu->cu_error.re_status);
415 struct rpc_err *errp)
417 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
419 *errp = cu->cu_error;
429 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
430 register XDR *xdrs = &(cu->cu_outxdrs);
432 xdrs->x_op = XDR_FREE;
433 return ((*xdr_res)(xdrs, res_ptr));
439 clntudp_abort(CLIENT *h)
449 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
453 cu->cu_total = *(struct timeval *)info;
456 *(struct timeval *)info = cu->cu_total;
458 case CLSET_RETRY_TIMEOUT:
459 cu->cu_wait = *(struct timeval *)info;
461 case CLGET_RETRY_TIMEOUT:
462 *(struct timeval *)info = cu->cu_wait;
464 case CLGET_SERVER_ADDR:
465 *(struct sockaddr_in *)info = cu->cu_raddr;
467 case CLGET_LOCAL_ADDR:
468 *(struct sockaddr_in *)info = cu->cu_laddr;
477 clntudp_destroy(CLIENT *cl)
479 register struct cu_data *cu = (struct cu_data *)cl->cl_private;
482 (void)closesocket(cu->cu_sock);
483 XDR_DESTROY(&(cu->cu_outxdrs));
484 mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
485 mem_free((caddr_t)cl, sizeof(CLIENT));