]> git.r.bdr.sh - rbdr/mobius/blame - hotline/tracker.go
Adopt more fixed size array types for struct fields
[rbdr/mobius] / hotline / tracker.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
8ff2b66d 4 "bufio"
6988a057
JH
5 "encoding/binary"
6 "fmt"
9a75b7cb 7 "io"
6988a057 8 "net"
9a75b7cb 9 "slices"
6988a057
JH
10 "strconv"
11 "time"
12)
13
8ff2b66d 14// TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
6988a057 15type TrackerRegistration struct {
9a75b7cb 16 Port [2]byte // Server's listening TCP port number
40414f92 17 UserCount int // Number of users connected to this particular server
9a75b7cb 18 PassID [4]byte // Random number generated by the server
95159e55 19 Name string // Server Name
40414f92 20 Description string // Description of the server
45ca5d60
JH
21
22 readOffset int // Internal offset to track read progress
6988a057
JH
23}
24
9a75b7cb
JH
25// Read implements io.Reader to write tracker registration payload bytes to slice
26func (tr *TrackerRegistration) Read(p []byte) (int, error) {
6988a057
JH
27 userCount := make([]byte, 2)
28 binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
29
45ca5d60 30 buf := slices.Concat(
9a75b7cb 31 []byte{0x00, 0x01}, // Magic number, always 1
40414f92 32 tr.Port[:],
6988a057 33 userCount,
9a75b7cb
JH
34 []byte{0x00, 0x00}, // Magic number, always 0
35 tr.PassID[:],
6988a057
JH
36 []byte{uint8(len(tr.Name))},
37 []byte(tr.Name),
38 []byte{uint8(len(tr.Description))},
39 []byte(tr.Description),
45ca5d60
JH
40 )
41
42 if tr.readOffset >= len(buf) {
43 return 0, io.EOF // All bytes have been read
44 }
45
46 n := copy(p, buf[tr.readOffset:])
47 tr.readOffset += n
48
49 return n, nil
6988a057
JH
50}
51
e42888eb 52func register(tracker string, tr *TrackerRegistration) error {
6988a057
JH
53 conn, err := net.Dial("udp", tracker)
54 if err != nil {
9a75b7cb 55 return fmt.Errorf("failed to dial tracker: %w", err)
6988a057 56 }
9a75b7cb 57 defer conn.Close()
6988a057 58
9a75b7cb
JH
59 if _, err := io.Copy(conn, tr); err != nil {
60 return fmt.Errorf("failed to write to connection: %w", err)
6988a057
JH
61 }
62
63 return nil
64}
65
6988a057
JH
66const trackerTimeout = 5 * time.Second
67
68// All string values use 8-bit ASCII character set encoding.
69// Client Interface with Tracker
70// After establishing a connection with tracker, the following information is sent:
71// Description Size Data Note
72// Magic number 4 ‘HTRK’
73// Version 2 1 or 2 Old protocol (1) or new (2)
74
75// Reply received from the tracker starts with a header:
6988a057
JH
76type TrackerHeader struct {
77 Protocol [4]byte // "HTRK" 0x4854524B
78 Version [2]byte // Old protocol (1) or new (2)
79}
80
95159e55 81// Message type 2 1 Sending list of servers
aebc4d36
JH
82// Message data size 2 Remaining size of this request
83// Number of servers 2 Number of servers in the server list
84// Number of servers 2 Same as previous field
6988a057
JH
85type ServerInfoHeader struct {
86 MsgType [2]byte // always has value of 1
87 MsgDataSize [2]byte // Remaining size of request
88 SrvCount [2]byte // Number of servers in the server list
89 SrvCountDup [2]byte // Same as previous field ¯\_(ツ)_/¯
90}
91
92type ServerRecord struct {
c5d9af5a
JH
93 IPAddr [4]byte
94 Port [2]byte
95 NumUsers [2]byte // Number of users connected to this particular server
96 Unused [2]byte
95159e55
JH
97 NameSize byte // Length of Name string
98 Name []byte // Server Name
6988a057
JH
99 DescriptionSize byte
100 Description []byte
101}
102
103func GetListing(addr string) ([]ServerRecord, error) {
104 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
9004987d
JH
105 if err != nil {
106 return []ServerRecord{}, err
107 }
0203a8c5
JH
108 defer func() { _ = conn.Close() }()
109
6988a057
JH
110 _, err = conn.Write(
111 []byte{
112 0x48, 0x54, 0x52, 0x4B, // HTRK
113 0x00, 0x01, // Version
114 },
115 )
116 if err != nil {
117 return nil, err
118 }
119
6988a057 120 var th TrackerHeader
8ff2b66d 121 if err := binary.Read(conn, binary.BigEndian, &th); err != nil {
6988a057
JH
122 return nil, err
123 }
124
125 var info ServerInfoHeader
8ff2b66d 126 if err := binary.Read(conn, binary.BigEndian, &info); err != nil {
6988a057
JH
127 return nil, err
128 }
129
6988a057
JH
130 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
131
8ff2b66d
JH
132 scanner := bufio.NewScanner(conn)
133 scanner.Split(serverScanner)
6988a057
JH
134
135 var servers []ServerRecord
136 for {
8ff2b66d 137 scanner.Scan()
a55350da 138
6988a057 139 var srv ServerRecord
95159e55 140 _, err = srv.Write(scanner.Bytes())
04f40273
JH
141 if err != nil {
142 return nil, err
143 }
0203a8c5 144
8ff2b66d 145 servers = append(servers, srv)
6988a057 146 if len(servers) == totalSrv {
8ff2b66d 147 break
6988a057 148 }
8ff2b66d 149 }
6988a057 150
8ff2b66d
JH
151 return servers, nil
152}
153
154// serverScanner implements bufio.SplitFunc for parsing the tracker list into ServerRecords tokens
155// Example payload:
156// 00000000 18 05 30 63 15 7c 00 02 00 00 10 54 68 65 20 4d |..0c.|.....The M|
157// 00000010 6f 62 69 75 73 20 53 74 72 69 70 40 48 6f 6d 65 |obius Strip@Home|
158// 00000020 20 6f 66 20 74 68 65 20 4d 6f 62 69 75 73 20 48 | of the Mobius H|
159// 00000030 6f 74 6c 69 6e 65 20 73 65 72 76 65 72 20 61 6e |otline server an|
160// 00000040 64 20 63 6c 69 65 6e 74 20 7c 20 54 52 54 50 48 |d client | TRTPH|
161// 00000050 4f 54 4c 2e 63 6f 6d 3a 35 35 30 30 2d 4f 3a b2 |OTL.com:5500-O:.|
162// 00000060 15 7c 00 00 00 00 08 53 65 6e 65 63 74 75 73 20 |.|.....Senectus |
163func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
04f40273
JH
164 // The name length field is the 11th byte of the server record. If we don't have that many bytes,
165 // return nil token so the Scanner reads more data and continues scanning.
8ff2b66d
JH
166 if len(data) < 10 {
167 return 0, nil, nil
6988a057 168 }
8ff2b66d
JH
169
170 // A server entry has two variable length fields: the name and description.
04f40273 171 // To get the token length, we first need the name length from the 10th byte
8ff2b66d
JH
172 nameLen := int(data[10])
173
04f40273
JH
174 // The description length field is at the 12th + nameLen byte of the server record.
175 // If we don't have that many bytes, return nil token so the Scanner reads more data and continues scanning.
176 if len(data) < 11+nameLen {
177 return 0, nil, nil
178 }
179
8ff2b66d
JH
180 // Next we need the description length from the 11+nameLen byte:
181 descLen := int(data[11+nameLen])
182
04f40273
JH
183 if len(data) < 12+nameLen+descLen {
184 return 0, nil, nil
185 }
186
8ff2b66d 187 return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
6988a057
JH
188}
189
95159e55
JH
190// Write implements io.Writer for ServerRecord
191func (s *ServerRecord) Write(b []byte) (n int, err error) {
c5d9af5a
JH
192 copy(s.IPAddr[:], b[0:4])
193 copy(s.Port[:], b[4:6])
194 copy(s.NumUsers[:], b[6:8])
6988a057 195 nameLen := int(b[10])
c5d9af5a 196
6988a057
JH
197 s.Name = b[11 : 11+nameLen]
198 s.DescriptionSize = b[11+nameLen]
199 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
200
201 return 12 + nameLen + int(s.DescriptionSize), nil
202}
203
6988a057
JH
204func (s *ServerRecord) Addr() string {
205 return fmt.Sprintf("%s:%s",
c5d9af5a
JH
206 net.IP(s.IPAddr[:]),
207 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),
6988a057
JH
208 )
209}