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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
|
#include "stdafx.h"
#include "MraFilesQueue.h"
#include "proto.h"
#define MRA_FT_HELLO "MRA_FT_HELLO"
#define MRA_FT_GET_FILE "MRA_FT_GET_FILE"
struct MRA_FILES_QUEUE : public LIST_MT
{
DWORD dwSendTimeOutInterval;
};
struct MRA_FILES_QUEUE_FILE
{
LPWSTR lpwszName;
size_t dwNameLen;
DWORDLONG dwSize;
};
struct MRA_FILES_QUEUE_ITEM : public LIST_MT_ITEM
{
// internal
bool bIsWorking;
DWORD dwSendTime;
// external
CMraProto *ppro;
DWORD dwIDRequest;
DWORD dwFlags;
MCONTACT hContact;
DWORDLONG dwFilesCount;
DWORDLONG dwFilesTotalSize;
MRA_FILES_QUEUE_FILE *pmfqfFiles;
LPWSTR pwszFilesList;
LPWSTR pwszDescription;
MRA_ADDR_LIST malAddrList;
LPWSTR lpwszPath;
size_t dwPathSize;
bool bSending;
HANDLE hConnection;
HANDLE hListen;
HANDLE hThread;
HANDLE hWaitHandle;
HANDLE hMraMrimProxyData;
};
struct MRA_FILES_THREADPROC_PARAMS
{
HANDLE hFilesQueueHandle;
MRA_FILES_QUEUE_ITEM *dat;
};
DWORD MraFilesQueueItemFindByID(HANDLE hQueue, DWORD dwIDRequest, MRA_FILES_QUEUE_ITEM **ppmrafqFilesQueueItem);
void MraFilesQueueItemFree(MRA_FILES_QUEUE_ITEM *dat);
/////////////////////////////////////////////////////////////////////////////////////////
// File transfer options
static WORD wMraFilesControlsList[] = {
IDC_FILE_SEND_NOOUTCONNECTIONONRECEIVE,
IDC_FILE_SEND_NOOUTCONNECTIONONSEND,
IDC_FILE_SEND_IGNORYADDITIONALPORTS,
IDC_FILE_SEND_HIDE_MY_ADDRESSES,
IDC_FILE_SEND_ADD_EXTRA_ADDRESS,
IDC_FILE_SEND_EXTRA_ADDRESS
};
void MraFilesQueueDlgEnableDirectConsControls(HWND hWndDlg, BOOL bEnabled)
{
EnableControlsArray(hWndDlg, (WORD*)&wMraFilesControlsList, _countof(wMraFilesControlsList), bEnabled);
EnableWindow(GetDlgItem(hWndDlg, IDC_FILE_SEND_EXTRA_ADDRESS), (bEnabled && IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ADD_EXTRA_ADDRESS)));
}
INT_PTR CALLBACK MraFilesQueueDlgProcOpts(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
CMraProto *ppro = (CMraProto*)GetWindowLongPtr(hWndDlg, GWLP_USERDATA);
switch (msg) {
case WM_INITDIALOG:
TranslateDialogDefault(hWndDlg);
SetWindowLongPtr(hWndDlg, GWLP_USERDATA, lParam);
ppro = (CMraProto*)lParam;
{
CheckDlgButton(hWndDlg, IDC_FILE_SEND_ENABLE_DIRECT_CONN, ppro->getByte("FileSendEnableDirectConn", MRA_DEF_FS_ENABLE_DIRECT_CONN) ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hWndDlg, IDC_FILE_SEND_NOOUTCONNECTIONONRECEIVE, ppro->getByte("FileSendNoOutConnOnRcv", MRA_DEF_FS_NO_OUT_CONN_ON_RCV) ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hWndDlg, IDC_FILE_SEND_NOOUTCONNECTIONONSEND, ppro->getByte("FileSendNoOutConnOnSend", MRA_DEF_FS_NO_OUT_CONN_ON_SEND) ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hWndDlg, IDC_FILE_SEND_IGNORYADDITIONALPORTS, ppro->getByte("FileSendIgnoryAdditionalPorts", MRA_DEF_FS_IGNORY_ADDITIONAL_PORTS) ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hWndDlg, IDC_FILE_SEND_HIDE_MY_ADDRESSES, ppro->getByte("FileSendHideMyAddresses", MRA_DEF_FS_HIDE_MY_ADDRESSES) ? BST_CHECKED : BST_UNCHECKED);
CheckDlgButton(hWndDlg, IDC_FILE_SEND_ADD_EXTRA_ADDRESS, ppro->getByte("FileSendAddExtraAddresses", MRA_DEF_FS_ADD_EXTRA_ADDRESSES) ? BST_CHECKED : BST_UNCHECKED);
CMStringW szBuff;
if (ppro->mraGetStringW(NULL, "FileSendExtraAddresses", szBuff))
SetDlgItemText(hWndDlg, IDC_FILE_SEND_EXTRA_ADDRESS, szBuff.c_str());
CheckDlgButton(hWndDlg, IDC_FILE_SEND_ENABLE_MRIMPROXY_CONS, ppro->getByte("FileSendEnableMRIMProxyCons", MRA_DEF_FS_ENABLE_MRIM_PROXY_CONS) ? BST_CHECKED : BST_UNCHECKED);
SetDlgItemInt(hWndDlg, IDC_FILE_SEND_BLOCK_SIZE, ppro->getDword("FileSendBlockSize", MRA_DEFAULT_FILE_SEND_BLOCK_SIZE), FALSE);
MraFilesQueueDlgEnableDirectConsControls(hWndDlg, IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ENABLE_DIRECT_CONN));
}
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_FILE_SEND_ENABLE_DIRECT_CONN)
MraFilesQueueDlgEnableDirectConsControls(hWndDlg, IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ENABLE_DIRECT_CONN));
if (LOWORD(wParam) == IDC_FILE_SEND_ADD_EXTRA_ADDRESS)
EnableWindow(GetDlgItem(hWndDlg, IDC_FILE_SEND_EXTRA_ADDRESS), IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ADD_EXTRA_ADDRESS));
if ((LOWORD(wParam) == IDC_FILE_SEND_EXTRA_ADDRESS || LOWORD(wParam) == IDC_FILE_SEND_BLOCK_SIZE) && (HIWORD(wParam) != EN_CHANGE || (HWND)lParam != GetFocus())) return FALSE;
SendMessage(GetParent(hWndDlg), PSM_CHANGED, 0, 0);
break;
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code) {
case PSN_APPLY:
ppro->setByte("FileSendEnableDirectConn", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ENABLE_DIRECT_CONN));
ppro->setByte("FileSendNoOutConnOnRcv", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_NOOUTCONNECTIONONRECEIVE));
ppro->setByte("FileSendNoOutConnOnSend", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_NOOUTCONNECTIONONSEND));
ppro->setByte("FileSendIgnoryAdditionalPorts", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_IGNORYADDITIONALPORTS));
ppro->setByte("FileSendHideMyAddresses", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_HIDE_MY_ADDRESSES));
ppro->setByte("FileSendAddExtraAddresses", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ADD_EXTRA_ADDRESS));
WCHAR szBuff[MAX_PATH];
GetDlgItemText(hWndDlg, IDC_FILE_SEND_EXTRA_ADDRESS, szBuff, _countof(szBuff));
ppro->mraSetStringW(NULL, "FileSendExtraAddresses", szBuff);
ppro->setDword("FileSendBlockSize", (DWORD)GetDlgItemInt(hWndDlg, IDC_FILE_SEND_BLOCK_SIZE, NULL, FALSE));
ppro->setByte("FileSendEnableMRIMProxyCons", IsDlgButtonChecked(hWndDlg, IDC_FILE_SEND_ENABLE_MRIMPROXY_CONS));
return TRUE;
}
break;
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////
// MRA files' queue
DWORD MraFilesQueueInitialize(DWORD dwSendTimeOutInterval, HANDLE *phFilesQueueHandle)
{
if (!phFilesQueueHandle)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = new MRA_FILES_QUEUE();
if (!pmrafqFilesQueue)
return GetLastError();
pmrafqFilesQueue->dwSendTimeOutInterval = dwSendTimeOutInterval;
*phFilesQueueHandle = (HANDLE)pmrafqFilesQueue;
return NO_ERROR;
}
void MraFilesQueueDestroy(HANDLE hQueue)
{
if (!hQueue)
return;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
{
mir_cslock l(pmrafqFilesQueue->cs);
while (ListMTItemGetFirst(pmrafqFilesQueue, NULL, (LPVOID*)&dat) == NO_ERROR)
MraFilesQueueItemFree(dat);
}
delete pmrafqFilesQueue;
}
DWORD MraFilesQueueItemFindByID(HANDLE hQueue, DWORD dwIDRequest, MRA_FILES_QUEUE_ITEM **ppmrafqFilesQueueItem)
{
if (!hQueue)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
LIST_MT_ITERATOR lmtiIterator;
mir_cslock l(pmrafqFilesQueue->cs);
ListMTIteratorMoveFirst(pmrafqFilesQueue, &lmtiIterator);
do {
if (ListMTIteratorGet(&lmtiIterator, NULL, (LPVOID*)&dat) == NO_ERROR)
if (dat->dwIDRequest == dwIDRequest) {
if (ppmrafqFilesQueueItem)
*ppmrafqFilesQueueItem = dat;
return 0;
}
}
while (ListMTIteratorMoveNext(&lmtiIterator));
return ERROR_NOT_FOUND;
}
HANDLE MraFilesQueueItemProxyByID(HANDLE hQueue, DWORD dwIDRequest)
{
MRA_FILES_QUEUE_ITEM *dat;
if (!MraFilesQueueItemFindByID(hQueue, dwIDRequest, &dat))
return dat->hMraMrimProxyData;
return NULL;
}
void MraFilesQueueItemFree(MRA_FILES_QUEUE_ITEM *dat)
{
LIST_MT *plmtListMT = (LIST_MT*)dat->lpListMT;
for (size_t i = 0; i < dat->dwFilesCount; i++)
mir_free(dat->pmfqfFiles[i].lpwszName);
mir_free(dat->pmfqfFiles);
mir_free(dat->pwszFilesList);
mir_free(dat->pwszDescription);
MraAddrListFree(&dat->malAddrList);
MraMrimProxyFree(dat->hMraMrimProxyData);
mir_free(dat->lpwszPath);
{
mir_cslock l(plmtListMT->cs);
ListMTItemDelete(plmtListMT, dat);
}
mir_free(dat);
}
size_t CMraProto::MraFilesQueueGetLocalAddressesList(LPSTR lpszBuff, size_t dwBuffSize, DWORD dwPort)
{
LPSTR lpszCurPos = lpszBuff;
if (getByte("FileSendHideMyAddresses", MRA_DEF_FS_HIDE_MY_ADDRESSES)) {// íå âûäà¸ì âðàãó íàø IP àäðåñ!!! :)
if (getByte("FileSendAddExtraAddresses", MRA_DEF_FS_ADD_EXTRA_ADDRESSES) == FALSE) {// òîëüêî åñëè íå äîáàâëÿåì àäðåñ ðîóòåðà
lpszCurPos += mir_snprintf(lpszCurPos, (dwBuffSize - ((size_t)lpszCurPos - (size_t)lpszBuff)), MRA_FILES_NULL_ADDRR);
}
}
else {// ñîçäà¸ì ñïèñîê íàøèõ IP àäðåñîâ
BYTE btAddress[32];
DWORD dwSelfExternalIP;
size_t dwAdapter = 0;
hostent *sh;
dwSelfExternalIP = ntohl(getDword("IP", 0));
if (dwSelfExternalIP) {
memcpy(&btAddress, &dwSelfExternalIP, sizeof(DWORD));
lpszCurPos += mir_snprintf(lpszCurPos, (dwBuffSize - ((size_t)lpszCurPos - (size_t)lpszBuff)), "%lu.%lu.%lu.%lu:%lu;", btAddress[0], btAddress[1], btAddress[2], btAddress[3], dwPort);
}
CHAR szHostName[MAX_PATH] = { 0 };
if (gethostname(szHostName, _countof(szHostName)) == 0)
if ((sh = gethostbyname((LPSTR)&szHostName))) {
while (sh->h_addr_list[dwAdapter]) {
lpszCurPos += mir_snprintf(lpszCurPos, (dwBuffSize - ((size_t)lpszCurPos - (size_t)lpszBuff)), "%s:%lu;", inet_ntoa(*((struct in_addr*)sh->h_addr_list[dwAdapter])), dwPort);
dwAdapter++;
}
}
}
CMStringA szHostName;
if (getByte("FileSendAddExtraAddresses", MRA_DEF_FS_ADD_EXTRA_ADDRESSES))// äîáàâëÿåì ïðîèçâîëüíûé àäðåñ
if (mraGetStringA(NULL, "FileSendExtraAddresses", szHostName))
lpszCurPos += mir_snprintf(lpszCurPos, (dwBuffSize - ((size_t)lpszCurPos - (size_t)lpszBuff)), "%s:%lu;", szHostName.c_str(), dwPort);
return lpszCurPos - lpszBuff;
}
DWORD CMraProto::MraFilesQueueAccept(HANDLE hQueue, DWORD dwIDRequest, LPCWSTR lpwszPath, size_t dwPathSize)
{
if (!hQueue || !lpwszPath || !dwPathSize)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
mir_cslock l(pmrafqFilesQueue->cs);
DWORD dwRetErrorCode = MraFilesQueueItemFindByID(hQueue, dwIDRequest, &dat);
if (dwRetErrorCode == NO_ERROR) {
MRA_FILES_THREADPROC_PARAMS *pmftpp = (MRA_FILES_THREADPROC_PARAMS*)mir_calloc(sizeof(MRA_FILES_THREADPROC_PARAMS));
dat->lpwszPath = (LPWSTR)mir_calloc((dwPathSize*sizeof(WCHAR)));
dat->dwPathSize = dwPathSize;
memcpy(dat->lpwszPath, lpwszPath, (dwPathSize*sizeof(WCHAR)));
if ((*(WCHAR*)(dat->lpwszPath + (dat->dwPathSize - 1))) != '\\') {// add slash at the end if needed
(*(WCHAR*)(dat->lpwszPath + dat->dwPathSize)) = '\\';
dat->dwPathSize++;
(*(WCHAR*)(dat->lpwszPath + dat->dwPathSize)) = 0;
}
pmftpp->hFilesQueueHandle = hFilesQueueHandle;
pmftpp->dat = dat;
dat->hThread = ForkThreadEx(&CMraProto::MraFilesQueueRecvThreadProc, pmftpp, 0);
}
return dwRetErrorCode;
}
DWORD CMraProto::MraFilesQueueCancel(HANDLE hQueue, DWORD dwIDRequest, BOOL bSendDecline)
{
if (!hQueue)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
mir_cslock l(pmrafqFilesQueue->cs);
DWORD dwRetErrorCode = MraFilesQueueItemFindByID(hQueue, dwIDRequest, &dat);
if (dwRetErrorCode == NO_ERROR) { //***deb closesocket, send message to thread
InterlockedExchange((volatile LONG*)&dat->bIsWorking, FALSE);
if (bSendDecline) {
CMStringA szEmail;
if (mraGetStringA(dat->hContact, "e-mail", szEmail))
MraFileTransferAck(FILE_TRANSFER_STATUS_DECLINE, szEmail, dwIDRequest, CMStringA());
}
MraMrimProxyCloseConnection(dat->hMraMrimProxyData);
NETLIB_CLOSEHANDLE(dat->hListen);
NETLIB_CLOSEHANDLE(dat->hConnection);
SetEvent(dat->hWaitHandle);
if (dat->hThread == NULL)
MraFilesQueueItemFree(dat);
}
return dwRetErrorCode;
}
DWORD CMraProto::MraFilesQueueStartMrimProxy(HANDLE hQueue, DWORD dwIDRequest)
{
if (!hQueue || !getByte("FileSendEnableMRIMProxyCons", MRA_DEF_FS_ENABLE_MRIM_PROXY_CONS))
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
mir_cslock l(pmrafqFilesQueue->cs);
if (!MraFilesQueueItemFindByID(hQueue, dwIDRequest, &dat))
if (dat->bSending == FALSE)
SetEvent(dat->hWaitHandle);// cancel wait incomming connection
return 0;
}
DWORD MraFilesQueueFree(HANDLE hQueue, DWORD dwIDRequest)
{
if (!hQueue)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
LIST_MT_ITERATOR lmtiIterator;
mir_cslock l(pmrafqFilesQueue->cs);
ListMTIteratorMoveFirst(pmrafqFilesQueue, &lmtiIterator);
do {
if (ListMTIteratorGet(&lmtiIterator, NULL, (LPVOID*)&dat) == NO_ERROR)
if (dat->dwIDRequest == dwIDRequest) {
MraFilesQueueItemFree(dat);
return 0;
}
}
while (ListMTIteratorMoveNext(&lmtiIterator));
return ERROR_NOT_FOUND;
}
DWORD CMraProto::MraFilesQueueSendMirror(HANDLE hQueue, DWORD dwIDRequest, const CMStringA &szAddresses)
{
if (!hQueue)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat;
mir_cslock l(pmrafqFilesQueue->cs);
DWORD dwRetErrorCode = MraFilesQueueItemFindByID(hQueue, dwIDRequest, &dat);
if (dwRetErrorCode == NO_ERROR) {
MraAddrListGetFromBuff(szAddresses, &dat->malAddrList);
MraAddrListStoreToContact(dat->hContact, &dat->malAddrList);
dat->hConnection = NULL;
SetEvent(dat->hWaitHandle);
}
return dwRetErrorCode;
}
bool CMraProto::MraFilesQueueHandCheck(HANDLE hConnection, MRA_FILES_QUEUE_ITEM *dat)
{
if (hConnection && dat) {
BYTE btBuff[((MAX_EMAIL_LEN * 2) + (sizeof(MRA_FT_HELLO)* 2) + 8)] = { 0 };
size_t dwBuffSize;
CMStringA szEmail, szEmailMy;
if (!mraGetStringA(NULL, "e-mail", szEmailMy) || !mraGetStringA(dat->hContact, "e-mail", szEmail))
return false;
szEmailMy.MakeLower();
szEmail.MakeLower();
if (dat->bSending == FALSE) {
// receiving
dwBuffSize = mir_snprintf((LPSTR)btBuff, _countof(btBuff), "%s %s", MRA_FT_HELLO, szEmailMy.c_str()) + 1;
if (dwBuffSize == (size_t)Netlib_Send(hConnection, (LPSTR)btBuff, (int)dwBuffSize, 0)) {
// my email sended
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, (HANDLE)dat->dwIDRequest, 0);
dwBuffSize = Netlib_Recv(hConnection, (LPSTR)btBuff, sizeof(btBuff), 0);
if ((szEmail.GetLength() + sizeof(MRA_FT_HELLO)+1) == dwBuffSize) {
// email received
mir_snprintf(((LPSTR)btBuff + dwBuffSize), (_countof(btBuff) - dwBuffSize), "%s %s", MRA_FT_HELLO, szEmail);
if (!_memicmp(btBuff, btBuff + dwBuffSize, dwBuffSize))
return true;
}
}
}
else {// sending
dwBuffSize = Netlib_Recv(hConnection, (LPSTR)btBuff, sizeof(btBuff), 0);
if ((szEmail.GetLength() + sizeof(MRA_FT_HELLO)+1) == dwBuffSize) {
// email received
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, (HANDLE)dat->dwIDRequest, 0);
mir_snprintf(((LPSTR)btBuff + dwBuffSize), (_countof(btBuff) - dwBuffSize), "%s %s", MRA_FT_HELLO, szEmail);
if (!_memicmp(btBuff, btBuff + dwBuffSize, dwBuffSize)) {
// email verified
dwBuffSize = (mir_snprintf((LPSTR)btBuff, _countof(btBuff), "%s %s", MRA_FT_HELLO, szEmailMy.c_str()) + 1);
if (dwBuffSize == (size_t)Netlib_Send(hConnection, (LPSTR)btBuff, (int)dwBuffSize, 0))
return true;
}
}
}
}
return false;
}
HANDLE CMraProto::MraFilesQueueConnectOut(MRA_FILES_QUEUE_ITEM *dat)
{
if (!dat)
return NULL;
if (getByte("FileSendEnableDirectConn", MRA_DEF_FS_ENABLE_DIRECT_CONN) && InterlockedExchangeAdd((volatile LONG*)&dat->bIsWorking, 0) && ((dat->bSending == FALSE && getByte("FileSendNoOutConnOnRcv", MRA_DEF_FS_NO_OUT_CONN_ON_RCV) == FALSE) || (dat->bSending == TRUE && getByte("FileSendNoOutConnOnSend", MRA_DEF_FS_NO_OUT_CONN_ON_SEND) == FALSE))) {
BOOL bFiltering = FALSE, bIsHTTPSProxyUsed = IsHTTPSProxyUsed(m_hNetlibUser);
DWORD dwLocalPort = 0, dwConnectReTryCount, dwCurConnectReTryCount;
size_t dwAddrCount = 0;
NETLIBOPENCONNECTION nloc = { 0 };
if (getByte("FileSendIgnoryAdditionalPorts", MRA_DEF_FS_IGNORY_ADDITIONAL_PORTS) || bIsHTTPSProxyUsed) {// ôèëüòðóåì ïîðòû äëÿ îäíîãî IP, âìåñòî 3 áóäåì êîííåêòèòñÿ òîëüêî ê îäíîìó
if (bIsHTTPSProxyUsed)
dwLocalPort = MRA_SERVER_PORT_HTTPS;
else if ((dwLocalPort = getWord("ServerPort", MRA_DEFAULT_SERVER_PORT)) == MRA_SERVER_PORT_STANDART_NLB)
dwLocalPort = MRA_SERVER_PORT_STANDART;
for (size_t i = 0; i < dat->malAddrList.dwAddrCount; i++) {
if (dwLocalPort == dat->malAddrList.pMailAddress[i].dwPort) {
bFiltering = TRUE;
dwAddrCount++;
}
}
}
if (bFiltering == FALSE)
dwAddrCount = dat->malAddrList.dwAddrCount;
if (dwAddrCount) {
dat->hConnection = NULL;
dwConnectReTryCount = getDword("ConnectReTryCountFileSend", MRA_DEFAULT_CONN_RETRY_COUNT_FILES);
nloc.cbSize = sizeof(nloc);
nloc.flags = NLOCF_V2;
nloc.timeout = getDword("TimeOutConnectFileSend", (int)((MRA_TIMEOUT_DIRECT_CONN - 1) / (dwAddrCount*dwConnectReTryCount)));// -1 ñåê ÷òîáû áûë çàïàñ
if (nloc.timeout < MRA_TIMEOUT_CONN_MIN) nloc.timeout = MRA_TIMEOUT_CONN_MIN;
if (nloc.timeout > MRA_TIMEOUT_CONN_MAX) nloc.timeout = MRA_TIMEOUT_CONN_MAX;
// Set up the sockaddr structure
for (size_t i = 0; i < dat->malAddrList.dwAddrCount; i++) {
if (dwLocalPort == dat->malAddrList.pMailAddress[i].dwPort || bFiltering == FALSE) {
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, (HANDLE)dat->dwIDRequest, 0);
nloc.szHost = inet_ntoa((*((in_addr*)&dat->malAddrList.pMailAddress[i].dwAddr)));
nloc.wPort = (WORD)dat->malAddrList.pMailAddress[i].dwPort;
dwCurConnectReTryCount = dwConnectReTryCount;
do {
dat->hConnection = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)m_hNetlibUser, (LPARAM)&nloc);
}
while (--dwCurConnectReTryCount && dat->hConnection == NULL);
if (dat->hConnection) {
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
if (MraFilesQueueHandCheck(dat->hConnection, dat)) {
// ñâÿçü óñòàíîâëåííàÿ ñ òåì êåì íóæíî
setDword(dat->hContact, "OldIP", getDword(dat->hContact, "IP", 0));
setDword(dat->hContact, "IP", ntohl(dat->malAddrList.pMailAddress[i].dwAddr));
break;
}
else // êàæåòñÿ íå òóäà ïîäêëþ÷èëèñü :)
NETLIB_CLOSEHANDLE(dat->hConnection);
}
}
}
}
}
return dat->hConnection;
}
LPWSTR GetFileNameFromFullPathW(LPWSTR lpwszFullPath, size_t dwFullPathSize)
{
LPWSTR lpwszFileName = lpwszFullPath, lpwszCurPos;
lpwszCurPos = (lpwszFullPath + dwFullPathSize);
for (; lpwszCurPos > lpwszFullPath; lpwszCurPos--) {
if ((*lpwszCurPos) == '\\') {
lpwszFileName = (lpwszCurPos + 1);
break;
}
}
return lpwszFileName;
}
HANDLE CMraProto::MraFilesQueueConnectIn(MRA_FILES_QUEUE_ITEM *dat)
{
if (!dat)
return NULL;
if (!InterlockedExchangeAdd((volatile LONG*)&dat->bIsWorking, 0))
return NULL;
CMStringA szEmail;
if (mraGetStringA(dat->hContact, "e-mail", szEmail)) {
CHAR szAddrList[2048] = { 0 };
size_t dwAddrListSize;
// êîïèðóåì àäðåñà â ñîîòâåòñòâèè ñ ïðàâèëàìè è íà÷èíàåì ñëóøàòü ïîðò
if (getByte("FileSendEnableDirectConn", MRA_DEF_FS_ENABLE_DIRECT_CONN)) {
NETLIBBIND nlbBind = { 0 };
nlbBind.cbSize = sizeof(nlbBind);
nlbBind.pfnNewConnectionV2 = MraFilesQueueConnectionReceived;
nlbBind.wPort = 0;
nlbBind.pExtra = (LPVOID)dat;
dat->hListen = (HANDLE)CallService(MS_NETLIB_BINDPORT, (WPARAM)m_hNetlibUser, (LPARAM)&nlbBind);
if (dat->hListen) {
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_LISTENING, (HANDLE)dat->dwIDRequest, 0);
dwAddrListSize = MraFilesQueueGetLocalAddressesList(szAddrList, sizeof(szAddrList), nlbBind.wPort);
}
// íå ñìîãëè ñëóøàòü ïîðò, õç ïî÷åìó.
else {
ShowFormattedErrorMessage(L"Files exchange: cant create listen soscket, will try connect to remonte host. Error", GetLastError());
//dwAddrListSize = 0;
memcpy(szAddrList, MRA_FILES_NULL_ADDRR, sizeof(MRA_FILES_NULL_ADDRR));
dwAddrListSize = (sizeof(MRA_FILES_NULL_ADDRR)-1);
}
}
// ïîäñòàâëÿåì ëîæíûé àäðåñ, ÷òîáû òî÷íî íå ïîäêëþ÷èëèñü è íå ñëóøàåì ïîðò
else {
memcpy(szAddrList, MRA_FILES_NULL_ADDRR, sizeof(MRA_FILES_NULL_ADDRR));
dwAddrListSize = (sizeof(MRA_FILES_NULL_ADDRR)-1);
}
if (dwAddrListSize) {
dat->hWaitHandle = CreateEvent(NULL, TRUE, FALSE, NULL);
if (dat->bSending == FALSE) // çàïðàøèâàåì çåðêàëüíîå ñîåäèíåíèå, òê ñàìè ïîäêëþ÷èòñÿ íå ñìîãëè
MraFileTransferAck(FILE_TRANSFER_MIRROR, szEmail, dat->dwIDRequest, szAddrList);
else { // çäåñü îòïðàâëÿåì çàïðîñ íà ïåðåäà÷ó(óñòàíîâëåíèå ñîåäèíåíèÿ)
// ñîçäà¸ì òåêñòîâûé ñïèñîê ôàéëîâ äëÿ îòïðàâêè äðóãîé ñòîðîíå
LPWSTR lpwszFiles, lpwszCurPos;
size_t dwFilesSize;
dwFilesSize = ((MAX_PATH * 2)*dat->dwFilesCount);
lpwszFiles = (LPWSTR)mir_calloc((dwFilesSize*sizeof(WCHAR)));
if (lpwszFiles) {
lpwszCurPos = lpwszFiles;
for (size_t i = 0; i < dat->dwFilesCount; i++) {
MRA_FILES_QUEUE_FILE &p = dat->pmfqfFiles[i];
lpwszCurPos += mir_snwprintf(lpwszCurPos, (dwFilesSize - ((size_t)lpwszCurPos - (size_t)lpwszFiles)), L"%s;%I64u;",
GetFileNameFromFullPathW(p.lpwszName, p.dwNameLen), p.dwSize);
}
dwFilesSize = (lpwszCurPos - lpwszFiles);// size in WCHARs
if (dat->hMraMrimProxyData) {
// óñòàíàâëèâàåì äàííûå äëÿ ìàéëîâñêîé ïðîêñè, åñëè îíà ðàçðåøåíà
CMStringA lpszFiles = lpwszFiles;
MraMrimProxySetData(dat->hMraMrimProxyData, szEmail, dat->dwIDRequest, MRIM_PROXY_TYPE_FILES, lpszFiles, "", NULL);
}
MraFileTransfer(szEmail, dat->dwIDRequest, dat->dwFilesTotalSize, lpwszFiles, szAddrList);
mir_free(lpwszFiles);
}
}
WaitForSingleObjectEx(dat->hWaitHandle, INFINITE, FALSE);
CloseHandle(dat->hWaitHandle);
dat->hWaitHandle = NULL;
}
}
return dat->hConnection;
}
// This function is called from the Netlib when someone is connecting to
// one of our incomming DC ports
void MraFilesQueueConnectionReceived(HANDLE hNewConnection, DWORD dwRemoteIP, void *pExtra)
{
if (pExtra) {
MRA_FILES_QUEUE_ITEM *dat = (MRA_FILES_QUEUE_ITEM*)pExtra;
ProtoBroadcastAck(dat->ppro->m_szModuleName, dat->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
if (dat->ppro->MraFilesQueueHandCheck(hNewConnection, dat)) { // ñâÿçü óñòàíîâëåííàÿ ñ òåì êåì íóæíî
dat->hConnection = hNewConnection;
ProtoBroadcastAck(dat->ppro->m_szModuleName, dat->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
dat->ppro->setDword(dat->hContact, "OldIP", dat->ppro->getDword(dat->hContact, "IP", 0));
dat->ppro->setDword(dat->hContact, "IP", dwRemoteIP);
SetEvent(dat->hWaitHandle);
}
else {// êàæåòñÿ êòî òî íå òóäà ïîäêëþ÷èëèñÿ :)
ProtoBroadcastAck(dat->ppro->m_szModuleName, dat->hContact, ACKTYPE_FILE, ACKRESULT_LISTENING, (HANDLE)dat->dwIDRequest, 0);
Netlib_CloseHandle(hNewConnection);
}
}
else Netlib_CloseHandle(hNewConnection);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Receive files
DWORD CMraProto::MraFilesQueueAddReceive(HANDLE hQueue, DWORD dwFlags, MCONTACT hContact, DWORD dwIDRequest, const CMStringW &lpwszFiles, const CMStringA &szAddresses)
{
if (!hQueue || !dwIDRequest)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat = (MRA_FILES_QUEUE_ITEM*)mir_calloc(sizeof(MRA_FILES_QUEUE_ITEM)+sizeof(LPSTR)+64);
if (!dat)
return GetLastError();
WCHAR szBuff[MAX_PATH];
size_t dwMemSize, dwAllocatedCount, dwFileNameTotalSize;
//dat->lmtListMTItem;
dat->ppro = this;
dat->bIsWorking = TRUE;
dat->dwSendTime = GetTickCount();
dat->dwIDRequest = dwIDRequest;
dat->dwFlags = dwFlags;
dat->hContact = hContact;
if (getByte("FileSendEnableMRIMProxyCons", MRA_DEF_FS_ENABLE_MRIM_PROXY_CONS))
dat->hMraMrimProxyData = MraMrimProxyCreate();
dwFileNameTotalSize = 0;
dwAllocatedCount = ALLOCATED_COUNT;
dat->dwFilesCount = 0;
dat->dwFilesTotalSize = 0;
dat->pmfqfFiles = (MRA_FILES_QUEUE_FILE*)mir_calloc((sizeof(MRA_FILES_QUEUE_FILE)*dwAllocatedCount));
int iStart = 0;
while (TRUE) {
CMStringW wszCurrFile = lpwszFiles.Tokenize(L";", iStart);
if (iStart == -1)
break;
CMStringW wszCurrSize = lpwszFiles.Tokenize(L";", iStart);
if (iStart == -1)
break;
if (dat->dwFilesCount == dwAllocatedCount) {
dwAllocatedCount *= 2;
dat->pmfqfFiles = (MRA_FILES_QUEUE_FILE*)mir_realloc(dat->pmfqfFiles, (sizeof(MRA_FILES_QUEUE_FILE)*dwAllocatedCount));
}
MRA_FILES_QUEUE_FILE &p = dat->pmfqfFiles[dat->dwFilesCount];
p.lpwszName = mir_wstrdup(wszCurrFile);
p.dwNameLen = mir_wstrlen(p.lpwszName);
p.dwSize = _wtoi(wszCurrSize);
dat->dwFilesTotalSize += p.dwSize;
dwFileNameTotalSize += p.dwNameLen * sizeof(wchar_t);
dat->dwFilesCount++;
}
dat->pmfqfFiles = (MRA_FILES_QUEUE_FILE*)mir_realloc(dat->pmfqfFiles, (sizeof(MRA_FILES_QUEUE_FILE)*(dat->dwFilesCount + 4)));
dwMemSize = (((dat->dwFilesCount + 4) * 64) + (dwFileNameTotalSize*sizeof(WCHAR)) + (szAddresses.GetLength()*sizeof(WCHAR)) + 128);
dat->pwszFilesList = (LPWSTR)mir_calloc(dwMemSize);
dat->pwszDescription = (LPWSTR)mir_calloc(dwMemSize);
LPWSTR lpwszDelimiter = dat->pwszFilesList;
LPWSTR lpwszCurrentItem = dat->pwszDescription;
StrFormatByteSizeW(dat->dwFilesTotalSize, szBuff, _countof(szBuff));
lpwszCurrentItem += mir_snwprintf(lpwszCurrentItem, ((dwMemSize - ((size_t)lpwszCurrentItem - (size_t)dat->pwszDescription)) / sizeof(WCHAR)), L"%I64u Files (%s)\r\n", dat->dwFilesCount, szBuff);
// description + filesnames
for (size_t i = 0; i < dat->dwFilesCount; i++) {
lpwszDelimiter += mir_snwprintf(lpwszDelimiter, ((dwMemSize - ((size_t)lpwszDelimiter - (size_t)dat->pwszFilesList)) / sizeof(WCHAR)), L"%s", dat->pmfqfFiles[i].lpwszName);
StrFormatByteSizeW(dat->pmfqfFiles[i].dwSize, szBuff, _countof(szBuff));
lpwszCurrentItem += mir_snwprintf(lpwszCurrentItem, ((dwMemSize - ((size_t)lpwszCurrentItem - (size_t)dat->pwszDescription)) / sizeof(WCHAR)), L"%s - %s\r\n", dat->pmfqfFiles[i].lpwszName, szBuff);
}
lpwszCurrentItem += MultiByteToWideChar(MRA_CODE_PAGE, 0, szAddresses, (int)szAddresses.GetLength(), lpwszCurrentItem, (int)((dwMemSize - ((size_t)lpwszCurrentItem - (size_t)dat->pwszDescription)) / sizeof(WCHAR)));
*lpwszCurrentItem = 0;
MraAddrListGetFromBuff(szAddresses, &dat->malAddrList);
MraAddrListStoreToContact(dat->hContact, &dat->malAddrList);
{
mir_cslock l(pmrafqFilesQueue->cs);
ListMTItemAdd(pmrafqFilesQueue, dat, dat);
}
// Send chain event
PROTORECVFILET prf;
prf.dwFlags = PRFF_UNICODE;
prf.timestamp = _time32(NULL);
prf.descr.w = dat->pwszDescription;
prf.fileCount = 1;
prf.files.w = &dat->pwszFilesList;
prf.lParam = dwIDRequest;
ProtoChainRecvFile(hContact, &prf);
return NO_ERROR;
}
void CMraProto::MraFilesQueueRecvThreadProc(LPVOID lpParameter)
{
DWORD dwRetErrorCode = NO_ERROR;
Thread_SetName("MRA: FilesQueueRecv");
if (lpParameter) {
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)((MRA_FILES_THREADPROC_PARAMS*)lpParameter)->hFilesQueueHandle;
MRA_FILES_QUEUE_ITEM *dat = ((MRA_FILES_THREADPROC_PARAMS*)lpParameter)->dat;
WCHAR wszFileName[MAX_FILEPATH] = { 0 };
WCHAR szErrorText[2048];
BYTE btBuff[BUFF_SIZE_RCV];
BOOL bContinue, bFailed, bOK, bConnected;
DWORD dwReceived, dwUpdateTimeNext, dwUpdateTimeCur;
HANDLE hFile;
size_t i, dwBuffSizeUsed;
LARGE_INTEGER liFileSize;
NETLIBSELECT nls = { 0 };
PROTOFILETRANSFERSTATUS pfts = { 0 };
mir_free(lpParameter);
bFailed = TRUE;
bConnected = FALSE;
nls.cbSize = sizeof(nls);
pfts.cbSize = sizeof(pfts);
pfts.hContact = dat->hContact;
pfts.flags = (PFTS_RECEIVING | PFTS_UNICODE);// pfts.sending = dat->bSending; //true if sending, false if receiving
//pfts.files;
pfts.totalFiles = dat->dwFilesCount;
//pfts.currentFileNumber = 0;
pfts.totalBytes = dat->dwFilesTotalSize;
//pfts.totalProgress = 0;
pfts.wszWorkingDir = dat->lpwszPath;
//pfts.currentFile;
//pfts.currentFileSize;
//pfts.currentFileProgress;
//pfts.currentFileTime; //as seconds since 1970
if (MraFilesQueueConnectOut(dat)) {
bConnected = TRUE;
}
else {
if (MraFilesQueueConnectIn(dat)) {
bConnected = TRUE;
}
else {
if (InterlockedExchangeAdd((volatile LONG*)&dat->bIsWorking, 0)) {
ProtoBroadcastAck(dat->hContact, ACKRESULT_CONNECTPROXY, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
if (MraMrimProxyConnect(dat->hMraMrimProxyData, &dat->hConnection) == NO_ERROR) {// ïîäêëþ÷èëèñü ê ïðîêñè, ïðîâåðÿåì òà ëè ñåññèÿ (åù¸ ðàç, íà ýòîò ðàç ñàìè)
if (MraFilesQueueHandCheck(dat->hConnection, dat)) {// ñâÿçü óñòàíîâëåííàÿ ñ òåì êåì íóæíî// dat->bSending
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
bConnected = TRUE;
}
}
}
}
}
if (bConnected) {// email verified
bFailed = FALSE;
for (i = 0; i < dat->dwFilesCount; i++) {// receiving files
pfts.currentFileNumber = (int)i;
pfts.wszCurrentFile = wszFileName;
pfts.currentFileSize = dat->pmfqfFiles[i].dwSize;
pfts.currentFileProgress = 0;
//pfts.currentFileTime; //as seconds since 1970
if ((dat->dwPathSize + dat->pmfqfFiles[i].dwNameLen) < _countof(wszFileName)) {
memcpy(wszFileName, dat->lpwszPath, (dat->dwPathSize*sizeof(WCHAR)));
memcpy((wszFileName + dat->dwPathSize), dat->pmfqfFiles[i].lpwszName, ((dat->pmfqfFiles[i].dwNameLen + 1)*sizeof(WCHAR)));
wszFileName[dat->dwPathSize + dat->pmfqfFiles[i].dwNameLen] = 0;
}
else {
dwRetErrorCode = ERROR_BAD_PATHNAME;
ShowFormattedErrorMessage(L"Receive files: error", dwRetErrorCode);
bFailed = TRUE;
break;
}
//***deb add
//dwBuffSizeUsed = ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, (HANDLE)dat->dwIDRequest, (LPARAM)&pfts);
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, (HANDLE)dat->dwIDRequest, 0);
//dwBuffSizeUsed = (mir_snprintf((LPSTR)btBuff, _countof(btBuff), "%s %S", MRA_FT_GET_FILE, dat->pmfqfFiles[i].lpwszName)+1);
memcpy(btBuff, MRA_FT_GET_FILE, sizeof(MRA_FT_GET_FILE));
btBuff[(sizeof(MRA_FT_GET_FILE)-1)] = ' ';
dwBuffSizeUsed = sizeof(MRA_FT_GET_FILE) + WideCharToMultiByte(MRA_CODE_PAGE, 0, dat->pmfqfFiles[i].lpwszName, (int)dat->pmfqfFiles[i].dwNameLen, (LPSTR)(btBuff + sizeof(MRA_FT_GET_FILE)), (int)(_countof(btBuff) - sizeof(MRA_FT_GET_FILE)), NULL, NULL);
btBuff[dwBuffSizeUsed] = 0;
dwBuffSizeUsed++;
if (dwBuffSizeUsed == (size_t)Netlib_Send(dat->hConnection, (LPSTR)btBuff, (int)dwBuffSizeUsed, 0)) {// file request sended
hFile = CreateFileW(wszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {// file opened/created, pre allocating disk space, for best perfomance
bOK = FALSE;
liFileSize.QuadPart = (LONGLONG)dat->pmfqfFiles[i].dwSize;
if (SetFilePointerEx(hFile, liFileSize, NULL, FILE_BEGIN))
if (SetEndOfFile(hFile)) {
liFileSize.QuadPart = 0;
bOK = SetFilePointerEx(hFile, liFileSize, NULL, FILE_BEGIN);
}
if (bOK) {// disk space pre allocated
bOK = FALSE;
bContinue = TRUE;
dwUpdateTimeNext = GetTickCount();
nls.dwTimeout = (1000 * getDword("TimeOutReceiveFileData", MRA_DEF_FS_TIMEOUT_RECV));
nls.hReadConns[0] = dat->hConnection;
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)dat->dwIDRequest, (LPARAM)&pfts);
while (bContinue) {
switch (CallService(MS_NETLIB_SELECT, 0, (LPARAM)&nls)) {
case SOCKET_ERROR:
case 0:// Time out
dwRetErrorCode = GetLastError();
ShowFormattedErrorMessage(L"Receive files: error on receive file data", dwRetErrorCode);
bContinue = FALSE;
break;
case 1:
dwReceived = Netlib_Recv(dat->hConnection, (LPSTR)&btBuff, _countof(btBuff), 0);
if (dwReceived == 0 || dwReceived == SOCKET_ERROR) {
dwRetErrorCode = GetLastError();
ShowFormattedErrorMessage(L"Receive files: error on receive file data", dwRetErrorCode);
bContinue = FALSE;
}
else {
if (WriteFile(hFile, (LPVOID)&btBuff, dwReceived, &dwReceived, NULL)) {
pfts.currentFileProgress += dwReceived;
pfts.totalProgress += dwReceived;
// progress updates
dwUpdateTimeCur = GetTickCount();
if (dwUpdateTimeNext <= dwUpdateTimeCur || pfts.currentFileProgress >= dat->pmfqfFiles[i].dwSize) {// we update it
dwUpdateTimeNext = dwUpdateTimeCur + MRA_FILES_QUEUE_PROGRESS_INTERVAL;
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)dat->dwIDRequest, (LPARAM)&pfts);
if (pfts.currentFileProgress >= dat->pmfqfFiles[i].dwSize) {// file received
bOK = TRUE;
bContinue = FALSE;
}
}
}
else {// err on write file
dwRetErrorCode = GetLastError();
ShowFormattedErrorMessage(L"Receive files: cant write file data, error", dwRetErrorCode);
bContinue = FALSE;
}
}
break;
}
}// end while
}
else {// err allocating file disk space
dwRetErrorCode = GetLastError();
mir_snwprintf(szErrorText, TranslateT("Receive files: can't allocate disk space for file, size %lu bytes, error"), dat->pmfqfFiles[i].dwSize);
ShowFormattedErrorMessage(szErrorText, dwRetErrorCode);
}
CloseHandle(hFile);
if (bOK == FALSE) {// file recv failed
DeleteFileW(wszFileName);
bFailed = TRUE;
break;
}
}
else {// err on open file
dwRetErrorCode = GetLastError();
mir_snwprintf(szErrorText, TranslateT("Receive files: can't open file %s, error"), wszFileName);
ShowFormattedErrorMessage(szErrorText, dwRetErrorCode);
bFailed = TRUE;
break;
}
}
else {// err on send request for file
dwRetErrorCode = GetLastError();
mir_snwprintf(szErrorText, TranslateT("Receive files: request for file %s not sent, error"), dat->pmfqfFiles[i].lpwszName);
ShowFormattedErrorMessage(szErrorText, NO_ERROR);
bFailed = TRUE;
break;
}
}// end for
NETLIB_CLOSEHANDLE(dat->hConnection);
}
if (bFailed) {
CMStringA szEmail;
if (mraGetStringA(dat->hContact, "e-mail", szEmail))
MraFileTransferAck(FILE_TRANSFER_STATUS_ERROR, szEmail, dat->dwIDRequest, CMStringA());
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)dat->dwIDRequest, 0);
}
else ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, (HANDLE)dat->dwIDRequest, 0);
mir_cslock l(pmrafqFilesQueue->cs);
MraFilesQueueItemFree(dat);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Send files
DWORD CMraProto::MraFilesQueueAddSend(HANDLE hQueue, DWORD dwFlags, MCONTACT hContact, LPWSTR *plpwszFiles, size_t dwFilesCount, DWORD *pdwIDRequest)
{
if (!hQueue)
return ERROR_INVALID_HANDLE;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)hQueue;
MRA_FILES_QUEUE_ITEM *dat = (MRA_FILES_QUEUE_ITEM*)mir_calloc(sizeof(MRA_FILES_QUEUE_ITEM)+sizeof(LPSTR)+64);
if (!dat)
return GetLastError();
ULARGE_INTEGER uliFileSize;
WIN32_FILE_ATTRIBUTE_DATA wfad;
dat->ppro = this;
dat->bIsWorking = TRUE;
dat->dwSendTime = GetTickCount();
dat->dwIDRequest = InterlockedIncrement((LONG volatile*)&dwCMDNum);// óíèêàëüíûé, ðàíäîìíûé èäåíòèôèêàòîð
dat->dwFlags = dwFlags;
dat->hContact = hContact;
if (getByte("FileSendEnableMRIMProxyCons", MRA_DEF_FS_ENABLE_MRIM_PROXY_CONS)) dat->hMraMrimProxyData = MraMrimProxyCreate();
dat->dwFilesCount = dwFilesCount;
dat->pmfqfFiles = (MRA_FILES_QUEUE_FILE*)mir_calloc((sizeof(MRA_FILES_QUEUE_FILE)*(dat->dwFilesCount + 1)));
dat->dwFilesTotalSize = 0;
for (size_t i = 0; i < dat->dwFilesCount; i++) {
MRA_FILES_QUEUE_FILE &p = dat->pmfqfFiles[i];
if (GetFileAttributesExW(plpwszFiles[i], GetFileExInfoStandard, &wfad)) {
uliFileSize.LowPart = wfad.nFileSizeLow;
uliFileSize.HighPart = wfad.nFileSizeHigh;
p.dwSize = uliFileSize.QuadPart;
dat->dwFilesTotalSize += uliFileSize.QuadPart;
}
else p.dwSize = 0;
p.dwNameLen = mir_wstrlen(plpwszFiles[i]);
p.lpwszName = mir_wstrdup(plpwszFiles[i]);
}
dat->bSending = TRUE;
if (pdwIDRequest) *pdwIDRequest = dat->dwIDRequest;
{
mir_cslock l(pmrafqFilesQueue->cs);
ListMTItemAdd(pmrafqFilesQueue, dat, dat);
}
MRA_FILES_THREADPROC_PARAMS *pmftpp = (MRA_FILES_THREADPROC_PARAMS*)mir_calloc(sizeof(MRA_FILES_THREADPROC_PARAMS));
pmftpp->hFilesQueueHandle = hQueue;
pmftpp->dat = dat;
dat->hThread = ForkThreadEx(&CMraProto::MraFilesQueueSendThreadProc, pmftpp, 0);
return NO_ERROR;
}
void CMraProto::MraFilesQueueSendThreadProc(LPVOID lpParameter)
{
DWORD dwRetErrorCode = NO_ERROR;
Thread_SetName("MRA: FilesQueueSend");
if (!lpParameter)
return;
MRA_FILES_QUEUE *pmrafqFilesQueue = (MRA_FILES_QUEUE*)((MRA_FILES_THREADPROC_PARAMS*)lpParameter)->hFilesQueueHandle;
MRA_FILES_QUEUE_ITEM *dat = ((MRA_FILES_THREADPROC_PARAMS*)lpParameter)->dat;
mir_free(lpParameter);
CHAR szFileName[MAX_FILEPATH] = { 0 };
WCHAR szErrorText[2048];
BYTE btBuff[BUFF_SIZE_RCV];
BOOL bFailed = TRUE, bOK, bConnected = FALSE;
DWORD dwReceived, dwSendBlockSize, dwUpdateTimeNext, dwUpdateTimeCur;
size_t i, j, dwBuffSizeUsed = 0;
LPWSTR lpwszFileName;
PROTOFILETRANSFERSTATUS pfts = { 0 };
pfts.cbSize = sizeof(pfts);
pfts.hContact = dat->hContact;
pfts.flags = (PFTS_SENDING | PFTS_UNICODE);// pfts.sending = dat->bSending; //true if sending, false if receiving
pfts.totalFiles = dat->dwFilesCount;
pfts.totalBytes = dat->dwFilesTotalSize;
pfts.wszWorkingDir = dat->lpwszPath;
dwSendBlockSize = getDword("FileSendBlockSize", MRA_DEFAULT_FILE_SEND_BLOCK_SIZE);
if (dwSendBlockSize > _countof(btBuff)) dwSendBlockSize = _countof(btBuff);
if (dwSendBlockSize < 512) dwSendBlockSize = MRA_DEFAULT_FILE_SEND_BLOCK_SIZE;
if (MraFilesQueueConnectIn(dat))
bConnected = TRUE;
else if (MraFilesQueueConnectOut(dat))
bConnected = TRUE;
else {
if (InterlockedExchangeAdd((volatile LONG*)&dat->bIsWorking, 0)) {
ProtoBroadcastAck(dat->hContact, ACKRESULT_CONNECTPROXY, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
if (MraMrimProxyConnect(dat->hMraMrimProxyData, &dat->hConnection) == NO_ERROR) {
// ïîäêëþ÷èëèñü ê ïðîêñè, ïðîâåðÿåì òà ëè ñåññèÿ (åù¸ ðàç, íà ýòîò ðàç ñàìè)
if (MraFilesQueueHandCheck(dat->hConnection, dat)) {
// ñâÿçü óñòàíîâëåííàÿ ñ òåì êåì íóæíî// dat->bSending
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)dat->dwIDRequest, 0);
bConnected = TRUE;
}
}
}
}
if (bConnected) { // email verified
bFailed = FALSE;
for (i = 0; i < dat->dwFilesCount; i++) { // sending files
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, (HANDLE)dat->dwIDRequest, 0);
dwBuffSizeUsed = 0;
while (TRUE) {
dwReceived = Netlib_Recv(dat->hConnection, ((LPSTR)btBuff + dwBuffSizeUsed), (int)(_countof(btBuff) - dwBuffSizeUsed), 0);
if (dwReceived == 0 || dwReceived == SOCKET_ERROR) { // err on receive file name to send
dwRetErrorCode = GetLastError();
ShowFormattedErrorMessage(L"Send files: file send request not received, error", dwRetErrorCode);
bFailed = TRUE;
break;
}
else {
dwBuffSizeUsed += dwReceived;
if (MemoryFindByte((dwBuffSizeUsed - dwReceived), btBuff, dwBuffSizeUsed, 0))
break;
}
}// end while (file name passible received)*/
if (bFailed)
break;
// ...received
if (dwBuffSizeUsed > (sizeof(MRA_FT_GET_FILE)+1)) {// file name received
if (!_memicmp(btBuff, MRA_FT_GET_FILE, sizeof(MRA_FT_GET_FILE)-1)) {
// MRA_FT_GET_FILE verified
bFailed = TRUE;
for (j = 0; j < dat->dwFilesCount; j++) {
lpwszFileName = GetFileNameFromFullPathW(dat->pmfqfFiles[j].lpwszName, dat->pmfqfFiles[j].dwNameLen);
szFileName[WideCharToMultiByte(MRA_CODE_PAGE, 0, lpwszFileName, (int)(dat->pmfqfFiles[j].dwNameLen - (lpwszFileName - dat->pmfqfFiles[j].lpwszName)), szFileName, _countof(szFileName), NULL, NULL)] = 0;
if (!_memicmp(btBuff + sizeof(MRA_FT_GET_FILE), szFileName, dwBuffSizeUsed - (sizeof(MRA_FT_GET_FILE)+1))) {
bFailed = FALSE;
break;
}
}
if (bFailed == FALSE) {
HANDLE hFile = CreateFileW(dat->pmfqfFiles[j].lpwszName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, (FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN), NULL);
if (hFile != INVALID_HANDLE_VALUE) {
bOK = FALSE;
dwUpdateTimeNext = GetTickCount();
pfts.currentFileNumber = (int)i;
pfts.wszCurrentFile = dat->pmfqfFiles[j].lpwszName;
pfts.currentFileSize = dat->pmfqfFiles[j].dwSize;
pfts.currentFileProgress = 0;
//pfts.currentFileTime; //as seconds since 1970
WideCharToMultiByte(MRA_CODE_PAGE, 0, dat->pmfqfFiles[j].lpwszName, (int)dat->pmfqfFiles[j].dwNameLen, szFileName, _countof(szFileName), NULL, NULL);
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)dat->dwIDRequest, (LPARAM)&pfts);
while (TRUE) { // read and sending
if (ReadFile(hFile, btBuff, dwSendBlockSize, (DWORD*)&dwBuffSizeUsed, NULL)) {
dwReceived = Netlib_Send(dat->hConnection, (LPSTR)btBuff, (int)dwBuffSizeUsed, 0);
if (dwBuffSizeUsed == dwReceived) {
pfts.currentFileProgress += dwBuffSizeUsed;
pfts.totalProgress += dwBuffSizeUsed;
// progress updates
dwUpdateTimeCur = GetTickCount();
if (dwUpdateTimeNext <= dwUpdateTimeCur || pfts.currentFileProgress >= dat->pmfqfFiles[j].dwSize) { // we update it
dwUpdateTimeNext = dwUpdateTimeCur + MRA_FILES_QUEUE_PROGRESS_INTERVAL;
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)dat->dwIDRequest, (LPARAM)&pfts);
if (pfts.currentFileProgress >= dat->pmfqfFiles[j].dwSize) { // file received
bOK = TRUE;
break;
}
}
}
else { // err on send file data
dwRetErrorCode = GetLastError();
ShowFormattedErrorMessage(L"Send files: error on send file data", dwRetErrorCode);
break;
}
}
else {// read failure
dwRetErrorCode = GetLastError();
ShowFormattedErrorMessage(L"Send files: cant read file data, error", dwRetErrorCode);
break;
}
}// end while
CloseHandle(hFile);
if (bOK == FALSE) { // file recv failed
bFailed = TRUE;
break;
}
}
else { // err on open file
dwRetErrorCode = GetLastError();
mir_snwprintf(szErrorText, TranslateT("Send files: can't open file %s, error"), dat->pmfqfFiles[j].lpwszName);
ShowFormattedErrorMessage(szErrorText, dwRetErrorCode);
bFailed = TRUE;
break;
}
}
else {
mir_snwprintf(szErrorText, TranslateT("Send files: requested file: %S - not found in send files list."), (((LPSTR)btBuff) + sizeof(MRA_FT_GET_FILE)));
ShowFormattedErrorMessage(szErrorText, NO_ERROR);
bFailed = TRUE;
break;
}
}
else { // err on receive, trash
ShowFormattedErrorMessage(L"Send files: bad file send request - invalid header", NO_ERROR);
bFailed = TRUE;
break;
}
}
else { // bad file name or trash
ShowFormattedErrorMessage(L"Send files: bad file send request - to small packet", NO_ERROR);
bFailed = TRUE;
break;
}
}// end for
NETLIB_CLOSEHANDLE(dat->hConnection);
}
if (bFailed) {
CMStringA szEmail;
if (mraGetStringA(dat->hContact, "e-mail", szEmail))
MraFileTransferAck(FILE_TRANSFER_STATUS_ERROR, szEmail, dat->dwIDRequest, CMStringA());
ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)dat->dwIDRequest, 0);
}
else ProtoBroadcastAck(dat->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, (HANDLE)dat->dwIDRequest, 0);
mir_cslock l(pmrafqFilesQueue->cs);
MraFilesQueueItemFree(dat);
}
|