pmap_rmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * pmap_rmt.c
  3. * Client interface to pmap rpc service.
  4. * remote call and broadcast service
  5. *
  6. * Copyright (c) 2010, Oracle America, Inc.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials
  17. * provided with the distribution.
  18. * * Neither the name of the "Oracle America, Inc." nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  27. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  29. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <libintl.h>
  38. #include <rpc/rpc.h>
  39. #include <rpc/pmap_prot.h>
  40. #include <rpc/pmap_clnt.h>
  41. #include <rpc/pmap_rmt.h>
  42. #include <sys/poll.h>
  43. #include <sys/socket.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <sys/param.h>
  47. #include <net/if.h>
  48. #include <ifaddrs.h>
  49. #include <sys/ioctl.h>
  50. #include <arpa/inet.h>
  51. #include <shlib-compat.h>
  52. #define MAX_BROADCAST_SIZE 1400
  53. extern u_long _create_xid (void);
  54. static const struct timeval timeout = {3, 0};
  55. /*
  56. * pmapper remote-call-service interface.
  57. * This routine is used to call the pmapper remote call service
  58. * which will look up a service program in the port maps, and then
  59. * remotely call that routine with the given parameters. This allows
  60. * programs to do a lookup and call in one step.
  61. */
  62. enum clnt_stat
  63. pmap_rmtcall (struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
  64. xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
  65. struct timeval tout, u_long *port_ptr)
  66. {
  67. int socket = -1;
  68. CLIENT *client;
  69. struct rmtcallargs a;
  70. struct rmtcallres r;
  71. enum clnt_stat stat;
  72. addr->sin_port = htons (PMAPPORT);
  73. client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &socket);
  74. if (client != (CLIENT *) NULL)
  75. {
  76. a.prog = prog;
  77. a.vers = vers;
  78. a.proc = proc;
  79. a.args_ptr = argsp;
  80. a.xdr_args = xdrargs;
  81. r.port_ptr = port_ptr;
  82. r.results_ptr = resp;
  83. r.xdr_results = xdrres;
  84. stat = CLNT_CALL (client, PMAPPROC_CALLIT,
  85. (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. libc_hidden_nolink_sunrpc (pmap_rmtcall, GLIBC_2_0)
  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_nolink_sunrpc (xdr_rmtcall_args, GLIBC_2_0)
  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),
  139. (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_nolink_sunrpc (xdr_rmtcallres, GLIBC_2_0)
  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. getbroadcastnets (struct in_addr *addrs, int naddrs)
  155. {
  156. struct ifaddrs *ifa;
  157. if (getifaddrs (&ifa) != 0)
  158. {
  159. perror ("broadcast: getifaddrs");
  160. return 0;
  161. }
  162. int i = 0;
  163. struct ifaddrs *run = ifa;
  164. while (run != NULL && i < naddrs)
  165. {
  166. if ((run->ifa_flags & IFF_BROADCAST) != 0
  167. && (run->ifa_flags & IFF_UP) != 0
  168. && run->ifa_addr != NULL
  169. && run->ifa_addr->sa_family == AF_INET)
  170. /* Copy the broadcast address. */
  171. addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
  172. run = run->ifa_next;
  173. }
  174. freeifaddrs (ifa);
  175. return i;
  176. }
  177. enum clnt_stat
  178. clnt_broadcast (/* program number */
  179. u_long prog,
  180. /* version number */
  181. u_long vers,
  182. /* procedure number */
  183. u_long proc,
  184. /* xdr routine for args */
  185. xdrproc_t xargs,
  186. /* pointer to args */
  187. caddr_t argsp,
  188. /* xdr routine for results */
  189. xdrproc_t xresults,
  190. /* pointer to results */
  191. caddr_t resultsp,
  192. /* call with each result obtained */
  193. resultproc_t eachresult)
  194. {
  195. enum clnt_stat stat = RPC_FAILED;
  196. AUTH *unix_auth = authunix_create_default ();
  197. XDR xdr_stream;
  198. XDR *xdrs = &xdr_stream;
  199. struct timeval t;
  200. int outlen, inlen, nets;
  201. socklen_t fromlen;
  202. int sock;
  203. int on = 1;
  204. struct pollfd fd;
  205. int milliseconds;
  206. int i;
  207. bool_t done = FALSE;
  208. u_long xid;
  209. u_long port;
  210. struct in_addr addrs[20];
  211. struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
  212. struct rmtcallargs a;
  213. struct rmtcallres r;
  214. struct rpc_msg msg;
  215. char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  216. /*
  217. * initialization: create a socket, a broadcast address, and
  218. * preserialize the arguments into a send buffer.
  219. */
  220. if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  221. {
  222. perror (_("Cannot create socket for broadcast rpc"));
  223. stat = RPC_CANTSEND;
  224. goto done_broad;
  225. }
  226. #ifdef SO_BROADCAST
  227. if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
  228. {
  229. perror (_("Cannot set socket option SO_BROADCAST"));
  230. stat = RPC_CANTSEND;
  231. goto done_broad;
  232. }
  233. #endif /* def SO_BROADCAST */
  234. fd.fd = sock;
  235. fd.events = POLLIN;
  236. nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
  237. memset ((char *) &baddr, 0, sizeof (baddr));
  238. baddr.sin_family = AF_INET;
  239. baddr.sin_port = htons (PMAPPORT);
  240. baddr.sin_addr.s_addr = htonl (INADDR_ANY);
  241. /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
  242. msg.rm_xid = xid = _create_xid ();
  243. t.tv_usec = 0;
  244. msg.rm_direction = CALL;
  245. msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  246. msg.rm_call.cb_prog = PMAPPROG;
  247. msg.rm_call.cb_vers = PMAPVERS;
  248. msg.rm_call.cb_proc = PMAPPROC_CALLIT;
  249. msg.rm_call.cb_cred = unix_auth->ah_cred;
  250. msg.rm_call.cb_verf = unix_auth->ah_verf;
  251. a.prog = prog;
  252. a.vers = vers;
  253. a.proc = proc;
  254. a.xdr_args = xargs;
  255. a.args_ptr = argsp;
  256. r.port_ptr = &port;
  257. r.xdr_results = xresults;
  258. r.results_ptr = resultsp;
  259. xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
  260. if ((!xdr_callmsg (xdrs, &msg))
  261. || (!xdr_rmtcall_args (xdrs, &a)))
  262. {
  263. stat = RPC_CANTENCODEARGS;
  264. goto done_broad;
  265. }
  266. outlen = (int) xdr_getpos (xdrs);
  267. xdr_destroy (xdrs);
  268. /*
  269. * Basic loop: broadcast a packet and wait a while for response(s).
  270. * The response timeout grows larger per iteration.
  271. */
  272. for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
  273. {
  274. for (i = 0; i < nets; i++)
  275. {
  276. baddr.sin_addr = addrs[i];
  277. if (__sendto (sock, outbuf, outlen, 0,
  278. (struct sockaddr *) &baddr,
  279. sizeof (struct sockaddr)) != outlen)
  280. {
  281. perror (_("Cannot send broadcast packet"));
  282. stat = RPC_CANTSEND;
  283. goto done_broad;
  284. }
  285. }
  286. if (eachresult == NULL)
  287. {
  288. stat = RPC_SUCCESS;
  289. goto done_broad;
  290. }
  291. recv_again:
  292. msg.acpted_rply.ar_verf = _null_auth;
  293. msg.acpted_rply.ar_results.where = (caddr_t) & r;
  294. msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
  295. milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
  296. switch (__poll(&fd, 1, milliseconds))
  297. {
  298. case 0: /* timed out */
  299. stat = RPC_TIMEDOUT;
  300. continue;
  301. case -1: /* some kind of error */
  302. if (errno == EINTR)
  303. goto recv_again;
  304. perror (_("Broadcast poll problem"));
  305. stat = RPC_CANTRECV;
  306. goto done_broad;
  307. } /* end of poll results switch */
  308. try_again:
  309. fromlen = sizeof (struct sockaddr);
  310. inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
  311. (struct sockaddr *) &raddr, &fromlen);
  312. if (inlen < 0)
  313. {
  314. if (errno == EINTR)
  315. goto try_again;
  316. perror (_("Cannot receive reply to broadcast"));
  317. stat = RPC_CANTRECV;
  318. goto done_broad;
  319. }
  320. if ((size_t) inlen < sizeof (u_long))
  321. goto recv_again;
  322. /*
  323. * see if reply transaction id matches sent id.
  324. * If so, decode the results.
  325. */
  326. xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
  327. if (xdr_replymsg (xdrs, &msg))
  328. {
  329. if (((uint32_t) msg.rm_xid == (uint32_t) xid) &&
  330. (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  331. (msg.acpted_rply.ar_stat == SUCCESS))
  332. {
  333. raddr.sin_port = htons ((u_short) port);
  334. done = (*eachresult) (resultsp, &raddr);
  335. }
  336. /* otherwise, we just ignore the errors ... */
  337. }
  338. else
  339. {
  340. #ifdef notdef
  341. /* some kind of deserialization problem ... */
  342. if ((uint32_t) msg.rm_xid == (uint32_t) xid)
  343. fprintf (stderr, "Broadcast deserialization problem");
  344. /* otherwise, just random garbage */
  345. #endif
  346. }
  347. xdrs->x_op = XDR_FREE;
  348. msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  349. (void) xdr_replymsg (xdrs, &msg);
  350. (void) (*xresults) (xdrs, resultsp);
  351. xdr_destroy (xdrs);
  352. if (done)
  353. {
  354. stat = RPC_SUCCESS;
  355. goto done_broad;
  356. }
  357. else
  358. {
  359. goto recv_again;
  360. }
  361. }
  362. done_broad:
  363. (void) __close (sock);
  364. AUTH_DESTROY (unix_auth);
  365. return stat;
  366. }
  367. libc_hidden_nolink_sunrpc (clnt_broadcast, GLIBC_2_0)