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