-
Notifications
You must be signed in to change notification settings - Fork 1
/
op.go
65 lines (58 loc) · 1.48 KB
/
op.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package chotki
import (
"github.com/drpcorg/chotki/protocol"
"github.com/drpcorg/chotki/rdx"
)
func ParsePacket(pack []byte) (lit byte, id, ref rdx.ID, body []byte, err error) {
lit, hlen, blen := protocol.ProbeHeader(pack)
if lit == 0 || lit == '-' || hlen+blen > len(pack) {
err = rdx.ErrBadPacket
return
}
body = pack[hlen : hlen+blen]
i, ihlen, iblen := protocol.ProbeHeader(body)
if lit != 'D' && lit != 'V' && lit != 'H' {
if i != 'I' && i != '0' {
err = rdx.ErrBadPacket
return
}
} else {
if i != 'T' && i != '0' {
err = rdx.ErrBadPacket
return
}
}
id = rdx.IDFromZipBytes(body[ihlen : ihlen+iblen])
body = body[ihlen+iblen:]
r, rhlen, rblen := protocol.ProbeHeader(body)
if r == 'R' {
ref = rdx.IDFromZipBytes(body[rhlen : rhlen+rblen])
body = body[rhlen+rblen:]
}
return
}
func ParseHandshake(body []byte) (mode SyncMode, vv rdx.VV, trace_id []byte, err error) {
// handshake: H(T{pro,src} M(mode) V(V{p,s}+), T(trace_id) ...)
var mbody, vbody []byte
rest := body
mbody, rest = protocol.Take('M', rest)
if mbody == nil {
return 0, nil, nil, ErrBadHPacket
}
vbody, rest = protocol.Take('V', rest)
if vbody == nil {
return 0, nil, nil, ErrBadHPacket
}
vv = make(rdx.VV)
if err := vv.PutTLV(vbody); err != nil {
return 0, nil, nil, err
}
if err := mode.Unzip(mbody); err != nil {
return 0, nil, nil, err
}
trace_id, _ = protocol.Take('S', rest)
if trace_id == nil {
return 0, nil, nil, ErrBadHPacket
}
return mode, vv, trace_id, nil
}