pmap_rmt.c 11 KB

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