pmap_rmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * pmap_rmt.c
  35. * Client interface to pmap rpc service.
  36. * remote call and broadcast service
  37. *
  38. * Copyright (C) 1984, Sun Microsystems, Inc.
  39. */
  40. #include <rpc/rpc.h>
  41. #include <rpc/pmap_prot.h>
  42. #include <rpc/pmap_clnt.h>
  43. #include <rpc/pmap_rmt.h>
  44. #include <sys/socket.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. //#include <net/if.h>
  48. #include <sys/ioctl.h>
  49. #include <arpa/inet.h>
  50. #define MAX_BROADCAST_SIZE 1400
  51. extern int errno;
  52. static struct timeval timeout = { 3, 0 };
  53. /*
  54. * pmapper remote-call-service interface.
  55. * This routine is used to call the pmapper remote call service
  56. * which will look up a service program in the port maps, and then
  57. * remotely call that routine with the given parameters. This allows
  58. * programs to do a lookup and call in one step.
  59. */
  60. enum clnt_stat
  61. pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
  62. struct sockaddr_in *addr;
  63. u_long prog, vers, proc;
  64. xdrproc_t xdrargs, xdrres;
  65. caddr_t argsp, resp;
  66. struct timeval tout;
  67. u_long *port_ptr;
  68. {
  69. int socket = -1;
  70. register CLIENT *client;
  71. struct rmtcallargs a;
  72. struct rmtcallres r;
  73. enum clnt_stat stat;
  74. addr->sin_port = htons(PMAPPORT);
  75. client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
  76. if (client != (CLIENT *)NULL) {
  77. a.prog = prog;
  78. a.vers = vers;
  79. a.proc = proc;
  80. a.args_ptr = argsp;
  81. a.xdr_args = xdrargs;
  82. r.port_ptr = port_ptr;
  83. r.results_ptr = resp;
  84. r.xdr_results = xdrres;
  85. stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
  86. xdr_rmtcallres, &r, tout);
  87. CLNT_DESTROY(client);
  88. } else {
  89. stat = RPC_FAILED;
  90. }
  91. (void)close(socket);
  92. addr->sin_port = 0;
  93. return (stat);
  94. }
  95. /*
  96. * XDR remote call arguments
  97. * written for XDR_ENCODE direction only
  98. */
  99. bool_t
  100. xdr_rmtcall_args(xdrs, cap)
  101. register XDR *xdrs;
  102. register struct rmtcallargs *cap;
  103. {
  104. u_int lenposition, argposition, position;
  105. if (xdr_u_long(xdrs, &(cap->prog)) &&
  106. xdr_u_long(xdrs, &(cap->vers)) &&
  107. xdr_u_long(xdrs, &(cap->proc))) {
  108. lenposition = XDR_GETPOS(xdrs);
  109. if (! xdr_u_long(xdrs, &(cap->arglen)))
  110. return (FALSE);
  111. argposition = XDR_GETPOS(xdrs);
  112. if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
  113. return (FALSE);
  114. position = XDR_GETPOS(xdrs);
  115. cap->arglen = (u_long)position - (u_long)argposition;
  116. XDR_SETPOS(xdrs, lenposition);
  117. if (! xdr_u_long(xdrs, &(cap->arglen)))
  118. return (FALSE);
  119. XDR_SETPOS(xdrs, position);
  120. return (TRUE);
  121. }
  122. return (FALSE);
  123. }
  124. /*
  125. * XDR remote call results
  126. * written for XDR_DECODE direction only
  127. */
  128. bool_t
  129. xdr_rmtcallres(xdrs, crp)
  130. register XDR *xdrs;
  131. register struct rmtcallres *crp;
  132. {
  133. caddr_t port_ptr;
  134. port_ptr = (caddr_t)crp->port_ptr;
  135. if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
  136. xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
  137. crp->port_ptr = (u_long *)port_ptr;
  138. return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
  139. }
  140. return (FALSE);
  141. }
  142. /*
  143. * The following is kludged-up support for simple rpc broadcasts.
  144. * Someday a large, complicated system will replace these trivial
  145. * routines which only support udp/ip .
  146. */
  147. static int
  148. getbroadcastnets(addrs, sock, buf)
  149. struct in_addr *addrs;
  150. int sock; /* any valid socket will do */
  151. char *buf; /* why allocxate more when we can use existing... */
  152. {
  153. #ifdef __linux__
  154. struct sockaddr_in addr;
  155. get_myaddress(&addr);
  156. #if 1
  157. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  158. #else
  159. addrs[0] = inet_makeaddr(inet_netof(addr.sin_addr), INADDR_ANY);
  160. #endif
  161. return 1;
  162. #else
  163. struct ifconf ifc;
  164. struct ifreq ifreq, *ifr;
  165. struct sockaddr_in *sin;
  166. int n, i;
  167. ifc.ifc_len = UDPMSGSIZE;
  168. ifc.ifc_buf = buf;
  169. if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
  170. perror("broadcast: ioctl (get interface configuration)");
  171. return (0);
  172. }
  173. ifr = ifc.ifc_req;
  174. for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
  175. ifreq = *ifr;
  176. if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  177. perror("broadcast: ioctl (get interface flags)");
  178. continue;
  179. }
  180. if ((ifreq.ifr_flags & IFF_BROADCAST) &&
  181. (ifreq.ifr_flags & IFF_UP) &&
  182. ifr->ifr_addr.sa_family == AF_INET) {
  183. sin = (struct sockaddr_in *)&ifr->ifr_addr;
  184. #ifdef SIOCGIFBRDADDR /* 4.3BSD */
  185. if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
  186. #if 1
  187. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  188. #else
  189. addrs[i++] = inet_makeaddr(inet_netof
  190. (sin->sin_addr.s_addr), INADDR_ANY);
  191. #endif
  192. } else {
  193. addrs[i++] = ((struct sockaddr_in*)
  194. &ifreq.ifr_addr)->sin_addr;
  195. }
  196. #else /* 4.2 BSD */
  197. #if 1
  198. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  199. #else
  200. addrs[i++] = inet_makeaddr(inet_netof
  201. (sin->sin_addr.s_addr), INADDR_ANY);
  202. #endif
  203. #endif
  204. }
  205. }
  206. return (i);
  207. #endif
  208. }
  209. typedef bool_t (*resultproc_t)();
  210. enum clnt_stat
  211. clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
  212. u_long prog; /* program number */
  213. u_long vers; /* version number */
  214. u_long proc; /* procedure number */
  215. xdrproc_t xargs; /* xdr routine for args */
  216. caddr_t argsp; /* pointer to args */
  217. xdrproc_t xresults; /* xdr routine for results */
  218. caddr_t resultsp; /* pointer to results */
  219. resultproc_t eachresult; /* call with each result obtained */
  220. {
  221. enum clnt_stat stat;
  222. AUTH *unix_auth = authunix_create_default();
  223. XDR xdr_stream;
  224. register XDR *xdrs = &xdr_stream;
  225. int outlen, inlen, fromlen, nets;
  226. register int sock;
  227. int on = 1;
  228. #ifdef FD_SETSIZE
  229. fd_set mask;
  230. fd_set readfds;
  231. #else
  232. int readfds;
  233. register int mask;
  234. #endif /* def FD_SETSIZE */
  235. register int i;
  236. bool_t done = FALSE;
  237. register u_long xid;
  238. u_long port;
  239. struct in_addr addrs[20];
  240. struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
  241. struct rmtcallargs a;
  242. struct rmtcallres r;
  243. struct rpc_msg msg;
  244. struct timeval t;
  245. char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  246. /*
  247. * initialization: create a socket, a broadcast address, and
  248. * preserialize the arguments into a send buffer.
  249. */
  250. if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  251. perror("Cannot create socket for broadcast rpc");
  252. stat = RPC_CANTSEND;
  253. goto done_broad;
  254. }
  255. #ifdef SO_BROADCAST
  256. if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
  257. perror("Cannot set socket option SO_BROADCAST");
  258. stat = RPC_CANTSEND;
  259. goto done_broad;
  260. }
  261. #endif /* def SO_BROADCAST */
  262. #ifdef FD_SETSIZE
  263. FD_ZERO(&mask);
  264. FD_SET(sock, &mask);
  265. #else
  266. mask = (1 << sock);
  267. #endif /* def FD_SETSIZE */
  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(_rpc_dtablesize(), &readfds, (int *)NULL,
  324. (int *)NULL, &t)) {
  325. case 0: /* timed out */
  326. stat = RPC_TIMEDOUT;
  327. continue;
  328. case -1: /* some kind of error */
  329. if (errno == EINTR)
  330. goto recv_again;
  331. perror("Broadcast select problem");
  332. stat = RPC_CANTRECV;
  333. goto done_broad;
  334. } /* end of select results switch */
  335. try_again:
  336. fromlen = sizeof(struct sockaddr);
  337. inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
  338. (struct sockaddr *)&raddr, &fromlen);
  339. if (inlen < 0) {
  340. if (errno == EINTR)
  341. goto try_again;
  342. perror("Cannot receive reply to broadcast");
  343. stat = RPC_CANTRECV;
  344. goto done_broad;
  345. }
  346. if (inlen < sizeof(u_long))
  347. goto recv_again;
  348. /*
  349. * see if reply transaction id matches sent id.
  350. * If so, decode the results.
  351. */
  352. xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
  353. if (xdr_replymsg(xdrs, &msg)) {
  354. if ((msg.rm_xid == xid) &&
  355. (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  356. (msg.acpted_rply.ar_stat == SUCCESS)) {
  357. raddr.sin_port = htons((u_short)port);
  358. done = (*eachresult)(resultsp, &raddr);
  359. }
  360. /* otherwise, we just ignore the errors ... */
  361. } else {
  362. #ifdef notdef
  363. /* some kind of deserialization problem ... */
  364. if (msg.rm_xid == xid)
  365. fprintf(stderr, "Broadcast deserialization problem");
  366. /* otherwise, just random garbage */
  367. #endif
  368. }
  369. xdrs->x_op = XDR_FREE;
  370. msg.acpted_rply.ar_results.proc = xdr_void;
  371. (void)xdr_replymsg(xdrs, &msg);
  372. (void)(*xresults)(xdrs, resultsp);
  373. xdr_destroy(xdrs);
  374. if (done) {
  375. stat = RPC_SUCCESS;
  376. goto done_broad;
  377. } else {
  378. goto recv_again;
  379. }
  380. }
  381. done_broad:
  382. (void)close(sock);
  383. AUTH_DESTROY(unix_auth);
  384. return (stat);
  385. }