pmap_rmt.c 11 KB

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