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 *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/ 32 /*static char *sccsid = "from: @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";*/ 33 static char *rcsid = "$FreeBSD$"; 34 #endif 35 36 /* 37 * pmap_rmt.c 38 * Client interface to pmap rpc service. 39 * remote call and broadcast service 40 * 41 * Copyright (C) 1984, Sun Microsystems, Inc. 42 */ 43 44 #include "namespace.h" 45 #include <rpc/rpc.h> 46 #include <rpc/pmap_prot.h> 47 #include <rpc/pmap_clnt.h> 48 #include <rpc/pmap_rmt.h> 49 #include <sys/socket.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 #include <errno.h> 54 #include <string.h> 55 #include <net/if.h> 56 #include <sys/ioctl.h> 57 #include <arpa/inet.h> 58 #include "un-namespace.h" 59 60 #define MAX_BROADCAST_SIZE 1400 61 62 static struct timeval timeout = { 3, 0 }; 63 64 /* 65 * pmapper remote-call-service interface. 66 * This routine is used to call the pmapper remote call service 67 * which will look up a service program in the port maps, and then 68 * remotely call that routine with the given parameters. This allows 69 * programs to do a lookup and call in one step. 70 */ 71 enum clnt_stat 72 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr) 73 struct sockaddr_in *addr; 74 u_long prog, vers, proc; 75 xdrproc_t xdrargs, xdrres; 76 caddr_t argsp, resp; 77 struct timeval tout; 78 u_long *port_ptr; 79 { 80 int socket = -1; 81 register CLIENT *client; 82 struct rmtcallargs a; 83 struct rmtcallres r; 84 enum clnt_stat stat; 85 86 addr->sin_port = htons(PMAPPORT); 87 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket); 88 if (client != (CLIENT *)NULL) { 89 a.prog = prog; 90 a.vers = vers; 91 a.proc = proc; 92 a.args_ptr = argsp; 93 a.xdr_args = xdrargs; 94 r.port_ptr = port_ptr; 95 r.results_ptr = resp; 96 r.xdr_results = xdrres; 97 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a, 98 xdr_rmtcallres, &r, tout); 99 CLNT_DESTROY(client); 100 } else { 101 stat = RPC_FAILED; 102 } 103 if (socket != -1) 104 (void)_close(socket); 105 addr->sin_port = 0; 106 return (stat); 107 } 108 109 110 /* 111 * XDR remote call arguments 112 * written for XDR_ENCODE direction only 113 */ 114 bool_t 115 xdr_rmtcall_args(xdrs, cap) 116 register XDR *xdrs; 117 register struct rmtcallargs *cap; 118 { 119 u_int lenposition, argposition, position; 120 121 if (xdr_u_long(xdrs, &(cap->prog)) && 122 xdr_u_long(xdrs, &(cap->vers)) && 123 xdr_u_long(xdrs, &(cap->proc))) { 124 lenposition = XDR_GETPOS(xdrs); 125 if (! xdr_u_long(xdrs, &(cap->arglen))) 126 return (FALSE); 127 argposition = XDR_GETPOS(xdrs); 128 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr)) 129 return (FALSE); 130 position = XDR_GETPOS(xdrs); 131 cap->arglen = (u_long)position - (u_long)argposition; 132 XDR_SETPOS(xdrs, lenposition); 133 if (! xdr_u_long(xdrs, &(cap->arglen))) 134 return (FALSE); 135 XDR_SETPOS(xdrs, position); 136 return (TRUE); 137 } 138 return (FALSE); 139 } 140 141 /* 142 * XDR remote call results 143 * written for XDR_DECODE direction only 144 */ 145 bool_t 146 xdr_rmtcallres(xdrs, crp) 147 register XDR *xdrs; 148 register struct rmtcallres *crp; 149 { 150 caddr_t port_ptr; 151 152 port_ptr = (caddr_t)crp->port_ptr; 153 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), 154 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) { 155 crp->port_ptr = (u_long *)port_ptr; 156 return ((*(crp->xdr_results))(xdrs, crp->results_ptr)); 157 } 158 return (FALSE); 159 } 160 161 162 /* 163 * The following is kludged-up support for simple rpc broadcasts. 164 * Someday a large, complicated system will replace these trivial 165 * routines which only support udp/ip . 166 */ 167 168 static int 169 getbroadcastnets(addrs, sock, buf) 170 struct in_addr *addrs; 171 int sock; /* any valid socket will do */ 172 char *buf; /* why allocxate more when we can use existing... */ 173 { 174 struct ifconf ifc; 175 struct ifreq ifreq, *ifr; 176 struct sockaddr_in *sin; 177 struct in_addr addr; 178 char *cp, *cplim; 179 int n, i = 0; 180 181 ifc.ifc_len = UDPMSGSIZE; 182 ifc.ifc_buf = buf; 183 if (_ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { 184 perror("broadcast: ioctl (get interface configuration)"); 185 return (0); 186 } 187 #define max(a, b) (a > b ? a : b) 188 #define size(p) max((p).sa_len, sizeof(p)) 189 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ 190 for (cp = buf; cp < cplim; 191 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { 192 ifr = (struct ifreq *)cp; 193 if (ifr->ifr_addr.sa_family != AF_INET) 194 continue; 195 memcpy(&ifreq, ifr, sizeof(ifreq)); 196 if (_ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 197 perror("broadcast: ioctl (get interface flags)"); 198 continue; 199 } 200 if ((ifreq.ifr_flags & IFF_BROADCAST) && 201 (ifreq.ifr_flags & IFF_UP)) { 202 sin = (struct sockaddr_in *)&ifr->ifr_addr; 203 #ifdef SIOCGIFBRDADDR /* 4.3BSD */ 204 if (_ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 205 addr = 206 inet_makeaddr(inet_netof(sin->sin_addr), 207 INADDR_ANY); 208 } else { 209 addr = ((struct sockaddr_in*) 210 &ifreq.ifr_addr)->sin_addr; 211 } 212 #else /* 4.2 BSD */ 213 addr = inet_makeaddr(inet_netof(sin->sin_addr), 214 INADDR_ANY); 215 #endif 216 for (n=i-1; n>=0; n--) { 217 if (addr.s_addr == addrs[n].s_addr) 218 break; 219 } 220 if (n<0) { 221 addrs[i++] = addr; 222 } 223 } 224 } 225 return (i); 226 } 227 228 typedef bool_t (*resultproc_t)(); 229 230 enum clnt_stat 231 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 232 u_long prog; /* program number */ 233 u_long vers; /* version number */ 234 u_long proc; /* procedure number */ 235 xdrproc_t xargs; /* xdr routine for args */ 236 caddr_t argsp; /* pointer to args */ 237 xdrproc_t xresults; /* xdr routine for results */ 238 caddr_t resultsp; /* pointer to results */ 239 resultproc_t eachresult; /* call with each result obtained */ 240 { 241 enum clnt_stat stat; 242 AUTH *unix_auth = authunix_create_default(); 243 XDR xdr_stream; 244 register XDR *xdrs = &xdr_stream; 245 int outlen, inlen, fromlen, nets; 246 register int sock; 247 int on = 1; 248 fd_set *fds, readfds; 249 register int i; 250 bool_t done = FALSE; 251 register u_long xid; 252 u_long port; 253 struct in_addr addrs[20]; 254 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */ 255 struct rmtcallargs a; 256 struct rmtcallres r; 257 struct rpc_msg msg; 258 struct timeval t, tv; 259 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE]; 260 static u_int32_t disrupt; 261 262 if (disrupt == 0) 263 disrupt = (u_int32_t)(long)resultsp; 264 265 /* 266 * initialization: create a socket, a broadcast address, and 267 * preserialize the arguments into a send buffer. 268 */ 269 if ((sock = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 270 perror("Cannot create socket for broadcast rpc"); 271 stat = RPC_CANTSEND; 272 goto done_broad; 273 } 274 #ifdef SO_BROADCAST 275 if (_setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 276 perror("Cannot set socket option SO_BROADCAST"); 277 stat = RPC_CANTSEND; 278 goto done_broad; 279 } 280 #endif /* def SO_BROADCAST */ 281 if (sock + 1 > FD_SETSIZE) { 282 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask); 283 fds = (fd_set *)malloc(bytes); 284 if (fds == NULL) { 285 stat = RPC_CANTSEND; 286 goto done_broad; 287 } 288 memset(fds, 0, bytes); 289 } else { 290 fds = &readfds; 291 FD_ZERO(fds); 292 } 293 294 nets = getbroadcastnets(addrs, sock, inbuf); 295 memset(&baddr, 0, sizeof (baddr)); 296 baddr.sin_len = sizeof(struct sockaddr_in); 297 baddr.sin_family = AF_INET; 298 baddr.sin_port = htons(PMAPPORT); 299 baddr.sin_addr.s_addr = htonl(INADDR_ANY); 300 (void)gettimeofday(&t, (struct timezone *)0); 301 msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec; 302 t.tv_usec = 0; 303 msg.rm_direction = CALL; 304 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 305 msg.rm_call.cb_prog = PMAPPROG; 306 msg.rm_call.cb_vers = PMAPVERS; 307 msg.rm_call.cb_proc = PMAPPROC_CALLIT; 308 msg.rm_call.cb_cred = unix_auth->ah_cred; 309 msg.rm_call.cb_verf = unix_auth->ah_verf; 310 a.prog = prog; 311 a.vers = vers; 312 a.proc = proc; 313 a.xdr_args = xargs; 314 a.args_ptr = argsp; 315 r.port_ptr = &port; 316 r.xdr_results = xresults; 317 r.results_ptr = resultsp; 318 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE); 319 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) { 320 stat = RPC_CANTENCODEARGS; 321 goto done_broad; 322 } 323 outlen = (int)xdr_getpos(xdrs); 324 xdr_destroy(xdrs); 325 /* 326 * Basic loop: broadcast a packet and wait a while for response(s). 327 * The response timeout grows larger per iteration. 328 * 329 * XXX This will loop about 5 times the stop. If there are 330 * lots of signals being received by the process it will quit 331 * send them all in one quick burst, not paying attention to 332 * the intended function of sending them slowly over half a 333 * minute or so 334 */ 335 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) { 336 for (i = 0; i < nets; i++) { 337 baddr.sin_addr = addrs[i]; 338 if (_sendto(sock, outbuf, outlen, 0, 339 (struct sockaddr *)&baddr, 340 sizeof (struct sockaddr)) != outlen) { 341 perror("Cannot send broadcast packet"); 342 stat = RPC_CANTSEND; 343 goto done_broad; 344 } 345 } 346 if (eachresult == NULL) { 347 stat = RPC_SUCCESS; 348 goto done_broad; 349 } 350 recv_again: 351 msg.acpted_rply.ar_verf = _null_auth; 352 msg.acpted_rply.ar_results.where = (caddr_t)&r; 353 msg.acpted_rply.ar_results.proc = xdr_rmtcallres; 354 /* XXX we know the other bits are still clear */ 355 FD_SET(sock, fds); 356 tv = t; /* for _select() that copies back */ 357 switch (_select(sock + 1, fds, NULL, NULL, &tv)) { 358 359 case 0: /* timed out */ 360 stat = RPC_TIMEDOUT; 361 continue; 362 363 case -1: /* some kind of error */ 364 if (errno == EINTR) 365 goto recv_again; 366 perror("Broadcast select problem"); 367 stat = RPC_CANTRECV; 368 goto done_broad; 369 370 } /* end of select results switch */ 371 try_again: 372 fromlen = sizeof(struct sockaddr); 373 inlen = _recvfrom(sock, inbuf, UDPMSGSIZE, 0, 374 (struct sockaddr *)&raddr, &fromlen); 375 if (inlen < 0) { 376 if (errno == EINTR) 377 goto try_again; 378 perror("Cannot receive reply to broadcast"); 379 stat = RPC_CANTRECV; 380 goto done_broad; 381 } 382 if (inlen < sizeof(u_int32_t)) 383 goto recv_again; 384 /* 385 * see if reply transaction id matches sent id. 386 * If so, decode the results. 387 */ 388 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE); 389 if (xdr_replymsg(xdrs, &msg)) { 390 if ((msg.rm_xid == xid) && 391 (msg.rm_reply.rp_stat == MSG_ACCEPTED) && 392 (msg.acpted_rply.ar_stat == SUCCESS)) { 393 raddr.sin_port = htons((u_short)port); 394 done = (*eachresult)(resultsp, &raddr); 395 } 396 /* otherwise, we just ignore the errors ... */ 397 } 398 xdrs->x_op = XDR_FREE; 399 msg.acpted_rply.ar_results.proc = xdr_void; 400 (void)xdr_replymsg(xdrs, &msg); 401 (void)(*xresults)(xdrs, resultsp); 402 xdr_destroy(xdrs); 403 if (done) { 404 stat = RPC_SUCCESS; 405 goto done_broad; 406 } else { 407 goto recv_again; 408 } 409 } 410 done_broad: 411 if (fds != &readfds) 412 free(fds); 413 if (sock >= 0) 414 (void)_close(sock); 415 AUTH_DESTROY(unix_auth); 416 return (stat); 417 } 418 419