pmap_rmt.c 12 KB

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