-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
1761 lines (1630 loc) · 70.3 KB
/
Util.cs
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
using System;
using System.Collections.Generic;
using System.Net.Sockets;
//susing System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace covertFiles
{
public static class Util
{
#region static Variables
/*Forms static pointers*/
public static String Version = covertFiles.Resources.version;
private static FormMain _MainForm = null;
public static FormMain MainForm
{
get
{
return _MainForm;
}
set
{
_MainForm = value;
}
}
private static FormAbout _AboutForm = null;
public static FormAbout AboutForm
{
get
{
return _AboutForm;
}
set
{
_AboutForm = value;
}
}
private static FormHelp _HelpForm = null;
public static FormHelp HelpForm
{
get
{
return _HelpForm;
}
set
{
_HelpForm = value;
}
}
private static System.Drawing.SizeF _MessageFormPercent = new System.Drawing.SizeF((float)0.7, (float)0.5);
public static System.Drawing.SizeF MessageFormPercent
{
get
{
return _MessageFormPercent;
}
set
{
if (_MessageFormPercent != value)
{
_MessageFormPercent = value;
Util.MainForm.Size = new System.Drawing.Size(Util.MainForm.Size.Width, Util.MainForm.Size.Height + 1);
Util.MainForm.Size = new System.Drawing.Size(Util.MainForm.Size.Width, Util.MainForm.Size.Height - 1);
}
}
}
private static bool _formsIgnoreResizeEvents = false;
public static bool formsIgnoreResizeEvents
{
get
{
return _formsIgnoreResizeEvents;
}
set
{
_formsIgnoreResizeEvents = value;
Util.HelpForm.ignoreResizeEvents = value;
Util.MessageForm.ignoreResizeEvents = value;
Util.StatForm.ignoreResizeEvents = value;
}
}
private static FormMessageControl _MessageForm = null;
public static FormMessageControl MessageForm
{
get
{
return _MessageForm;
}
set
{
_MessageForm = value;
}
}
private static FormStatistics _StatForm = null;
public static FormStatistics StatForm
{
get
{
return _StatForm;
}
set
{
_StatForm = value;
}
}
private static FormProperties _PropertiesForm = null;
public static FormProperties PropertiesForm
{
get
{
return _PropertiesForm;
}
set
{
_PropertiesForm = value;
}
}
#endregion
#region static Functions
static public string LocalIPAddress()
{
System.Net.IPHostEntry host;
string localIP = "";
host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
return localIP;
}
#endregion
public static class covertChannel
{
#region Definitions
/*Queu with all sending bits*/
public static Queue<int> sendQueu = new Queue<int>();
/*Queu with all receiving bits*/
public static Queue<int> reciviedBits = new Queue<int>();
/*Queu of the receiving message*/
private static String reciveidQueu = "";
/*interval time for timers in ms*/
public static int Interval = 1000;
public static bool isRunning;
private static int startBit = 2;
private static int endBit = 3;
private static int synchronizationSilentBit = 4;
static int silentsendBitCount = 0;
public static bool hammingErrorCorrection = true;
public static int silentIntervals = 0;
public static int intervalAdjusting = 0;
public static bool startOfFrame = true;
private enum CovType
{
UseTCP,
UseCPU,
UseFileLock,
UseSharedFile,
Other
}
private static CovType Type;
#endregion
#region functions
public delegate void invokeFunct(String msg);
/*intitialize the util library*/
public static void init()
{
isRunning = true;
//init statistics
Util.covertChannel.Statistics.init();
Util.covertChannel.useShareFile.init();
Util.covertChannel.useTCP.init();
Util.covertChannel.useFileLock.init();
Util.covertChannel.useSharedCpu.init();
Type = CovType.Other;
}
/*finalize util*/
public static void finalize()
{
isRunning = false;
Util.covertChannel.useShareFile.finalize();
Util.covertChannel.useTCP.finalize();
Util.covertChannel.useFileLock.finalize();
}
/*
* function to send a message
* input:msg the message to send
*
*/
public static void sendMessage(String Msg)
{
sendBit(startBit);
char[] tmp = Msg.ToCharArray();
foreach (char c in tmp) // for all bytes in message
{
sendByte(c);
}
sendByte((char)0); /*send the end of message byte */
sendBit(endBit);
}
/*
* function to send a byte
* input:c the byte to send
*
*/
private static void sendByte(char c)
{
int[] hammingByte = new int[13];
int tmpBit;
if (hammingErrorCorrection == true)
{
hammingByte[3] = c % 2;
hammingByte[5] = (c / 2) % 2;
hammingByte[6] = (c / 4) % 2;
hammingByte[7] = (c / 8) % 2;
hammingByte[9] = (c / 16) % 2;
hammingByte[10] = (c / 32) % 2;
hammingByte[11] = (c / 64) % 2;
hammingByte[12] = (c / 128) % 2;
hammingByte[1] = hammingByte[3] ^ hammingByte[5] ^ hammingByte[7] ^ hammingByte[9] ^ hammingByte[11];
hammingByte[2] = hammingByte[3] ^ hammingByte[6] ^ hammingByte[7] ^ hammingByte[10] ^ hammingByte[11];
hammingByte[4] = hammingByte[5] ^ hammingByte[6] ^ hammingByte[7] ^ hammingByte[12];
hammingByte[8] = hammingByte[9] ^ hammingByte[10] ^ hammingByte[11] ^ hammingByte[12];
for (int i = 1; i < hammingByte.Length; i++)
{
sendBit(hammingByte[i]);
}
}
else
{
tmpBit = c % 2;
sendBit(tmpBit);
tmpBit = (c / 2) % 2;
sendBit(tmpBit);
tmpBit = (c / 4) % 2;
sendBit(tmpBit);
tmpBit = (c / 8) % 2;
sendBit(tmpBit);
tmpBit = (c / 16) % 2;
sendBit(tmpBit);
tmpBit = (c / 32) % 2;
sendBit(tmpBit);
tmpBit = (c / 64) % 2;
sendBit(tmpBit);
tmpBit = (c / 128) % 2;
sendBit(tmpBit);
}
}
/*
* function to send a bit
* input:bit the bit to send
*
*/
private static void sendBit(int bit)
{
if (silentIntervals != 0)
{
if (silentsendBitCount >= silentIntervals)
{
silentsendBitCount = 0;
sendBit(synchronizationSilentBit);
}
silentsendBitCount++;
}
sendQueu.Enqueue(bit);
}
private static void receivedBit(int bit)
{
reciviedBits.Enqueue(bit);
int byteSize = 8;
if (hammingErrorCorrection == true)
{
byteSize = 12;
}
if (reciviedBits.Count >= byteSize)
{// received a byte
int tmpByte = 0;
if (hammingErrorCorrection == true)
{
int hammError = 0;
int[] hammingByte = new int[13];
for (int i = 1; i < hammingByte.Length; i++)
{
hammingByte[i] = reciviedBits.Dequeue();
}
if (hammingByte[1] != (hammingByte[3] ^ hammingByte[5] ^ hammingByte[7] ^ hammingByte[9] ^ hammingByte[11]))
{
hammError += 1;
}
if (hammingByte[2] != (hammingByte[3] ^ hammingByte[6] ^ hammingByte[7] ^ hammingByte[10] ^ hammingByte[11]))
{
hammError += 2;
}
if (hammingByte[4] != (hammingByte[5] ^ hammingByte[6] ^ hammingByte[7] ^ hammingByte[12]))
{
hammError += 4;
}
if (hammingByte[8] != (hammingByte[9] ^ hammingByte[10] ^ hammingByte[11] ^ hammingByte[12]))
{
hammError += 8;
}
if (hammError < hammingByte.Length)
{
if (hammingByte[hammError] == 0)
{
Util.covertChannel.Statistics.zeroBitReceivedCount--;
}
hammingByte[hammError] = 1 - hammingByte[hammError];
Util.covertChannel.Statistics.ErrorBitReceivedCount++;
}
tmpByte += hammingByte[3];
tmpByte += hammingByte[5] << 1;
tmpByte += hammingByte[6] << 2;
tmpByte += hammingByte[7] << 3;
tmpByte += hammingByte[9] << 4;
tmpByte += hammingByte[10] << 5;
tmpByte += hammingByte[11] << 6;
tmpByte += hammingByte[12] << 7;
}
else
{
tmpByte += reciviedBits.Dequeue();
tmpByte += reciviedBits.Dequeue() << 1;
tmpByte += reciviedBits.Dequeue() << 2;
tmpByte += reciviedBits.Dequeue() << 3;
tmpByte += reciviedBits.Dequeue() << 4;
tmpByte += reciviedBits.Dequeue() << 5;
tmpByte += reciviedBits.Dequeue() << 6;
tmpByte += reciviedBits.Dequeue() << 7;
}
reciveidQueu += ((char)tmpByte).ToString();
if (tmpByte == 0)
{// received end of message byte
//update graphs
MessageForm.Invoke(new invokeFunct(MessageForm.recieveMesage), new Object[] { reciveidQueu });
reciveidQueu = "";
}
}
if (bit == 0)
{
Util.covertChannel.Statistics.zeroBitReceivedCount++;
}
}
public static int calculateETA()
{
int res = 0;
if (Type == CovType.UseCPU)
{
res = useSharedCpu.calculateETA();
}
else if (Type == CovType.UseFileLock)
{
res = useFileLock.calculateETA();
}
else if (Type == CovType.UseSharedFile)
{
res = 0;
}
else if (Type == CovType.UseTCP)
{
res = useTCP.calculateETA();
}
return res;
}
#endregion
#region covert channel Over TCP timming
public static class useTCP
{
#region Definitions
/*represent the delay time
between two continuous package
for transmition of zero bit*/
static private int _TimeZeroBit = 0;
static public int timeZeroBit
{
get
{
return _TimeZeroBit;
}
set
{
_TimeZeroBit = value;
}
}
/* represent the delay time
* between two continuous package
* for transmition of zero bit*/
static private int _TimeOneBit = 1000;
static public int timeOneBit
{
get
{
return _TimeOneBit;
}
set
{
_TimeOneBit = value;
}
}
/* represent the timing error which is
* valid to understate zero or one bit*/
static private int _timeError = 300;
static public int timeError
{
get
{
return _timeError;
}
set
{
_timeError = value;
}
}
static private int _MessageTimeOut = 3000;
static public int messageTimeOut
{
get
{
return _MessageTimeOut;
}
set
{
_MessageTimeOut = value;
}
}
/*the message of the sending package*/
static public String packageMessage = "123456789";
/*the remote hostname*/
static private String _RemoteHostName = "nmhlios.selfip.com";//"localhost";
static public String remoteHostName
{
get
{
return _RemoteHostName;
}
set
{
_RemoteHostName = value;
}
}
/*the remote port*/
static private int _RemotePort = 6666;
static public int remotePort
{
get
{
return _RemotePort;
}
set
{
_RemotePort = value;
}
}
/*the local port for incomming connection*/
static private int _LocalPort = 6666;
static public int localPort
{
get
{
return _LocalPort;
}
set
{
_LocalPort = value;
}
}
static Socket tcpListener;
static Socket tcpClient;
/*
* Timers for to update input an output
*/
private static System.Threading.Thread ThreadWriter;
private static System.Threading.Thread ThreadReader;
#endregion
static public void init()
{
/*init input and output threads*/
ThreadReader = new System.Threading.Thread(useTCP.updateReciver);
ThreadWriter = new System.Threading.Thread(useTCP.updateSender);
}
static public void finalize()
{
localStop();
ThreadReader.Abort();
ThreadWriter.Abort();
}
public static void remoteStop()
{
try
{
tcpClient.Close();
}
catch
{
}
}
public static void localStop()
{
try
{
tcpListener.Close();
}
catch
{
}
}
public static void localStart()
{
ThreadReader.Start();
}
public static void remoteStart()
{
ThreadWriter.Start();
}
public static void start()
{
Type = CovType.UseTCP;
}
public static void stop()
{
localStop();
remoteStop();
}
public static void sendPackage()
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
tcpClient.Send(encoding.GetBytes(packageMessage));
}
public static void receivePackage(Socket socket)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] buffer = new byte[encoding.GetBytes(packageMessage).Length];
//read next package;
socket.Receive(buffer);
}
public static int calculateETA()
{
int res = 0;
int[] msg;
msg = sendQueu.ToArray();
foreach (int bit in msg)
{
if (bit == startBit)
{
res += messageTimeOut;
}
else if (bit == endBit)
{
;
}
else if (bit == synchronizationSilentBit)
{
res+=messageTimeOut;
}
else if (bit == 1)
{
res+=timeOneBit;
}
else if (bit == 0)
{
res+=timeZeroBit;
}
else
{
}
}
return res;
}
/*
* this function is a callback function that called from WriteTimer each Interval ms
* comment: sends data from buffer sendQueu
*/
public static void updateSender()
{
double timeSpace;
/*time last package received */
DateTime lastTime = DateTime.Now;
try
{
tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpClient.NoDelay = true;
// tcpClient.SendBufferSize = 100;
// tcpClient.DontFragment = true;
// tcpClient.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.BsdUrgent, true);
// tcpClient.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay,true);
// tcpClient.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.PacketInformation);
//tcpClient.BeginConnect(remoteHostName, remotePort,null,null);
tcpClient.Connect(remoteHostName, remotePort);
}
catch (Exception exp)
{
MessageForm.Invoke(new invokeFunct(MessageForm.errorMessage), new Object[] { exp.Message });
return;
}
while (isRunning == true)
{
if (sendQueu.Count == 0)
continue;
try
{
if (sendQueu.Peek() == 1)
{
System.Threading.Thread.Sleep(timeOneBit);
/*calculate the time between the two continious package*/
timeSpace = DateTime.Now.Subtract(lastTime).TotalMilliseconds;
//enumerate last package received time
lastTime = DateTime.Now;
Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.Transmition, Statistics.LogValue.Bit1, "Timespace:" + timeSpace);
sendPackage();
}
else if (sendQueu.Peek() == 0)
{
System.Threading.Thread.Sleep(timeZeroBit);
/*calculate the time between the two continious package*/
timeSpace = DateTime.Now.Subtract(lastTime).TotalMilliseconds;
//enumerate last package received time
lastTime = DateTime.Now;
Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.Transmition, Statistics.LogValue.Bit0, "Timespace:" + timeSpace);
sendPackage();
Util.covertChannel.Statistics.zeroBitSendCount++;
}
else if (sendQueu.Peek() == startBit || sendQueu.Peek() == synchronizationSilentBit)//send dammy bit
{
System.Threading.Thread.Sleep(messageTimeOut);
/*calculate the time between the two continious package*/
timeSpace = DateTime.Now.Subtract(lastTime).TotalMilliseconds;
//enumerate last package received time
lastTime = DateTime.Now;
Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.Transmition, Statistics.LogValue.Empty, "Dammy bit Timespace:" + timeSpace);
sendPackage();
if (startOfFrame == true)
{
System.Threading.Thread.Sleep((timeOneBit - timeZeroBit) / 4);
}
}
else if (sendQueu.Peek() == endBit)
{
Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.Clear, Statistics.LogValue.Empty, "End of message");
}
}
catch (Exception exp)
{
MessageForm.Invoke(new invokeFunct(MessageForm.errorMessage), new Object[] { exp.Message });
return;
}
sendQueu.Dequeue();
Statistics.bitSentCount++;
}
tcpClient.Close();
}
/*
* this function is a callback function that called from ReadTimer each Interval ms
* comment: update received Queu data
*/
public static void updateReciver()
{
try
{
double timeSpace;
/*time last package received */
DateTime lastTime = DateTime.Now;
/*initializing connection*/
tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpListener.Bind(new IPEndPoint(IPAddress.Any, localPort));
tcpListener.NoDelay = true;
tcpListener.ReceiveBufferSize = 100;
tcpListener.Listen(1);
Socket socket = tcpListener.Accept();
//tcpListener.BeginAccept(null,null);
while (isRunning == true)
{
try
{
if (socket.Connected)
{
receivePackage(socket);
/*calculate the time between the two continious package*/
timeSpace = DateTime.Now.Subtract(lastTime).TotalMilliseconds;
//enumerate last package received time
lastTime = DateTime.Now;
if (Math.Abs(timeSpace - timeZeroBit) <= timeError)//package represent 0 bit
{
receivedBit(0);
if (intervalAdjusting != 0)
{
lastTime.AddMilliseconds(intervalAdjusting * (timeSpace - timeZeroBit) / 100);
}
Statistics.UpdateLog(Statistics.LogSource.Receiver, Statistics.LogInstanceType.Transmition, Statistics.LogValue.Bit0, "Timespace:" + timeSpace);
}
else if (Math.Abs(timeSpace - timeOneBit) <= timeError)//package represent 1 bit
{
receivedBit(1);
if (intervalAdjusting != 0)
{
lastTime.AddMilliseconds(intervalAdjusting * (timeSpace - timeOneBit) / 100);
}
Statistics.UpdateLog(Statistics.LogSource.Receiver, Statistics.LogInstanceType.Transmition, Statistics.LogValue.Bit1, "Timespace:" + timeSpace);
}
else if (timeSpace > messageTimeOut - timeError)//error or dammy package
{
Statistics.UpdateLog(Statistics.LogSource.Receiver, Statistics.LogInstanceType.Error, Statistics.LogValue.Empty, "Error: dammy package timespace:" + timeSpace);
}
else //error
{
//TODO: Error Bit
receivedBit(0);
Statistics.UpdateLog(Statistics.LogSource.Receiver, Statistics.LogInstanceType.Error, Statistics.LogValue.Empty, "Error: timespace:" + timeSpace);
}
Statistics.bitReceivedCount++;
}
}
catch (Exception exp)
{
MessageForm.Invoke(new invokeFunct(MessageForm.errorMessage), new Object[] { exp.Message });
return;
}
}
socket.Close();
}
catch(Exception exp)
{
System.Windows.Forms.MessageBox.Show("Error: Socket is closed!");
}
}
}
#endregion
#region covert channel Over Shared File
public static class useShareFile
{
#region Definitions
/*
* Timers for to update input an output
*/
private static System.Windows.Forms.Timer ThreadWriter;
private static System.Windows.Forms.Timer ThreadReader;
public static String DefaultFilePAth
{
get
{
return "./";
}
}
public static String DefaultBit1File
{
get
{
return "1.txt";
}
}
public static String DefaultBit0File
{
get
{
return "0.txt";
}
}
public static String DefaultSendAckFile
{
get
{
return "sendAck.txt";
}
}
public static String DefaultSendFile
{
get
{
return "send.txt";
}
}
private static String _filesPath = DefaultFilePAth;
public static String FilesPath
{
get
{
return _filesPath;
}
set
{
_filesPath = value;
}
}
private static String _1bitFile = DefaultBit1File;
public static String Bit1File
{
get
{
return (_filesPath + "\\" + _1bitFile);
}
set
{
_1bitFile = value;
}
}
private static String _SendAckFile = DefaultSendAckFile;
public static String SendAckFile
{
get
{
return (_filesPath + "\\" + _SendAckFile);
}
set
{
_SendAckFile = value;
}
}
private static String _0bitFile = DefaultBit0File;
public static String Bit0File
{
get
{
return (_filesPath + "\\" + _0bitFile);
}
set
{
_0bitFile = value;
}
}
private static String _sendFile = DefaultSendFile;
public static String SendFile
{
get
{
return (_filesPath + "\\" + _sendFile);
}
set
{
_sendFile = value;
}
}
/*
* enum for transfer fsm
*/
private enum TransferState { idle, send, receive };
/* current transfer fsm state*/
private static TransferState transferFsm = TransferState.idle;
#endregion
static public void init()
{
/*init input and output timmers*/
ThreadWriter = new System.Windows.Forms.Timer();
ThreadWriter.Interval = Interval;
ThreadWriter.Tick += new EventHandler(useShareFile.updateSender);
ThreadReader = new System.Windows.Forms.Timer();
ThreadReader.Interval = Interval;
ThreadReader.Tick += new EventHandler(useShareFile.updateReciver);
}
static public void finalize()
{
ThreadReader.Dispose();
ThreadWriter.Dispose();
}
static public void start()
{
ThreadWriter.Start();
ThreadReader.Start();
Type = CovType.UseSharedFile;
}
public static void stop()
{
ThreadWriter.Stop();
ThreadReader.Stop();
}
/* creates a new file
* input:file the filepath of file
* return: false on erros
* true else
* comment: if(file is existing return false
*/
private static bool FileCreate(String file)
{
try
{
System.IO.FileStream fs = System.IO.File.Open(file, System.IO.FileMode.CreateNew);
fs.Close();
}
catch (Exception exp)
{
Statistics.UpdateLog(exp.Message);
return false;
}
return true;
}
/*
* this function is a callback function that called from WriteTimer each Interval ms
* comment: sends data from buffer sendQueu
*/
public static void updateSender(Object sender, EventArgs e)
{
bool status;
/*
* fsm state send
*/
if (transferFsm == TransferState.send && System.IO.File.Exists(Util.covertChannel.useShareFile.SendAckFile))
{
try
{
System.IO.File.Delete(Util.covertChannel.useShareFile.SendAckFile);
Util.covertChannel.Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.SendAckMutex, Statistics.LogValue.Down, "Changing SendAck mutex down");
}
catch
{
Util.covertChannel.Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.Error, Statistics.LogValue.Empty, "Error: can't Change SendAck mutex down");
//???????????
return;
}
/*next fsm state is idle*/
transferFsm = TransferState.idle;
}
/*fsm is idle and there is data to send*/
if (transferFsm == TransferState.idle && sendQueu.Count != 0 && System.IO.File.Exists(Util.covertChannel.useShareFile.SendFile) == false && System.IO.File.Exists(Util.covertChannel.useShareFile.SendAckFile) == false)
{
//try to send
int bit = sendQueu.Peek();
if (bit == 0)
{
status = FileCreate(Util.covertChannel.useShareFile.Bit0File);
if (status == false)
{
transferFsm = TransferState.idle;
Util.covertChannel.Statistics.UpdateLog(Statistics.LogSource.Sender, Statistics.LogInstanceType.Error, Statistics.LogValue.Empty, "Collition Detect in bit0");
//colision detect enumerate bits
return;
}