client.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. int ventidoublechecksha1 = 1;
  5. static int
  6. vtfcallrpc(VtConn *z, VtFcall *ou, VtFcall *in)
  7. {
  8. Packet *p;
  9. p = vtfcallpack(ou);
  10. if(p == nil)
  11. return -1;
  12. if((p = _vtrpc(z, p, ou)) == nil)
  13. return -1;
  14. if(vtfcallunpack(in, p) < 0){
  15. packetfree(p);
  16. return -1;
  17. }
  18. if(chattyventi)
  19. fprint(2, "%s <- %F\n", argv0, in);
  20. if(in->msgtype == VtRerror){
  21. werrstr(in->error);
  22. vtfcallclear(in);
  23. packetfree(p);
  24. return -1;
  25. }
  26. if(in->msgtype != ou->msgtype+1){
  27. werrstr("type mismatch: sent %c%d got %c%d",
  28. "TR"[ou->msgtype&1], ou->msgtype>>1,
  29. "TR"[in->msgtype&1], in->msgtype>>1);
  30. vtfcallclear(in);
  31. packetfree(p);
  32. return -1;
  33. }
  34. packetfree(p);
  35. return 0;
  36. }
  37. int
  38. vthello(VtConn *z)
  39. {
  40. VtFcall tx, rx;
  41. memset(&tx, 0, sizeof tx);
  42. tx.msgtype = VtThello;
  43. tx.version = z->version;
  44. tx.uid = z->uid;
  45. if(tx.uid == nil)
  46. tx.uid = "anonymous";
  47. if(vtfcallrpc(z, &tx, &rx) < 0)
  48. return -1;
  49. z->sid = rx.sid;
  50. rx.sid = 0;
  51. vtfcallclear(&rx);
  52. return 0;
  53. }
  54. Packet*
  55. vtreadpacket(VtConn *z, uchar score[VtScoreSize], uint type, int n)
  56. {
  57. VtFcall tx, rx;
  58. if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
  59. return packetalloc();
  60. memset(&tx, 0, sizeof tx);
  61. tx.msgtype = VtTread;
  62. tx.blocktype = type;
  63. tx.count = n;
  64. memmove(tx.score, score, VtScoreSize);
  65. if(vtfcallrpc(z, &tx, &rx) < 0)
  66. return nil;
  67. if(packetsize(rx.data) > n){
  68. werrstr("read returned too much data");
  69. packetfree(rx.data);
  70. return nil;
  71. }
  72. if(ventidoublechecksha1){
  73. packetsha1(rx.data, tx.score);
  74. if(memcmp(score, tx.score, VtScoreSize) != 0){
  75. werrstr("read asked for %V got %V", score, tx.score);
  76. packetfree(rx.data);
  77. return nil;
  78. }
  79. }
  80. return rx.data;
  81. }
  82. int
  83. vtread(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
  84. {
  85. int nn;
  86. Packet *p;
  87. if((p = vtreadpacket(z, score, type, n)) == nil)
  88. return -1;
  89. nn = packetsize(p);
  90. if(packetconsume(p, buf, nn) < 0)
  91. abort();
  92. packetfree(p);
  93. return nn;
  94. }
  95. int
  96. vtwritepacket(VtConn *z, uchar score[VtScoreSize], uint type, Packet *p)
  97. {
  98. VtFcall tx, rx;
  99. if(packetsize(p) == 0){
  100. memmove(score, vtzeroscore, VtScoreSize);
  101. return 0;
  102. }
  103. tx.msgtype = VtTwrite;
  104. tx.blocktype = type;
  105. tx.data = p;
  106. if(ventidoublechecksha1)
  107. packetsha1(p, score);
  108. if(vtfcallrpc(z, &tx, &rx) < 0)
  109. return -1;
  110. if(ventidoublechecksha1){
  111. if(memcmp(score, rx.score, VtScoreSize) != 0){
  112. werrstr("sha1 hash mismatch: want %V got %V", score, rx.score);
  113. return -1;
  114. }
  115. }else
  116. memmove(score, rx.score, VtScoreSize);
  117. return 0;
  118. }
  119. int
  120. vtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
  121. {
  122. Packet *p;
  123. int nn;
  124. p = packetforeign(buf, n, 0, nil);
  125. nn = vtwritepacket(z, score, type, p);
  126. packetfree(p);
  127. return nn;
  128. }
  129. int
  130. vtsync(VtConn *z)
  131. {
  132. VtFcall tx, rx;
  133. tx.msgtype = VtTsync;
  134. return vtfcallrpc(z, &tx, &rx);
  135. }
  136. int
  137. vtping(VtConn *z)
  138. {
  139. VtFcall tx, rx;
  140. tx.msgtype = VtTping;
  141. return vtfcallrpc(z, &tx, &rx);
  142. }
  143. int
  144. vtconnect(VtConn *z)
  145. {
  146. if(vtversion(z) < 0)
  147. return -1;
  148. if(vthello(z) < 0)
  149. return -1;
  150. return 0;
  151. }
  152. int
  153. vtgoodbye(VtConn *z)
  154. {
  155. VtFcall tx, rx;
  156. tx.msgtype = VtTgoodbye;
  157. vtfcallrpc(z, &tx, &rx); /* always fails: no VtRgoodbye */
  158. return 0;
  159. }