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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#include "query.hh"
#include "config.hh"
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
static int ApplicationType;
static char LoginData[30];
void SetQueryManagerLoginData(int Type, const char *Data){
ApplicationType = Type;
if(Data != NULL){
strncpy(LoginData, Data, sizeof(LoginData));
LoginData[sizeof(LoginData) - 1] = 0;
}else{
LoginData[0] = 0;
}
}
// TODO(fusion): An initializer list executes in the same order fields are
// declared. This initialization should work as long as `BufferSize` is the
// first field declared in `TQueryManagerConnection`, which it is.
TQueryManagerConnection::TQueryManagerConnection(int QueryBufferSize) :
BufferSize(std::max<int>(QueryBufferSize, KB(16))),
Buffer(new uint8[this->BufferSize]),
ReadBuffer(this->Buffer, this->BufferSize),
WriteBuffer(this->Buffer, this->BufferSize),
Socket(-1),
QueryOk(false)
{
this->connect();
}
TQueryManagerConnection::~TQueryManagerConnection(void){
this->disconnect();
delete[] this->Buffer;
}
bool ResolveHostNameAddress(const char *HostName, in_addr_t *OutAddr){
// TODO(fusion): Just use `getaddrinfo`?
int ErrorCode;
hostent HostEnt;
hostent *HostEntResult;
char Buffer[2048];
int Ret = gethostbyname_r(HostName, &HostEnt,
Buffer, sizeof(Buffer), &HostEntResult, &ErrorCode);
if(Ret != 0 || HostEntResult == NULL){
return false;
}
if(OutAddr){
*OutAddr = ((in_addr_t)HostEntResult->h_addr_list[0][0] << 24)
| ((in_addr_t)HostEntResult->h_addr_list[0][1] << 16)
| ((in_addr_t)HostEntResult->h_addr_list[0][2] << 8)
| ((in_addr_t)HostEntResult->h_addr_list[0][3] << 0);
}
return true;
}
void TQueryManagerConnection::connect(void){
for(int i = 0; i < NumberOfQueryManagers; i += 1){
in_addr_t Addr = inet_addr(QUERY_MANAGER[i].Host);
if(Addr == 0xFFFFFFFF && !ResolveHostNameAddress(QUERY_MANAGER[i].Host, &Addr)){
print(2, "TQueryManagerConnection::connect: Kann Rechnernamen nicht auflösen.\n");
continue;
}
this->Socket = socket(AF_INET, SOCK_STREAM, 0);
if(this->Socket == -1){
print(2, "TQueryManagerConnection::connect: Kann Socket nicht öffnen.\n");
continue;
}
struct sockaddr_in QueryManagerAddress = {};
QueryManagerAddress.sin_family = AF_INET;
QueryManagerAddress.sin_port = htons(QUERY_MANAGER[i].Port);
QueryManagerAddress.sin_addr.s_addr = Addr;
if(::connect(this->Socket, (struct sockaddr*)&QueryManagerAddress, sizeof(QueryManagerAddress)) == -1){
this->disconnect();
continue;
}
this->prepareQuery(0);
this->sendByte((uint8)ApplicationType);
this->sendString(QUERY_MANAGER[i].Password);
if(ApplicationType == 1){
this->sendString(LoginData);
}
int Ret = this->executeQuery(30, false);
if(Ret != 0){
print(2, "TQueryManagerConnection::connect: Anmeldung fehlgeschlagen (%d).\n", Ret);
this->disconnect();
continue;
}
break;
}
if(!this->isConnected()){
print(2, "TQueryManagerConnection::connect: Kein Query-Manager verfügbar.\n");
}
}
void TQueryManagerConnection::disconnect(void){
if(this->Socket != -1){
if(close(this->Socket) == -1){
error("TQueryManagerConnection::disconnect: Fehler %d beim Schließen der Socket.\n", errno);
}
this->Socket = -1;
}
}
int TQueryManagerConnection::write(const uint8 *Buffer, int Size){
int Attempts = 50;
int BytesToWrite = Size;
const uint8 *WritePtr = Buffer;
while(BytesToWrite > 0){
int BytesWritten = (int)::write(this->Socket, WritePtr, BytesToWrite);
if(BytesWritten > 0){
BytesToWrite -= BytesWritten;
WritePtr += BytesWritten;
}else if(BytesWritten == 0){
break;
}else{
// TODO(fusion): We don't set the socket as non blocking so I don't
// think we can even get `EAGAIN` here.
if(errno != EAGAIN || Attempts <= 0){
return -1;
}
DelayThread(0, 100000);
Attempts -= 1;
}
}
return Size - BytesToWrite;
}
int TQueryManagerConnection::read(uint8 *Buffer, int Size, int Timeout){
int Attempts = 50;
int BytesToRead = Size;
uint8 *ReadPtr = Buffer;
while(BytesToRead > 0){
struct pollfd pollfd = {};
pollfd.fd = this->Socket;
pollfd.events = POLLIN;
if(poll(&pollfd, 1, Timeout * 1000) != 1){
return -2;
}
int BytesRead = ::read(this->Socket, ReadPtr, BytesToRead);
if(BytesRead > 0){
BytesToRead -= BytesRead;
ReadPtr += BytesRead;
}else if(BytesRead == 0){
break;
}else if(errno != EINTR){
if(errno != EAGAIN || BytesToRead == Size || Attempts <= 0){
return -1;
}
DelayThread(0, 100000);
Attempts -= 1;
}
}
return Size - BytesToRead;
}
void TQueryManagerConnection::prepareQuery(int QueryType){
this->QueryOk = true;
this->WriteBuffer.Position = 0;
try{
this->WriteBuffer.writeWord(0);
this->WriteBuffer.writeByte((uint8)QueryType);
}catch(const char *str){
error("TQueryManagerConnection::prepareQuery: %s.\n", str);
this->QueryOk = false;
}
}
void TQueryManagerConnection::sendFlag(bool Flag){
this->sendByte(Flag ? 0x01 : 0x00);
}
void TQueryManagerConnection::sendByte(uint8 Byte){
try{
this->WriteBuffer.writeByte(Byte);
}catch(const char *str){
error("TQueryManagerConnection::sendByte: %s.\n", str);
this->QueryOk = false;
}
}
void TQueryManagerConnection::sendWord(uint16 Word){
try{
this->WriteBuffer.writeWord(Word);
}catch(const char *str){
error("TQueryManagerConnection::sendWord: %s.\n", str);
this->QueryOk = false;
}
}
void TQueryManagerConnection::sendQuad(uint32 Quad){
try{
this->WriteBuffer.writeQuad(Quad);
}catch(const char *str){
error("TQueryManagerConnection::sendQuad: %s.\n", str);
this->QueryOk = false;
}
}
void TQueryManagerConnection::sendString(const char *String){
try{
this->WriteBuffer.writeString(String);
}catch(const char *str){
error("TQueryManagerConnection::sendString: %s.\n", str);
this->QueryOk = false;
}
}
void TQueryManagerConnection::sendBytes(const uint8 *Buffer, int Count){
try{
this->WriteBuffer.writeBytes(Buffer, Count);
}catch(const char *str){
error("TQueryManagerConnection::sendBytes: %s.\n", str);
this->QueryOk = false;
}
}
bool TQueryManagerConnection::getFlag(void){
return this->getByte() != 0;
}
uint8 TQueryManagerConnection::getByte(void){
uint8 Result = 0;
try{
Result = this->ReadBuffer.readByte();
}catch(const char *str){
error("TQueryManagerConnection::getByte: %s.\n", str);
}
return Result;
}
uint16 TQueryManagerConnection::getWord(void){
uint16 Result = 0;
try{
Result = this->ReadBuffer.readWord();
}catch(const char *str){
error("TQueryManagerConnection::getWord: %s.\n", str);
}
return Result;
}
uint32 TQueryManagerConnection::getQuad(void){
uint32 Result = 0;
try{
Result = this->ReadBuffer.readQuad();
}catch(const char *str){
error("TQueryManagerConnection::getQuad: %s.\n", str);
}
return Result;
}
void TQueryManagerConnection::getString(char *Buffer, int MaxLength){
try{
this->ReadBuffer.readString(Buffer, MaxLength);
}catch(const char *str){
error("TQueryManagerConnection::getString: %s.\n", str);
if(MaxLength > 0){
Buffer[0] = 0;
}
}
}
void TQueryManagerConnection::getBytes(uint8 *Buffer, int Count){
try{
this->ReadBuffer.readBytes(Buffer, Count);
}catch(const char *str){
error("TQueryManagerConnection::getBytes: %s.\n", str);
}
}
int TQueryManagerConnection::executeQuery(int Timeout, bool AutoReconnect){
// NOTE(fusion): `prepareQuery` will already leave two extra bytes at the
// beginning of the buffer for writing the request's length.
int PacketSize = this->WriteBuffer.Position;
int PayloadSize = PacketSize - 2;
this->WriteBuffer.Position = 0;
if(PayloadSize < 0xFFFF){
this->sendWord((uint16)PayloadSize);
}else{
PacketSize += 4;
if(PacketSize > this->BufferSize){
error("TQueryManagerConnection::executeQuery: Puffer zu klein.\n");
return QUERY_FAILED;
}
memmove(&this->Buffer[6], &this->Buffer[2], PayloadSize);
this->sendWord(0xFFFF);
this->sendQuad((uint32)PayloadSize);
}
if(!this->QueryOk){
error("TQueryManagerConnection::executeQuery: Fehler beim Zusammenbauen der Anfrage.\n");
return QUERY_FAILED;
}
for(int Attempt = 0; true; Attempt += 1){
if(!this->isConnected()){
if(!AutoReconnect){
return QUERY_FAILED;
}
// TODO(fusion): There was also a "fast" path that allocated on the
// stack with variable length arrays. It would be a problem to mimic
// that with `alloca` since its memory is only released at the end
// of the function and we're in a loop.
uint8 *TempBuffer = new uint8[PacketSize];
memcpy(TempBuffer, this->Buffer, PacketSize);
this->connect();
memcpy(this->Buffer, TempBuffer, PacketSize);
delete[] TempBuffer;
if(!this->isConnected()){
return QUERY_FAILED;
}
}
if(this->write(this->Buffer, PacketSize) != PacketSize){
this->disconnect();
if(Attempt > 0){
error("TQueryManagerConnection::executeQuery: Fehler beim Abschicken der Anfrage.\n");
return QUERY_FAILED;
}
continue;
}
uint8 Help[4];
int BytesRead = this->read(Help, 2, Timeout);
if(BytesRead != 2){
this->disconnect();
if(BytesRead == -2 || Attempt > 0){
return QUERY_FAILED;
}
continue;
}
int ResponseSize = ((int)Help[0]) | ((int)Help[1] << 8);
if(ResponseSize == 0xFFFF){
if(this->read(Help, 4, Timeout) != 4){
this->disconnect();
return QUERY_FAILED;
}
ResponseSize = ((int)Help[0]) | ((int)Help[1] << 8)
| ((int)Help[2] << 16) | ((int)Help[3] << 24);
}
if(ResponseSize <= 0 || ResponseSize > this->BufferSize){
this->disconnect();
error("TQueryManagerConnection::executeQuery: Ungültige Datengröße %d.\n", ResponseSize);
return QUERY_FAILED;
}
if(this->read(this->Buffer, ResponseSize, Timeout) != ResponseSize){
this->disconnect();
error("TQueryManagerConnection::executeQuery: Fehler beim Auslesen der Daten.\n");
return QUERY_FAILED;
}
int Status = this->getByte();
if(Status == QUERY_FAILED){
error("TQueryManagerConnection::executeQuery: Anfrage fehlgeschlagen.\n");
}
return Status;
}
}
|