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>
6 056fe1ba 2003-11-23 devnull vtfcallpack(VtFcall *f)
8 33b446b8 2009-05-25 rsc uchar buf[10];
9 056fe1ba 2003-11-23 devnull Packet *p;
11 056fe1ba 2003-11-23 devnull p = packetalloc();
13 7643b263 2005-07-13 devnull buf[0] = f->msgtype;
14 056fe1ba 2003-11-23 devnull buf[1] = f->tag;
15 056fe1ba 2003-11-23 devnull packetappend(p, buf, 2);
17 7643b263 2005-07-13 devnull switch(f->msgtype){
19 7643b263 2005-07-13 devnull werrstr("vtfcallpack: unknown packet type %d", f->msgtype);
20 056fe1ba 2003-11-23 devnull goto Err;
22 056fe1ba 2003-11-23 devnull case VtRerror:
23 056fe1ba 2003-11-23 devnull if(vtputstring(p, f->error) < 0)
24 056fe1ba 2003-11-23 devnull goto Err;
27 056fe1ba 2003-11-23 devnull case VtTping:
30 056fe1ba 2003-11-23 devnull case VtRping:
33 056fe1ba 2003-11-23 devnull case VtThello:
34 056fe1ba 2003-11-23 devnull if(vtputstring(p, f->version) < 0
35 056fe1ba 2003-11-23 devnull || vtputstring(p, f->uid) < 0)
36 056fe1ba 2003-11-23 devnull goto Err;
37 056fe1ba 2003-11-23 devnull buf[0] = f->strength;
38 056fe1ba 2003-11-23 devnull buf[1] = f->ncrypto;
39 056fe1ba 2003-11-23 devnull packetappend(p, buf, 2);
40 056fe1ba 2003-11-23 devnull packetappend(p, f->crypto, f->ncrypto);
41 056fe1ba 2003-11-23 devnull buf[0] = f->ncodec;
42 056fe1ba 2003-11-23 devnull packetappend(p, buf, 1);
43 056fe1ba 2003-11-23 devnull packetappend(p, f->codec, f->ncodec);
46 056fe1ba 2003-11-23 devnull case VtRhello:
47 056fe1ba 2003-11-23 devnull if(vtputstring(p, f->sid) < 0)
48 056fe1ba 2003-11-23 devnull goto Err;
49 056fe1ba 2003-11-23 devnull buf[0] = f->rcrypto;
50 056fe1ba 2003-11-23 devnull buf[1] = f->rcodec;
51 056fe1ba 2003-11-23 devnull packetappend(p, buf, 2);
54 056fe1ba 2003-11-23 devnull case VtTgoodbye:
57 056fe1ba 2003-11-23 devnull case VtTread:
58 056fe1ba 2003-11-23 devnull packetappend(p, f->score, VtScoreSize);
59 7643b263 2005-07-13 devnull buf[0] = vttodisktype(f->blocktype);
60 056fe1ba 2003-11-23 devnull if(~buf[0] == 0)
61 056fe1ba 2003-11-23 devnull goto Err;
62 056fe1ba 2003-11-23 devnull buf[1] = 0;
63 33b446b8 2009-05-25 rsc if(f->count >= (1<<16)) {
64 33b446b8 2009-05-25 rsc buf[2] = f->count >> 24;
65 33b446b8 2009-05-25 rsc buf[3] = f->count >> 16;
66 33b446b8 2009-05-25 rsc buf[4] = f->count >> 8;
67 33b446b8 2009-05-25 rsc buf[5] = f->count;
68 33b446b8 2009-05-25 rsc packetappend(p, buf, 6);
70 33b446b8 2009-05-25 rsc buf[2] = f->count >> 8;
71 33b446b8 2009-05-25 rsc buf[3] = f->count;
72 33b446b8 2009-05-25 rsc packetappend(p, buf, 4);
76 056fe1ba 2003-11-23 devnull case VtRread:
77 056fe1ba 2003-11-23 devnull packetconcat(p, f->data);
80 056fe1ba 2003-11-23 devnull case VtTwrite:
81 7643b263 2005-07-13 devnull buf[0] = vttodisktype(f->blocktype);
82 056fe1ba 2003-11-23 devnull if(~buf[0] == 0)
83 056fe1ba 2003-11-23 devnull goto Err;
84 056fe1ba 2003-11-23 devnull buf[1] = 0;
85 056fe1ba 2003-11-23 devnull buf[2] = 0;
86 056fe1ba 2003-11-23 devnull buf[3] = 0;
87 056fe1ba 2003-11-23 devnull packetappend(p, buf, 4);
88 056fe1ba 2003-11-23 devnull packetconcat(p, f->data);
91 056fe1ba 2003-11-23 devnull case VtRwrite:
92 056fe1ba 2003-11-23 devnull packetappend(p, f->score, VtScoreSize);
95 056fe1ba 2003-11-23 devnull case VtTsync:
98 056fe1ba 2003-11-23 devnull case VtRsync:
102 056fe1ba 2003-11-23 devnull return p;
105 056fe1ba 2003-11-23 devnull packetfree(p);
106 056fe1ba 2003-11-23 devnull return nil;
110 056fe1ba 2003-11-23 devnull vtfcallunpack(VtFcall *f, Packet *p)
112 056fe1ba 2003-11-23 devnull uchar buf[4];
114 056fe1ba 2003-11-23 devnull memset(f, 0, sizeof *f);
116 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, 2) < 0)
117 056fe1ba 2003-11-23 devnull return -1;
119 7643b263 2005-07-13 devnull f->msgtype = buf[0];
120 056fe1ba 2003-11-23 devnull f->tag = buf[1];
122 7643b263 2005-07-13 devnull switch(f->msgtype){
123 056fe1ba 2003-11-23 devnull default:
124 7643b263 2005-07-13 devnull werrstr("vtfcallunpack: unknown bad packet type %d", f->msgtype);
125 056fe1ba 2003-11-23 devnull vtfcallclear(f);
126 056fe1ba 2003-11-23 devnull return -1;
128 056fe1ba 2003-11-23 devnull case VtRerror:
129 056fe1ba 2003-11-23 devnull if(vtgetstring(p, &f->error) < 0)
130 056fe1ba 2003-11-23 devnull goto Err;
133 056fe1ba 2003-11-23 devnull case VtTping:
136 056fe1ba 2003-11-23 devnull case VtRping:
139 056fe1ba 2003-11-23 devnull case VtThello:
140 056fe1ba 2003-11-23 devnull if(vtgetstring(p, &f->version) < 0
141 056fe1ba 2003-11-23 devnull || vtgetstring(p, &f->uid) < 0
142 056fe1ba 2003-11-23 devnull || packetconsume(p, buf, 2) < 0)
143 056fe1ba 2003-11-23 devnull goto Err;
144 056fe1ba 2003-11-23 devnull f->strength = buf[0];
145 056fe1ba 2003-11-23 devnull f->ncrypto = buf[1];
146 056fe1ba 2003-11-23 devnull if(f->ncrypto){
147 056fe1ba 2003-11-23 devnull f->crypto = vtmalloc(f->ncrypto);
148 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, f->ncrypto) < 0)
149 056fe1ba 2003-11-23 devnull goto Err;
151 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, 1) < 0)
152 056fe1ba 2003-11-23 devnull goto Err;
153 056fe1ba 2003-11-23 devnull f->ncodec = buf[0];
154 056fe1ba 2003-11-23 devnull if(f->ncodec){
155 056fe1ba 2003-11-23 devnull f->codec = vtmalloc(f->ncodec);
156 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, f->ncodec) < 0)
157 056fe1ba 2003-11-23 devnull goto Err;
161 056fe1ba 2003-11-23 devnull case VtRhello:
162 056fe1ba 2003-11-23 devnull if(vtgetstring(p, &f->sid) < 0
163 056fe1ba 2003-11-23 devnull || packetconsume(p, buf, 2) < 0)
164 056fe1ba 2003-11-23 devnull goto Err;
165 056fe1ba 2003-11-23 devnull f->rcrypto = buf[0];
166 056fe1ba 2003-11-23 devnull f->rcodec = buf[1];
169 056fe1ba 2003-11-23 devnull case VtTgoodbye:
172 056fe1ba 2003-11-23 devnull case VtTread:
173 056fe1ba 2003-11-23 devnull if(packetconsume(p, f->score, VtScoreSize) < 0
174 33b446b8 2009-05-25 rsc || packetconsume(p, buf, 2) < 0)
175 056fe1ba 2003-11-23 devnull goto Err;
176 7643b263 2005-07-13 devnull f->blocktype = vtfromdisktype(buf[0]);
177 7643b263 2005-07-13 devnull if(~f->blocktype == 0)
178 056fe1ba 2003-11-23 devnull goto Err;
179 33b446b8 2009-05-25 rsc switch(packetsize(p)) {
183 33b446b8 2009-05-25 rsc if(packetconsume(p, buf, 2) < 0)
185 6f4a41c6 2009-07-26 rsc f->count = (buf[0] << 8) | buf[1];
188 33b446b8 2009-05-25 rsc if(packetconsume(p, buf, 4) < 0)
190 33b446b8 2009-05-25 rsc f->count = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
195 056fe1ba 2003-11-23 devnull case VtRread:
196 056fe1ba 2003-11-23 devnull f->data = packetalloc();
197 056fe1ba 2003-11-23 devnull packetconcat(f->data, p);
200 056fe1ba 2003-11-23 devnull case VtTwrite:
201 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, 4) < 0)
202 056fe1ba 2003-11-23 devnull goto Err;
203 7643b263 2005-07-13 devnull f->blocktype = vtfromdisktype(buf[0]);
204 7643b263 2005-07-13 devnull if(~f->blocktype == 0)
205 056fe1ba 2003-11-23 devnull goto Err;
206 056fe1ba 2003-11-23 devnull f->data = packetalloc();
207 056fe1ba 2003-11-23 devnull packetconcat(f->data, p);
210 056fe1ba 2003-11-23 devnull case VtRwrite:
211 056fe1ba 2003-11-23 devnull if(packetconsume(p, f->score, VtScoreSize) < 0)
212 056fe1ba 2003-11-23 devnull goto Err;
215 056fe1ba 2003-11-23 devnull case VtTsync:
218 056fe1ba 2003-11-23 devnull case VtRsync:
222 056fe1ba 2003-11-23 devnull if(packetsize(p) != 0)
223 056fe1ba 2003-11-23 devnull goto Err;
225 056fe1ba 2003-11-23 devnull return 0;
228 056fe1ba 2003-11-23 devnull werrstr("bad packet");
229 6fc7da3c 2006-10-19 devnull vtfcallclear(f);
230 056fe1ba 2003-11-23 devnull return -1;
234 056fe1ba 2003-11-23 devnull vtfcallclear(VtFcall *f)
236 056fe1ba 2003-11-23 devnull vtfree(f->error);
237 056fe1ba 2003-11-23 devnull f->error = nil;
238 056fe1ba 2003-11-23 devnull vtfree(f->uid);
239 056fe1ba 2003-11-23 devnull f->uid = nil;
240 056fe1ba 2003-11-23 devnull vtfree(f->sid);
241 056fe1ba 2003-11-23 devnull f->sid = nil;
242 056fe1ba 2003-11-23 devnull vtfree(f->version);
243 056fe1ba 2003-11-23 devnull f->version = nil;
244 056fe1ba 2003-11-23 devnull vtfree(f->crypto);
245 056fe1ba 2003-11-23 devnull f->crypto = nil;
246 056fe1ba 2003-11-23 devnull vtfree(f->codec);
247 056fe1ba 2003-11-23 devnull f->codec = nil;
248 056fe1ba 2003-11-23 devnull vtfree(f->auth);
249 056fe1ba 2003-11-23 devnull f->auth = nil;
250 056fe1ba 2003-11-23 devnull packetfree(f->data);
251 7cb74894 2004-06-17 devnull f->data = nil;