pmap_rmt.c 12 KB

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