pmap_rmt.c 12 KB

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