-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules.go
112 lines (87 loc) · 1.76 KB
/
rules.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package goseq
import (
"bytes"
"encoding/binary"
"time"
)
// Rules is a key-value pair of
// convar settings for the server.
type RuleMap map[string]string
func (serv *iserver) Rules(limit time.Duration) (rmap RuleMap, err error) {
timer := time.NewTimer(limit)
done := make(chan bool)
go func() { rmap, err = serv.get_rules(); done <- true }()
select {
case <-timer.C:
return nil, Timeout
case <-done:
return
}
}
func (serv *iserver) get_rules() (rmap RuleMap, err error) {
rmap = newRuleMap()
var challenge int32
if challenge, err = serv.getChallenge(byte('V')); err != nil {
return
}
conn, err := serv.getConnection()
if err != nil {
return
}
buf := bytes.NewBuffer(packetHeader[0:])
chal := wrRuleReq{Header: byte('V'), Challenge: challenge}
if err = binary.Write(buf, byteOrder, chal); err != nil {
return
}
chal_bytes := buf.Bytes()
if _, err = conn.Write(chal_bytes); err != nil {
return
}
st := newPacketStream()
if err = st.Gobble(conn); err != nil {
return
}
payload, err := st.GetFullPayload()
if err != nil {
return
}
buf = bytes.NewBuffer(payload)
var rsp_wrapped struct {
Header [4]byte
wrRuleResponse
}
if err = binary.Read(buf, byteOrder, &rsp_wrapped); err != nil {
return
}
rsp := rsp_wrapped.wrRuleResponse
if rsp.Header != byte('E') {
err = PacketHeaderErr
return
}
// for each rule parse the string
for n := int16(0); n < rsp.NumRules; n++ {
var key string
var val string
key, err = rcstr(buf)
if err != nil {
return
}
val, err = rcstr(buf)
if err != nil {
return
}
rmap[key] = val
}
return
}
func newRuleMap() RuleMap {
return make(RuleMap)
}
type wrRuleReq struct {
Header byte
Challenge int32
}
type wrRuleResponse struct {
Header byte
NumRules int16
}