pmap_rmt.c 12 KB

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