/
query.go
193 lines (161 loc) · 4.58 KB
/
query.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package extinfo
import (
"bufio"
"errors"
"net"
"strconv"
"time"
"github.com/sauerbraten/cubecode"
)
// builds a request
func buildRequest(infoType byte, extendedInfoType byte, clientNum int) []byte {
request := []byte{}
// extended info request
if infoType == InfoTypeExtended {
request = append(request, infoType, extendedInfoType)
// client stats has to include the clientNum (-1 for all)
if extendedInfoType == ExtInfoTypeClientInfo {
request = append(request, byte(clientNum))
}
}
// basic info request
if infoType == InfoTypeBasic {
request = append(request, byte(infoType))
}
return request
}
// queries the given server and returns the response and an error in case something went wrong. clientNum is optional, put 0 if not needed.
func (s *Server) queryServer(request []byte) (response *cubecode.Packet, err error) {
// connect to server at port+1 (port is the port you connect to in game, sauerbraten listens on the one higher port for BasicInfo queries
var conn *net.UDPConn
conn, err = net.DialUDP("udp", nil, s.addr)
if err != nil {
return
}
defer conn.Close()
// set up a buffered reader
bufconn := bufio.NewReader(conn)
// send the request to server
_, err = conn.Write(request)
if err != nil {
return
}
// receive response from server with 5 second timeout
rawResponse := make([]byte, MaxPacketLength)
var bytesRead int
conn.SetReadDeadline(time.Now().Add(s.timeOut))
bytesRead, err = bufconn.Read(rawResponse)
if err != nil {
return
}
// trim response to what's actually from the server
packet := cubecode.NewPacket(rawResponse[:bytesRead])
// response must include the entire request, ExtInfoAck, ExtInfoVersion, and either ExtInfoError or uptime
if bytesRead < len(request)+3 {
err = errors.New("extinfo: invalid response: too short")
return
}
infoType, err := packet.ReadByte()
if err != nil {
return
}
// end of basic info response handling
if infoType == InfoTypeBasic {
response, err = packet.SubPacketFromRemaining()
return
}
// make sure the entire request is correctly replayed
for i, b := range request {
if rawResponse[i] != b {
err = errors.New("extinfo: invalid response: response does not match request")
return
}
}
command, err := packet.ReadByte()
if err != nil {
return
}
// skip rest of request
for i := 2; i < len(request); i++ {
_, err = packet.ReadByte()
if err != nil {
return
}
}
// validate ack
ack, err := packet.ReadByte()
if err != nil {
return
}
if ack != ExtInfoACK {
err = errors.New("extinfo: invalid response: expected " + strconv.Itoa(int(ExtInfoACK)) + " (ACK), got " + strconv.Itoa(int(ack)))
return
}
// validate version
version, err := packet.ReadByte()
if err != nil {
return
}
// this package only supports protocol version 105
if version != ExtInfoVersion {
err = errors.New("extinfo: wrong version: expected " + strconv.Itoa(int(ExtInfoVersion)) + ", got " + strconv.Itoa(int(version)))
return
}
// end of uptime request handling
if command == ExtInfoTypeUptime {
response, err = packet.SubPacketFromRemaining()
return
}
commandError, err := packet.ReadByte()
if err != nil {
return
}
if commandError == ExtInfoError {
switch command {
case ExtInfoTypeClientInfo:
err = errors.New("extinfo: no client with cn " + strconv.Itoa(int(request[2])))
case ExtInfoTypeTeamScores:
err = errors.New("extinfo: server is not running a team mode")
}
return
}
// end of team scores request handling
if command == ExtInfoTypeTeamScores {
response, err = packet.SubPacketFromRemaining()
return
}
// handle response to ExtInfoTypeClientInfo
// some server mods silently fail to implement responses → fail gracefully
clientNumsHeader, err := packet.ReadByte()
if err != nil {
return
}
if clientNumsHeader != ClientInfoResponseTypeCNs {
err = errors.New("extinfo: invalid response: expected " + strconv.Itoa(int(ExtInfoVersion)) + ", got " + strconv.Itoa(int(version)))
return
}
// count (but discard) CNs
numberOfClients := 0
for packet.HasRemaining() {
_, err = packet.ReadInt()
if err != nil {
return
}
numberOfClients++
}
// for each client, receive a packet and append it to a new slice
clientInfos := make([]byte, 0, MaxPacketLength*numberOfClients)
for i := 0; i < numberOfClients; i++ {
// read from connection
clientInfo := make([]byte, MaxPacketLength)
conn.SetReadDeadline(time.Now().Add(s.timeOut))
_, err = bufconn.Read(clientInfo)
if err != nil {
return
}
// append bytes to slice
clientInfos = append(clientInfos, clientInfo...)
}
response = cubecode.NewPacket(clientInfos)
return
}