-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdateCheckThread.cpp
executable file
·447 lines (346 loc) · 14.2 KB
/
UpdateCheckThread.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
//---------------------------------------------------------------------------
#include "stdinc.h"
//---------------------------------------------------------------------------
#include "ServerManager.h"
#include "Utility.h"
//---------------------------------------------------------------------------
#ifdef _WIN32
#pragma hdrstop
#endif
//---------------------------------------------------------------------------
#include "UpdateCheckThread.h"
//---------------------------------------------------------------------------
#include "../gui.win/MainWindow.h"
//---------------------------------------------------------------------------
clsUpdateCheckThread * clsUpdateCheckThread::mPtr = NULL;
//---------------------------------------------------------------------------
clsUpdateCheckThread::clsUpdateCheckThread() {
sSocket = INVALID_SOCKET;
ui32FileLen = 0;
ui32RecvBufLen = ui32RecvBufSize = 0;
ui32BytesRead = ui32BytesSent = 0;
hThread = INVALID_HANDLE_VALUE;
sRecvBuf = NULL;
bOk = bData = bTerminated = false;
sMsg[0] = '\0';
}
//---------------------------------------------------------------------------
clsUpdateCheckThread::~clsUpdateCheckThread() {
clsServerManager::ui64BytesRead += (uint64_t)ui32BytesRead;
clsServerManager::ui64BytesSent += (uint64_t)ui32BytesSent;
if(sSocket != INVALID_SOCKET) {
#ifdef _WIN32
shutdown(sSocket, SD_SEND);
closesocket(sSocket);
#else
shutdown(sSocket, 1);
close(sSocket);
#endif
sSocket = INVALID_SOCKET;
}
free(sRecvBuf);
if(hThread != INVALID_HANDLE_VALUE) {
::CloseHandle(hThread);
}
}
//---------------------------------------------------------------------------
unsigned __stdcall ExecuteUpdateCheck(void * /*pArguments*/) {
clsUpdateCheckThread::mPtr->Run();
return 0;
}
//---------------------------------------------------------------------------
void clsUpdateCheckThread::Resume() {
hThread = (HANDLE)_beginthreadex(NULL, 0, ExecuteUpdateCheck, NULL, 0, NULL);
if(hThread == 0) {
AppendDebugLog("%s - [ERR] Failed to create new UpdateCheckThread\n", 0);
}
}
//---------------------------------------------------------------------------
void clsUpdateCheckThread::Run() {
struct addrinfo * pResult = NULL;
struct addrinfo hints;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_socktype = SOCK_STREAM;
if(clsServerManager::bUseIPv6 == true) {
hints.ai_family = AF_UNSPEC;
} else {
hints.ai_family = AF_INET;
}
if(::getaddrinfo("www.Lib.org", "80", &hints, &pResult) != 0 || (pResult->ai_family != AF_INET && pResult->ai_family != AF_INET6)) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check resolve error %s (%d).", WSErrorStr(iError), iError);
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Run") == true) {
Message(sMsg, iMsgLen);
}
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
if(pResult != NULL) {
::freeaddrinfo(pResult);
}
return;
}
// Initialise socket we want to use for connect
#ifdef _WIN32
if((sSocket = socket(pResult->ai_family, pResult->ai_socktype, pResult->ai_protocol)) == INVALID_SOCKET) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check create error %s (%d).", WSErrorStr(iError), iError);
#else
if((sSocket = socket(pResult->ai_family, pResult->ai_socktype, pResult->ai_protocol)) == -1) {
int iMsgLen = sprintf(sMsg, "Update check create error %s (%d).", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Run1") == true) {
Message(sMsg, iMsgLen);
}
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
::freeaddrinfo(pResult);
return;
}
// Set the receive buffer
int32_t bufsize = 8192;
#ifdef _WIN32
if(setsockopt(sSocket, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize, sizeof(bufsize)) == SOCKET_ERROR) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check recv buff error %s (%d).", WSErrorStr(iError), iError);
#else
if(setsockopt(sSocket, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize)) == -1) {
int iMsgLen = sprintf(sMsg, "Update check recv buff error %s (%d).", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Run2") == true) {
Message(sMsg, iMsgLen);
}
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
::freeaddrinfo(pResult);
return;
}
// Set the send buffer
bufsize = 2048;
#ifdef _WIN32
if(setsockopt(sSocket, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize, sizeof(bufsize)) == SOCKET_ERROR) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check send buff error %s (%d).", WSErrorStr(iError), iError);
#else
if(setsockopt(sSocket, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)) == -1) {
int iMsgLen = sprintf(sMsg, "Update check buff error %s (%d).", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Run3") == true) {
Message(sMsg, iMsgLen);
}
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
::freeaddrinfo(pResult);
return;
}
Message("Connecting to Lib.org ...", 28);
// Finally, time to connect ! ;)
#ifdef _WIN32
if(connect(sSocket, pResult->ai_addr, (int)pResult->ai_addrlen) == SOCKET_ERROR) {
int iError = WSAGetLastError();
if(iError != WSAEWOULDBLOCK) {
int iMsgLen = sprintf(sMsg, "Update check connect error %s (%d).", WSErrorStr(iError), iError);
#else
if(connect(sSocket, pResult->ai_addr, (int)pResult->ai_addrlen) == -1) {
if(errno != EAGAIN) {
int iMsgLen = sprintf(sMsg, "Update check connect error %s (%d).", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Run4") == true) {
Message(sMsg, iMsgLen);
}
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
::freeaddrinfo(pResult);
return;
}
}
::freeaddrinfo(pResult);
Message("Connected to Lib.org, sending request...", 43);
if(SendHeader() == false) {
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
return;
}
Message("Request to Lib.org sent, receiving data...", 45);
// Set non-blocking mode
#ifdef _WIN32
uint32_t block = 1;
if(ioctlsocket(sSocket, FIONBIO, (unsigned long *)&block) == SOCKET_ERROR) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check non-block error %s (%d).", WSErrorStr(iError), iError);
#else
int32_t oldFlag = fcntl(u->s, F_GETFL, 0);
if(fcntl(sSocket, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
int iMsgLen = sprintf(sMsg, "Update check non-block error %s (%d).", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Run5") == true) {
Message(sMsg, iMsgLen);
}
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
return;
}
sRecvBuf = (char *)malloc(512);
if(sRecvBuf == NULL) {
AppendDebugLog("%s - [MEM] Cannot allocate 512 bytes for sRecvBuf in clsUpdateCheckThread::Run\n", 0);
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
return;
}
uint16_t iLoops = 0;
while(bTerminated == false && iLoops < 4000) {
iLoops++;
if(Receive() == false) {
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
return;
}
::Sleep(75);
}
if(bTerminated == false) {
Message("Update check timeout.", 21);
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
}
}
//---------------------------------------------------------------------------
void clsUpdateCheckThread::Close() {
bTerminated = true;
}
//---------------------------------------------------------------------------
void clsUpdateCheckThread::WaitFor() {
::WaitForSingleObject(hThread, INFINITE);
}
//---------------------------------------------------------------------------
void clsUpdateCheckThread::Message(char * sMessage, const size_t &szLen) {
char *sMess = (char *)malloc(szLen + 1);
if(sMess == NULL) {
AppendDebugLog("%s - [MEM] Cannot allocate %" PRIu64 " bytes for sMess in clsUpdateCheckThread::Message\n", (uint64_t)(szLen+1));
return;
}
memcpy(sMess, sMessage, szLen);
sMess[szLen] = '\0';
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_MSG, 0, (LPARAM)sMess);
}
//---------------------------------------------------------------------------
bool clsUpdateCheckThread::SendHeader() {
char * sDataToSend = "GET /version HTTP/1.1\r\nUser-Agent: Lib " LibVersionString " [" BUILD_NUMBER "]"
"\r\nHost: www.Lib.org\r\nConnection: close\r\nCache-Control: no-cache\r\nAccept: */*\r\nAccept-Language: en\r\n\r\n";
int iBytes = send(sSocket, sDataToSend, (int)strlen(sDataToSend), 0);
#ifdef _WIN32
if(iBytes == SOCKET_ERROR) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check send error %s (%d).", WSErrorStr(iError), iError);
#else
if(iBytes == -1) {
int iMsgLen = sprintf(sMsg, "Update check send error %s (%d).)", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::SendHeader") == true) {
Message(sMsg, iMsgLen);
}
return false;
}
ui32BytesSent += iBytes;
return true;
}
//---------------------------------------------------------------------------
bool clsUpdateCheckThread::Receive() {
u_long ui32bytes = 0;
if(ioctlsocket(sSocket, FIONREAD, &ui32bytes) == SOCKET_ERROR) {
int iError = WSAGetLastError();
int iMsgLen = sprintf(sMsg, "Update check ioctlsocket(FIONREAD) error %s (%d).", WSErrorStr(iError), iError);
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Receive") == true) {
Message(sMsg, iMsgLen);
}
return false;
}
if(ui32bytes == 0) {
// we need to try receive to catch connection error, or if server closed connection
ui32bytes = 16;
} else if(ui32bytes > 8192) {
// receive max. 8192 bytes to receive buffer
ui32bytes = 8192;
}
if(ui32RecvBufSize < ui32RecvBufLen + ui32bytes) {
size_t szAllignLen = ((ui32RecvBufLen + ui32bytes + 1) & 0xFFFFFE00) + 0x200;
char * pOldBuf = sRecvBuf;
sRecvBuf = (char *)realloc(sRecvBuf, szAllignLen);
if(sRecvBuf == NULL) {
sRecvBuf = pOldBuf;
AppendDebugLog("%s - [MEM] Cannot reallocate %" PRIu64 " bytes for sRecvBuf in clsUpdateCheckThread::Receive\n", (uint64_t)szAllignLen);
return false;
}
ui32RecvBufSize = (int)szAllignLen - 1;
}
int iBytes = recv(sSocket, sRecvBuf + ui32RecvBufLen, ui32RecvBufSize - ui32RecvBufLen, 0);
#ifdef _WIN32
if(iBytes == SOCKET_ERROR) {
int iError = WSAGetLastError();
if(iError != WSAEWOULDBLOCK) {
int iMsgLen = sprintf(sMsg, "Update check recv error %s (%d).", WSErrorStr(iError), iError);
#else
if(iBytes == -1) {
if(errno != EAGAIN) {
int iMsgLen = sprintf(sMsg, "Update check recv error %s (%d).", WSErrorStr(errno), errno);
#endif
if(CheckSprintf(iMsgLen, 2048, "clsUpdateCheckThread::Receive2") == true) {
Message(sMsg, iMsgLen);
}
return false;
} else {
return true;
}
} else if(iBytes == 0) {
Message("Update check closed connection by server.", 41);
return false;
}
ui32BytesRead += iBytes;
ui32RecvBufLen += iBytes;
sRecvBuf[ui32RecvBufLen] = '\0';
if(bData == false) {
char *sBuffer = sRecvBuf;
for(uint32_t ui32i = 0; ui32i < ui32RecvBufLen; ui32i++) {
if(sRecvBuf[ui32i] == '\n') {
sRecvBuf[ui32i] = '\0';
uint32_t ui32iCommandLen = (uint32_t)((sRecvBuf+ui32i) - sBuffer) + 1;
if(ui32iCommandLen > 7 && strncmp(sBuffer, "HTTP", 4) == NULL && strstr(sBuffer, "200") != NULL) {
bOk = true;
} else if(ui32iCommandLen == 2 && sBuffer[0] == '\r') {
if(bOk == true && ui32FileLen != 0) {
bData = true;
} else {
Message("Update check failed.", 20);
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
return false;
}
} else if(ui32iCommandLen > 16 && strncmp(sBuffer, "Content-Length: ", 16) == NULL) {
ui32FileLen = atoi(sBuffer+16);
}
sBuffer += ui32iCommandLen;
if(bData == true) {
break;
}
}
}
ui32RecvBufLen -= (uint32_t)(sBuffer - sRecvBuf);
if(ui32RecvBufLen == 0) {
sRecvBuf[0] = '\0';
} else if(ui32RecvBufLen != 1) {
memmove(sRecvBuf, sBuffer, ui32RecvBufLen);
sRecvBuf[ui32RecvBufLen] = '\0';
} else {
if(sBuffer[0] == '\n') {
sRecvBuf[0] = '\0';
ui32RecvBufLen = 0;
} else {
sRecvBuf[0] = sBuffer[0];
sRecvBuf[1] = '\0';
}
}
}
if(bData == true) {
if(ui32RecvBufLen == (uint32_t)ui32FileLen) {
char *sMess = (char *)malloc(ui32RecvBufLen + 1);
if(sMess == NULL) {
AppendDebugLog("%s - [MEM] Cannot allocate %" PRIu64 " bytes for sMess in clsUpdateCheckThread::Receive\n", (uint64_t)(ui32RecvBufLen+1));
return false;
}
memcpy(sMess, sRecvBuf, ui32RecvBufLen);
sMess[ui32RecvBufLen] = '\0';
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_DATA, 0, (LPARAM)sMess);
::PostMessage(clsMainWindow::mPtr->m_hWnd, WM_UPDATE_CHECK_TERMINATE, 0, 0);
}
}
return true;
}
//---------------------------------------------------------------------------