server.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. #include "session.h"
  5. static char EAuthState[] = "bad authentication state";
  6. static char ENotServer[] = "not a server session";
  7. static char EVersion[] = "incorrect version number";
  8. static char EProtocolBotch[] = "venti protocol botch";
  9. VtSession *
  10. vtServerAlloc(VtServerVtbl *vtbl)
  11. {
  12. VtSession *z = vtAlloc();
  13. z->vtbl = vtMemAlloc(sizeof(VtServerVtbl));
  14. *z->vtbl = *vtbl;
  15. return z;
  16. }
  17. static int
  18. srvHello(VtSession *z, char *version, char *uid, int , uchar *, int , uchar *, int )
  19. {
  20. vtLock(z->lk);
  21. if(z->auth.state != VtAuthHello) {
  22. vtSetError(EAuthState);
  23. goto Err;
  24. }
  25. if(strcmp(version, vtGetVersion(z)) != 0) {
  26. vtSetError(EVersion);
  27. goto Err;
  28. }
  29. vtMemFree(z->uid);
  30. z->uid = vtStrDup(uid);
  31. z->auth.state = VtAuthOK;
  32. vtUnlock(z->lk);
  33. return 1;
  34. Err:
  35. z->auth.state = VtAuthFailed;
  36. vtUnlock(z->lk);
  37. return 0;
  38. }
  39. static int
  40. dispatchHello(VtSession *z, Packet **pkt)
  41. {
  42. char *version, *uid;
  43. uchar *crypto, *codec;
  44. uchar buf[10];
  45. int ncrypto, ncodec, cryptoStrength;
  46. int ret;
  47. Packet *p;
  48. p = *pkt;
  49. version = nil;
  50. uid = nil;
  51. crypto = nil;
  52. codec = nil;
  53. ret = 0;
  54. if(!vtGetString(p, &version))
  55. goto Err;
  56. if(!vtGetString(p, &uid))
  57. goto Err;
  58. if(!packetConsume(p, buf, 2))
  59. goto Err;
  60. cryptoStrength = buf[0];
  61. ncrypto = buf[1];
  62. crypto = vtMemAlloc(ncrypto);
  63. if(!packetConsume(p, crypto, ncrypto))
  64. goto Err;
  65. if(!packetConsume(p, buf, 1))
  66. goto Err;
  67. ncodec = buf[0];
  68. codec = vtMemAlloc(ncodec);
  69. if(!packetConsume(p, codec, ncodec))
  70. goto Err;
  71. if(packetSize(p) != 0) {
  72. vtSetError(EProtocolBotch);
  73. goto Err;
  74. }
  75. if(!srvHello(z, version, uid, cryptoStrength, crypto, ncrypto, codec, ncodec)) {
  76. packetFree(p);
  77. *pkt = nil;
  78. } else {
  79. if(!vtAddString(p, vtGetSid(z)))
  80. goto Err;
  81. buf[0] = vtGetCrypto(z);
  82. buf[1] = vtGetCodec(z);
  83. packetAppend(p, buf, 2);
  84. }
  85. ret = 1;
  86. Err:
  87. vtMemFree(version);
  88. vtMemFree(uid);
  89. vtMemFree(crypto);
  90. vtMemFree(codec);
  91. return ret;
  92. }
  93. static int
  94. dispatchRead(VtSession *z, Packet **pkt)
  95. {
  96. Packet *p;
  97. int type, n;
  98. uchar score[VtScoreSize], buf[4];
  99. p = *pkt;
  100. if(!packetConsume(p, score, VtScoreSize))
  101. return 0;
  102. if(!packetConsume(p, buf, 4))
  103. return 0;
  104. type = buf[0];
  105. n = (buf[2]<<8) | buf[3];
  106. if(packetSize(p) != 0) {
  107. vtSetError(EProtocolBotch);
  108. return 0;
  109. }
  110. packetFree(p);
  111. *pkt = (*z->vtbl->read)(z, score, type, n);
  112. return 1;
  113. }
  114. static int
  115. dispatchWrite(VtSession *z, Packet **pkt)
  116. {
  117. Packet *p;
  118. int type;
  119. uchar score[VtScoreSize], buf[4];
  120. p = *pkt;
  121. if(!packetConsume(p, buf, 4))
  122. return 0;
  123. type = buf[0];
  124. if(!(z->vtbl->write)(z, score, type, p)) {
  125. *pkt = 0;
  126. } else {
  127. *pkt = packetAlloc();
  128. packetAppend(*pkt, score, VtScoreSize);
  129. }
  130. return 1;
  131. }
  132. static int
  133. dispatchSync(VtSession *z, Packet **pkt)
  134. {
  135. (z->vtbl->sync)(z);
  136. if(packetSize(*pkt) != 0) {
  137. vtSetError(EProtocolBotch);
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. int
  143. vtExport(VtSession *z)
  144. {
  145. Packet *p;
  146. uchar buf[10], *hdr;
  147. int op, tid, clean;
  148. if(z->vtbl == nil) {
  149. vtSetError(ENotServer);
  150. return 0;
  151. }
  152. /* fork off slave */
  153. switch(rfork(RFNOWAIT|RFMEM|RFPROC)){
  154. case -1:
  155. vtOSError();
  156. return 0;
  157. case 0:
  158. break;
  159. default:
  160. return 1;
  161. }
  162. p = nil;
  163. clean = 0;
  164. vtAttach();
  165. if(!vtConnect(z, nil))
  166. goto Exit;
  167. vtDebug(z, "server connected!\n");
  168. if(0) vtSetDebug(z, 1);
  169. for(;;) {
  170. p = vtRecvPacket(z);
  171. if(p == nil) {
  172. break;
  173. }
  174. vtDebug(z, "server recv: ");
  175. vtDebugMesg(z, p, "\n");
  176. if(!packetConsume(p, buf, 2)) {
  177. vtSetError(EProtocolBotch);
  178. break;
  179. }
  180. op = buf[0];
  181. tid = buf[1];
  182. switch(op) {
  183. default:
  184. vtSetError(EProtocolBotch);
  185. goto Exit;
  186. case VtQPing:
  187. break;
  188. case VtQGoodbye:
  189. clean = 1;
  190. goto Exit;
  191. case VtQHello:
  192. if(!dispatchHello(z, &p))
  193. goto Exit;
  194. break;
  195. case VtQRead:
  196. if(!dispatchRead(z, &p))
  197. goto Exit;
  198. break;
  199. case VtQWrite:
  200. if(!dispatchWrite(z, &p))
  201. goto Exit;
  202. break;
  203. case VtQSync:
  204. if(!dispatchSync(z, &p))
  205. goto Exit;
  206. break;
  207. }
  208. if(p != nil) {
  209. hdr = packetHeader(p, 2);
  210. hdr[0] = op+1;
  211. hdr[1] = tid;
  212. } else {
  213. p = packetAlloc();
  214. hdr = packetHeader(p, 2);
  215. hdr[0] = VtRError;
  216. hdr[1] = tid;
  217. if(!vtAddString(p, vtGetError()))
  218. goto Exit;
  219. }
  220. vtDebug(z, "server send: ");
  221. vtDebugMesg(z, p, "\n");
  222. if(!vtSendPacket(z, p)) {
  223. p = nil;
  224. goto Exit;
  225. }
  226. }
  227. Exit:
  228. if(p != nil)
  229. packetFree(p);
  230. if(z->vtbl->closing)
  231. z->vtbl->closing(z, clean);
  232. vtClose(z);
  233. vtFree(z);
  234. vtDetach();
  235. exits(0);
  236. return 0; /* never gets here */
  237. }