1 /* $NetBSD: pmap_rmt.c,v 1.6 1995/06/03 22:37:25 mycroft Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 32 #if defined(LIBC_SCCS) && !defined(lint) 33 /*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/ 34 /*static char *sccsid = "from: @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";*/ 35 static char *rcsid = "$NetBSD: pmap_rmt.c,v 1.6 1995/06/03 22:37:25 mycroft Exp $"; 36 #endif 37 38 /* 39 * pmap_rmt.c 40 * Client interface to pmap rpc service. 41 * remote call and broadcast service 42 * 43 * Copyright (C) 1984, Sun Microsystems, Inc. 44 */ 45 46 #include <rpc/rpc.h> 47 #include <rpc/pmap_prot.h> 48 #include <rpc/pmap_clnt.h> 49 #include <rpc/pmap_rmt.h> 50 #include <sys/socket.h> 51 #include <stdio.h> 52 #include <errno.h> 53 #include <string.h> 54 #include <net/if.h> 55 #include <sys/ioctl.h> 56 #include <arpa/inet.h> 57 #define MAX_BROADCAST_SIZE 1400 58 59 static struct timeval timeout = { 3, 0 }; 60 61 62 /* 63 * pmapper remote-call-service interface. 64 * This routine is used to call the pmapper remote call service 65 * which will look up a service program in the port maps, and then 66 * remotely call that routine with the given parameters. This allows 67 * programs to do a lookup and call in one step. 68 */ 69 enum clnt_stat 70 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr) 71 struct sockaddr_in *addr; 72 u_long prog, vers, proc; 73 xdrproc_t xdrargs, xdrres; 74 caddr_t argsp, resp; 75 struct timeval tout; 76 u_long *port_ptr; 77 { 78 int socket = -1; 79 register CLIENT *client; 80 struct rmtcallargs a; 81 struct rmtcallres r; 82 enum clnt_stat stat; 83 84 addr->sin_port = htons(PMAPPORT); 85 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket); 86 if (client != (CLIENT *)NULL) { 87 a.prog = prog; 88 a.vers = vers; 89 a.proc = proc; 90 a.args_ptr = argsp; 91 a.xdr_args = xdrargs; 92 r.port_ptr = port_ptr; 93 r.results_ptr = resp; 94 r.xdr_results = xdrres; 95 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a, 96 xdr_rmtcallres, &r, tout); 97 CLNT_DESTROY(client); 98 } else { 99 stat = RPC_FAILED; 100 } 101 (void)close(socket); 102 addr->sin_port = 0; 103 return (stat); 104 } 105 106 107 /* 108 * XDR remote call arguments 109 * written for XDR_ENCODE direction only 110 */ 111 bool_t 112 xdr_rmtcall_args(xdrs, cap) 113 register XDR *xdrs; 114 register struct rmtcallargs *cap; 115 { 116 u_int lenposition, argposition, position; 117 118 if (xdr_u_long(xdrs, &(cap->prog)) && 119 xdr_u_long(xdrs, &(cap->vers)) && 120 xdr_u_long(xdrs, &(cap->proc))) { 121 lenposition = XDR_GETPOS(xdrs); 122 if (! xdr_u_long(xdrs, &(cap->arglen))) 123 return (FALSE); 124 argposition = XDR_GETPOS(xdrs); 125 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr)) 126 return (FALSE); 127 position = XDR_GETPOS(xdrs); 128 cap->arglen = (u_long)position - (u_long)argposition; 129 XDR_SETPOS(xdrs, lenposition); 130 if (! xdr_u_long(xdrs, &(cap->arglen))) 131 return (FALSE); 132 XDR_SETPOS(xdrs, position); 133 return (TRUE); 134 } 135 return (FALSE); 136 } 137 138 /* 139 * XDR remote call results 140 * written for XDR_DECODE direction only 141 */ 142 bool_t 143 xdr_rmtcallres(xdrs, crp) 144 register XDR *xdrs; 145 register struct rmtcallres *crp; 146 { 147 caddr_t port_ptr; 148 149 port_ptr = (caddr_t)crp->port_ptr; 150 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), 151 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) { 152 crp->port_ptr = (u_long *)port_ptr; 153 return ((*(crp->xdr_results))(xdrs, crp->results_ptr)); 154 } 155 return (FALSE); 156 } 157 158 159 /* 160 * The following is kludged-up support for simple rpc broadcasts. 161 * Someday a large, complicated system will replace these trivial 162 * routines which only support udp/ip . 163 */ 164 165 static int 166 getbroadcastnets(addrs, sock, buf) 167 struct in_addr *addrs; 168 int sock; /* any valid socket will do */ 169 char *buf; /* why allocxate more when we can use existing... */ 170 { 171 struct ifconf ifc; 172 struct ifreq ifreq, *ifr; 173 struct sockaddr_in *sin; 174 char *cp, *cplim; 175 int n, i = 0; 176 177 ifc.ifc_len = UDPMSGSIZE; 178 ifc.ifc_buf = buf; 179 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { 180 perror("broadcast: ioctl (get interface configuration)"); 181 return (0); 182 } 183 #define max(a, b) (a > b ? a : b) 184 #define size(p) max((p).sa_len, sizeof(p)) 185 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ 186 for (cp = buf; cp < cplim; 187 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { 188 ifr = (struct ifreq *)cp; 189 if (ifr->ifr_addr.sa_family != AF_INET) 190 continue; 191 ifreq = *ifr; 192 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 193 perror("broadcast: ioctl (get interface flags)"); 194 continue; 195 } 196 if ((ifreq.ifr_flags & IFF_BROADCAST) && 197 (ifreq.ifr_flags & IFF_UP)) { 198 sin = (struct sockaddr_in *)&ifr->ifr_addr; 199 #ifdef SIOCGIFBRDADDR /* 4.3BSD */ 200 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 201 addrs[i++] = 202 inet_makeaddr(inet_netof(sin->sin_addr), 203 INADDR_ANY); 204 } else { 205 addrs[i++] = ((struct sockaddr_in*) 206 &ifreq.ifr_addr)->sin_addr; 207 } 208 #else /* 4.2 BSD */ 209 addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr), 210 INADDR_ANY); 211 #endif 212 } 213 } 214 return (i); 215 } 216 217 typedef bool_t (*resultproc_t)(); 218 219 enum clnt_stat 220 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) 221 u_long prog; /* program number */ 222 u_long vers; /* version number */ 223 u_long proc; /* procedure number */ 224 xdrproc_t xargs; /* xdr routine for args */ 225 caddr_t argsp; /* pointer to args */ 226 xdrproc_t xresults; /* xdr routine for results */ 227 caddr_t resultsp; /* pointer to results */ 228 resultproc_t eachresult; /* call with each result obtained */ 229 { 230 enum clnt_stat stat; 231 AUTH *unix_auth = authunix_create_default(); 232 XDR xdr_stream; 233 register XDR *xdrs = &xdr_stream; 234 int outlen, inlen, fromlen, nets; 235 register int sock; 236 int on = 1; 237 fd_set mask; 238 fd_set readfds; 239 register int i; 240 bool_t done = FALSE; 241 register u_long xid; 242 u_long port; 243 struct in_addr addrs[20]; 244 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */ 245 struct rmtcallargs a; 246 struct rmtcallres r; 247 struct rpc_msg msg; 248 struct timeval t; 249 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE]; 250 251 /* 252 * initialization: create a socket, a broadcast address, and 253 * preserialize the arguments into a send buffer. 254 */ 255 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 256 perror("Cannot create socket for broadcast rpc"); 257 stat = RPC_CANTSEND; 258 goto done_broad; 259 } 260 #ifdef SO_BROADCAST 261 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 262 perror("Cannot set socket option SO_BROADCAST"); 263 stat = RPC_CANTSEND; 264 goto done_broad; 265 } 266 #endif /* def SO_BROADCAST */ 267 FD_ZERO(&mask); 268 FD_SET(sock, &mask); 269 nets = getbroadcastnets(addrs, sock, inbuf); 270 memset(&baddr, 0, sizeof (baddr)); 271 baddr.sin_len = sizeof(struct sockaddr_in); 272 baddr.sin_family = AF_INET; 273 baddr.sin_port = htons(PMAPPORT); 274 baddr.sin_addr.s_addr = htonl(INADDR_ANY); 275 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */ 276 (void)gettimeofday(&t, (struct timezone *)0); 277 msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec; 278 t.tv_usec = 0; 279 msg.rm_direction = CALL; 280 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 281 msg.rm_call.cb_prog = PMAPPROG; 282 msg.rm_call.cb_vers = PMAPVERS; 283 msg.rm_call.cb_proc = PMAPPROC_CALLIT; 284 msg.rm_call.cb_cred = unix_auth->ah_cred; 285 msg.rm_call.cb_verf = unix_auth->ah_verf; 286 a.prog = prog; 287 a.vers = vers; 288 a.proc = proc; 289 a.xdr_args = xargs; 290 a.args_ptr = argsp; 291 r.port_ptr = &port; 292 r.xdr_results = xresults; 293 r.results_ptr = resultsp; 294 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE); 295 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) { 296 stat = RPC_CANTENCODEARGS; 297 goto done_broad; 298 } 299 outlen = (int)xdr_getpos(xdrs); 300 xdr_destroy(xdrs); 301 /* 302 * Basic loop: broadcast a packet and wait a while for response(s). 303 * The response timeout grows larger per iteration. 304 */ 305 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) { 306 for (i = 0; i < nets; i++) { 307 baddr.sin_addr = addrs[i]; 308 if (sendto(sock, outbuf, outlen, 0, 309 (struct sockaddr *)&baddr, 310 sizeof (struct sockaddr)) != outlen) { 311 perror("Cannot send broadcast packet"); 312 stat = RPC_CANTSEND; 313 goto done_broad; 314 } 315 } 316 if (eachresult == NULL) { 317 stat = RPC_SUCCESS; 318 goto done_broad; 319 } 320 recv_again: 321 msg.acpted_rply.ar_verf = _null_auth; 322 msg.acpted_rply.ar_results.where = (caddr_t)&r; 323 msg.acpted_rply.ar_results.proc = xdr_rmtcallres; 324 readfds = mask; 325 switch (select(sock+1, &readfds, (int *)NULL, 326 (int *)NULL, &t)) { 327 328 case 0: /* timed out */ 329 stat = RPC_TIMEDOUT; 330 continue; 331 332 case -1: /* some kind of error */ 333 if (errno == EINTR) 334 goto recv_again; 335 perror("Broadcast select problem"); 336 stat = RPC_CANTRECV; 337 goto done_broad; 338 339 } /* end of select results switch */ 340 try_again: 341 fromlen = sizeof(struct sockaddr); 342 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0, 343 (struct sockaddr *)&raddr, &fromlen); 344 if (inlen < 0) { 345 if (errno == EINTR) 346 goto try_again; 347 perror("Cannot receive reply to broadcast"); 348 stat = RPC_CANTRECV; 349 goto done_broad; 350 } 351 if (inlen < sizeof(u_int32_t)) 352 goto recv_again; 353 /* 354 * see if reply transaction id matches sent id. 355 * If so, decode the results. 356 */ 357 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE); 358 if (xdr_replymsg(xdrs, &msg)) { 359 if ((msg.rm_xid == xid) && 360 (msg.rm_reply.rp_stat == MSG_ACCEPTED) && 361 (msg.acpted_rply.ar_stat == SUCCESS)) { 362 raddr.sin_port = htons((u_short)port); 363 done = (*eachresult)(resultsp, &raddr); 364 } 365 /* otherwise, we just ignore the errors ... */ 366 } else { 367 #ifdef notdef 368 /* some kind of deserialization problem ... */ 369 if (msg.rm_xid == xid) 370 fprintf(stderr, "Broadcast deserialization problem"); 371 /* otherwise, just random garbage */ 372 #endif 373 } 374 xdrs->x_op = XDR_FREE; 375 msg.acpted_rply.ar_results.proc = xdr_void; 376 (void)xdr_replymsg(xdrs, &msg); 377 (void)(*xresults)(xdrs, resultsp); 378 xdr_destroy(xdrs); 379 if (done) { 380 stat = RPC_SUCCESS; 381 goto done_broad; 382 } else { 383 goto recv_again; 384 } 385 } 386 done_broad: 387 (void)close(sock); 388 AUTH_DESTROY(unix_auth); 389 return (stat); 390 } 391 392