-
Notifications
You must be signed in to change notification settings - Fork 9
/
cochannel.py
162 lines (130 loc) · 4.76 KB
/
cochannel.py
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
# LabunskyA wrote this file
# Distributed under the Simplified BSD License
import string, time
from baudot import Baudot
class CoverChannelStateAPI:
def send_bit(self, bit: bool, dest): raise NotImplementedError
def receive_bit(self, src) -> bool: raise NotImplementedError
def get_state(self, dest) -> bool: raise NotImplementedError
class CoverChannelEncoder:
bit_lim = 0
@staticmethod
def encode(data: string) -> [int]: raise NotImplementedError
@staticmethod
def decode(data: [int]) -> string: raise NotImplementedError
@staticmethod
def get_sym(code: int): raise NotImplementedError
class CovertChannel:
def __init__(self, api: CoverChannelStateAPI, endpoint, clock_len: int = 1, tail_nulls: int = 10,
verbose: bool = False, encoder: CoverChannelEncoder = Baudot):
self.__api = api
self.__api_clock = clock_len
self.__tail_nuls = tail_nulls
self.__endpoint = endpoint
self.__state = False
self.__verbose = verbose
self.__encoder = encoder
@staticmethod
def __clock():
return time.perf_counter()
def __sync_on(self, clock):
total_skipped = 0
spent = self.__clock() - clock
while spent >= self.__api_clock:
skipped = int(spent / self.__api_clock)
clock += skipped * self.__api_clock
total_skipped += skipped
spent = self.__clock() - clock + spent % self.__api_clock
time.sleep(clock + self.__api_clock - self.__clock())
return total_skipped
def __send_bit(self, bit):
if self.__state != bit:
self.__api.send_bit(bit, self.__endpoint)
self.__state = bit
def __receive_bit(self) -> bool:
return self.__api.receive_bit(self.__endpoint)
def send(self, message):
codes = self.__encoder.encode(message)
self.send_raw(codes)
def receive(self):
return self.__encoder.decode(self.receive_raw())
def receive_raw(self) -> [int]:
clock = self.__clock()
# Wait for the transmission to start
self.__state = self.__receive_bit()
while True:
skipped = self.__sync_on(clock)
clock += (skipped + 1) * self.__api_clock
if self.__state != self.__receive_bit():
break
codes = []
code = 0
code_bit = 1
nulls = 0
while nulls < self.__tail_nuls:
skipped = self.__sync_on(clock)
clock += (skipped + 1) * self.__api_clock
while skipped > 0:
skipped -= 1
code_bit <<= 1
if self.__verbose:
print("X", end="")
if code_bit == self.__encoder.bit_lim:
codes.append(code)
if self.__verbose:
print("", self.__encoder.get_sym(code))
code = 0
code_bit = 1
bit = self.__receive_bit()
if bit:
nulls = 0
code |= code_bit
if self.__verbose:
print("+", end="")
else:
nulls += 1
if self.__verbose:
print("-", end="")
code_bit <<= 1
if code_bit == self.__encoder.bit_lim:
codes.append(code)
if self.__verbose:
print("", self.__encoder.get_sym(code))
code = 0
code_bit = 1
return codes
def send_raw(self, codes: [int]):
self.__state = self.__api.get_state(self.__endpoint)
clock = self.__clock()
pos = 0
code_bit = 1
self.__send_bit(not self.__state)
while True:
skipped = self.__sync_on(clock)
clock += (skipped + 1) * self.__api_clock
while skipped > 0:
skipped -= 1
if self.__verbose:
print("X", end="")
code_bit <<= 1
if code_bit == self.__encoder.bit_lim:
if self.__verbose:
print("", self.__encoder.get_sym(codes[pos]))
pos += 1
code_bit = 1
if pos >= len(codes):
break
bit = (codes[pos] & code_bit) != 0
self.__send_bit(bit)
if self.__verbose:
if bit:
print("+", end="")
else:
print("-", end="")
code_bit <<= 1
if code_bit == self.__encoder.bit_lim:
if self.__verbose:
print("", self.__encoder.get_sym(codes[pos]))
pos += 1
code_bit = 1
self.__send_bit(False)