pmap_rmt.c 11 KB

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