forked from daniestevez/free-outernet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
protocols.py
254 lines (215 loc) · 7.53 KB
/
protocols.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
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
# Copyright 2016 Daniel Estevez <[email protected]>.
#
# This 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 3, or (at your option)
# any later version.
#
# This software 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 software; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
"""Othernet OP and LDP protocols"""
__author__ = 'Daniel Estevez'
__copyright__ = 'Copyright 2016, Daniel Estevez'
__license__ = 'GPLv3'
__maintainer__ = 'Daniel Estevez'
__email__ = '[email protected]'
import struct
import zfec
from crcmod.predefined import PredefinedCrc
class OP:
"""
Othernet Protocol (OP) packet
OP is the L3 protocol of Othernet
"""
__header_len = 5
def __init__(self, data):
"""
Create OP packet
Returns a new OP packet
Args:
data (bytes): packet contents
Throws ValueError if packet is malformed
"""
header = data[:self.__header_len]
if len(header) < self.__header_len:
raise ValueError('Malformed OP packet: too short')
self.length, self.fragment_type, self.carousel_id, \
self.last_fragment, self.fragment_index = struct.unpack('>BBBBB', header)
self.payload = data[self.__header_len : self.__header_len + self.length - 4]
class PartialLDP:
"""
Fragmented LDP packet
A LDP packet which has not yet been completely received.
"""
def __init__(self):
self.reset()
"""
Reset the internal state
"""
def reset(self):
self.__fragments = {}
self.__frag_recv = 0
self.__fec_recv = 0
self.frag_size = None
self.frag_count = None
self.fec_count = None
self.next_index = 0
"""
Push a data block
Args:
index (int): the index of the fragment
payload (bytes): the actual data
"""
def push_data(self, index, payload):
if index in self.__fragments:
return
self.__fragments[index] = payload
self.__frag_recv += 1
"""
Push a FEC block
Args:
index (int): the index of the FEC block
payload (bytes): the actual FEC data
"""
def push_fec(self, index, payload):
if not self.frag_count or (self.frag_count + index) in self.__fragments:
return
self.__fragments[self.frag_count + index] = payload
self.__fec_recv += 1
"""
Indicates whether a reconstruction is possible
"""
@property
def complete(self):
return self.frag_count and self.__fec_recv + self.__frag_recv >= self.frag_count
"""
Decode the packet
"""
def decode(self):
if self.__frag_recv == self.frag_count: # No error FEC decoding necessary
return b''.join([self.__fragments[s] for s in range(self.frag_count)])
k = self.frag_count
n = k + self.fec_count
decoder = zfec.Decoder(k, n)
sharenums = list(self.__fragments.keys())
if len(sharenums) != k:
print("[ERROR] Unexpected number of fragments. k = {}, sharenums = {}".format(k, sharenums))
return
return b''.join(decoder.decode([self.__fragments[s] for s in sharenums], sharenums))
class OPDefragmenter:
"""
OP defragmenter
Performs defragmentation of OP packets. New packets can be pushed
with .push(), which will return an L4 packet if defragmentation
is successful
"""
def __init__(self):
"""
Initialize defragmenter
"""
self.__pending = {}
def push(self, packet):
"""
Push new packet into defragmenter
Returns a payload (bytes) if defragmentation is succesful,
None otherwise
Args:
packet (OP): Packet to push
"""
ldp = self.__pending.get(packet.carousel_id)
if not ldp:
ldp = PartialLDP()
self.__pending[packet.carousel_id] = ldp
if packet.fragment_type == 0x3c or packet.fragment_type == 0xc3:
if packet.fragment_type == 0x3c and packet.fragment_index == 0: # TODO Verify correctness
return packet.payload
if packet.fragment_index < ldp.next_index:
ldp.reset()
if not ldp.frag_size:
ldp.frag_size = packet.length - 4
if not ldp.frag_count:
ldp.frag_count = packet.last_fragment + 1
ldp.next_index = packet.fragment_index + 1
ldp.push_data(packet.fragment_index, packet.payload)
if packet.fragment_type == 0x3c and ldp.complete:
decoded = ldp.decode()
ldp.reset()
return decoded
elif packet.fragment_type == 0x69:
if not ldp.frag_size:
return
if not ldp.fec_count:
ldp.fec_count = packet.last_fragment + 1
ldp.push_fec(packet.fragment_index, packet.payload)
if ldp.complete:
decoded = ldp.decode()
ldp.reset()
return decoded
else:
print('Unsupported fragment type: {:02x}'.format(packet.fragment_type))
class LDP:
"""
Lightweight Datagram Protocol (LDP) packet
LDP is the L4 protocol of Outernet
"""
__header_len = 4
__checksum_len = 4
def __init__(self, data):
"""
Create an LDP packet
Returns a new LDP packet
Args:
data (bytes) : packet contents
Throws ValueError if packet is malformed
"""
if len(data) < self.__header_len + self.__checksum_len:
raise ValueError('Malformed LDP packet: too short')
header = struct.unpack('>L', data[:self.__header_len])[0]
self.type = header >> 24
self.length = header & 0xffffff
if self.length > len(data):
raise ValueError('Malformed LDP packet: invalid length')
crc = PredefinedCrc('crc-32-mpeg')
crc.update(data[:self.length])
if crc.crcValue != 0:
raise ValueError('Malformed LDP packet: invalid checksum')
self.payload = data[self.__header_len:self.length-self.__checksum_len]
class LDPRouter:
"""
LDP router
Sends an LDP packet to the appropriate function according to the
a and b fields in the LDP packet. The packet handler functions
are previously registered whith LDPRouter().register()
"""
def __init__(self):
"""
Create a new LDP router
"""
self.__registrations = dict()
def route(self, packet):
"""
Push an LDP packet into the router, calling the appropriate
packet handler function
Args:
packet (LDP): packet to route
"""
if packet.type not in self.__registrations:
print('Unknown routing for packet with type {:02x}'.format(packet.type))
else:
self.__registrations[packet.type](packet)
def register(self, fun, type):
"""
Register a packet handler
Args:
fun: packet handler function (it must take a single argument of type LDP)
type: type of the packets to handle
"""
self.__registrations[type] = fun