-
Notifications
You must be signed in to change notification settings - Fork 1
/
op.go
64 lines (60 loc) · 1.61 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
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
}
// PacketSeqSrc picks the I field from the packet.
// Returns 0,0 if nothing found.
func PacketSrcSeq(pack []byte) (src, seq uint32) { // FIXME offset
lit, hlen, blen := protocol.ProbeHeader(pack)
if lit == 0 || hlen+blen > len(pack) {
return
}
v, vhlen, vblen := protocol.ProbeHeader(pack[hlen:])
if v != 'V' || vhlen+vblen > len(pack)-blen {
return
}
big, lil := rdx.UnzipUint64Pair(pack[hlen+vhlen : hlen+vhlen+vblen])
seq = uint32(big)
src = uint32(lil)
return
}
func PacketID(pack []byte) rdx.ID {
lit, hlen, blen := protocol.ProbeHeader(pack)
if lit == 0 || hlen+blen > len(pack) {
return rdx.ZeroId
}
v, vhlen, vblen := protocol.ProbeHeader(pack[hlen:])
if v != 'V' || vhlen+vblen > len(pack)-blen {
return rdx.ZeroId
}
return rdx.IDFromZipBytes(pack[hlen+vhlen : hlen+vhlen+vblen])
}