1 056fe1ba 2003-11-23 devnull #include <u.h>
2 056fe1ba 2003-11-23 devnull #include <libc.h>
3 056fe1ba 2003-11-23 devnull #include <venti.h>
5 a09e80f9 2004-05-23 devnull int ventidoublechecksha1 = 1;
7 056fe1ba 2003-11-23 devnull static int
8 056fe1ba 2003-11-23 devnull vtfcallrpc(VtConn *z, VtFcall *ou, VtFcall *in)
10 056fe1ba 2003-11-23 devnull Packet *p;
12 056fe1ba 2003-11-23 devnull p = vtfcallpack(ou);
13 056fe1ba 2003-11-23 devnull if(p == nil)
14 056fe1ba 2003-11-23 devnull return -1;
15 7252036f 2005-11-02 devnull if((p = _vtrpc(z, p, ou)) == nil)
16 056fe1ba 2003-11-23 devnull return -1;
17 056fe1ba 2003-11-23 devnull if(vtfcallunpack(in, p) < 0){
18 056fe1ba 2003-11-23 devnull packetfree(p);
19 056fe1ba 2003-11-23 devnull return -1;
21 a09e80f9 2004-05-23 devnull if(chattyventi)
22 a09e80f9 2004-05-23 devnull fprint(2, "%s <- %F\n", argv0, in);
23 7643b263 2005-07-13 devnull if(in->msgtype == VtRerror){
24 056fe1ba 2003-11-23 devnull werrstr(in->error);
25 056fe1ba 2003-11-23 devnull vtfcallclear(in);
26 056fe1ba 2003-11-23 devnull packetfree(p);
27 056fe1ba 2003-11-23 devnull return -1;
29 7643b263 2005-07-13 devnull if(in->msgtype != ou->msgtype+1){
30 056fe1ba 2003-11-23 devnull werrstr("type mismatch: sent %c%d got %c%d",
31 7643b263 2005-07-13 devnull "TR"[ou->msgtype&1], ou->msgtype>>1,
32 7643b263 2005-07-13 devnull "TR"[in->msgtype&1], in->msgtype>>1);
33 056fe1ba 2003-11-23 devnull vtfcallclear(in);
34 056fe1ba 2003-11-23 devnull packetfree(p);
35 056fe1ba 2003-11-23 devnull return -1;
37 056fe1ba 2003-11-23 devnull packetfree(p);
38 056fe1ba 2003-11-23 devnull return 0;
42 056fe1ba 2003-11-23 devnull vthello(VtConn *z)
44 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
46 056fe1ba 2003-11-23 devnull memset(&tx, 0, sizeof tx);
47 7643b263 2005-07-13 devnull tx.msgtype = VtThello;
48 056fe1ba 2003-11-23 devnull tx.version = z->version;
49 056fe1ba 2003-11-23 devnull tx.uid = z->uid;
50 056fe1ba 2003-11-23 devnull if(tx.uid == nil)
51 056fe1ba 2003-11-23 devnull tx.uid = "anonymous";
52 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
53 056fe1ba 2003-11-23 devnull return -1;
54 056fe1ba 2003-11-23 devnull z->sid = rx.sid;
55 056fe1ba 2003-11-23 devnull rx.sid = 0;
56 056fe1ba 2003-11-23 devnull vtfcallclear(&rx);
57 056fe1ba 2003-11-23 devnull return 0;
61 056fe1ba 2003-11-23 devnull vtreadpacket(VtConn *z, uchar score[VtScoreSize], uint type, int n)
63 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
65 7cb74894 2004-06-17 devnull if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
66 7cb74894 2004-06-17 devnull return packetalloc();
68 33b446b8 2009-05-25 rsc if(z->version[1] == '2' && n >= (1<<16)) {
69 33b446b8 2009-05-25 rsc werrstr("read count too large for protocol");
72 056fe1ba 2003-11-23 devnull memset(&tx, 0, sizeof tx);
73 7643b263 2005-07-13 devnull tx.msgtype = VtTread;
74 7643b263 2005-07-13 devnull tx.blocktype = type;
75 056fe1ba 2003-11-23 devnull tx.count = n;
76 056fe1ba 2003-11-23 devnull memmove(tx.score, score, VtScoreSize);
77 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
78 056fe1ba 2003-11-23 devnull return nil;
79 056fe1ba 2003-11-23 devnull if(packetsize(rx.data) > n){
80 056fe1ba 2003-11-23 devnull werrstr("read returned too much data");
81 056fe1ba 2003-11-23 devnull packetfree(rx.data);
82 056fe1ba 2003-11-23 devnull return nil;
84 a09e80f9 2004-05-23 devnull if(ventidoublechecksha1){
85 a09e80f9 2004-05-23 devnull packetsha1(rx.data, tx.score);
86 a09e80f9 2004-05-23 devnull if(memcmp(score, tx.score, VtScoreSize) != 0){
87 a09e80f9 2004-05-23 devnull werrstr("read asked for %V got %V", score, tx.score);
88 a09e80f9 2004-05-23 devnull packetfree(rx.data);
89 a09e80f9 2004-05-23 devnull return nil;
92 056fe1ba 2003-11-23 devnull return rx.data;
96 056fe1ba 2003-11-23 devnull vtread(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
99 056fe1ba 2003-11-23 devnull Packet *p;
101 056fe1ba 2003-11-23 devnull if((p = vtreadpacket(z, score, type, n)) == nil)
102 056fe1ba 2003-11-23 devnull return -1;
103 056fe1ba 2003-11-23 devnull nn = packetsize(p);
104 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, nn) < 0)
105 056fe1ba 2003-11-23 devnull abort();
106 c5eb6860 2004-06-16 devnull packetfree(p);
107 056fe1ba 2003-11-23 devnull return nn;
111 056fe1ba 2003-11-23 devnull vtwritepacket(VtConn *z, uchar score[VtScoreSize], uint type, Packet *p)
113 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
115 7cb74894 2004-06-17 devnull if(packetsize(p) == 0){
116 7cb74894 2004-06-17 devnull memmove(score, vtzeroscore, VtScoreSize);
117 7cb74894 2004-06-17 devnull return 0;
119 7643b263 2005-07-13 devnull tx.msgtype = VtTwrite;
120 7643b263 2005-07-13 devnull tx.blocktype = type;
121 056fe1ba 2003-11-23 devnull tx.data = p;
122 a09e80f9 2004-05-23 devnull if(ventidoublechecksha1)
123 a09e80f9 2004-05-23 devnull packetsha1(p, score);
124 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
125 056fe1ba 2003-11-23 devnull return -1;
126 a09e80f9 2004-05-23 devnull if(ventidoublechecksha1){
127 a09e80f9 2004-05-23 devnull if(memcmp(score, rx.score, VtScoreSize) != 0){
128 a09e80f9 2004-05-23 devnull werrstr("sha1 hash mismatch: want %V got %V", score, rx.score);
129 a09e80f9 2004-05-23 devnull return -1;
132 4192ac1d 2004-06-09 devnull memmove(score, rx.score, VtScoreSize);
133 056fe1ba 2003-11-23 devnull return 0;
137 056fe1ba 2003-11-23 devnull vtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
139 056fe1ba 2003-11-23 devnull Packet *p;
142 0dc99502 2005-01-14 devnull p = packetforeign(buf, n, 0, nil);
143 361e279c 2005-01-18 devnull nn = vtwritepacket(z, score, type, p);
144 361e279c 2005-01-18 devnull packetfree(p);
145 361e279c 2005-01-18 devnull return nn;
149 056fe1ba 2003-11-23 devnull vtsync(VtConn *z)
151 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
153 7643b263 2005-07-13 devnull tx.msgtype = VtTsync;
154 056fe1ba 2003-11-23 devnull return vtfcallrpc(z, &tx, &rx);
158 056fe1ba 2003-11-23 devnull vtping(VtConn *z)
160 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
162 7643b263 2005-07-13 devnull tx.msgtype = VtTping;
163 056fe1ba 2003-11-23 devnull return vtfcallrpc(z, &tx, &rx);
167 056fe1ba 2003-11-23 devnull vtconnect(VtConn *z)
169 056fe1ba 2003-11-23 devnull if(vtversion(z) < 0)
170 056fe1ba 2003-11-23 devnull return -1;
171 056fe1ba 2003-11-23 devnull if(vthello(z) < 0)
172 056fe1ba 2003-11-23 devnull return -1;
173 056fe1ba 2003-11-23 devnull return 0;
177 6fc7da3c 2006-10-19 devnull vtgoodbye(VtConn *z)
179 6fc7da3c 2006-10-19 devnull VtFcall tx, rx;
181 6fc7da3c 2006-10-19 devnull tx.msgtype = VtTgoodbye;
182 6fc7da3c 2006-10-19 devnull vtfcallrpc(z, &tx, &rx); /* always fails: no VtRgoodbye */
183 6fc7da3c 2006-10-19 devnull return 0;