server.c 4.9 KB

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