pmap_rmt.c 11 KB

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