]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
95159e55 | 4 | "bufio" |
9174dbe8 | 5 | "bytes" |
6988a057 JH |
6 | "encoding/binary" |
7 | "errors" | |
8 | "fmt" | |
95159e55 | 9 | "io" |
6988a057 | 10 | "math/rand" |
9c44621e | 11 | "slices" |
6988a057 JH |
12 | ) |
13 | ||
14 | const ( | |
d005ef04 JH |
15 | TranError = 0 |
16 | TranGetMsgs = 101 | |
17 | TranNewMsg = 102 | |
18 | TranOldPostNews = 103 | |
19 | TranServerMsg = 104 | |
20 | TranChatSend = 105 | |
21 | TranChatMsg = 106 | |
22 | TranLogin = 107 | |
23 | TranSendInstantMsg = 108 | |
24 | TranShowAgreement = 109 | |
25 | TranDisconnectUser = 110 | |
26 | TranDisconnectMsg = 111 // TODO: implement server initiated friendly disconnect | |
27 | TranInviteNewChat = 112 | |
28 | TranInviteToChat = 113 | |
29 | TranRejectChatInvite = 114 | |
30 | TranJoinChat = 115 | |
31 | TranLeaveChat = 116 | |
32 | TranNotifyChatChangeUser = 117 | |
33 | TranNotifyChatDeleteUser = 118 | |
34 | TranNotifyChatSubject = 119 | |
35 | TranSetChatSubject = 120 | |
36 | TranAgreed = 121 | |
37 | TranServerBanner = 122 | |
38 | TranGetFileNameList = 200 | |
39 | TranDownloadFile = 202 | |
40 | TranUploadFile = 203 | |
41 | TranNewFolder = 205 | |
42 | TranDeleteFile = 204 | |
43 | TranGetFileInfo = 206 | |
44 | TranSetFileInfo = 207 | |
45 | TranMoveFile = 208 | |
46 | TranMakeFileAlias = 209 | |
47 | TranDownloadFldr = 210 | |
48 | TranDownloadInfo = 211 // TODO: implement file transfer queue | |
49 | TranDownloadBanner = 212 | |
50 | TranUploadFldr = 213 | |
51 | TranGetUserNameList = 300 | |
52 | TranNotifyChangeUser = 301 | |
53 | TranNotifyDeleteUser = 302 | |
54 | TranGetClientInfoText = 303 | |
55 | TranSetClientUserInfo = 304 | |
56 | TranListUsers = 348 | |
57 | TranUpdateUser = 349 | |
58 | TranNewUser = 350 | |
59 | TranDeleteUser = 351 | |
60 | TranGetUser = 352 | |
61 | TranSetUser = 353 | |
62 | TranUserAccess = 354 | |
63 | TranUserBroadcast = 355 | |
64 | TranGetNewsCatNameList = 370 | |
65 | TranGetNewsArtNameList = 371 | |
66 | TranDelNewsItem = 380 | |
67 | TranNewNewsFldr = 381 | |
68 | TranNewNewsCat = 382 | |
69 | TranGetNewsArtData = 400 | |
70 | TranPostNewsArt = 410 | |
71 | TranDelNewsArt = 411 | |
72 | TranKeepAlive = 500 | |
6988a057 JH |
73 | ) |
74 | ||
75 | type Transaction struct { | |
153e2eac JH |
76 | Flags byte // Reserved (should be 0) |
77 | IsReply byte // Request (0) or reply (1) | |
78 | Type [2]byte // Requested operation (user defined) | |
79 | ID [4]byte // Unique transaction ID (must be != 0) | |
80 | ErrorCode [4]byte // Used in the reply (user defined, 0 = no error) | |
81 | TotalSize [4]byte // Total data size for the transaction (all parts) | |
82 | DataSize [4]byte // Size of data in this transaction part. This allows splitting large transactions into smaller parts. | |
83 | ParamCount [2]byte // Number of the parameters for this transaction | |
6988a057 | 84 | Fields []Field |
95159e55 JH |
85 | |
86 | clientID *[]byte // Internal identifier for target client | |
87 | readOffset int // Internal offset to track read progress | |
6988a057 JH |
88 | } |
89 | ||
90 | func NewTransaction(t int, clientID *[]byte, fields ...Field) *Transaction { | |
91 | typeSlice := make([]byte, 2) | |
92 | binary.BigEndian.PutUint16(typeSlice, uint16(t)) | |
93 | ||
94 | idSlice := make([]byte, 4) | |
95 | binary.BigEndian.PutUint32(idSlice, rand.Uint32()) | |
96 | ||
97 | return &Transaction{ | |
153e2eac JH |
98 | clientID: clientID, |
99 | Type: [2]byte(typeSlice), | |
100 | ID: [4]byte(idSlice), | |
101 | Fields: fields, | |
6988a057 JH |
102 | } |
103 | } | |
104 | ||
854a92fc JH |
105 | // Write implements io.Writer interface for Transaction |
106 | func (t *Transaction) Write(p []byte) (n int, err error) { | |
107 | totalSize := binary.BigEndian.Uint32(p[12:16]) | |
6988a057 JH |
108 | |
109 | // the buf may include extra bytes that are not part of the transaction | |
110 | // tranLen represents the length of bytes that are part of the transaction | |
111 | tranLen := int(20 + totalSize) | |
112 | ||
854a92fc JH |
113 | if tranLen > len(p) { |
114 | return n, errors.New("buflen too small for tranLen") | |
6988a057 | 115 | } |
95159e55 JH |
116 | |
117 | // Create a new scanner for parsing incoming bytes into transaction tokens | |
118 | scanner := bufio.NewScanner(bytes.NewReader(p[22:tranLen])) | |
119 | scanner.Split(fieldScanner) | |
120 | ||
121 | for i := 0; i < int(binary.BigEndian.Uint16(p[20:22])); i++ { | |
122 | scanner.Scan() | |
123 | ||
124 | var field Field | |
125 | if _, err := field.Write(scanner.Bytes()); err != nil { | |
126 | return 0, fmt.Errorf("error reading field: %w", err) | |
127 | } | |
128 | t.Fields = append(t.Fields, field) | |
6988a057 JH |
129 | } |
130 | ||
854a92fc JH |
131 | t.Flags = p[0] |
132 | t.IsReply = p[1] | |
153e2eac JH |
133 | t.Type = [2]byte(p[2:4]) |
134 | t.ID = [4]byte(p[4:8]) | |
135 | t.ErrorCode = [4]byte(p[8:12]) | |
136 | t.TotalSize = [4]byte(p[12:16]) | |
137 | t.DataSize = [4]byte(p[16:20]) | |
138 | t.ParamCount = [2]byte(p[20:22]) | |
854a92fc JH |
139 | |
140 | return len(p), err | |
6988a057 JH |
141 | } |
142 | ||
3178ae58 | 143 | const tranHeaderLen = 20 // fixed length of transaction fields before the variable length fields |
6988a057 | 144 | |
3178ae58 JH |
145 | // transactionScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens |
146 | func transactionScanner(data []byte, _ bool) (advance int, token []byte, err error) { | |
147 | // The bytes that contain the size of a transaction are from 12:16, so we need at least 16 bytes | |
148 | if len(data) < 16 { | |
149 | return 0, nil, nil | |
150 | } | |
6988a057 | 151 | |
3178ae58 | 152 | totalSize := binary.BigEndian.Uint32(data[12:16]) |
6988a057 | 153 | |
3178ae58 JH |
154 | // tranLen represents the length of bytes that are part of the transaction |
155 | tranLen := int(tranHeaderLen + totalSize) | |
156 | if tranLen > len(data) { | |
157 | return 0, nil, nil | |
6988a057 JH |
158 | } |
159 | ||
3178ae58 | 160 | return tranLen, data[0:tranLen], nil |
6988a057 JH |
161 | } |
162 | ||
163 | const minFieldLen = 4 | |
164 | ||
165 | func ReadFields(paramCount []byte, buf []byte) ([]Field, error) { | |
166 | paramCountInt := int(binary.BigEndian.Uint16(paramCount)) | |
167 | if paramCountInt > 0 && len(buf) < minFieldLen { | |
168 | return []Field{}, fmt.Errorf("invalid field length %v", len(buf)) | |
169 | } | |
170 | ||
171 | // A Field consists of: | |
172 | // ID: 2 bytes | |
173 | // Size: 2 bytes | |
174 | // Data: FieldSize number of bytes | |
175 | var fields []Field | |
176 | for i := 0; i < paramCountInt; i++ { | |
177 | if len(buf) < minFieldLen { | |
178 | return []Field{}, fmt.Errorf("invalid field length %v", len(buf)) | |
179 | } | |
180 | fieldID := buf[0:2] | |
181 | fieldSize := buf[2:4] | |
182 | fieldSizeInt := int(binary.BigEndian.Uint16(buf[2:4])) | |
183 | expectedLen := minFieldLen + fieldSizeInt | |
184 | if len(buf) < expectedLen { | |
185 | return []Field{}, fmt.Errorf("field length too short") | |
186 | } | |
187 | ||
188 | fields = append(fields, Field{ | |
95159e55 JH |
189 | ID: [2]byte(fieldID), |
190 | FieldSize: [2]byte(fieldSize), | |
6988a057 JH |
191 | Data: buf[4 : 4+fieldSizeInt], |
192 | }) | |
193 | ||
194 | buf = buf[fieldSizeInt+4:] | |
195 | } | |
196 | ||
197 | if len(buf) != 0 { | |
198 | return []Field{}, fmt.Errorf("extra field bytes") | |
199 | } | |
200 | ||
201 | return fields, nil | |
202 | } | |
203 | ||
95159e55 JH |
204 | // Read implements the io.Reader interface for Transaction |
205 | func (t *Transaction) Read(p []byte) (int, error) { | |
6988a057 JH |
206 | payloadSize := t.Size() |
207 | ||
208 | fieldCount := make([]byte, 2) | |
209 | binary.BigEndian.PutUint16(fieldCount, uint16(len(t.Fields))) | |
210 | ||
95159e55 JH |
211 | bbuf := new(bytes.Buffer) |
212 | ||
6988a057 | 213 | for _, field := range t.Fields { |
0ed51327 JH |
214 | f := field |
215 | _, err := bbuf.ReadFrom(&f) | |
95159e55 JH |
216 | if err != nil { |
217 | return 0, fmt.Errorf("error reading field: %w", err) | |
218 | } | |
6988a057 JH |
219 | } |
220 | ||
95159e55 | 221 | buf := slices.Concat( |
6988a057 | 222 | []byte{t.Flags, t.IsReply}, |
153e2eac JH |
223 | t.Type[:], |
224 | t.ID[:], | |
225 | t.ErrorCode[:], | |
6988a057 JH |
226 | payloadSize, |
227 | payloadSize, // this is the dataSize field, but seeming the same as totalSize | |
228 | fieldCount, | |
95159e55 JH |
229 | bbuf.Bytes(), |
230 | ) | |
231 | ||
232 | if t.readOffset >= len(buf) { | |
233 | return 0, io.EOF // All bytes have been read | |
234 | } | |
235 | ||
236 | n := copy(p, buf[t.readOffset:]) | |
237 | t.readOffset += n | |
238 | ||
239 | return n, nil | |
6988a057 JH |
240 | } |
241 | ||
242 | // Size returns the total size of the transaction payload | |
0a92e50b | 243 | func (t *Transaction) Size() []byte { |
6988a057 JH |
244 | bs := make([]byte, 4) |
245 | ||
246 | fieldSize := 0 | |
247 | for _, field := range t.Fields { | |
248 | fieldSize += len(field.Data) + 4 | |
249 | } | |
250 | ||
251 | binary.BigEndian.PutUint32(bs, uint32(fieldSize+2)) | |
252 | ||
253 | return bs | |
254 | } | |
255 | ||
0a92e50b | 256 | func (t *Transaction) GetField(id int) Field { |
6988a057 | 257 | for _, field := range t.Fields { |
95159e55 | 258 | if id == int(binary.BigEndian.Uint16(field.ID[:])) { |
6988a057 JH |
259 | return field |
260 | } | |
261 | } | |
262 | ||
263 | return Field{} | |
264 | } | |
9174dbe8 JH |
265 | |
266 | func (t *Transaction) IsError() bool { | |
153e2eac | 267 | return t.ErrorCode == [4]byte{0, 0, 0, 1} |
9174dbe8 | 268 | } |