blob: 40ee85175cb73d830cf53e64e0bdd4dba448ac19 [file] [log] [blame]
rsc056fe1b2003-11-23 18:19:58 +00001#include <u.h>
2#include <libc.h>
3#include <venti.h>
4
rsca09e80f2004-05-23 00:59:17 +00005int ventidoublechecksha1 = 1;
6
rsc056fe1b2003-11-23 18:19:58 +00007static int
8vtfcallrpc(VtConn *z, VtFcall *ou, VtFcall *in)
9{
10 Packet *p;
11
12 p = vtfcallpack(ou);
13 if(p == nil)
14 return -1;
rsc72520362005-11-02 19:08:43 +000015 if((p = _vtrpc(z, p, ou)) == nil)
rsc056fe1b2003-11-23 18:19:58 +000016 return -1;
17 if(vtfcallunpack(in, p) < 0){
18 packetfree(p);
19 return -1;
20 }
rsca09e80f2004-05-23 00:59:17 +000021 if(chattyventi)
22 fprint(2, "%s <- %F\n", argv0, in);
rsc7643b262005-07-13 10:52:39 +000023 if(in->msgtype == VtRerror){
rsc056fe1b2003-11-23 18:19:58 +000024 werrstr(in->error);
25 vtfcallclear(in);
26 packetfree(p);
27 return -1;
28 }
rsc7643b262005-07-13 10:52:39 +000029 if(in->msgtype != ou->msgtype+1){
rsc056fe1b2003-11-23 18:19:58 +000030 werrstr("type mismatch: sent %c%d got %c%d",
rsc7643b262005-07-13 10:52:39 +000031 "TR"[ou->msgtype&1], ou->msgtype>>1,
32 "TR"[in->msgtype&1], in->msgtype>>1);
rsc056fe1b2003-11-23 18:19:58 +000033 vtfcallclear(in);
34 packetfree(p);
35 return -1;
36 }
37 packetfree(p);
38 return 0;
39}
40
41int
42vthello(VtConn *z)
43{
44 VtFcall tx, rx;
45
46 memset(&tx, 0, sizeof tx);
rsc7643b262005-07-13 10:52:39 +000047 tx.msgtype = VtThello;
rsc056fe1b2003-11-23 18:19:58 +000048 tx.version = z->version;
49 tx.uid = z->uid;
50 if(tx.uid == nil)
51 tx.uid = "anonymous";
52 if(vtfcallrpc(z, &tx, &rx) < 0)
53 return -1;
54 z->sid = rx.sid;
55 rx.sid = 0;
56 vtfcallclear(&rx);
57 return 0;
58}
59
60Packet*
61vtreadpacket(VtConn *z, uchar score[VtScoreSize], uint type, int n)
62{
63 VtFcall tx, rx;
64
rsc7cb74892004-06-17 18:51:50 +000065 if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
66 return packetalloc();
67
rsc056fe1b2003-11-23 18:19:58 +000068 memset(&tx, 0, sizeof tx);
rsc7643b262005-07-13 10:52:39 +000069 tx.msgtype = VtTread;
70 tx.blocktype = type;
rsc056fe1b2003-11-23 18:19:58 +000071 tx.count = n;
72 memmove(tx.score, score, VtScoreSize);
73 if(vtfcallrpc(z, &tx, &rx) < 0)
74 return nil;
75 if(packetsize(rx.data) > n){
76 werrstr("read returned too much data");
77 packetfree(rx.data);
78 return nil;
79 }
rsca09e80f2004-05-23 00:59:17 +000080 if(ventidoublechecksha1){
81 packetsha1(rx.data, tx.score);
82 if(memcmp(score, tx.score, VtScoreSize) != 0){
83 werrstr("read asked for %V got %V", score, tx.score);
84 packetfree(rx.data);
85 return nil;
86 }
rsc056fe1b2003-11-23 18:19:58 +000087 }
rsc056fe1b2003-11-23 18:19:58 +000088 return rx.data;
89}
90
91int
92vtread(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
93{
94 int nn;
95 Packet *p;
96
97 if((p = vtreadpacket(z, score, type, n)) == nil)
98 return -1;
99 nn = packetsize(p);
100 if(packetconsume(p, buf, nn) < 0)
101 abort();
rscc5eb6862004-06-16 03:12:39 +0000102 packetfree(p);
rsc056fe1b2003-11-23 18:19:58 +0000103 return nn;
104}
105
106int
107vtwritepacket(VtConn *z, uchar score[VtScoreSize], uint type, Packet *p)
108{
109 VtFcall tx, rx;
110
rsc7cb74892004-06-17 18:51:50 +0000111 if(packetsize(p) == 0){
112 memmove(score, vtzeroscore, VtScoreSize);
113 return 0;
114 }
rsc7643b262005-07-13 10:52:39 +0000115 tx.msgtype = VtTwrite;
116 tx.blocktype = type;
rsc056fe1b2003-11-23 18:19:58 +0000117 tx.data = p;
rsca09e80f2004-05-23 00:59:17 +0000118 if(ventidoublechecksha1)
119 packetsha1(p, score);
rsc056fe1b2003-11-23 18:19:58 +0000120 if(vtfcallrpc(z, &tx, &rx) < 0)
121 return -1;
rsca09e80f2004-05-23 00:59:17 +0000122 if(ventidoublechecksha1){
123 if(memcmp(score, rx.score, VtScoreSize) != 0){
124 werrstr("sha1 hash mismatch: want %V got %V", score, rx.score);
125 return -1;
126 }
rsc4192ac12004-06-09 14:03:54 +0000127 }else
128 memmove(score, rx.score, VtScoreSize);
rsc056fe1b2003-11-23 18:19:58 +0000129 return 0;
130}
131
132int
133vtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
134{
135 Packet *p;
rsc361e2792005-01-18 20:15:18 +0000136 int nn;
rsc056fe1b2003-11-23 18:19:58 +0000137
rsc0dc99502005-01-14 18:21:12 +0000138 p = packetforeign(buf, n, 0, nil);
rsc361e2792005-01-18 20:15:18 +0000139 nn = vtwritepacket(z, score, type, p);
140 packetfree(p);
141 return nn;
rsc056fe1b2003-11-23 18:19:58 +0000142}
143
144int
145vtsync(VtConn *z)
146{
147 VtFcall tx, rx;
148
rsc7643b262005-07-13 10:52:39 +0000149 tx.msgtype = VtTsync;
rsc056fe1b2003-11-23 18:19:58 +0000150 return vtfcallrpc(z, &tx, &rx);
151}
152
153int
154vtping(VtConn *z)
155{
156 VtFcall tx, rx;
157
rsc7643b262005-07-13 10:52:39 +0000158 tx.msgtype = VtTping;
rsc056fe1b2003-11-23 18:19:58 +0000159 return vtfcallrpc(z, &tx, &rx);
160}
161
162int
163vtconnect(VtConn *z)
164{
165 if(vtversion(z) < 0)
166 return -1;
167 if(vthello(z) < 0)
168 return -1;
169 return 0;
170}
171
rsc6fc7da32006-10-19 21:58:59 +0000172int
173vtgoodbye(VtConn *z)
174{
175 VtFcall tx, rx;
176
177 tx.msgtype = VtTgoodbye;
178 vtfcallrpc(z, &tx, &rx); /* always fails: no VtRgoodbye */
179 return 0;
180}