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