pmap_rmt.c 11 KB

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