rpc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #define SHORT(x) r->x = (p[1] | (p[0]<<8)); p += 2
  11. #define LONG(x) r->x = (p[3] | (p[2]<<8) |\
  12. (p[1]<<16) | (p[0]<<24)); p += 4
  13. #define SKIPLONG p += 4
  14. #define PTR(x, n) r->x = (void *)(p); p += ROUNDUP(n)
  15. int
  16. rpcM2S(void *ap, Rpccall *r, int n)
  17. {
  18. int k;
  19. uint8_t *p;
  20. Udphdr *up;
  21. /* copy IPv4 header fields from Udphdr */
  22. up = ap;
  23. p = &up->raddr[IPaddrlen - IPv4addrlen];
  24. LONG(host);
  25. USED(p);
  26. p = &up->laddr[IPaddrlen - IPv4addrlen];
  27. LONG(lhost);
  28. USED(p);
  29. /* ignore up->ifcaddr */
  30. p = up->rport;
  31. SHORT(port);
  32. SHORT(lport);
  33. LONG(xid);
  34. LONG(mtype);
  35. switch(r->mtype){
  36. case CALL:
  37. LONG(rpcvers);
  38. if(r->rpcvers != 2)
  39. break;
  40. LONG(prog);
  41. LONG(vers);
  42. LONG(proc);
  43. LONG(cred.flavor);
  44. LONG(cred.count);
  45. PTR(cred.data, r->cred.count);
  46. LONG(verf.flavor);
  47. LONG(verf.count);
  48. PTR(verf.data, r->verf.count);
  49. r->up = 0;
  50. k = n - (p - (uint8_t *)ap);
  51. if(k < 0)
  52. break;
  53. PTR(args, k);
  54. break;
  55. case REPLY:
  56. LONG(stat);
  57. switch(r->stat){
  58. case MSG_ACCEPTED:
  59. LONG(averf.flavor);
  60. LONG(averf.count);
  61. PTR(averf.data, r->averf.count);
  62. LONG(astat);
  63. switch(r->astat){
  64. case SUCCESS:
  65. k = n - (p - (uint8_t *)ap);
  66. if(k < 0)
  67. break;
  68. PTR(results, k);
  69. break;
  70. case PROG_MISMATCH:
  71. LONG(plow);
  72. LONG(phigh);
  73. break;
  74. }
  75. break;
  76. case MSG_DENIED:
  77. LONG(rstat);
  78. switch(r->rstat){
  79. case RPC_MISMATCH:
  80. LONG(rlow);
  81. LONG(rhigh);
  82. break;
  83. case AUTH_ERROR:
  84. LONG(authstat);
  85. break;
  86. }
  87. break;
  88. }
  89. break;
  90. }
  91. n -= p - (uint8_t *)ap;
  92. return n;
  93. }
  94. int
  95. auth2unix(Auth *arg, Authunix *r)
  96. {
  97. int i, n;
  98. uint8_t *p;
  99. if(arg->flavor != AUTH_UNIX)
  100. return -1;
  101. p = arg->data;
  102. LONG(stamp);
  103. LONG(mach.n);
  104. PTR(mach.s, r->mach.n);
  105. LONG(uid);
  106. LONG(gid);
  107. LONG(gidlen);
  108. n = r->gidlen;
  109. for(i=0; i<n && i < nelem(r->gids); i++){
  110. LONG(gids[i]);
  111. }
  112. for(; i<n; i++){
  113. SKIPLONG;
  114. }
  115. return arg->count - (p - (uint8_t *)arg->data);
  116. }
  117. int
  118. string2S(void *arg, String *r)
  119. {
  120. uint8_t *p;
  121. char *s;
  122. p = arg;
  123. LONG(n);
  124. PTR(s, r->n);
  125. /* must NUL terminate */
  126. s = malloc(r->n+1);
  127. if(s == nil)
  128. panic("malloc(%ld) failed in string2S\n", r->n+1);
  129. memmove(s, r->s, r->n);
  130. s[r->n] = '\0';
  131. r->s = strstore(s);
  132. free(s);
  133. return p - (uint8_t *)arg;
  134. }
  135. #undef SHORT
  136. #undef LONG
  137. #undef PTR
  138. #define SHORT(x) p[1] = r->x; p[0] = r->x>>8; p += 2
  139. #define LONG(x) p[3] = r->x; p[2] = r->x>>8; p[1] = r->x>>16; p[0] = r->x>>24; p += 4
  140. #define PTR(x,n) memmove(p, r->x, n); p += ROUNDUP(n)
  141. int
  142. rpcS2M(Rpccall *r, int ndata, void *ap)
  143. {
  144. uint8_t *p;
  145. Udphdr *up;
  146. /* copy header fields to Udphdr */
  147. up = ap;
  148. memmove(up->raddr, v4prefix, IPaddrlen);
  149. p = &up->raddr[IPaddrlen - IPv4addrlen];
  150. LONG(host);
  151. USED(p);
  152. memmove(up->laddr, v4prefix, IPaddrlen);
  153. p = &up->laddr[IPaddrlen - IPv4addrlen];
  154. LONG(lhost);
  155. USED(p);
  156. memmove(up->ifcaddr, IPnoaddr, sizeof up->ifcaddr);
  157. p = up->rport;
  158. SHORT(port);
  159. SHORT(lport);
  160. LONG(xid);
  161. LONG(mtype);
  162. switch(r->mtype){
  163. case CALL:
  164. LONG(rpcvers);
  165. LONG(prog);
  166. LONG(vers);
  167. LONG(proc);
  168. LONG(cred.flavor);
  169. LONG(cred.count);
  170. PTR(cred.data, r->cred.count);
  171. LONG(verf.flavor);
  172. LONG(verf.count);
  173. PTR(verf.data, r->verf.count);
  174. PTR(args, ndata);
  175. break;
  176. case REPLY:
  177. LONG(stat);
  178. switch(r->stat){
  179. case MSG_ACCEPTED:
  180. LONG(averf.flavor);
  181. LONG(averf.count);
  182. PTR(averf.data, r->averf.count);
  183. LONG(astat);
  184. switch(r->astat){
  185. case SUCCESS:
  186. PTR(results, ndata);
  187. break;
  188. case PROG_MISMATCH:
  189. LONG(plow);
  190. LONG(phigh);
  191. break;
  192. }
  193. break;
  194. case MSG_DENIED:
  195. LONG(rstat);
  196. switch(r->rstat){
  197. case RPC_MISMATCH:
  198. LONG(rlow);
  199. LONG(rhigh);
  200. break;
  201. case AUTH_ERROR:
  202. LONG(authstat);
  203. break;
  204. }
  205. break;
  206. }
  207. break;
  208. }
  209. return p - (uint8_t *)ap;
  210. }
  211. #undef SHORT
  212. #undef LONG
  213. #undef PTR
  214. #define LONG(m, x) fprint(fd, "%s = %ld\n", m, r->x)
  215. #define PTR(m, count) fprint(fd, "%s [%ld]\n", m, count)
  216. void
  217. rpcprint(int fd, Rpccall *r)
  218. {
  219. fprint(fd, "%s: host = %I, port = %ld\n",
  220. argv0, r->host, r->port);
  221. LONG("xid", xid);
  222. LONG("mtype", mtype);
  223. switch(r->mtype){
  224. case CALL:
  225. LONG("rpcvers", rpcvers);
  226. LONG("prog", prog);
  227. LONG("vers", vers);
  228. LONG("proc", proc);
  229. LONG("cred.flavor", cred.flavor);
  230. PTR("cred.data", r->cred.count);
  231. LONG("verf.flavor", verf.flavor);
  232. PTR("verf.data", r->verf.count);
  233. fprint(fd, "args...\n");
  234. break;
  235. case REPLY:
  236. LONG("stat", stat);
  237. switch(r->stat){
  238. case MSG_ACCEPTED:
  239. LONG("averf.flavor", averf.flavor);
  240. PTR("averf.data", r->averf.count);
  241. LONG("astat", astat);
  242. switch(r->astat){
  243. case SUCCESS:
  244. fprint(fd, "results...\n");
  245. break;
  246. case PROG_MISMATCH:
  247. LONG("plow", plow);
  248. LONG("phigh", phigh);
  249. break;
  250. }
  251. break;
  252. case MSG_DENIED:
  253. LONG("rstat", rstat);
  254. switch(r->rstat){
  255. case RPC_MISMATCH:
  256. LONG("rlow", rlow);
  257. LONG("rhigh", rhigh);
  258. break;
  259. case AUTH_ERROR:
  260. LONG("authstat", authstat);
  261. break;
  262. }
  263. break;
  264. }
  265. }
  266. }
  267. void
  268. showauth(Auth *ap)
  269. {
  270. Authunix au;
  271. int i;
  272. if(auth2unix(ap, &au) != 0){
  273. chat("auth flavor=%ld, count=%ld",
  274. ap->flavor, ap->count);
  275. for(i=0; i<ap->count; i++)
  276. chat(" %.2ux", ((uint8_t *)ap->data)[i]);
  277. }else{
  278. chat("auth: %ld %.*s u=%ld g=%ld",
  279. au.stamp, utfnlen(au.mach.s, au.mach.n), au.mach.s, au.uid, au.gid);
  280. for(i=0; i<au.gidlen; i++)
  281. chat(", %ld", au.gids[i]);
  282. }
  283. chat("...");
  284. }
  285. int
  286. garbage(Rpccall *reply, char *msg)
  287. {
  288. chat("%s\n", msg ? msg : "garbage");
  289. reply->astat = GARBAGE_ARGS;
  290. return 0;
  291. }
  292. int
  293. error(Rpccall *reply, int errno)
  294. {
  295. uint8_t *dataptr = reply->results;
  296. chat("error %d\n", errno);
  297. PLONG(errno);
  298. return dataptr - (uint8_t *)reply->results;
  299. }