Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <venti.h>
5 int ventidoublechecksha1 = 1;
7 static int
8 vtfcallrpc(VtConn *z, VtFcall *ou, VtFcall *in)
9 {
10 Packet *p;
12 p = vtfcallpack(ou);
13 if(p == nil)
14 return -1;
15 if((p = _vtrpc(z, p, ou)) == nil)
16 return -1;
17 if(vtfcallunpack(in, p) < 0){
18 packetfree(p);
19 return -1;
20 }
21 if(chattyventi)
22 fprint(2, "%s <- %F\n", argv0, in);
23 if(in->msgtype == VtRerror){
24 werrstr(in->error);
25 vtfcallclear(in);
26 packetfree(p);
27 return -1;
28 }
29 if(in->msgtype != ou->msgtype+1){
30 werrstr("type mismatch: sent %c%d got %c%d",
31 "TR"[ou->msgtype&1], ou->msgtype>>1,
32 "TR"[in->msgtype&1], in->msgtype>>1);
33 vtfcallclear(in);
34 packetfree(p);
35 return -1;
36 }
37 packetfree(p);
38 return 0;
39 }
41 int
42 vthello(VtConn *z)
43 {
44 VtFcall tx, rx;
46 memset(&tx, 0, sizeof tx);
47 tx.msgtype = VtThello;
48 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 }
60 Packet*
61 vtreadpacket(VtConn *z, uchar score[VtScoreSize], uint type, int n)
62 {
63 VtFcall tx, rx;
65 if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
66 return packetalloc();
68 if(z->version[1] == '2' && n >= (1<<16)) {
69 werrstr("read count too large for protocol");
70 return nil;
71 }
72 memset(&tx, 0, sizeof tx);
73 tx.msgtype = VtTread;
74 tx.blocktype = type;
75 tx.count = n;
76 memmove(tx.score, score, VtScoreSize);
77 if(vtfcallrpc(z, &tx, &rx) < 0)
78 return nil;
79 if(packetsize(rx.data) > n){
80 werrstr("read returned too much data");
81 packetfree(rx.data);
82 return nil;
83 }
84 if(ventidoublechecksha1){
85 packetsha1(rx.data, tx.score);
86 if(memcmp(score, tx.score, VtScoreSize) != 0){
87 werrstr("read asked for %V got %V", score, tx.score);
88 packetfree(rx.data);
89 return nil;
90 }
91 }
92 return rx.data;
93 }
95 int
96 vtread(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
97 {
98 int nn;
99 Packet *p;
101 if((p = vtreadpacket(z, score, type, n)) == nil)
102 return -1;
103 nn = packetsize(p);
104 if(packetconsume(p, buf, nn) < 0)
105 abort();
106 packetfree(p);
107 return nn;
110 int
111 vtwritepacket(VtConn *z, uchar score[VtScoreSize], uint type, Packet *p)
113 VtFcall tx, rx;
115 if(packetsize(p) == 0){
116 memmove(score, vtzeroscore, VtScoreSize);
117 return 0;
119 tx.msgtype = VtTwrite;
120 tx.blocktype = type;
121 tx.data = p;
122 if(ventidoublechecksha1)
123 packetsha1(p, score);
124 if(vtfcallrpc(z, &tx, &rx) < 0)
125 return -1;
126 if(ventidoublechecksha1){
127 if(memcmp(score, rx.score, VtScoreSize) != 0){
128 werrstr("sha1 hash mismatch: want %V got %V", score, rx.score);
129 return -1;
131 }else
132 memmove(score, rx.score, VtScoreSize);
133 return 0;
136 int
137 vtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
139 Packet *p;
140 int nn;
142 p = packetforeign(buf, n, 0, nil);
143 nn = vtwritepacket(z, score, type, p);
144 packetfree(p);
145 return nn;
148 int
149 vtsync(VtConn *z)
151 VtFcall tx, rx;
153 tx.msgtype = VtTsync;
154 return vtfcallrpc(z, &tx, &rx);
157 int
158 vtping(VtConn *z)
160 VtFcall tx, rx;
162 tx.msgtype = VtTping;
163 return vtfcallrpc(z, &tx, &rx);
166 int
167 vtconnect(VtConn *z)
169 if(vtversion(z) < 0)
170 return -1;
171 if(vthello(z) < 0)
172 return -1;
173 return 0;
176 int
177 vtgoodbye(VtConn *z)
179 VtFcall tx, rx;
181 tx.msgtype = VtTgoodbye;
182 vtfcallrpc(z, &tx, &rx); /* always fails: no VtRgoodbye */
183 return 0;