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 056fe1ba 2003-11-23 devnull static int
6 056fe1ba 2003-11-23 devnull vtfcallrpc(VtConn *z, VtFcall *ou, VtFcall *in)
8 056fe1ba 2003-11-23 devnull Packet *p;
10 056fe1ba 2003-11-23 devnull p = vtfcallpack(ou);
11 056fe1ba 2003-11-23 devnull if(p == nil)
12 056fe1ba 2003-11-23 devnull return -1;
13 056fe1ba 2003-11-23 devnull if((p = vtrpc(z, p)) == nil)
14 056fe1ba 2003-11-23 devnull return -1;
15 056fe1ba 2003-11-23 devnull if(vtfcallunpack(in, p) < 0){
16 056fe1ba 2003-11-23 devnull packetfree(p);
17 056fe1ba 2003-11-23 devnull return -1;
19 056fe1ba 2003-11-23 devnull if(in->type == VtRerror){
20 056fe1ba 2003-11-23 devnull werrstr(in->error);
21 056fe1ba 2003-11-23 devnull vtfcallclear(in);
22 056fe1ba 2003-11-23 devnull packetfree(p);
23 056fe1ba 2003-11-23 devnull return -1;
25 056fe1ba 2003-11-23 devnull if(in->type != ou->type+1){
26 056fe1ba 2003-11-23 devnull werrstr("type mismatch: sent %c%d got %c%d",
27 056fe1ba 2003-11-23 devnull "TR"[ou->type&1], ou->type>>1,
28 056fe1ba 2003-11-23 devnull "TR"[in->type&1], in->type>>1);
29 056fe1ba 2003-11-23 devnull vtfcallclear(in);
30 056fe1ba 2003-11-23 devnull packetfree(p);
31 056fe1ba 2003-11-23 devnull return -1;
33 056fe1ba 2003-11-23 devnull packetfree(p);
34 056fe1ba 2003-11-23 devnull return 0;
38 056fe1ba 2003-11-23 devnull vthello(VtConn *z)
40 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
42 056fe1ba 2003-11-23 devnull memset(&tx, 0, sizeof tx);
43 056fe1ba 2003-11-23 devnull tx.type = VtThello;
44 056fe1ba 2003-11-23 devnull tx.version = z->version;
45 056fe1ba 2003-11-23 devnull tx.uid = z->uid;
46 056fe1ba 2003-11-23 devnull if(tx.uid == nil)
47 056fe1ba 2003-11-23 devnull tx.uid = "anonymous";
48 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
49 056fe1ba 2003-11-23 devnull return -1;
50 056fe1ba 2003-11-23 devnull z->sid = rx.sid;
51 056fe1ba 2003-11-23 devnull rx.sid = 0;
52 056fe1ba 2003-11-23 devnull vtfcallclear(&rx);
53 056fe1ba 2003-11-23 devnull return 0;
57 056fe1ba 2003-11-23 devnull vtreadpacket(VtConn *z, uchar score[VtScoreSize], uint type, int n)
59 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
61 056fe1ba 2003-11-23 devnull memset(&tx, 0, sizeof tx);
62 056fe1ba 2003-11-23 devnull tx.type = VtTread;
63 056fe1ba 2003-11-23 devnull tx.dtype = type;
64 056fe1ba 2003-11-23 devnull tx.count = n;
65 056fe1ba 2003-11-23 devnull memmove(tx.score, score, VtScoreSize);
66 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
67 056fe1ba 2003-11-23 devnull return nil;
68 056fe1ba 2003-11-23 devnull if(packetsize(rx.data) > n){
69 056fe1ba 2003-11-23 devnull werrstr("read returned too much data");
70 056fe1ba 2003-11-23 devnull packetfree(rx.data);
71 056fe1ba 2003-11-23 devnull return nil;
73 056fe1ba 2003-11-23 devnull packetsha1(rx.data, tx.score);
74 056fe1ba 2003-11-23 devnull if(memcmp(score, tx.score, VtScoreSize) != 0){
75 056fe1ba 2003-11-23 devnull werrstr("read asked for %V got %V", score, tx.score);
76 056fe1ba 2003-11-23 devnull packetfree(rx.data);
77 056fe1ba 2003-11-23 devnull return nil;
80 056fe1ba 2003-11-23 devnull return rx.data;
84 056fe1ba 2003-11-23 devnull vtread(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
87 056fe1ba 2003-11-23 devnull Packet *p;
89 056fe1ba 2003-11-23 devnull if((p = vtreadpacket(z, score, type, n)) == nil)
90 056fe1ba 2003-11-23 devnull return -1;
91 056fe1ba 2003-11-23 devnull nn = packetsize(p);
92 056fe1ba 2003-11-23 devnull if(packetconsume(p, buf, nn) < 0)
94 056fe1ba 2003-11-23 devnull return nn;
98 056fe1ba 2003-11-23 devnull vtwritepacket(VtConn *z, uchar score[VtScoreSize], uint type, Packet *p)
100 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
102 056fe1ba 2003-11-23 devnull tx.type = VtTwrite;
103 056fe1ba 2003-11-23 devnull tx.dtype = type;
104 056fe1ba 2003-11-23 devnull tx.data = p;
105 056fe1ba 2003-11-23 devnull packetsha1(p, score);
106 056fe1ba 2003-11-23 devnull if(vtfcallrpc(z, &tx, &rx) < 0)
107 056fe1ba 2003-11-23 devnull return -1;
108 056fe1ba 2003-11-23 devnull if(memcmp(score, rx.score, VtScoreSize) != 0){
109 056fe1ba 2003-11-23 devnull werrstr("sha1 hash mismatch: want %V got %V", score, rx.score);
110 056fe1ba 2003-11-23 devnull return -1;
112 056fe1ba 2003-11-23 devnull return 0;
116 056fe1ba 2003-11-23 devnull vtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
118 056fe1ba 2003-11-23 devnull Packet *p;
120 056fe1ba 2003-11-23 devnull p = packetforeign(buf, n, nil, nil);
121 056fe1ba 2003-11-23 devnull return vtwritepacket(z, score, type, p);
125 056fe1ba 2003-11-23 devnull vtsync(VtConn *z)
127 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
129 056fe1ba 2003-11-23 devnull tx.type = VtTsync;
130 056fe1ba 2003-11-23 devnull return vtfcallrpc(z, &tx, &rx);
134 056fe1ba 2003-11-23 devnull vtping(VtConn *z)
136 056fe1ba 2003-11-23 devnull VtFcall tx, rx;
138 056fe1ba 2003-11-23 devnull tx.type = VtTping;
139 056fe1ba 2003-11-23 devnull return vtfcallrpc(z, &tx, &rx);
143 056fe1ba 2003-11-23 devnull vtconnect(VtConn *z)
145 056fe1ba 2003-11-23 devnull if(vtversion(z) < 0)
146 056fe1ba 2003-11-23 devnull return -1;
147 056fe1ba 2003-11-23 devnull if(vthello(z) < 0)
148 056fe1ba 2003-11-23 devnull return -1;
149 056fe1ba 2003-11-23 devnull return 0;