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, xdr_rmtcall_args, &a,
  90. xdr_rmtcallres, &r, tout);
  91. CLNT_DESTROY(client);
  92. } else {
  93. stat = RPC_FAILED;
  94. }
  95. (void) close(socket);
  96. addr->sin_port = 0;
  97. return (stat);
  98. }
  99. /*
  100. * XDR remote call arguments
  101. * written for XDR_ENCODE direction only
  102. */
  103. bool_t xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
  104. {
  105. u_int lenposition, argposition, position;
  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. void get_myaddress( struct sockaddr_in *addr);
  142. /*
  143. * The following is kludged-up support for simple rpc broadcasts.
  144. * Someday a large, complicated system will replace these trivial
  145. * routines which only support udp/ip .
  146. */
  147. static int getbroadcastnets(addrs, sock, buf)
  148. struct in_addr *addrs;
  149. int sock; /* any valid socket will do */
  150. char *buf; /* why allocxate more when we can use existing... */
  151. {
  152. #ifdef __linux__
  153. struct sockaddr_in addr;
  154. get_myaddress(&addr);
  155. #if 1
  156. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  157. #else
  158. addrs[0] = inet_makeaddr(inet_netof(addr.sin_addr), INADDR_ANY);
  159. #endif
  160. return 1;
  161. #else
  162. struct ifconf ifc;
  163. struct ifreq ifreq, *ifr;
  164. struct sockaddr_in *sin;
  165. int n, i;
  166. ifc.ifc_len = UDPMSGSIZE;
  167. ifc.ifc_buf = buf;
  168. if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
  169. perror("broadcast: ioctl (get interface configuration)");
  170. return (0);
  171. }
  172. ifr = ifc.ifc_req;
  173. for (i = 0, n = ifc.ifc_len / sizeof(struct ifreq); n > 0; n--, ifr++) {
  174. ifreq = *ifr;
  175. if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
  176. perror("broadcast: ioctl (get interface flags)");
  177. continue;
  178. }
  179. if ((ifreq.ifr_flags & IFF_BROADCAST) &&
  180. (ifreq.ifr_flags & IFF_UP) &&
  181. ifr->ifr_addr.sa_family == AF_INET) {
  182. sin = (struct sockaddr_in *) &ifr->ifr_addr;
  183. #ifdef SIOCGIFBRDADDR /* 4.3BSD */
  184. if (ioctl(sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0) {
  185. #if 1
  186. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  187. #else
  188. addrs[i++] = inet_makeaddr(inet_netof
  189. (sin->sin_addr.s_addr),
  190. INADDR_ANY);
  191. #endif
  192. } else {
  193. addrs[i++] = ((struct sockaddr_in *)
  194. &ifreq.ifr_addr)->sin_addr;
  195. }
  196. #else /* 4.2 BSD */
  197. #if 1
  198. printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__);
  199. #else
  200. addrs[i++] = inet_makeaddr(inet_netof
  201. (sin->sin_addr.s_addr), INADDR_ANY);
  202. #endif
  203. #endif
  204. }
  205. }
  206. return (i);
  207. #endif
  208. }
  209. //typedef bool_t(*resultproc_t) ();
  210. //void _rpc_dtablesize(void );
  211. enum clnt_stat
  212. clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
  213. 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;
  224. AUTH *unix_auth = authunix_create_default();
  225. XDR xdr_stream;
  226. register XDR *xdrs = &xdr_stream;
  227. int outlen, inlen, fromlen, nets;
  228. register int sock;
  229. int on = 1;
  230. #ifdef FD_SETSIZE
  231. fd_set mask;
  232. fd_set readfds;
  233. #else
  234. int readfds;
  235. register int mask;
  236. #endif /* def FD_SETSIZE */
  237. register int i;
  238. bool_t done = FALSE;
  239. register u_long xid;
  240. u_long port;
  241. struct in_addr addrs[20];
  242. struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
  243. struct rmtcallargs a;
  244. struct rmtcallres r;
  245. struct rpc_msg msg;
  246. struct timeval t;
  247. char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  248. /*
  249. * initialization: create a socket, a broadcast address, and
  250. * preserialize the arguments into a send buffer.
  251. */
  252. if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  253. perror("Cannot create socket for broadcast rpc");
  254. stat = RPC_CANTSEND;
  255. goto done_broad;
  256. }
  257. #ifdef SO_BROADCAST
  258. if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
  259. perror("Cannot set socket option SO_BROADCAST");
  260. stat = RPC_CANTSEND;
  261. goto done_broad;
  262. }
  263. #endif /* def SO_BROADCAST */
  264. #ifdef FD_SETSIZE
  265. FD_ZERO(&mask);
  266. FD_SET(sock, &mask);
  267. #else
  268. mask = (1 << sock);
  269. #endif /* def FD_SETSIZE */
  270. nets = getbroadcastnets(addrs, sock, inbuf);
  271. bzero((char *) &baddr, sizeof(baddr));
  272. baddr.sin_family = AF_INET;
  273. baddr.sin_port = htons(PMAPPORT);
  274. baddr.sin_addr.s_addr = htonl(INADDR_ANY);
  275. /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
  276. (void) gettimeofday(&t, (struct timezone *) 0);
  277. msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
  278. t.tv_usec = 0;
  279. msg.rm_direction = CALL;
  280. msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  281. msg.rm_call.cb_prog = PMAPPROG;
  282. msg.rm_call.cb_vers = PMAPVERS;
  283. msg.rm_call.cb_proc = PMAPPROC_CALLIT;
  284. msg.rm_call.cb_cred = unix_auth->ah_cred;
  285. msg.rm_call.cb_verf = unix_auth->ah_verf;
  286. a.prog = prog;
  287. a.vers = vers;
  288. a.proc = proc;
  289. a.xdr_args = xargs;
  290. a.args_ptr = argsp;
  291. r.port_ptr = &port;
  292. r.xdr_results = xresults;
  293. r.results_ptr = resultsp;
  294. xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
  295. if ((!xdr_callmsg(xdrs, &msg)) || (!xdr_rmtcall_args(xdrs, &a))) {
  296. stat = RPC_CANTENCODEARGS;
  297. goto done_broad;
  298. }
  299. outlen = (int) xdr_getpos(xdrs);
  300. xdr_destroy(xdrs);
  301. /*
  302. * Basic loop: broadcast a packet and wait a while for response(s).
  303. * The response timeout grows larger per iteration.
  304. */
  305. for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
  306. for (i = 0; i < nets; i++) {
  307. baddr.sin_addr = addrs[i];
  308. if (sendto(sock, outbuf, outlen, 0,
  309. (struct sockaddr *) &baddr,
  310. sizeof(struct sockaddr)) != outlen) {
  311. perror("Cannot send broadcast packet");
  312. stat = RPC_CANTSEND;
  313. goto done_broad;
  314. }
  315. }
  316. if (eachresult == NULL) {
  317. stat = RPC_SUCCESS;
  318. goto done_broad;
  319. }
  320. recv_again:
  321. msg.acpted_rply.ar_verf = _null_auth;
  322. msg.acpted_rply.ar_results.where = (caddr_t) & r;
  323. msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
  324. readfds = mask;
  325. switch (select(_rpc_dtablesize(), &readfds, (int *) NULL,
  326. (int *) 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 = 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. }