-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOpenTFTPServerMT.cpp
2647 lines (2291 loc) · 63.5 KB
/
OpenTFTPServerMT.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
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
/**************************************************************************
* Copyright (C) 2005 by Achal Dhir *
* achaldhir@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
// TFTPServer.cpp
#include <stdio.h>
#include <winsock2.h>
#include <process.h>
#include <time.h>
#include <tchar.h>
#include <ws2tcpip.h>
#include <limits.h>
#include <iphlpapi.h>
#include <math.h>
#include "OpenTFTPServerMT.h"
//Global Variables
char serviceName[] = "TFTPServer";
char displayName[] = "Open TFTP Server, MultiThreaded";
char sVersion[] = "Open TFTP Server MultiThreaded Version 1.64 Windows Built 2001";
char iniFile[_MAX_PATH];
char logFile[_MAX_PATH];
char lnkFile[_MAX_PATH];
char tempbuff[256];
char extbuff[256];
char logBuff[512];
char fileSep = '\\';
char notFileSep = '/';
MYWORD blksize = 65464;
char verbatim = 0;
MYWORD timeout = 3;
MYWORD loggingDay;
data1 network;
data1 newNetwork;
data2 cfig;
//ThreadPool Variables
HANDLE tEvent;
HANDLE cEvent;
HANDLE sEvent;
HANDLE lEvent;
MYBYTE currentServer = UCHAR_MAX;
MYWORD totalThreads=0;
MYWORD minThreads=1;
MYWORD activeThreads=0;
//Service Variables
SERVICE_STATUS serviceStatus;
SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
HANDLE stopServiceEvent = 0;
void WINAPI ServiceControlHandler(DWORD controlCode)
{
switch (controlCode)
{
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
SetEvent(stopServiceEvent);
return ;
case SERVICE_CONTROL_PAUSE:
break;
case SERVICE_CONTROL_CONTINUE:
break;
default:
if (controlCode >= 128 && controlCode <= 255)
break;
else
break;
}
SetServiceStatus(serviceStatusHandle, &serviceStatus);
}
void WINAPI ServiceMain(DWORD /*argc*/, TCHAR* /*argv*/[])
{
serviceStatus.dwServiceType = SERVICE_WIN32;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwControlsAccepted = 0;
serviceStatus.dwWin32ExitCode = NO_ERROR;
serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);
if (serviceStatusHandle)
{
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
//init
verbatim = false;
if (_beginthread(init, 0, 0) == 0)
{
if (cfig.logLevel)
{
sprintf(logBuff, "Thread Creation Failed");
logMess(logBuff, 1);
}
exit(-1);
}
fd_set readfds;
timeval tv;
int fdsReady = 0;
tv.tv_sec = 20;
tv.tv_usec = 0;
stopServiceEvent = CreateEvent(0, FALSE, FALSE, 0);
serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
do
{
network.busy = false;
if (!network.tftpConn[0].ready || !network.ready)
{
Sleep(1000);
continue;
}
FD_ZERO(&readfds);
for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].ready; i++)
FD_SET(network.tftpConn[i].sock, &readfds);
int fdsReady = select(network.maxFD, &readfds, NULL, NULL, &tv);
for (int i = 0; fdsReady > 0 && i < MAX_SERVERS && network.tftpConn[i].ready; i++)
{
if (network.ready)
{
network.busy = true;
if (FD_ISSET(network.tftpConn[i].sock, &readfds))
{
WaitForSingleObject(sEvent, INFINITE);
currentServer = i;
if (!totalThreads || activeThreads >= totalThreads)
{
_beginthread(
processRequest, // thread function
0, // default security attributes
NULL); // argument to thread function
}
SetEvent(tEvent);
WaitForSingleObject(sEvent, INFINITE);
fdsReady--;
SetEvent(sEvent);
}
}
}
}
while (WaitForSingleObject(stopServiceEvent, 0) == WAIT_TIMEOUT);
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
sprintf(logBuff, "Closing Network Connections...");
logMess(logBuff, 1);
closeConn();
WSACleanup();
sprintf(logBuff, "TFTP Server Stopped !\n");
logMess(logBuff, 1);
if (cfig.logfile)
{
fclose(cfig.logfile);
cfig.logfile = NULL;
}
serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
CloseHandle(stopServiceEvent);
stopServiceEvent = 0;
}
}
void runService()
{
SERVICE_TABLE_ENTRY serviceTable[] =
{
{serviceName, ServiceMain},
{0, 0}
};
StartServiceCtrlDispatcher(serviceTable);
}
bool stopService(SC_HANDLE service)
{
if (service)
{
SERVICE_STATUS serviceStatus;
QueryServiceStatus(service, &serviceStatus);
if (serviceStatus.dwCurrentState != SERVICE_STOPPED)
{
ControlService(service, SERVICE_CONTROL_STOP, &serviceStatus);
printf("Stopping Service.");
for (int i = 0; i < 100; i++)
{
QueryServiceStatus(service, &serviceStatus);
if (serviceStatus.dwCurrentState == SERVICE_STOPPED)
{
printf("Stopped\n");
return true;
}
else
{
Sleep(500);
printf(".");
}
}
printf("Failed\n");
return false;
}
}
return true;
}
void installService()
{
SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (serviceControlManager)
{
SC_HANDLE service = OpenService(serviceControlManager,
serviceName, SERVICE_QUERY_STATUS);
if (service)
{
printf("Service Already Exists..\n");
StartService(service,0,NULL);
CloseServiceHandle(service);
}
else
{
TCHAR path[ _MAX_PATH + 1 ];
if (GetModuleFileName(0, path, sizeof(path) / sizeof(path[0])) > 0)
{
SC_HANDLE service = CreateService(serviceControlManager,
serviceName, displayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
0, 0, 0, 0, 0);
if (service)
{
printf("Successfully installed.. !\n");
StartService(service,0,NULL);
CloseServiceHandle(service);
}
else
printf("Installation Failed..\n");
}
}
CloseServiceHandle(serviceControlManager);
}
else
printWindowsError();
}
void uninstallService()
{
SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
if (serviceControlManager)
{
SC_HANDLE service = OpenService(serviceControlManager,
serviceName, SERVICE_QUERY_STATUS | SERVICE_STOP | DELETE);
if (service)
{
if (stopService(service))
{
DeleteService(service);
printf("Successfully Removed !\n");
}
else
printf("Failed to Stop Service..\n");
CloseServiceHandle(service);
}
CloseServiceHandle(serviceControlManager);
}
else
printWindowsError();
}
void printWindowsError()
{
MYDWORD dw = GetLastError();
if (dw)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
printf("Error: %s\nPress Enter..\n", lpMsgBuf);
getchar();
}
}
int main(int argc, TCHAR* argv[])
{
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
bool result = GetVersionEx(&osvi);
if (result && osvi.dwPlatformId >= VER_PLATFORM_WIN32_NT)
{
if (argc > 1 && lstrcmpi(argv[1], TEXT("-i")) == 0)
installService();
else if (argc > 1 && lstrcmpi(argv[1], TEXT("-u")) == 0)
uninstallService();
else if (argc > 1 && lstrcmpi(argv[1], TEXT("-v")) == 0)
{
SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
bool serviceStopped = true;
if (serviceControlManager)
{
SC_HANDLE service = OpenService(serviceControlManager,
serviceName, SERVICE_QUERY_STATUS | SERVICE_STOP);
if (service)
{
serviceStopped = stopService(service);
CloseServiceHandle(service);
}
CloseServiceHandle(serviceControlManager);
}
else
printWindowsError();
if (serviceStopped)
runProg();
else
printf("Failed to Stop Service\n");
}
else
runService();
}
else if (argc == 1 || lstrcmpi(argv[1], TEXT("-v")) == 0)
runProg();
else
printf("This option is not available on Windows95/98/ME\n");
return 0;
}
void runProg()
{
verbatim = true;
if (_beginthread(init, 0, 0) == 0)
{
if (cfig.logLevel)
{
sprintf(logBuff, "Thread Creation Failed");
logMess(logBuff, 1);
}
exit(-1);
}
fd_set readfds;
timeval tv;
int fdsReady = 0;
tv.tv_sec = 20;
tv.tv_usec = 0;
printf("\naccepting requests..\n");
do
{
network.busy = false;
//printf("Active=%u Total=%u\n",activeThreads, totalThreads);
if (!network.tftpConn[0].ready || !network.ready)
{
Sleep(1000);
continue;
}
FD_ZERO(&readfds);
for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].ready; i++)
FD_SET(network.tftpConn[i].sock, &readfds);
fdsReady = select(network.maxFD, &readfds, NULL, NULL, &tv);
if (!network.ready)
continue;
//errno = WSAGetLastError();
//if (errno)
// printf("%d\n", errno);
for (int i = 0; fdsReady > 0 && i < MAX_SERVERS && network.tftpConn[i].ready; i++)
{
if (network.ready)
{
network.busy = true;
if (FD_ISSET(network.tftpConn[i].sock, &readfds))
{
//printf("%d Requests Waiting\n", fdsReady);
WaitForSingleObject(sEvent, INFINITE);
currentServer = i;
if (!totalThreads || activeThreads >= totalThreads)
{
_beginthread(
processRequest, // thread function
0, // default security attributes
NULL); // argument to thread function
}
SetEvent(tEvent);
//printf("thread signalled=%u\n",SetEvent(tEvent));
WaitForSingleObject(sEvent, INFINITE);
fdsReady--;
SetEvent(sEvent);
}
}
}
}
while (true);
closeConn();
WSACleanup();
}
void closeConn()
{
for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].loaded; i++)
if (network.tftpConn[i].ready)
closesocket(network.tftpConn[i].sock);
}
void processRequest(void *lpParam)
{
//printf("New Thread %u\n",GetCurrentThreadId());
request req;
WaitForSingleObject(cEvent, INFINITE);
totalThreads++;
SetEvent(cEvent);
do
{
WaitForSingleObject(tEvent, INFINITE);
//printf("In Thread %u\n",GetCurrentThreadId());
WaitForSingleObject(cEvent, INFINITE);
activeThreads++;
SetEvent(cEvent);
if (currentServer >= MAX_SERVERS || !network.tftpConn[currentServer].port)
{
SetEvent(sEvent);
req.attempt = UCHAR_MAX;
continue;
}
memset(&req, 0, sizeof(request));
req.sock = INVALID_SOCKET;
req.clientsize = sizeof(req.client);
req.sockInd = currentServer;
currentServer = UCHAR_MAX;
req.knock = network.tftpConn[req.sockInd].sock;
if (req.knock == INVALID_SOCKET)
{
SetEvent(sEvent);
req.attempt = UCHAR_MAX;
continue;
}
errno = 0;
req.bytesRecd = recvfrom(req.knock, (char*)&req.mesin, sizeof(message), 0, (sockaddr*)&req.client, &req.clientsize);
errno = WSAGetLastError();
//printf("socket Signalled=%u\n",SetEvent(sEvent));
SetEvent(sEvent);
if (!errno && req.bytesRecd > 0)
{
if (cfig.hostRanges[0].rangeStart)
{
MYDWORD iip = ntohl(req.client.sin_addr.s_addr);
bool allowed = false;
for (int j = 0; j <= 32 && cfig.hostRanges[j].rangeStart; j++)
{
if (iip >= cfig.hostRanges[j].rangeStart && iip <= cfig.hostRanges[j].rangeEnd)
{
allowed = true;
break;
}
}
if (!allowed)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "Access Denied");
logMess(&req, 1);
sendto(req.knock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0, (sockaddr*)&req.client, req.clientsize);
req.attempt = UCHAR_MAX;
continue;
}
}
if ((htons(req.mesin.opcode) == 5))
{
sprintf(req.serverError.errormessage, "Error Code %i at Client, %s", ntohs(req.clientError.errorcode), req.clientError.errormessage);
logMess(&req, 2);
req.attempt = UCHAR_MAX;
continue;
}
else if (htons(req.mesin.opcode) != 1 && htons(req.mesin.opcode) != 2)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(5);
sprintf(req.serverError.errormessage, "Unknown Transfer Id");
logMess(&req, 2);
sendto(req.knock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0, (sockaddr*)&req.client, req.clientsize);
req.attempt = UCHAR_MAX;
continue;
}
}
else
{
sprintf(req.serverError.errormessage, "Communication Error");
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
req.blksize = 512;
req.timeout = timeout;
req.expiry = time(NULL) + req.timeout;
bool fetchAck = false;
req.sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (req.sock == INVALID_SOCKET)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(0);
strcpy(req.serverError.errormessage, "Thread Socket Creation Error");
sendto(req.knock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0, (sockaddr*)&req.client, req.clientsize);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = network.tftpConn[req.sockInd].server;
if (cfig.minport)
{
for (MYWORD comport = cfig.minport; ; comport++)
{
service.sin_port = htons(comport);
if (comport > cfig.maxport)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(0);
strcpy(req.serverError.errormessage, "No port is free");
sendto(req.knock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0, (sockaddr*)&req.client, req.clientsize);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
break;
}
else if (bind(req.sock, (sockaddr*) &service, sizeof(service)) == -1)
continue;
else
break;
}
}
else
{
service.sin_port = 0;
if (bind(req.sock, (sockaddr*) &service, sizeof(service)) == -1)
{
strcpy(req.serverError.errormessage, "Thread failed to bind");
sendto(req.knock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0, (sockaddr*)&req.client, req.clientsize);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
}
}
if (req.attempt >= 3)
continue;
if (connect(req.sock, (sockaddr*)&req.client, req.clientsize) == -1)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(0);
strcpy(req.serverError.errormessage, "Connect Failed");
sendto(req.knock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0, (sockaddr*)&req.client, req.clientsize);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
//sprintf(req.serverError.errormessage, "In Temp, Socket");
//logMess(&req, 1);
char *inPtr = req.mesin.buffer;
*(inPtr + (req.bytesRecd - 3)) = 0;
req.filename = inPtr;
if (!strlen(req.filename) || strlen(req.filename) > UCHAR_MAX)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(4);
strcpy(req.serverError.errormessage, "Malformed Request, Invalid/Missing Filename");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
req.attempt = UCHAR_MAX;
logMess(&req, 1);
continue;
}
inPtr += strlen(inPtr) + 1;
req.mode = inPtr;
if (!strlen(req.mode) || strlen(req.mode) > 25)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(4);
strcpy(req.serverError.errormessage, "Malformed Request, Invalid/Missing Mode");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
req.attempt = UCHAR_MAX;
logMess(&req, 1);
continue;
}
inPtr += strlen(inPtr) + 1;
for (MYDWORD i = 0; i < strlen(req.filename); i++)
if (req.filename[i] == notFileSep)
req.filename[i] = fileSep;
tempbuff[0] = '.';
tempbuff[1] = '.';
tempbuff[2] = fileSep;
tempbuff[3] = 0;
if (strstr(req.filename, tempbuff))
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "Access violation");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
if (req.filename[0] == fileSep)
req.filename++;
if (!cfig.homes[0].alias[0])
{
if (strlen(cfig.homes[0].target) + strlen(req.filename) >= sizeof(req.path))
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(4);
sprintf(req.serverError.errormessage, "Filename too large");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
strcpy(req.path, cfig.homes[0].target);
strcat(req.path, req.filename);
}
else
{
char *bname = strchr(req.filename, fileSep);
if (bname)
{
*bname = 0;
bname++;
}
else
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
sprintf(req.serverError.errormessage, "Missing directory/alias");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
for (int i = 0; i < 8; i++)
{
//printf("%s=%i\n", req.filename, cfig.homes[i].alias[0]);
if (cfig.homes[i].alias[0] && !strcasecmp(req.filename, cfig.homes[i].alias))
{
if (strlen(cfig.homes[i].target) + strlen(bname) >= sizeof(req.path))
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(4);
sprintf(req.serverError.errormessage, "Filename too large");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
break;
}
strcpy(req.path, cfig.homes[i].target);
strcat(req.path, bname);
break;
}
else if (i == 7 || !cfig.homes[i].alias[0])
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
sprintf(req.serverError.errormessage, "No such directory/alias %s", req.filename);
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
break;
}
}
}
if (req.attempt >= 3)
continue;
if (ntohs(req.mesin.opcode) == 1)
{
if (!cfig.fileRead)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "GET Access Denied");
logMess(&req, 1);
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
req.attempt = UCHAR_MAX;
continue;
}
if (*inPtr)
{
char *tmp = inPtr;
while (*tmp)
{
if (!strcasecmp(tmp, "blksize"))
{
tmp += strlen(tmp) + 1;
MYDWORD val = atol(tmp);
if (val < 512)
val = 512;
else if (val > blksize)
val = blksize;
req.blksize = val;
break;
}
tmp += strlen(tmp) + 1;
}
}
errno = 0;
if (!strcasecmp(req.mode, "netascii") || !strcasecmp(req.mode, "ascii"))
req.file = fopen(req.path, "rt");
else
req.file = fopen(req.path, "rb");
if (errno || !req.file)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(1);
strcpy(req.serverError.errormessage, "File not found or No Access");
logMess(&req, 1);
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
req.attempt = UCHAR_MAX;
continue;
}
}
else
{
if (!cfig.fileWrite && !cfig.fileOverwrite)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "PUT Access Denied");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
req.file = fopen(req.path, "rb");
if (req.file)
{
fclose(req.file);
req.file = NULL;
if (!cfig.fileOverwrite)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(6);
strcpy(req.serverError.errormessage, "File already exists");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
}
else if (!cfig.fileWrite)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "Create File Access Denied");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
errno = 0;
if (!strcasecmp(req.mode, "netascii") || !strcasecmp(req.mode, "ascii"))
req.file = fopen(req.path, "wt");
else
req.file = fopen(req.path, "wb");
if (errno || !req.file)
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "Invalid Path or No Access");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
continue;
}
}
setvbuf(req.file, NULL, _IOFBF, 5 * req.blksize);
if (*inPtr)
{
fetchAck = true;
char *outPtr = req.mesout.buffer;
req.mesout.opcode = htons(6);
MYDWORD val;
while (*inPtr)
{
//printf("%s\n", inPtr);
if (!strcasecmp(inPtr, "blksize"))
{
strcpy(outPtr, inPtr);
outPtr += strlen(outPtr) + 1;
inPtr += strlen(inPtr) + 1;
val = atol(inPtr);
if (val < 512)
val = 512;
else if (val > blksize)
val = blksize;
req.blksize = val;
sprintf(outPtr, "%u", val);
outPtr += strlen(outPtr) + 1;
}
else if (!strcasecmp(inPtr, "tsize"))
{
strcpy(outPtr, inPtr);
outPtr += strlen(outPtr) + 1;
inPtr += strlen(inPtr) + 1;
if (ntohs(req.mesin.opcode) == 1)
{
if (!fseek(req.file, 0, SEEK_END))
{
if (ftell(req.file) >= 0)
{
req.tsize = ftell(req.file);
sprintf(outPtr, "%u", req.tsize);
outPtr += strlen(outPtr) + 1;
}
else
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "Invalid Path or No Access");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
break;
}
}
else
{
req.serverError.opcode = htons(5);
req.serverError.errorcode = htons(2);
strcpy(req.serverError.errormessage, "Invalid Path or No Access");
send(req.sock, (const char*)&req.serverError, strlen(req.serverError.errormessage) + 5, 0);
logMess(&req, 1);
req.attempt = UCHAR_MAX;
break;
}
}
else
{
req.tsize = 0;
sprintf(outPtr, "%u", req.tsize);
outPtr += strlen(outPtr) + 1;
}
}
else if (!strcasecmp(inPtr, "timeout"))
{
strcpy(outPtr, inPtr);
outPtr += strlen(outPtr) + 1;
inPtr += strlen(inPtr) + 1;
val = atoi(inPtr);
if (val < 1)
val = 1;
else if (val > UCHAR_MAX)
val = UCHAR_MAX;
req.timeout = val;
req.expiry = time(NULL) + req.timeout;
sprintf(outPtr, "%u", val);
outPtr += strlen(outPtr) + 1;
}
inPtr += strlen(inPtr) + 1;
//printf("=%u\n", val);
}