Blame


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>
4 056fe1ba 2003-11-23 devnull
5 a09e80f9 2004-05-23 devnull int ventidoublechecksha1 = 1;
6 a09e80f9 2004-05-23 devnull
7 056fe1ba 2003-11-23 devnull static int
8 056fe1ba 2003-11-23 devnull vtfcallrpc(VtConn *z, VtFcall *ou, VtFcall *in)
9 056fe1ba 2003-11-23 devnull {
10 056fe1ba 2003-11-23 devnull Packet *p;
11 056fe1ba 2003-11-23 devnull
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;
20 056fe1ba 2003-11-23 devnull }
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;
28 056fe1ba 2003-11-23 devnull }
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;
36 056fe1ba 2003-11-23 devnull }
37 056fe1ba 2003-11-23 devnull packetfree(p);
38 056fe1ba 2003-11-23 devnull return 0;
39 056fe1ba 2003-11-23 devnull }
40 056fe1ba 2003-11-23 devnull
41 056fe1ba 2003-11-23 devnull int
42 056fe1ba 2003-11-23 devnull vthello(VtConn *z)
43 056fe1ba 2003-11-23 devnull {
44 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
45 056fe1ba 2003-11-23 devnull
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;
58 056fe1ba 2003-11-23 devnull }
59 056fe1ba 2003-11-23 devnull
60 056fe1ba 2003-11-23 devnull Packet*
61 056fe1ba 2003-11-23 devnull vtreadpacket(VtConn *z, uchar score[VtScoreSize], uint type, int n)
62 056fe1ba 2003-11-23 devnull {
63 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
64 056fe1ba 2003-11-23 devnull
65 7cb74894 2004-06-17 devnull if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
66 7cb74894 2004-06-17 devnull return packetalloc();
67 7cb74894 2004-06-17 devnull
68 73392c2c 2011-12-12 rsc if(z == nil){
69 73392c2c 2011-12-12 rsc werrstr("not connected");
70 73392c2c 2011-12-12 rsc return nil;
71 73392c2c 2011-12-12 rsc }
72 73392c2c 2011-12-12 rsc
73 33b446b8 2009-05-25 rsc if(z->version[1] == '2' && n >= (1<<16)) {
74 33b446b8 2009-05-25 rsc werrstr("read count too large for protocol");
75 33b446b8 2009-05-25 rsc return nil;
76 33b446b8 2009-05-25 rsc }
77 056fe1ba 2003-11-23 devnull memset(&tx, 0, sizeof tx);
78 7643b263 2005-07-13 devnull tx.msgtype = VtTread;
79 7643b263 2005-07-13 devnull tx.blocktype = type;
80 056fe1ba 2003-11-23 devnull tx.count = n;
81 056fe1ba 2003-11-23 devnull memmove(tx.score, score, VtScoreSize);
82 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
83 056fe1ba 2003-11-23 devnull return nil;
84 056fe1ba 2003-11-23 devnull if(packetsize(rx.data) > n){
85 056fe1ba 2003-11-23 devnull werrstr("read returned too much data");
86 056fe1ba 2003-11-23 devnull packetfree(rx.data);
87 056fe1ba 2003-11-23 devnull return nil;
88 056fe1ba 2003-11-23 devnull }
89 a09e80f9 2004-05-23 devnull if(ventidoublechecksha1){
90 a09e80f9 2004-05-23 devnull packetsha1(rx.data, tx.score);
91 a09e80f9 2004-05-23 devnull if(memcmp(score, tx.score, VtScoreSize) != 0){
92 a09e80f9 2004-05-23 devnull werrstr("read asked for %V got %V", score, tx.score);
93 a09e80f9 2004-05-23 devnull packetfree(rx.data);
94 a09e80f9 2004-05-23 devnull return nil;
95 a09e80f9 2004-05-23 devnull }
96 056fe1ba 2003-11-23 devnull }
97 056fe1ba 2003-11-23 devnull return rx.data;
98 056fe1ba 2003-11-23 devnull }
99 056fe1ba 2003-11-23 devnull
100 056fe1ba 2003-11-23 devnull int
101 056fe1ba 2003-11-23 devnull vtread(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
102 056fe1ba 2003-11-23 devnull {
103 056fe1ba 2003-11-23 devnull int nn;
104 056fe1ba 2003-11-23 devnull Packet *p;
105 056fe1ba 2003-11-23 devnull
106 056fe1ba 2003-11-23 devnull if((p = vtreadpacket(z, score, type, n)) == nil)
107 056fe1ba 2003-11-23 devnull return -1;
108 056fe1ba 2003-11-23 devnull nn = packetsize(p);
109 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, nn) < 0)
110 056fe1ba 2003-11-23 devnull abort();
111 c5eb6860 2004-06-16 devnull packetfree(p);
112 056fe1ba 2003-11-23 devnull return nn;
113 056fe1ba 2003-11-23 devnull }
114 056fe1ba 2003-11-23 devnull
115 056fe1ba 2003-11-23 devnull int
116 056fe1ba 2003-11-23 devnull vtwritepacket(VtConn *z, uchar score[VtScoreSize], uint type, Packet *p)
117 056fe1ba 2003-11-23 devnull {
118 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
119 056fe1ba 2003-11-23 devnull
120 7cb74894 2004-06-17 devnull if(packetsize(p) == 0){
121 7cb74894 2004-06-17 devnull memmove(score, vtzeroscore, VtScoreSize);
122 7cb74894 2004-06-17 devnull return 0;
123 7cb74894 2004-06-17 devnull }
124 7643b263 2005-07-13 devnull tx.msgtype = VtTwrite;
125 7643b263 2005-07-13 devnull tx.blocktype = type;
126 056fe1ba 2003-11-23 devnull tx.data = p;
127 a09e80f9 2004-05-23 devnull if(ventidoublechecksha1)
128 a09e80f9 2004-05-23 devnull packetsha1(p, score);
129 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
130 056fe1ba 2003-11-23 devnull return -1;
131 a09e80f9 2004-05-23 devnull if(ventidoublechecksha1){
132 a09e80f9 2004-05-23 devnull if(memcmp(score, rx.score, VtScoreSize) != 0){
133 a09e80f9 2004-05-23 devnull werrstr("sha1 hash mismatch: want %V got %V", score, rx.score);
134 a09e80f9 2004-05-23 devnull return -1;
135 a09e80f9 2004-05-23 devnull }
136 4192ac1d 2004-06-09 devnull }else
137 4192ac1d 2004-06-09 devnull memmove(score, rx.score, VtScoreSize);
138 056fe1ba 2003-11-23 devnull return 0;
139 056fe1ba 2003-11-23 devnull }
140 056fe1ba 2003-11-23 devnull
141 056fe1ba 2003-11-23 devnull int
142 056fe1ba 2003-11-23 devnull vtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
143 056fe1ba 2003-11-23 devnull {
144 056fe1ba 2003-11-23 devnull Packet *p;
145 361e279c 2005-01-18 devnull int nn;
146 056fe1ba 2003-11-23 devnull
147 0dc99502 2005-01-14 devnull p = packetforeign(buf, n, 0, nil);
148 361e279c 2005-01-18 devnull nn = vtwritepacket(z, score, type, p);
149 361e279c 2005-01-18 devnull packetfree(p);
150 361e279c 2005-01-18 devnull return nn;
151 056fe1ba 2003-11-23 devnull }
152 056fe1ba 2003-11-23 devnull
153 056fe1ba 2003-11-23 devnull int
154 056fe1ba 2003-11-23 devnull vtsync(VtConn *z)
155 056fe1ba 2003-11-23 devnull {
156 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
157 056fe1ba 2003-11-23 devnull
158 7643b263 2005-07-13 devnull tx.msgtype = VtTsync;
159 056fe1ba 2003-11-23 devnull return vtfcallrpc(z, &tx, &rx);
160 056fe1ba 2003-11-23 devnull }
161 056fe1ba 2003-11-23 devnull
162 056fe1ba 2003-11-23 devnull int
163 056fe1ba 2003-11-23 devnull vtping(VtConn *z)
164 056fe1ba 2003-11-23 devnull {
165 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
166 056fe1ba 2003-11-23 devnull
167 7643b263 2005-07-13 devnull tx.msgtype = VtTping;
168 056fe1ba 2003-11-23 devnull return vtfcallrpc(z, &tx, &rx);
169 056fe1ba 2003-11-23 devnull }
170 056fe1ba 2003-11-23 devnull
171 056fe1ba 2003-11-23 devnull int
172 056fe1ba 2003-11-23 devnull vtconnect(VtConn *z)
173 056fe1ba 2003-11-23 devnull {
174 056fe1ba 2003-11-23 devnull if(vtversion(z) < 0)
175 056fe1ba 2003-11-23 devnull return -1;
176 056fe1ba 2003-11-23 devnull if(vthello(z) < 0)
177 056fe1ba 2003-11-23 devnull return -1;
178 056fe1ba 2003-11-23 devnull return 0;
179 056fe1ba 2003-11-23 devnull }
180 056fe1ba 2003-11-23 devnull
181 6fc7da3c 2006-10-19 devnull int
182 6fc7da3c 2006-10-19 devnull vtgoodbye(VtConn *z)
183 6fc7da3c 2006-10-19 devnull {
184 6fc7da3c 2006-10-19 devnull VtFcall tx, rx;
185 fa325e9b 2020-01-10 cross
186 6fc7da3c 2006-10-19 devnull tx.msgtype = VtTgoodbye;
187 6fc7da3c 2006-10-19 devnull vtfcallrpc(z, &tx, &rx); /* always fails: no VtRgoodbye */
188 6fc7da3c 2006-10-19 devnull return 0;
189 6fc7da3c 2006-10-19 devnull }