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