pmap_rmt.c 12 KB

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