pmap_rmt.c 12 KB

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