pmap_rmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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, cap)
  101. register XDR *xdrs;
  102. register struct rmtcallargs *cap;
  103. {
  104. u_int lenposition, argposition, position;
  105. if (xdr_u_long(xdrs, &(cap->prog)) &&
  106. xdr_u_long(xdrs, &(cap->vers)) && xdr_u_long(xdrs, &(cap->proc))) {
  107. lenposition = XDR_GETPOS(xdrs);
  108. if (!xdr_u_long(xdrs, &(cap->arglen)))
  109. return (FALSE);
  110. argposition = XDR_GETPOS(xdrs);
  111. if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
  112. return (FALSE);
  113. position = XDR_GETPOS(xdrs);
  114. cap->arglen = (u_long) position - (u_long) argposition;
  115. XDR_SETPOS(xdrs, lenposition);
  116. if (!xdr_u_long(xdrs, &(cap->arglen)))
  117. return (FALSE);
  118. XDR_SETPOS(xdrs, position);
  119. return (TRUE);
  120. }
  121. return (FALSE);
  122. }
  123. /*
  124. * XDR remote call results
  125. * written for XDR_DECODE direction only
  126. */
  127. bool_t xdr_rmtcallres(xdrs, crp)
  128. register XDR *xdrs;
  129. register struct rmtcallres *crp;
  130. {
  131. caddr_t port_ptr;
  132. port_ptr = (caddr_t) crp->port_ptr;
  133. if (xdr_reference(xdrs, &port_ptr, sizeof(u_long),
  134. xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
  135. crp->port_ptr = (u_long *) port_ptr;
  136. return ((*(crp->xdr_results)) (xdrs, crp->results_ptr));
  137. }
  138. return (FALSE);
  139. }
  140. /*
  141. * The following is kludged-up support for simple rpc broadcasts.
  142. * Someday a large, complicated system will replace these trivial
  143. * routines which only support udp/ip .
  144. */
  145. static int getbroadcastnets(addrs, sock, buf)
  146. struct in_addr *addrs;
  147. int sock; /* any valid socket will do */
  148. char *buf; /* why allocxate more when we can use existing... */
  149. {
  150. #ifdef __linux__
  151. struct sockaddr_in addr;
  152. get_myaddress(&addr);
  153. #if 1
  154. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  155. #else
  156. addrs[0] = inet_makeaddr(inet_netof(addr.sin_addr), INADDR_ANY);
  157. #endif
  158. return 1;
  159. #else
  160. struct ifconf ifc;
  161. struct ifreq ifreq, *ifr;
  162. struct sockaddr_in *sin;
  163. int n, i;
  164. ifc.ifc_len = UDPMSGSIZE;
  165. ifc.ifc_buf = buf;
  166. if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
  167. perror("broadcast: ioctl (get interface configuration)");
  168. return (0);
  169. }
  170. ifr = ifc.ifc_req;
  171. for (i = 0, n = ifc.ifc_len / sizeof(struct ifreq); n > 0; n--, ifr++) {
  172. ifreq = *ifr;
  173. if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
  174. perror("broadcast: ioctl (get interface flags)");
  175. continue;
  176. }
  177. if ((ifreq.ifr_flags & IFF_BROADCAST) &&
  178. (ifreq.ifr_flags & IFF_UP) &&
  179. ifr->ifr_addr.sa_family == AF_INET) {
  180. sin = (struct sockaddr_in *) &ifr->ifr_addr;
  181. #ifdef SIOCGIFBRDADDR /* 4.3BSD */
  182. if (ioctl(sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0) {
  183. #if 1
  184. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  185. #else
  186. addrs[i++] = inet_makeaddr(inet_netof
  187. (sin->sin_addr.s_addr),
  188. INADDR_ANY);
  189. #endif
  190. } else {
  191. addrs[i++] = ((struct sockaddr_in *)
  192. &ifreq.ifr_addr)->sin_addr;
  193. }
  194. #else /* 4.2 BSD */
  195. #if 1
  196. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  197. #else
  198. addrs[i++] = inet_makeaddr(inet_netof
  199. (sin->sin_addr.s_addr), INADDR_ANY);
  200. #endif
  201. #endif
  202. }
  203. }
  204. return (i);
  205. #endif
  206. }
  207. typedef bool_t(*resultproc_t) ();
  208. enum clnt_stat
  209. clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
  210. eachresult)
  211. u_long prog; /* program number */
  212. u_long vers; /* version number */
  213. u_long proc; /* procedure number */
  214. xdrproc_t xargs; /* xdr routine for args */
  215. caddr_t argsp; /* pointer to args */
  216. xdrproc_t xresults; /* xdr routine for results */
  217. caddr_t resultsp; /* pointer to results */
  218. resultproc_t eachresult; /* call with each result obtained */
  219. {
  220. enum clnt_stat stat;
  221. AUTH *unix_auth = authunix_create_default();
  222. XDR xdr_stream;
  223. register XDR *xdrs = &xdr_stream;
  224. int outlen, inlen, fromlen, nets;
  225. register int sock;
  226. int on = 1;
  227. #ifdef FD_SETSIZE
  228. fd_set mask;
  229. fd_set readfds;
  230. #else
  231. int readfds;
  232. register int mask;
  233. #endif /* def FD_SETSIZE */
  234. register int i;
  235. bool_t done = FALSE;
  236. register 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. struct timeval t;
  244. char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  245. /*
  246. * initialization: create a socket, a broadcast address, and
  247. * preserialize the arguments into a send buffer.
  248. */
  249. if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  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. perror("Cannot set socket option SO_BROADCAST");
  257. stat = RPC_CANTSEND;
  258. goto done_broad;
  259. }
  260. #endif /* def SO_BROADCAST */
  261. #ifdef FD_SETSIZE
  262. FD_ZERO(&mask);
  263. FD_SET(sock, &mask);
  264. #else
  265. mask = (1 << sock);
  266. #endif /* def FD_SETSIZE */
  267. nets = getbroadcastnets(addrs, sock, inbuf);
  268. bzero((char *) &baddr, sizeof(baddr));
  269. baddr.sin_family = AF_INET;
  270. baddr.sin_port = htons(PMAPPORT);
  271. baddr.sin_addr.s_addr = htonl(INADDR_ANY);
  272. /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
  273. (void) gettimeofday(&t, (struct timezone *) 0);
  274. msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
  275. t.tv_usec = 0;
  276. msg.rm_direction = CALL;
  277. msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  278. msg.rm_call.cb_prog = PMAPPROG;
  279. msg.rm_call.cb_vers = PMAPVERS;
  280. msg.rm_call.cb_proc = PMAPPROC_CALLIT;
  281. msg.rm_call.cb_cred = unix_auth->ah_cred;
  282. msg.rm_call.cb_verf = unix_auth->ah_verf;
  283. a.prog = prog;
  284. a.vers = vers;
  285. a.proc = proc;
  286. a.xdr_args = xargs;
  287. a.args_ptr = argsp;
  288. r.port_ptr = &port;
  289. r.xdr_results = xresults;
  290. r.results_ptr = resultsp;
  291. xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
  292. if ((!xdr_callmsg(xdrs, &msg)) || (!xdr_rmtcall_args(xdrs, &a))) {
  293. stat = RPC_CANTENCODEARGS;
  294. goto done_broad;
  295. }
  296. outlen = (int) xdr_getpos(xdrs);
  297. xdr_destroy(xdrs);
  298. /*
  299. * Basic loop: broadcast a packet and wait a while for response(s).
  300. * The response timeout grows larger per iteration.
  301. */
  302. for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
  303. for (i = 0; i < nets; i++) {
  304. baddr.sin_addr = addrs[i];
  305. if (sendto(sock, outbuf, outlen, 0,
  306. (struct sockaddr *) &baddr,
  307. sizeof(struct sockaddr)) != outlen) {
  308. perror("Cannot send broadcast packet");
  309. stat = RPC_CANTSEND;
  310. goto done_broad;
  311. }
  312. }
  313. if (eachresult == NULL) {
  314. stat = RPC_SUCCESS;
  315. goto done_broad;
  316. }
  317. recv_again:
  318. msg.acpted_rply.ar_verf = _null_auth;
  319. msg.acpted_rply.ar_results.where = (caddr_t) & r;
  320. msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
  321. readfds = mask;
  322. switch (select(_rpc_dtablesize(), &readfds, (int *) NULL,
  323. (int *) NULL, &t)) {
  324. case 0: /* timed out */
  325. stat = RPC_TIMEDOUT;
  326. continue;
  327. case -1: /* some kind of error */
  328. if (errno == EINTR)
  329. goto recv_again;
  330. perror("Broadcast select problem");
  331. stat = RPC_CANTRECV;
  332. goto done_broad;
  333. } /* end of select results switch */
  334. try_again:
  335. fromlen = sizeof(struct sockaddr);
  336. inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
  337. (struct sockaddr *) &raddr, &fromlen);
  338. if (inlen < 0) {
  339. if (errno == EINTR)
  340. goto try_again;
  341. perror("Cannot receive reply to broadcast");
  342. stat = RPC_CANTRECV;
  343. goto done_broad;
  344. }
  345. if (inlen < sizeof(u_long))
  346. goto recv_again;
  347. /*
  348. * see if reply transaction id matches sent id.
  349. * If so, decode the results.
  350. */
  351. xdrmem_create(xdrs, inbuf, (u_int) inlen, XDR_DECODE);
  352. if (xdr_replymsg(xdrs, &msg)) {
  353. if ((msg.rm_xid == xid) &&
  354. (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  355. (msg.acpted_rply.ar_stat == SUCCESS)) {
  356. raddr.sin_port = htons((u_short) port);
  357. done = (*eachresult) (resultsp, &raddr);
  358. }
  359. /* otherwise, we just ignore the errors ... */
  360. } else {
  361. #ifdef notdef
  362. /* some kind of deserialization problem ... */
  363. if (msg.rm_xid == xid)
  364. fprintf(stderr, "Broadcast deserialization problem");
  365. /* otherwise, just random garbage */
  366. #endif
  367. }
  368. xdrs->x_op = XDR_FREE;
  369. msg.acpted_rply.ar_results.proc = xdr_void;
  370. (void) xdr_replymsg(xdrs, &msg);
  371. (void) (*xresults) (xdrs, resultsp);
  372. xdr_destroy(xdrs);
  373. if (done) {
  374. stat = RPC_SUCCESS;
  375. goto done_broad;
  376. } else {
  377. goto recv_again;
  378. }
  379. }
  380. done_broad:
  381. (void) close(sock);
  382. AUTH_DESTROY(unix_auth);
  383. return (stat);
  384. }