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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
#include "stdafx.h"
#define PING_TIMEOUT 10
/*static void start_ping_timer(connection *c);
void __stdcall ping_alarm(PVOID arg, BOOLEAN TimerOrWaitFired)
{
struct connection *c = (connection*)arg;
if (tglt_get_double_time() - c->last_receive_time > 6 * PING_TIMEOUT)
{
c->TLS->callback.logprintf("fail connection: reason: ping timeout");
}
else if (tglt_get_double_time() - c->last_receive_time > 3 * PING_TIMEOUT)
{
tgl_do_send_ping(c->TLS, c);
start_ping_timer(c);
}
else
{
start_ping_timer(c);
}
}
static void start_ping_timer(connection *c)
{
CreateTimerQueueTimer(&c->ping_timer, hQueue, ping_alarm, c, PING_TIMEOUT * 1000, 0, 0);
}
static void stop_ping_timer(struct connection *c)
{
DeleteTimerQueueTimer(hQueue, c->ping_timer, 0);
c->ping_timer = 0;
}
int mtgln_write_out(struct connection *c, const void *_data, int len)
{
return Netlib_Send(c->hNetlibConn, (char*)_data, len, 0);
}
int mtgln_read_in(struct connection *c, void *_data, int len)
{
return Netlib_Recv(c->hNetlibConn, (char*)_data, len, 0);
}
int mtgln_read_in_lookup(struct connection *c, void *_data, int len)
{
return Netlib_Recv(c->hNetlibConn, (char*)_data, len, MSG_PEEK);
}
connection* mtgln_create_connection(struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods)
{
connection *conn = new connection;
conn->TLS = TLS;
conn->ip = mir_strdup(host);
conn->port = port;
conn->dc = dc;
conn->session = session;
conn->methods = methods;
NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
nloc.szHost = conn->ip;
nloc.wPort = port;
if (!(conn->hNetlibConn = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)((MirTLS*)TLS)->m_proto->m_hNetlibUser, (LPARAM)&nloc)))
{
delete conn;
return 0;
}
char b = 0xfe;
mtgln_write_out(conn, &b, 1);
conn->last_receive_time = tglt_get_double_time();
start_ping_timer(conn);
return conn;
}
static void incr_out_packet_num(struct connection *c) {
}
static struct tgl_dc *get_dc(struct connection *c) {
return c->dc;
}
static struct tgl_session *get_session(struct connection *c) {
return c->session;
}
static void tgln_free(struct connection *c)
{
Netlib_CloseHandle(c->hNetlibConn);
mir_free(c->ip);
delete c;
}
void mtgln_flush_out(struct connection *c) {
}*/
#define vlogprintf(verbosity_level,...) \
do { \
if (TLS->verbosity >= verbosity_level) {\
TLS->callback.logprintf(__VA_ARGS__); \
} \
} while (0)
#define assert(x) x
static void fail_connection(struct connection *c);
#define PING_TIMEOUT 10
static void start_ping_timer(struct connection *c);
void __stdcall ping_alarm(PVOID arg, BOOLEAN TimerOrWaitFired)
{
struct connection *c = (connection*)arg;
if (tglt_get_double_time() - c->last_receive_time > 6 * PING_TIMEOUT)
{
c->TLS->callback.logprintf("fail connection: reason: ping timeout");
}
else if (tglt_get_double_time() - c->last_receive_time > 3 * PING_TIMEOUT)
{
tgl_do_send_ping(c->TLS, c);
start_ping_timer(c);
}
else
{
start_ping_timer(c);
}
}
static void start_ping_timer(connection *c)
{
CreateTimerQueueTimer(&c->ping_timer, hQueue, ping_alarm, c, PING_TIMEOUT * 1000, 0, 0);
}
static void stop_ping_timer(struct connection *c)
{
DeleteTimerQueueTimer(hQueue, c->ping_timer, 0);
c->ping_timer = 0;
}
static void restart_connection(struct connection *c);
void __stdcall fail_alarm(PVOID arg, BOOLEAN TimerOrWaitFired)
{
struct connection *c = (connection*)arg;
c->in_fail_timer = 0;
restart_connection(c);
}
static void start_fail_timer(struct connection *c) {
if (c->in_fail_timer) { return; }
CreateTimerQueueTimer(&c->in_fail_timer, hQueue, fail_alarm, c, PING_TIMEOUT * 1000, 0, 0);
}
connection_buffer *new_connection_buffer(int size) {
connection_buffer *b = (connection_buffer*)talloc0(sizeof(*b));
b->start = (PBYTE)talloc(size);
b->end = b->start + size;
b->rptr = b->wptr = b->start;
return b;
}
static void delete_connection_buffer(struct connection_buffer *b) {
tfree(b->start, b->end - b->start);
tfree(b, sizeof (*b));
}
int mtgln_write_out(struct connection *c, const void *_data, int len)
{
struct tgl_state *TLS = c->TLS;
vlogprintf(E_DEBUG, "write_out: %d bytes\n", len);
const unsigned char *data = (PBYTE)_data;
if (!len) { return 0; }
assert(len > 0);
int x = 0;
if (!c->out_bytes) {
NotifyEventHooks(c->write_ev, (WPARAM)c);
}
if (!c->out_head) {
struct connection_buffer *b = new_connection_buffer(1 << 20);
c->out_head = c->out_tail = b;
}
while (len) {
if (c->out_tail->end - c->out_tail->wptr >= len) {
memcpy(c->out_tail->wptr, data, len);
c->out_tail->wptr += len;
c->out_bytes += len;
return x + len;
}
else {
int y = c->out_tail->end - c->out_tail->wptr;
assert(y < len);
memcpy(c->out_tail->wptr, data, y);
x += y;
len -= y;
data += y;
struct connection_buffer *b = new_connection_buffer(1 << 20);
c->out_tail->next = b;
b->next = 0;
c->out_tail = b;
c->out_bytes += y;
}
}
return x;
}
int tgln_read_in(struct connection *c, void *_data, int len) {
unsigned char *data = (PBYTE)_data;
if (!len) { return 0; }
assert(len > 0);
if (len > c->in_bytes) {
len = c->in_bytes;
}
int x = 0;
while (len) {
int y = c->in_head->wptr - c->in_head->rptr;
if (y > len) {
memcpy(data, c->in_head->rptr, len);
c->in_head->rptr += len;
c->in_bytes -= len;
return x + len;
}
else {
memcpy(data, c->in_head->rptr, y);
c->in_bytes -= y;
x += y;
data += y;
len -= y;
void *old = c->in_head;
c->in_head = c->in_head->next;
if (!c->in_head) {
c->in_tail = 0;
}
delete_connection_buffer((connection_buffer*)old);
}
}
return x;
}
int tgln_read_in_lookup(struct connection *c, void *_data, int len) {
unsigned char *data = (PBYTE)_data;
if (!len || !c->in_bytes) { return 0; }
assert(len > 0);
if (len > c->in_bytes) {
len = c->in_bytes;
}
int x = 0;
struct connection_buffer *b = c->in_head;
while (len) {
int y = b->wptr - b->rptr;
if (y >= len) {
memcpy(data, b->rptr, len);
return x + len;
}
else {
memcpy(data, b->rptr, y);
x += y;
data += y;
len -= y;
b = b->next;
}
}
return x;
}
static void try_write(struct connection *c);
void tgln_flush_out(struct connection *c)
{
// try_write(c);
}
#define MAX_CONNECTIONS 100
static struct connection *Connections[MAX_CONNECTIONS];
static int max_connection_fd;
static void rotate_port(struct connection *c) {
switch (c->port) {
case 443:
c->port = 80;
break;
case 80:
c->port = 25;
break;
case 25:
c->port = 443;
break;
}
}
static void try_read(struct connection *c);
static void try_write(struct connection *c);
void __stdcall conn_try_read(void *param, BYTE){
struct connection *c = (connection*)param;
struct tgl_state *TLS = c->TLS;
vlogprintf(E_DEBUG + 1, "Try read. Fd = %d\n", c->hNetlibConn);
try_read(c);
}
static int conn_try_write(WPARAM w, LPARAM)
{
struct connection *c = (connection*)w;
struct tgl_state *TLS = c->TLS;
if (c->state == conn_connecting) {
c->state = conn_ready;
c->methods->ready(TLS, c);
}
try_write(c);
if (c->out_bytes) {
NotifyEventHooks(c->write_ev, (WPARAM)c);
}
return 0;
}
struct connection *tgln_create_connection(struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods)
{
struct connection *c = new connection;
c->TLS = TLS;
c->ip = tstrdup(host);
c->port = port;
NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
nloc.szHost = c->ip;
nloc.wPort = port;
if (!(c->hNetlibConn = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)((MirTLS*)TLS)->m_proto->m_hNetlibUser, (LPARAM)&nloc)))
{
delete c;
return 0;
}
c->state = conn_connecting;
c->last_receive_time = tglt_get_double_time();
c->flags = 0;
char szTmp[2048] = "TGL_NET_TMPHOOK/";
char *dst = &szTmp[strlen(szTmp)];
MUUID uuid;
CoCreateGuid((GUID*)&uuid);
mir_snprintf(dst, 2048 - strlen(szTmp) - 1, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
uuid.a, uuid.b, uuid.c, uuid.d[0], uuid.d[1], uuid.d[2], uuid.d[3], uuid.d[4], uuid.d[5], uuid.d[6], uuid.d[7]);
c->write_ev = CreateHookableEvent(szTmp);
HookEvent(szTmp, conn_try_write);
//CoCreateGuid((GUID*)&uuid);
//mir_snprintf(dst, 2048 - strlen(szTmp) - 1, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
// uuid.a, uuid.b, uuid.c, uuid.d[0], uuid.d[1], uuid.d[2], uuid.d[3], uuid.d[4], uuid.d[5], uuid.d[6], uuid.d[7]);
//
//c->read_ev = CreateHookableEvent(szTmp);
//HookEvent(szTmp, conn_try_read);
start_ping_timer(c);
c->dc = dc;
c->session = session;
c->methods = methods;
char byte = 0xef;
assert(mtgln_write_out(c, &byte, 1) == 1);
tgln_flush_out(c);
CreateTimerQueueTimer(&c->write_ev, hQueue, conn_try_read, c, 1, 5000, 0);
return c;
}
static void restart_connection(struct connection *c)
{
struct tgl_state *TLS = c->TLS;
if (c->last_connect_time == time(0)) {
start_fail_timer(c);
return;
}
c->state = conn_connecting;
c->last_receive_time = tglt_get_double_time();
Netlib_CloseHandle(c->hNetlibConn);
NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
nloc.szHost = c->ip;
nloc.wPort = c->port;
c->hNetlibConn = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)((MirTLS*)TLS)->m_proto->m_hNetlibUser, (LPARAM)&nloc);
char byte = 0xef;
assert(mtgln_write_out(c, &byte, 1) == 1);
tgln_flush_out(c);
}
static void fail_connection(struct connection *c)
{
struct tgl_state *TLS = c->TLS;
if (c->state == conn_ready || c->state == conn_connecting) {
stop_ping_timer(c);
}
// DestroyHookableEvent(c->read_ev);
rotate_port(c);
struct connection_buffer *b = c->out_head;
while (b) {
struct connection_buffer *d = b;
b = b->next;
delete_connection_buffer(d);
}
b = c->in_head;
while (b) {
struct connection_buffer *d = b;
b = b->next;
delete_connection_buffer(d);
}
c->out_head = c->out_tail = c->in_head = c->in_tail = 0;
c->state = conn_failed;
c->out_bytes = c->in_bytes = 0;
Netlib_CloseHandle(c->hNetlibConn);
vlogprintf(E_NOTICE, "Lost connection to server... %s:%d\n", c->ip, c->port);
restart_connection(c);
}
//extern FILE *log_net_f;
static void try_write(struct connection *c)
{
struct tgl_state *TLS = c->TLS;
vlogprintf(E_DEBUG, "try write: fd = %d\n", c->hNetlibConn);
int x = 0;
while (c->out_head) {
int r = Netlib_Send(c->hNetlibConn, (char*)c->out_head->rptr, c->out_head->wptr - c->out_head->rptr, MSG_RAW);
if (r != SOCKET_ERROR) {
x += r;
c->out_head->rptr += r;
if (c->out_head->rptr != c->out_head->wptr) {
break;
}
struct connection_buffer *b = c->out_head;
c->out_head = b->next;
if (!c->out_head) {
c->out_tail = 0;
}
delete_connection_buffer(b);
}
else {
if (/*WSAGetLastError() != EAGAIN && */WSAGetLastError() != WSAEWOULDBLOCK) {
vlogprintf(E_NOTICE, "fail_connection: write_error %s\n", GetLastErrorStr(WSAGetLastError()));
fail_connection(c);
return;
}
else {
break;
}
}
}
vlogprintf(E_DEBUG, "Sent %d bytes to %d\n", x, c->hNetlibConn);
c->out_bytes -= x;
}
static void try_rpc_read(struct connection *c) {
assert(c->in_head);
struct tgl_state *TLS = c->TLS;
while (1) {
if (c->in_bytes < 1) { return; }
unsigned len = 0;
unsigned t = 0;
assert(tgln_read_in_lookup(c, &len, 1) == 1);
if (len >= 1 && len <= 0x7e) {
if (c->in_bytes < (int)(1 + 4 * len)) { return; }
}
else {
if (c->in_bytes < 4) { return; }
assert(tgln_read_in_lookup(c, &len, 4) == 4);
len = (len >> 8);
if (c->in_bytes < (int)(4 + 4 * len)) { return; }
len = 0x7f;
}
if (len >= 1 && len <= 0x7e) {
assert(tgln_read_in(c, &t, 1) == 1);
assert(t == len);
assert(len >= 1);
}
else {
assert(len == 0x7f);
assert(tgln_read_in(c, &len, 4) == 4);
len = (len >> 8);
assert(len >= 1);
}
len *= 4;
int op;
assert(tgln_read_in_lookup(c, &op, 4) == 4);
if (c->methods->execute(TLS, c, op, len) < 0) {
return;
}
}
}
static void try_read(struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf(E_DEBUG, "try read: fd = %d\n", c->hNetlibConn);
if (!c->in_tail) {
c->in_head = c->in_tail = new_connection_buffer(1 << 20);
}
#ifdef EVENT_V1
struct timeval tv = { 5, 0 };
event_add(c->read_ev, &tv);
#endif
int x = 0;
while (1) {
int r = Netlib_Recv(c->hNetlibConn, (char*)c->in_tail->wptr, c->in_tail->end - c->in_tail->wptr, MSG_RAW);
if (r != SOCKET_ERROR) {
c->last_receive_time = tglt_get_double_time();
stop_ping_timer(c);
start_ping_timer(c);
}
if (r >= 0) {
c->in_tail->wptr += r;
x += r;
if (c->in_tail->wptr != c->in_tail->end) {
break;
}
struct connection_buffer *b = new_connection_buffer(1 << 20);
c->in_tail->next = b;
c->in_tail = b;
}
else {
if (/*WSAGetLastError() != EAGAIN &&*/ WSAGetLastError() != WSAEWOULDBLOCK) {
vlogprintf(E_NOTICE, "fail_connection: read_error %s\n", GetLastErrorStr(WSAGetLastError()));
fail_connection(c);
return;
}
else {
break;
}
}
}
vlogprintf(E_DEBUG, "Received %d bytes from %d\n", x, c->hNetlibConn);
c->in_bytes += x;
if (x) {
try_rpc_read(c);
}
}
static void incr_out_packet_num(struct connection *c) {
c->out_packet_num++;
}
static struct tgl_dc *get_dc(struct connection *c) {
return c->dc;
}
static struct tgl_session *get_session(struct connection *c) {
return c->session;
}
static void tgln_free(struct connection *c) {
if (c->ip) { tfree_str(c->ip); }
if (c->ping_ev) { }
if (c->fail_ev) { }
// if (c->read_ev) { DestroyHookableEvent(c->read_ev); }
if (c->write_ev) { DestroyHookableEvent(c->write_ev); }
struct connection_buffer *b = c->out_head;
while (b) {
struct connection_buffer *d = b;
b = b->next;
delete_connection_buffer(d);
}
b = c->in_head;
while (b) {
struct connection_buffer *d = b;
b = b->next;
delete_connection_buffer(d);
}
if (c->hNetlibConn) Netlib_CloseHandle(c->hNetlibConn);
delete c;
}
struct tgl_net_methods mtgl_conn_methods = {
mtgln_write_out,
tgln_read_in,
tgln_read_in_lookup,
tgln_flush_out,
incr_out_packet_num,
tgln_free,
get_dc,
get_session,
tgln_create_connection
};
void CTelegramProto::InitNetwork()
{
NETLIBUSER nlu = { 0 };
nlu.cbSize = sizeof(nlu);
nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE;
nlu.ptszDescriptiveName = L"TGL";
nlu.szSettingsModule = m_szModuleName;
m_hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
tgl_set_net_methods(TLS, &mtgl_conn_methods);
}
|