pmap_rmt.c 11 KB

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