6 static char EProtocolBotch[] = "venti protocol botch";
7 static char ELumpSize[] = "illegal lump size";
8 static char ENotConnected[] = "not connected to venti server";
10 static Packet *vtRPC(VtSession *z, int op, Packet *p);
15 VtSession *z = vtAlloc();
20 vtDial(char *host, int canfail)
28 host = getenv("venti");
34 werrstr("no venti host set");
38 na = netmkaddr(host, 0, "venti");
39 fd = dial(na, 0, 0, 0);
44 vtSetError("venti dialstring %s: %s", na, e);
50 strcpy(z->fderror, e);
56 vtRedial(VtSession *z, char *host)
62 host = getenv("venti");
66 na = netmkaddr(host, 0, "venti");
67 fd = dial(na, 0, 0, 0);
78 vtStdioServer(char *server)
86 if(access(server, AEXEC) < 0) {
106 execl(server, "ventiserver", "-i", nil);
107 exits("exec failed");
119 Packet *p = packetAlloc();
121 p = vtRPC(z, VtQPing, p);
129 vtHello(VtSession *z)
139 if(!vtAddString(p, vtGetVersion(z)))
141 if(!vtAddString(p, vtGetUid(z)))
143 buf[0] = vtGetCryptoStrength(z);
146 packetAppend(p, buf, 3);
147 p = vtRPC(z, VtQHello, p);
150 if(!vtGetString(p, &sid))
152 if(!packetConsume(p, buf, 2))
154 if(packetSize(p) != 0) {
155 vtSetError(EProtocolBotch);
168 z->auth.state = VtAuthOK;
169 vtSha1Free(z->inHash);
171 vtSha1Free(z->outHash);
185 Packet *p = packetAlloc();
187 p = vtRPC(z, VtQSync, p);
190 if(packetSize(p) != 0){
191 vtSetError(EProtocolBotch);
203 vtWrite(VtSession *z, uchar score[VtScoreSize], int type, uchar *buf, int n)
205 Packet *p = packetAlloc();
207 packetAppend(p, buf, n);
208 return vtWritePacket(z, score, type, p);
212 vtWritePacket(VtSession *z, uchar score[VtScoreSize], int type, Packet *p)
214 int n = packetSize(p);
217 if(n > VtMaxLumpSize || n < 0) {
218 vtSetError(ELumpSize);
223 memmove(score, vtZeroScore, VtScoreSize);
227 hdr = packetHeader(p, 4);
229 hdr[1] = 0; /* pad */
230 hdr[2] = 0; /* pad */
231 hdr[3] = 0; /* pad */
232 p = vtRPC(z, VtQWrite, p);
235 if(!packetConsume(p, score, VtScoreSize))
237 if(packetSize(p) != 0) {
238 vtSetError(EProtocolBotch);
249 vtRead(VtSession *z, uchar score[VtScoreSize], int type, uchar *buf, int n)
253 p = vtReadPacket(z, score, type, n);
257 packetCopy(p, buf, 0, n);
263 vtReadPacket(VtSession *z, uchar score[VtScoreSize], int type, int n)
268 if(n < 0 || n > VtMaxLumpSize) {
269 vtSetError(ELumpSize);
274 if(memcmp(score, vtZeroScore, VtScoreSize) == 0)
277 packetAppend(p, score, VtScoreSize);
279 buf[1] = 0; /* pad */
282 packetAppend(p, buf, 4);
283 return vtRPC(z, VtQRead, p);
288 vtRPC(VtSession *z, int op, Packet *p)
294 vtSetError(ENotConnected);
299 * single threaded for the momment
302 if(z->cstate != VtStateConnected){
303 vtSetError(ENotConnected);
306 hdr = packetHeader(p, 2);
307 hdr[0] = op; /* op */
308 hdr[1] = 0; /* tid */
309 vtDebug(z, "client send: ");
310 vtDebugMesg(z, p, "\n");
311 if(!vtSendPacket(z, p)) {
318 vtDebug(z, "client recv: ");
319 vtDebugMesg(z, p, "\n");
320 if(!packetConsume(p, buf, 2))
322 if(buf[0] == VtRError) {
323 if(!vtGetString(p, &err)) {
324 vtSetError(EProtocolBotch);
333 if(buf[0] != op+1 || buf[1] != 0) {
334 vtSetError(EProtocolBotch);
340 vtDebug(z, "vtRPC failed: %s\n", vtGetError());