server.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include <sunrpc.h>
  5. /*
  6. * Sun RPC server; for now, no reply cache
  7. */
  8. static void sunRpcProc(void*);
  9. static void sunRpcRequestThread(void*);
  10. static void sunRpcReplyThread(void*);
  11. static void sunRpcForkThread(void*);
  12. static SunProg *sunFindProg(SunSrv*, SunMsg*, SunRpc*, Channel**);
  13. typedef struct Targ Targ;
  14. struct Targ
  15. {
  16. void (*fn)(void*);
  17. void *arg;
  18. };
  19. SunSrv*
  20. sunSrv(void)
  21. {
  22. SunSrv *srv;
  23. srv = emalloc(sizeof(SunSrv));
  24. srv->chatty = 0;
  25. srv->crequest = chancreate(sizeof(SunMsg*), 16);
  26. srv->creply = chancreate(sizeof(SunMsg*), 16);
  27. srv->cthread = chancreate(sizeof(Targ), 4);
  28. proccreate(sunRpcProc, srv, SunStackSize);
  29. return srv;
  30. }
  31. void
  32. sunSrvProg(SunSrv *srv, SunProg *prog, Channel *c)
  33. {
  34. if(srv->nprog%16 == 0){
  35. srv->prog = erealloc(srv->prog, (srv->nprog+16)*sizeof(srv->prog[0]));
  36. srv->cdispatch = erealloc(srv->cdispatch, (srv->nprog+16)*sizeof(srv->cdispatch[0]));
  37. }
  38. srv->prog[srv->nprog] = prog;
  39. srv->cdispatch[srv->nprog] = c;
  40. srv->nprog++;
  41. }
  42. static void
  43. sunRpcProc(void *v)
  44. {
  45. threadcreate(sunRpcReplyThread, v, SunStackSize);
  46. threadcreate(sunRpcRequestThread, v, SunStackSize);
  47. threadcreate(sunRpcForkThread, v, SunStackSize);
  48. }
  49. static void
  50. sunRpcForkThread(void *v)
  51. {
  52. SunSrv *srv = v;
  53. Targ t;
  54. while(recv(srv->cthread, &t) == 1)
  55. threadcreate(t.fn, t.arg, SunStackSize);
  56. }
  57. void
  58. sunSrvThreadCreate(SunSrv *srv, void (*fn)(void*), void *arg)
  59. {
  60. Targ t;
  61. t.fn = fn;
  62. t.arg = arg;
  63. send(srv->cthread, &t);
  64. }
  65. static void
  66. sunRpcRequestThread(void *v)
  67. {
  68. uchar *p, *ep;
  69. Channel *c;
  70. SunSrv *srv = v;
  71. SunMsg *m;
  72. SunProg *pg;
  73. SunStatus ok;
  74. while((m = recvp(srv->crequest)) != nil){
  75. /* could look up in cache here? */
  76. if(srv->chatty) fprint(2, "sun msg %p count %d\n", m, m->count);
  77. m->srv = srv;
  78. p = m->data;
  79. ep = p+m->count;
  80. if(sunRpcUnpack(p, ep, &p, &m->rpc) != SunSuccess){
  81. fprint(2, "in: %.*H unpack failed\n", m->count, m->data);
  82. sunMsgDrop(m);
  83. continue;
  84. }
  85. if(srv->chatty)
  86. fprint(2, "in: %B\n", &m->rpc);
  87. if(srv->alwaysReject){
  88. if(srv->chatty)
  89. fprint(2, "\trejecting\n");
  90. sunMsgReplyError(m, SunAuthTooWeak);
  91. continue;
  92. }
  93. if(!m->rpc.iscall){
  94. sunMsgReplyError(m, SunGarbageArgs);
  95. continue;
  96. }
  97. if((pg = sunFindProg(srv, m, &m->rpc, &c)) == nil){
  98. /* sunFindProg sent error */
  99. continue;
  100. }
  101. p = m->rpc.data;
  102. ep = p+m->rpc.ndata;
  103. m->call = nil;
  104. if((ok = sunCallUnpackAlloc(pg, m->rpc.proc<<1, p, ep, &p, &m->call)) != SunSuccess){
  105. sunMsgReplyError(m, ok);
  106. continue;
  107. }
  108. m->call->rpc = m->rpc;
  109. if(srv->chatty)
  110. fprint(2, "\t%C\n", m->call);
  111. m->pg = pg;
  112. sendp(c, m);
  113. }
  114. }
  115. static SunProg*
  116. sunFindProg(SunSrv *srv, SunMsg *m, SunRpc *rpc, Channel **pc)
  117. {
  118. int i, vlo, vhi;
  119. SunProg *pg;
  120. vlo = 0x7fffffff;
  121. vhi = -1;
  122. for(i=0; i<srv->nprog; i++){
  123. pg = srv->prog[i];
  124. if(pg->prog != rpc->prog)
  125. continue;
  126. if(pg->vers == rpc->vers){
  127. *pc = srv->cdispatch[i];
  128. return pg;
  129. }
  130. /* right program, wrong version: record range */
  131. if(pg->vers < vlo)
  132. vlo = pg->vers;
  133. if(pg->vers > vhi)
  134. vhi = pg->vers;
  135. }
  136. if(vhi == -1){
  137. if(srv->chatty)
  138. fprint(2, "\tprogram %ud unavailable\n", rpc->prog);
  139. sunMsgReplyError(m, SunProgUnavail);
  140. }else{
  141. /* putting these in rpc is a botch */
  142. rpc->low = vlo;
  143. rpc->high = vhi;
  144. if(srv->chatty)
  145. fprint(2, "\tversion %ud unavailable; have %d-%d\n", rpc->vers, vlo, vhi);
  146. sunMsgReplyError(m, SunProgMismatch);
  147. }
  148. return nil;
  149. }
  150. static void
  151. sunRpcReplyThread(void *v)
  152. {
  153. SunMsg *m;
  154. SunSrv *srv = v;
  155. while((m = recvp(srv->creply)) != nil){
  156. /* could record in cache here? */
  157. sendp(m->creply, m);
  158. }
  159. }
  160. int
  161. sunMsgReplyError(SunMsg *m, SunStatus error)
  162. {
  163. uchar *p, *bp, *ep;
  164. int n;
  165. m->rpc.status = error;
  166. m->rpc.iscall = 0;
  167. m->rpc.verf.flavor = SunAuthNone;
  168. m->rpc.data = nil;
  169. m->rpc.ndata = 0;
  170. if(m->srv->chatty)
  171. fprint(2, "out: %B\n", &m->rpc);
  172. n = sunRpcSize(&m->rpc);
  173. bp = emalloc(n);
  174. ep = bp+n;
  175. p = bp;
  176. if(sunRpcPack(p, ep, &p, &m->rpc) < 0){
  177. fprint(2, "sunRpcPack failed\n");
  178. sunMsgDrop(m);
  179. return 0;
  180. }
  181. if(p != ep){
  182. fprint(2, "sunMsgReplyError: rpc sizes didn't work out\n");
  183. sunMsgDrop(m);
  184. return 0;
  185. }
  186. free(m->data);
  187. m->data = bp;
  188. m->count = n;
  189. sendp(m->srv->creply, m);
  190. return 0;
  191. }
  192. int
  193. sunMsgReply(SunMsg *m, SunCall *c)
  194. {
  195. int n1, n2;
  196. uchar *bp, *p, *ep;
  197. c->type = m->call->type+1;
  198. c->rpc.iscall = 0;
  199. c->rpc.prog = m->rpc.prog;
  200. c->rpc.vers = m->rpc.vers;
  201. c->rpc.proc = m->rpc.proc;
  202. c->rpc.xid = m->rpc.xid;
  203. if(m->srv->chatty){
  204. fprint(2, "out: %B\n", &c->rpc);
  205. fprint(2, "\t%C\n", c);
  206. }
  207. n1 = sunRpcSize(&c->rpc);
  208. n2 = sunCallSize(m->pg, c);
  209. bp = emalloc(n1+n2);
  210. ep = bp+n1+n2;
  211. p = bp;
  212. if(sunRpcPack(p, ep, &p, &c->rpc) != SunSuccess){
  213. fprint(2, "sunRpcPack failed\n");
  214. return sunMsgDrop(m);
  215. }
  216. if(sunCallPack(m->pg, p, ep, &p, c) != SunSuccess){
  217. fprint(2, "pg->pack failed\n");
  218. return sunMsgDrop(m);
  219. }
  220. if(p != ep){
  221. fprint(2, "sunMsgReply: sizes didn't work out\n");
  222. return sunMsgDrop(m);
  223. }
  224. free(m->data);
  225. m->data = bp;
  226. m->count = n1+n2;
  227. sendp(m->srv->creply, m);
  228. return 0;
  229. }
  230. int
  231. sunMsgDrop(SunMsg *m)
  232. {
  233. free(m->data);
  234. free(m->call);
  235. memset(m, 0xFB, sizeof *m);
  236. free(m);
  237. return 0;
  238. }