pmap_rmt.c 11 KB

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