pmap_rmt.c 12 KB

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