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 056fe1ba 2003-11-23 devnull uchar buf[4];
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 056fe1ba 2003-11-23 devnull buf[2] = f->count >> 8;
64 056fe1ba 2003-11-23 devnull buf[3] = f->count;
65 056fe1ba 2003-11-23 devnull packetappend(p, buf, 4);
68 056fe1ba 2003-11-23 devnull case VtRread:
69 056fe1ba 2003-11-23 devnull packetconcat(p, f->data);
72 056fe1ba 2003-11-23 devnull case VtTwrite:
73 7643b263 2005-07-13 devnull buf[0] = vttodisktype(f->blocktype);
74 056fe1ba 2003-11-23 devnull if(~buf[0] == 0)
75 056fe1ba 2003-11-23 devnull goto Err;
76 056fe1ba 2003-11-23 devnull buf[1] = 0;
77 056fe1ba 2003-11-23 devnull buf[2] = 0;
78 056fe1ba 2003-11-23 devnull buf[3] = 0;
79 056fe1ba 2003-11-23 devnull packetappend(p, buf, 4);
80 056fe1ba 2003-11-23 devnull packetconcat(p, f->data);
83 056fe1ba 2003-11-23 devnull case VtRwrite:
84 056fe1ba 2003-11-23 devnull packetappend(p, f->score, VtScoreSize);
87 056fe1ba 2003-11-23 devnull case VtTsync:
90 056fe1ba 2003-11-23 devnull case VtRsync:
94 056fe1ba 2003-11-23 devnull return p;
97 056fe1ba 2003-11-23 devnull packetfree(p);
98 056fe1ba 2003-11-23 devnull return nil;
102 056fe1ba 2003-11-23 devnull vtfcallunpack(VtFcall *f, Packet *p)
104 056fe1ba 2003-11-23 devnull uchar buf[4];
106 056fe1ba 2003-11-23 devnull memset(f, 0, sizeof *f);
108 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, 2) < 0)
109 056fe1ba 2003-11-23 devnull return -1;
111 7643b263 2005-07-13 devnull f->msgtype = buf[0];
112 056fe1ba 2003-11-23 devnull f->tag = buf[1];
114 7643b263 2005-07-13 devnull switch(f->msgtype){
115 056fe1ba 2003-11-23 devnull default:
116 7643b263 2005-07-13 devnull werrstr("vtfcallunpack: unknown bad packet type %d", f->msgtype);
117 056fe1ba 2003-11-23 devnull vtfcallclear(f);
118 056fe1ba 2003-11-23 devnull return -1;
120 056fe1ba 2003-11-23 devnull case VtRerror:
121 056fe1ba 2003-11-23 devnull if(vtgetstring(p, &f->error) < 0)
122 056fe1ba 2003-11-23 devnull goto Err;
125 056fe1ba 2003-11-23 devnull case VtTping:
128 056fe1ba 2003-11-23 devnull case VtRping:
131 056fe1ba 2003-11-23 devnull case VtThello:
132 056fe1ba 2003-11-23 devnull if(vtgetstring(p, &f->version) < 0
133 056fe1ba 2003-11-23 devnull || vtgetstring(p, &f->uid) < 0
134 056fe1ba 2003-11-23 devnull || packetconsume(p, buf, 2) < 0)
135 056fe1ba 2003-11-23 devnull goto Err;
136 056fe1ba 2003-11-23 devnull f->strength = buf[0];
137 056fe1ba 2003-11-23 devnull f->ncrypto = buf[1];
138 056fe1ba 2003-11-23 devnull if(f->ncrypto){
139 056fe1ba 2003-11-23 devnull f->crypto = vtmalloc(f->ncrypto);
140 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, f->ncrypto) < 0)
141 056fe1ba 2003-11-23 devnull goto Err;
143 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, 1) < 0)
144 056fe1ba 2003-11-23 devnull goto Err;
145 056fe1ba 2003-11-23 devnull f->ncodec = buf[0];
146 056fe1ba 2003-11-23 devnull if(f->ncodec){
147 056fe1ba 2003-11-23 devnull f->codec = vtmalloc(f->ncodec);
148 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, f->ncodec) < 0)
149 056fe1ba 2003-11-23 devnull goto Err;
153 056fe1ba 2003-11-23 devnull case VtRhello:
154 056fe1ba 2003-11-23 devnull if(vtgetstring(p, &f->sid) < 0
155 056fe1ba 2003-11-23 devnull || packetconsume(p, buf, 2) < 0)
156 056fe1ba 2003-11-23 devnull goto Err;
157 056fe1ba 2003-11-23 devnull f->rcrypto = buf[0];
158 056fe1ba 2003-11-23 devnull f->rcodec = buf[1];
161 056fe1ba 2003-11-23 devnull case VtTgoodbye:
164 056fe1ba 2003-11-23 devnull case VtTread:
165 056fe1ba 2003-11-23 devnull if(packetconsume(p, f->score, VtScoreSize) < 0
166 056fe1ba 2003-11-23 devnull || packetconsume(p, buf, 4) < 0)
167 056fe1ba 2003-11-23 devnull goto Err;
168 7643b263 2005-07-13 devnull f->blocktype = vtfromdisktype(buf[0]);
169 7643b263 2005-07-13 devnull if(~f->blocktype == 0)
170 056fe1ba 2003-11-23 devnull goto Err;
171 056fe1ba 2003-11-23 devnull f->count = (buf[2] << 8) | buf[3];
174 056fe1ba 2003-11-23 devnull case VtRread:
175 056fe1ba 2003-11-23 devnull f->data = packetalloc();
176 056fe1ba 2003-11-23 devnull packetconcat(f->data, p);
179 056fe1ba 2003-11-23 devnull case VtTwrite:
180 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, 4) < 0)
181 056fe1ba 2003-11-23 devnull goto Err;
182 7643b263 2005-07-13 devnull f->blocktype = vtfromdisktype(buf[0]);
183 7643b263 2005-07-13 devnull if(~f->blocktype == 0)
184 056fe1ba 2003-11-23 devnull goto Err;
185 056fe1ba 2003-11-23 devnull f->data = packetalloc();
186 056fe1ba 2003-11-23 devnull packetconcat(f->data, p);
189 056fe1ba 2003-11-23 devnull case VtRwrite:
190 056fe1ba 2003-11-23 devnull if(packetconsume(p, f->score, VtScoreSize) < 0)
191 056fe1ba 2003-11-23 devnull goto Err;
194 056fe1ba 2003-11-23 devnull case VtTsync:
197 056fe1ba 2003-11-23 devnull case VtRsync:
201 056fe1ba 2003-11-23 devnull if(packetsize(p) != 0)
202 056fe1ba 2003-11-23 devnull goto Err;
204 056fe1ba 2003-11-23 devnull return 0;
207 056fe1ba 2003-11-23 devnull werrstr("bad packet");
208 6fc7da3c 2006-10-19 devnull vtfcallclear(f);
209 056fe1ba 2003-11-23 devnull return -1;
213 056fe1ba 2003-11-23 devnull vtfcallclear(VtFcall *f)
215 056fe1ba 2003-11-23 devnull vtfree(f->error);
216 056fe1ba 2003-11-23 devnull f->error = nil;
217 056fe1ba 2003-11-23 devnull vtfree(f->uid);
218 056fe1ba 2003-11-23 devnull f->uid = nil;
219 056fe1ba 2003-11-23 devnull vtfree(f->sid);
220 056fe1ba 2003-11-23 devnull f->sid = nil;
221 056fe1ba 2003-11-23 devnull vtfree(f->version);
222 056fe1ba 2003-11-23 devnull f->version = nil;
223 056fe1ba 2003-11-23 devnull vtfree(f->crypto);
224 056fe1ba 2003-11-23 devnull f->crypto = nil;
225 056fe1ba 2003-11-23 devnull vtfree(f->codec);
226 056fe1ba 2003-11-23 devnull f->codec = nil;
227 056fe1ba 2003-11-23 devnull vtfree(f->auth);
228 056fe1ba 2003-11-23 devnull f->auth = nil;
229 056fe1ba 2003-11-23 devnull packetfree(f->data);
230 7cb74894 2004-06-17 devnull f->data = nil;