mport.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. typedef struct Rpcconn Rpcconn;
  11. struct Rpcconn
  12. {
  13. int data;
  14. int ctl;
  15. Rpccall cmd;
  16. Rpccall reply;
  17. uint8_t rpcbuf[8192];
  18. uint8_t argbuf[8192];
  19. };
  20. void putauth(char*, Auth*);
  21. int rpccall(Rpcconn*, int);
  22. int rpcdebug;
  23. Rpcconn r;
  24. char * mach;
  25. void
  26. main(int argc, char **argv)
  27. {
  28. char addr[64], dir[64], name[128];
  29. char buf[33], *p;
  30. unsigned char *dataptr, *argptr;
  31. int i, fd, n, remport;
  32. ARGBEGIN{
  33. case 'm':
  34. mach = ARGF();
  35. break;
  36. case 'D':
  37. ++rpcdebug;
  38. break;
  39. }ARGEND
  40. if(argc != 1)
  41. exits("usage");
  42. snprint(addr, sizeof addr, "udp!%s!111", argv[0]);
  43. r.data = dial(addr, 0, dir, &r.ctl);
  44. if(r.data < 0){
  45. fprint(2, "dial %s: %r\n", addr);
  46. exits("dial");
  47. }
  48. if(rpcdebug)
  49. fprint(2, "dial %s: dir=%s\n", addr, dir);
  50. if(fprint(r.ctl, "headers") < 0){
  51. fprint(2, "can't set header mode: %r\n");
  52. exits("headers");
  53. }
  54. sprint(name, "%s/remote", dir);
  55. fd = open(name, OREAD);
  56. if(fd < 0){
  57. fprint(2, "can't open %s: %r\n", name);
  58. exits("remote");
  59. }
  60. n = read(fd, buf, sizeof buf-1);
  61. if(n < 0){
  62. fprint(2, "can't read %s: %r\n", name);
  63. exits("remote");
  64. }
  65. close(fd);
  66. buf[n] = 0;
  67. p = buf;
  68. r.cmd.host = 0;
  69. for(i=0; i<4; i++, p++)
  70. r.cmd.host = (r.cmd.host<<8)|strtol(p, &p, 10);
  71. r.cmd.port = strtol(p, 0, 10);
  72. fprint(2, "host=%ld.%ld.%ld.%ld, port=%lud\n",
  73. (r.cmd.host>>24)&0xff, (r.cmd.host>>16)&0xff,
  74. (r.cmd.host>>8)&0xff, r.cmd.host&0xff, r.cmd.port);
  75. fprint(r.ctl, "disconnect");
  76. r.cmd.xid = time(0);
  77. r.cmd.mtype = CALL;
  78. r.cmd.rpcvers = 2;
  79. r.cmd.args = r.argbuf;
  80. if(mach)
  81. putauth(mach, &r.cmd.cred);
  82. r.cmd.prog = 100000; /* portmapper */
  83. r.cmd.vers = 2; /* vers */
  84. r.cmd.proc = 3; /* getport */
  85. dataptr = r.cmd.args;
  86. PLONG(100005); /* mount */
  87. PLONG(1); /* vers */
  88. PLONG(IPPROTO_UDP);
  89. PLONG(0);
  90. i = rpccall(&r, dataptr-(unsigned char*)r.cmd.args);
  91. if(i != 4)
  92. exits("trouble");
  93. argptr = r.reply.results;
  94. remport = GLONG();
  95. fprint(2, "remote port = %d\n", remport);
  96. r.cmd.port = remport;
  97. r.cmd.prog = 100005; /* mount */
  98. r.cmd.vers = 1; /* vers */
  99. r.cmd.proc = 0; /* null */
  100. dataptr = r.cmd.args;
  101. i = rpccall(&r, dataptr-(unsigned char*)r.cmd.args);
  102. if(i != 0)
  103. exits("trouble");
  104. fprint(2, "OK ping mount\n");
  105. r.cmd.prog = 100005; /* mount */
  106. r.cmd.vers = 1; /* vers */
  107. r.cmd.proc = 5; /* export */
  108. dataptr = r.cmd.args;
  109. i = rpccall(&r, dataptr-(unsigned char*)r.cmd.args);
  110. fprint(2, "export: %d bytes returned\n", i);
  111. argptr = r.reply.results;
  112. while (GLONG() != 0) {
  113. n = GLONG();
  114. p = GPTR(n);
  115. print("%.*s\n", utfnlen(p, n), p);
  116. while (GLONG() != 0) {
  117. n = GLONG();
  118. p = GPTR(n);
  119. print("\t%.*s\n", utfnlen(p, n), p);
  120. }
  121. }
  122. exits(0);
  123. }
  124. void
  125. putauth(char *mach, Auth *a)
  126. {
  127. uint8_t *dataptr;
  128. int32_t stamp = time(0);
  129. int n = strlen(mach);
  130. dataptr = realloc(a->data, 2*4+ROUNDUP(n)+4*4);
  131. a->data = dataptr;
  132. a->flavor = AUTH_UNIX;
  133. PLONG(stamp);
  134. PLONG(n);
  135. PPTR(mach, n);
  136. PLONG(0);
  137. PLONG(1);
  138. PLONG(1);
  139. PLONG(0);
  140. a->count = dataptr - (uint8_t*)a->data;
  141. }
  142. int
  143. rpccall(Rpcconn *r, int narg)
  144. {
  145. int n;
  146. r->cmd.xid++;
  147. n = rpcS2M(&r->cmd, narg, r->rpcbuf);
  148. if(rpcdebug)
  149. rpcprint(2, &r->cmd);
  150. if(write(r->data, r->rpcbuf, n) < 0){
  151. fprint(2, "rpc write: %r\n");
  152. exits("rpc");
  153. }
  154. n = read(r->data, r->rpcbuf, sizeof r->rpcbuf);
  155. if(n < 0){
  156. fprint(2, "rpc read: %r\n");
  157. exits("rpc");
  158. }
  159. if(rpcM2S(r->rpcbuf, &r->reply, n) != 0){
  160. fprint(2, "rpc reply format error\n");
  161. exits("rpc");
  162. }
  163. if(rpcdebug)
  164. rpcprint(2, &r->reply);
  165. if(r->reply.mtype != REPLY || r->reply.stat != MSG_ACCEPTED ||
  166. r->reply.astat != SUCCESS){
  167. fprint(2, "rpc mtype, stat, astat = %ld, %ld, %ld\n",
  168. r->reply.mtype, r->reply.stat, r->reply.astat);
  169. exits("rpc");
  170. }
  171. return n - (((uint8_t *)r->reply.results) - r->rpcbuf);
  172. }