forked from dugdmitry/adhoc_routing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessages.py
1097 lines (943 loc) · 46.5 KB
/
Messages.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
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
#!/usr/bin/python3
"""
@package Messages
Created on Oct 7, 2016
@author: Dmitrii Dugaev
This module describes all possible message types, which can be transmitted by the routing protocol.
All the message types are assigned with a unique type ID and a corresponding header, which contains
additional service information about the packet. The header is called "DSR-header" for now, since it is attached to
a packet between the L2 and L3 headers, as it is done in original DSR routing protocol.
But this is the last similarity with the DSR - our protocol doesn't have to do anything with the concept of
Dynamic Source Routing, and is based on its own concept of packet routing - based on the convergence of reactive
routing and the Reinforcement Learning method - to enable route selection based on current network conditions.
This routing protocol is close to a so-called concept of "Opportunistic Routing" where the nodes have more freedom
to select a next hop, not being completely relied on a current fixed route table state.
CURRENT MESSAGE TYPES:
------------------------------------------------------------------------------------------------------------------------
| TYPE | MESSAGE | LENGTH (BYTES) | DESCRIPTION |
------------------------------------------------------------------------------------------------------------------------
| 0 | Unicast Data Packet | 4 | Unicast data packet from the user (network) interface |
| | | | |
| 1 | Broadcast Data Packet | 4 | Broadcast data packet from the user (network) interface|
| | | | |
| 2 | RREQ4 | 12 | Route Request service message for IPv4 destination |
| | | | |
| 3 | RREQ6 | 36 | Route Request service message for IPv6 destination |
| | | | |
| 4 | RREP4 | 12 | Route Reply service message for IPv4 destination |
| | | | |
| 5 | RREP6 | 36 | Route Reply service message for IPv6 destination |
| | | | |
| 6 | HELLO | from 4 to 56 | Hello service message |
| | | | |
| 7 | ACK | 8 | ACK service message for reliable transmission |
| | | | |
| 8 | REWARD | 8 | Reward service message |
| | | | |
| 9 | Reliable Data Packet | 4 | Unicast data packet which is transmitted using ARQ |
------------------------------------------------------------------------------------------------------------------------
The messages (headers) are described as CType classes with pre-defined fields, depending on a message type.
A detailed description of the fields and its functionality can be found in the documentation.
"""
# Import necessary python modules from the standard library
from random import randint
from socket import AF_INET6, inet_pton, inet_aton, inet_ntoa, inet_ntop
from socket import error as sock_error
from math import ceil
import ctypes
import struct
import binascii
## @var DEFAULT_ROUTE
# Define a default address for the packets with outside destination.
DEFAULT_ROUTE = "0.0.0.0"
## @var DEFAULT_IPV6
# Define a default IPv6 address in order to correctly parse the value into RREQ6/RREP6 messages.
DEFAULT_IPV6 = "fe80::"
# Define static functions for packing and unpacking the message object to and from the binary dsr header.
## Pack the Message object to dsr header. Return the byte array.
# @param message Message object from Messages module.
# @return packed byte array bytearray() value.
def pack_message(message):
if isinstance(message, UnicastPacket):
return UnicastHeader().pack(message)
elif isinstance(message, BroadcastPacket):
return BroadcastHeader().pack(message)
elif isinstance(message, RreqMessage):
# Try to convert an address into binary form. If failed to convert from IPv4,
# then assume that the address is IPv6.
try:
inet_aton(message.src_ip)
message.type = 2
return Rreq4Header().pack(message)
except sock_error:
message.type = 3
return Rreq6Header().pack(message)
elif isinstance(message, RrepMessage):
# Try to convert an address into binary form. If failed to convert from IPv4,
# then assume that the address is IPv6.
try:
inet_aton(message.src_ip)
message.type = 4
return Rrep4Header().pack(message)
except sock_error:
message.type = 5
return Rrep6Header().pack(message)
elif isinstance(message, HelloMessage):
return HelloHeader().pack(message)
elif isinstance(message, AckMessage):
return AckHeader().pack(message)
elif isinstance(message, RewardMessage):
return RewardHeader().pack(message)
elif isinstance(message, ReliableDataPacket):
return ReliableDataHeader().pack(message)
else:
return None
## Unpack the Message object from the dsr header' bytearray value. Return the message object from Messages module.
# @param binary_header Binary header (bytearray) with the packed message.
# @return Message object, length of the unpacked message.
def unpack_message(binary_header):
class TypeField(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
]
# Get a field with type value
type_binary_field = binary_header[:4]
type_field_unpacked = TypeField.from_buffer_copy(type_binary_field)
type_value = type_field_unpacked.TYPE
if type_value == 0:
return UnicastHeader().unpack(binary_header)
elif type_value == 1:
return BroadcastHeader().unpack(binary_header)
elif type_value == 2:
return Rreq4Header().unpack(binary_header)
elif type_value == 3:
return Rreq6Header().unpack(binary_header)
elif type_value == 4:
return Rrep4Header().unpack(binary_header)
elif type_value == 5:
return Rrep6Header().unpack(binary_header)
elif type_value == 6:
return HelloHeader().unpack(binary_header)
elif type_value == 7:
return AckHeader().unpack(binary_header)
elif type_value == 8:
return RewardHeader().unpack(binary_header)
elif type_value == 9:
return ReliableDataHeader().unpack(binary_header)
else:
return None
# TODO: make constructors for all messages
# Describe all message classes, whose instances will be used to manipulate and "pack" the data to dsr binary header.
## Unicast data packet.
class UnicastPacket:
## Type ID of Unicast Data Packet
type = 0
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var id
# Unique packet ID.
# Max value is (2**20 - 1), since the id field size is 20 bits.
self.id = randint(0, 1048575)
## @var hop_count
# Current hop count value.
self.hop_count = 0
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: , ID: , HOP_COUNT: ".
def __str__(self):
out_tuple = (self.type, self.id, self.hop_count)
out_string = "TYPE: %s, ID: %s, HOP_COUNT: %s" % out_tuple
return out_string
## Broadcast data packet.
class BroadcastPacket:
## Type ID of Broadcast Data Packet
type = 1
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var id
# Unique packet ID.
self.id = self.id = randint(0, 1048575)
## @var broadcast_ttl
# Broadcast TTL counter.
self.broadcast_ttl = 0
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: , ID: , BROADCAST_TTL: ".
def __str__(self):
out_tuple = (self.type, self.id, self.broadcast_ttl)
out_string = "TYPE: %s, ID: %s, BROADCAST_TTL: %s" % out_tuple
return out_string
## Route Request service message.
# This service message is used for both IPv4 and IPv6 L3 addressing cases.
class RreqMessage:
## Type ID of RREQ message.
# This type ID value is being set in Messages.pack_message function, depending on L3 addressing type this
# message contains. In case of IPv4 - type ID is 2, in case of IPv6 - type ID is 3.
type = int()
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var id
# Unique message ID.
self.id = randint(0, 1048575)
## @var src_ip
# Source IP address in a string representation form of IPv4 or IPv6 addresses.
self.src_ip = str()
## @var dst_ip
# Destination IP address in a string representation form of IPv4 or IPv6 addresses.
self.dst_ip = str()
## @var hop_count
# Current hop count value.
self.hop_count = 0
## Default print method.
# @param self The object pointer.
# @return String with "ID: , SRC_IP: , DST_IP: , HOP_COUNT: ".
def __str__(self):
out_tuple = (self.id, self.src_ip, self.dst_ip, self.hop_count)
out_string = "ID: %s, SRC_IP: %s, DST_IP: %s, HOP_COUNT: %s" % out_tuple
return out_string
## Route Reply service message.
# This service message is used for both IPv4 and IPv6 L3 addressing cases.
class RrepMessage:
## Type ID of RREP message.
# This type ID value is being set in Messages.pack_message function, depending on L3 addressing type this
# message contains. In case of IPv4 - type ID is 4, in case of IPv6 - type ID is 5.
type = int()
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var id
# Unique message ID.
self.id = randint(0, 1048575)
## @var src_ip
# Source IP address in a string representation form of IPv4 or IPv6 addresses.
self.src_ip = str()
## @var dst_ip
# Destination IP address in a string representation form of IPv4 or IPv6 addresses.
self.dst_ip = str()
## @var hop_count
# Current hop count value.
self.hop_count = 0
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: ,ID: , SRC_IP: , DST_IP: , HOP_COUNT: ".
def __str__(self):
out_tuple = (self.type, self.id, self.src_ip, self.dst_ip, self.hop_count)
out_string = "TYPE: %s, ID: %s, SRC_IP: %s, DST_IP: %s, HOP_COUNT: %s" % out_tuple
return out_string
## Hello service message.
class HelloMessage:
## Type ID of Hello service message.
type = 6
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var ipv4_count
# Amount of unicast IPv4 addresses, assigned to the virtual network interface.
# There can be only one IPv4 addressed assigned to the network interface, therefore, all possible values of
# this variable are: 0 - no IPv4 address assigned, 1 - IPv4 address is assigned.
self.ipv4_count = 0
## @var ipv6_count
# Amount of unicast IPv6 addresses, assigned to the virtual network interface.
# In the current implementation of Messages.HelloHeader, there could be assigned up to 3 IPv6
# addresses, which can then be transmitted inside the Hello message.
# All possible values are:
# 0 - no IPv6 address assigned.
# 1 - 1 IPv6 address is assigned.
# 2 - 2 IPv6 addresses are assigned.
# 3 - 3 IPv6 addresses are assigned.
self.ipv6_count = 0
## @var ipv4_address
# IPv4 address in string representation.
# If no IPv4 address is assigned to the network interface, the variable contains empty string "".
self.ipv4_address = str()
## @var ipv6_addresses
# List of all assigned IPv6 addresses, in string representation.
# If no IPv6 addresses are assigned to the network interface, the variable contains empty list [].
self.ipv6_addresses = list()
## @var tx_count
# Number of message rebroadcast times.
# Contains a number of times this particular Hello message has been rebroadcasted at the current moment.
self.tx_count = 0
## @var gw_mode
# Flag which indicates whether the node operates in the gateway mode or not.
# Possible values:
# 0 - GW_MODE is Off.
# 1 - GW_MODE is On.
self.gw_mode = 0
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: , IPV4_ADDRESS: , IPV6_ADDRESSES: , TX_COUNT: ".
def __str__(self):
out_tuple = (self.type, self.ipv4_address, self.ipv6_addresses, self.tx_count, self.gw_mode)
out_string = "TYPE: %s, IPV4_ADDRESS: %s, IPV6_ADDRESSES: %s, TX_COUNT: %s, GW_MODE: %s" % out_tuple
return out_string
## Acknowledgement (ACK) service message.
class AckMessage:
## Type ID of ACK service message.
type = 7
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var id
# Unique message ID.
self.id = randint(0, 1048575)
## @var tx_count
# Number of message retransmission times.
# Contains a number of times this particular ACK message has been retransmitted at the current moment.
self.tx_count = 0
## @var msg_hash
# Hash value of the data/service packet this ACK replies to.
# This hash value is calculated from two attributes of the data/service packet: destination IP of the packet,
# and the MAC address of a receiving node.
self.msg_hash = 0
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: , ID: , TX_COUNT: , MSG_HASH: ".
def __str__(self):
out_tuple = (self.type, self.id, self.tx_count, self.msg_hash)
out_string = "TYPE: %s, ID: %s, TX_COUNT: %s, MSG_HASH: %s" % out_tuple
return out_string
## Reward service message.
class RewardMessage:
## Type ID of reward service message.
type = 8
## Constructor.
# @param self The object pointer.
# @param reward_value Assigned reward value.
# @param msg_hash Hash value of the packet.
# @return None
def __init__(self, reward_value, msg_hash):
## @var id
# Unique message ID.
self.id = randint(0, 1048575)
## @var reward_value
# Assigned reward value.
# This reward value is calculated and sent back by a node after it has received the packet with a given
# (destination IP + MAC address) pair.
self.reward_value = int(ceil(reward_value))
## @var msg_hash
# Hash value of the data packet this Reward message replies to.
# This hash value is calculated from two attributes of the data/service packet: destination IP of the packet,
# and the MAC address of a receiving node.
self.msg_hash = msg_hash
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: , ID: , REWARD_VALUE: , MSG_HASH: ".
def __str__(self):
out_tuple = (self.type, self.id, self.reward_value, self.msg_hash)
out_string = "TYPE: %s, ID: %s, REWARD_VALUE: %s, MSG_HASH: %s" % out_tuple
return out_string
## Unicast data packet, transmitted using ARQ module.
# This is a message for unicast data packets, which are enabled to be sent with the ARQ.
class ReliableDataPacket:
## Type ID of ARQ-based Unicast Data Packet.
type = 9
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
## @var id
# Unique packet ID.
# Max value is (2**20 - 1), since the id field size is 20 bits.
self.id = randint(0, 1048575)
## @var hop_count
# Current hop count value.
self.hop_count = 0
## Default print method.
# @param self The object pointer.
# @return String with "TYPE: , ID: , HOP_COUNT: ".
def __str__(self):
out_tuple = (self.type, self.id, self.hop_count)
out_string = "TYPE: %s, ID: %s, HOP_COUNT: %s" % out_tuple
return out_string
#######################################################################################################################
# ## Describe DSR headers which will pack the initial message object and return a binary string ## #
## Unicast header.
class UnicastHeader:
## Unicast data header structure.
# This sub-class describes a header structure for unicast data packet.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, HOP_COUNT: 8 bits. Total length: 32 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("HOP_COUNT", ctypes.c_uint32, 8)
]
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param unicast_message The Messages.UnicastPacket object.
# @return A header binary string in hex representation.
def pack(self, unicast_message):
header = self.Header(unicast_message.type, unicast_message.id, unicast_message.hop_count)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = UnicastPacket()
message.id = header_unpacked.ID
message.hop_count = header_unpacked.HOP_COUNT
# Return the message
return message, len(bytearray(header_unpacked))
## Broadcast header.
class BroadcastHeader:
## Broadcast data header structure.
# This sub-class describes a header structure for broadcast data packet.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, BROADCAST_TTL: 8 bits. Total length: 32 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("BROADCAST_TTL", ctypes.c_uint32, 8)
]
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param broadcast_message The Messages.BroadcastPacket object.
# @return A header binary string in hex representation.
def pack(self, broadcast_message):
header = self.Header(broadcast_message.type, broadcast_message.id, broadcast_message.broadcast_ttl)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = BroadcastPacket()
message.id = header_unpacked.ID
message.broadcast_ttl = header_unpacked.BROADCAST_TTL
# Return the message
return message, len(bytearray(header_unpacked))
## RREQ4 header.
class Rreq4Header:
## RREQ4 header structure.
# This sub-class describes a header structure for RREQ4 service message.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, HOP_COUNT: 8 bits, SRC_IP: 32 bits, DST_IP: 32 bits. Total length: 96 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("HOP_COUNT", ctypes.c_uint32, 8),
("SRC_IP", ctypes.c_uint32, 32),
("DST_IP", ctypes.c_uint32, 32)
]
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param rreq4_message The Messages.RreqMessage object.
# @return A header binary string in hex representation.
def pack(self, rreq4_message):
# Turn string representations of IP addresses into an integer form
src_ip = struct.unpack("!I", inet_aton(rreq4_message.src_ip))[0]
dst_ip = struct.unpack("!I", inet_aton(rreq4_message.dst_ip))[0]
header = self.Header(rreq4_message.type, rreq4_message.id, rreq4_message.hop_count, src_ip, dst_ip)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = RreqMessage()
message.type = header_unpacked.TYPE
message.id = header_unpacked.ID
message.hop_count = header_unpacked.HOP_COUNT
message.src_ip = inet_ntoa(struct.pack("!I", header_unpacked.SRC_IP))
message.dst_ip = inet_ntoa(struct.pack("!I", header_unpacked.DST_IP))
# Return the message
return message, len(bytearray(header_unpacked))
## RREQ6 header.
class Rreq6Header:
## RREQ6 header structure.
# This sub-class describes a header structure for RREQ6 service message.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, HOP_COUNT: 8 bits, SRC_IP1: 32 bits, SRC_IP2: 32 bits, SRC_IP3: 32 bits,
# SRC_IP4: 32 bits, DST_IP1: 32 bits, DST_IP2: 32 bits, DST_IP3: 32 bits, DST_IP4: 32 bits. Total length: 288 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("HOP_COUNT", ctypes.c_uint32, 8),
("SRC_IP1", ctypes.c_uint32, 32),
("SRC_IP2", ctypes.c_uint32, 32),
("SRC_IP3", ctypes.c_uint32, 32),
("SRC_IP4", ctypes.c_uint32, 32),
("DST_IP1", ctypes.c_uint32, 32),
("DST_IP2", ctypes.c_uint32, 32),
("DST_IP3", ctypes.c_uint32, 32),
("DST_IP4", ctypes.c_uint32, 32)
]
## 64-bit mask constant.
max_int64 = 0xFFFFFFFFFFFFFFFF
## 32-bit mask constant.
max_int32 = 0xFFFFFFFF
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param rreq6_message The Messages.RreqMessage object.
# @return A header binary string in hex representation.
def pack(self, rreq6_message):
# Turn string representations of IP addresses into an integer form
src_ip = int(binascii.hexlify(inet_pton(AF_INET6, rreq6_message.src_ip)), 16)
# Check the destination IP if it is default address or not.
# If yes, then change it to the corresponding IPv6 value.
if rreq6_message.dst_ip == DEFAULT_ROUTE:
rreq6_message.dst_ip = DEFAULT_IPV6
dst_ip = int(binascii.hexlify(inet_pton(AF_INET6, rreq6_message.dst_ip)), 16)
# Split each IPv6 128-bit value into four 32-bit parts
src_ip_left_64 = (src_ip >> 64) & self.max_int64
src_ip_right_64 = src_ip & self.max_int64
dst_ip_left_64 = (dst_ip >> 64) & self.max_int64
dst_ip_right_64 = dst_ip & self.max_int64
# Pack the values
header = self.Header(rreq6_message.type, rreq6_message.id, rreq6_message.hop_count,
(src_ip_left_64 >> 32) & self.max_int32, src_ip_left_64 & self.max_int32,
(src_ip_right_64 >> 32) & self.max_int32, src_ip_right_64 & self.max_int32,
(dst_ip_left_64 >> 32) & self.max_int32, dst_ip_left_64 & self.max_int32,
(dst_ip_right_64 >> 32) & self.max_int32, dst_ip_right_64 & self.max_int32)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = RreqMessage()
message.type = header_unpacked.TYPE
message.id = header_unpacked.ID
message.hop_count = header_unpacked.HOP_COUNT
# Merge the parts of 128-bit IPv6 address together
src_ip_left_64 = (header_unpacked.SRC_IP1 << 32 | header_unpacked.SRC_IP2)
src_ip_right_64 = (header_unpacked.SRC_IP3 << 32 | header_unpacked.SRC_IP4)
dst_ip_left_64 = (header_unpacked.DST_IP1 << 32 | header_unpacked.DST_IP2)
dst_ip_right_64 = (header_unpacked.DST_IP3 << 32 | header_unpacked.DST_IP4)
src_ip_packed_value = struct.pack(b"!QQ", src_ip_left_64, src_ip_right_64)
dst_ip_packed_value = struct.pack(b"!QQ", dst_ip_left_64, dst_ip_right_64)
message.src_ip = inet_ntop(AF_INET6, src_ip_packed_value)
message.dst_ip = inet_ntop(AF_INET6, dst_ip_packed_value)
# Check the destination IP if it is the default IPv6 value.
# If yes, then change it back to the value of the DEFAULT_ROUTE
if message.dst_ip == DEFAULT_IPV6:
message.dst_ip = DEFAULT_ROUTE
# Return the message
return message, len(bytearray(header_unpacked))
## RREP4 header.
class Rrep4Header:
## RREP4 header structure.
# This sub-class describes a header structure for RREP4 service message.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, HOP_COUNT: 8 bits, SRC_IP: 32 bits, DST_IP: 32 bits. Total length: 96 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("HOP_COUNT", ctypes.c_uint32, 8),
("SRC_IP", ctypes.c_uint32, 32),
("DST_IP", ctypes.c_uint32, 32)
]
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param rrep4_message The Messages.RrepMessage object.
# @return A header binary string in hex representation.
def pack(self, rrep4_message):
# Turn string representations of IP addresses into an integer form
src_ip = struct.unpack("!I", inet_aton(rrep4_message.src_ip))[0]
dst_ip = struct.unpack("!I", inet_aton(rrep4_message.dst_ip))[0]
header = self.Header(rrep4_message.type, rrep4_message.id, rrep4_message.hop_count, src_ip, dst_ip)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = RrepMessage()
message.type = header_unpacked.TYPE
message.id = header_unpacked.ID
message.hop_count = header_unpacked.HOP_COUNT
message.src_ip = inet_ntoa(struct.pack("!I", header_unpacked.SRC_IP))
message.dst_ip = inet_ntoa(struct.pack("!I", header_unpacked.DST_IP))
# Return the message
return message, len(bytearray(header_unpacked))
## RREP6 header.
class Rrep6Header:
## RREP6 header structure.
# This sub-class describes a header structure for RREP6 service message.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, HOP_COUNT: 8 bits, SRC_IP1: 32 bits, SRC_IP2: 32 bits, SRC_IP3: 32 bits,
# SRC_IP4: 32 bits, DST_IP1: 32 bits, DST_IP2: 32 bits, DST_IP3: 32 bits, DST_IP4: 32 bits. Total length: 288 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("HOP_COUNT", ctypes.c_uint32, 8),
("SRC_IP1", ctypes.c_uint32, 32),
("SRC_IP2", ctypes.c_uint32, 32),
("SRC_IP3", ctypes.c_uint32, 32),
("SRC_IP4", ctypes.c_uint32, 32),
("DST_IP1", ctypes.c_uint32, 32),
("DST_IP2", ctypes.c_uint32, 32),
("DST_IP3", ctypes.c_uint32, 32),
("DST_IP4", ctypes.c_uint32, 32)
]
## 64-bit mask constant.
max_int64 = 0xFFFFFFFFFFFFFFFF
## 32-bit mask constant.
max_int32 = 0xFFFFFFFF
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param rrep6_message The Messages.RrepMessage object.
# @return A header binary string in hex representation.
def pack(self, rrep6_message):
# Turn string representations of IP addresses into an integer form
# Check the source IP if it is default address or not.
# If yes, then change it to the corresponding IPv6 value.
if rrep6_message.src_ip == DEFAULT_ROUTE:
rrep6_message.src_ip = DEFAULT_IPV6
src_ip = int(binascii.hexlify(inet_pton(AF_INET6, rrep6_message.src_ip)), 16)
dst_ip = int(binascii.hexlify(inet_pton(AF_INET6, rrep6_message.dst_ip)), 16)
# Split each IPv6 128-bit value into four 32-bit parts
src_ip_left_64 = (src_ip >> 64) & self.max_int64
src_ip_right_64 = src_ip & self.max_int64
dst_ip_left_64 = (dst_ip >> 64) & self.max_int64
dst_ip_right_64 = dst_ip & self.max_int64
# Pack the values
header = self.Header(rrep6_message.type, rrep6_message.id, rrep6_message.hop_count,
(src_ip_left_64 >> 32) & self.max_int32, src_ip_left_64 & self.max_int32,
(src_ip_right_64 >> 32) & self.max_int32, src_ip_right_64 & self.max_int32,
(dst_ip_left_64 >> 32) & self.max_int32, dst_ip_left_64 & self.max_int32,
(dst_ip_right_64 >> 32) & self.max_int32, dst_ip_right_64 & self.max_int32)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = RrepMessage()
message.type = header_unpacked.TYPE
message.id = header_unpacked.ID
message.hop_count = header_unpacked.HOP_COUNT
# Merge the parts of 128-bit IPv6 address together
src_ip_left_64 = (header_unpacked.SRC_IP1 << 32 | header_unpacked.SRC_IP2)
src_ip_right_64 = (header_unpacked.SRC_IP3 << 32 | header_unpacked.SRC_IP4)
dst_ip_left_64 = (header_unpacked.DST_IP1 << 32 | header_unpacked.DST_IP2)
dst_ip_right_64 = (header_unpacked.DST_IP3 << 32 | header_unpacked.DST_IP4)
src_ip_packed_value = struct.pack(b"!QQ", src_ip_left_64, src_ip_right_64)
dst_ip_packed_value = struct.pack(b"!QQ", dst_ip_left_64, dst_ip_right_64)
message.src_ip = inet_ntop(AF_INET6, src_ip_packed_value)
message.dst_ip = inet_ntop(AF_INET6, dst_ip_packed_value)
# Check the source IP if it is the default IPv6 value.
# If yes, then change it back to the value of the DEFAULT_ROUTE
if message.src_ip == DEFAULT_IPV6:
message.src_ip = DEFAULT_ROUTE
# Return the message
return message, len(bytearray(header_unpacked))
## Hello message header.
class HelloHeader:
## Hello message fixed fields structure.
# This structure defines fixed (constant) fields of the Hello header.
# Fields structure:
# TYPE: 4 bits, IPV4_COUNT: 1 bit, IPV6_COUNT: 2 bits, TX_COUNT: 25 bits. Total length: 32 bits.
class FixedHeader(ctypes.LittleEndianStructure):
_fields_ = [("TYPE", ctypes.c_uint32, 4),
("IPV4_COUNT", ctypes.c_uint32, 1),
("IPV6_COUNT", ctypes.c_uint32, 2),
("TX_COUNT", ctypes.c_uint32, 24),
("GW_MODE", ctypes.c_uint32, 1)
]
## Hello message header structure if only IPv4 address is present.
# Fields structure:
# TYPE: 4 bits, IPV4_COUNT: 1 bit, IPV6_COUNT: 2 bits, TX_COUNT: 25 bits, IPV4_ADDRESS: 32 bits.
# Total length: 64 bits.
class OnlyIpv4Header(ctypes.LittleEndianStructure):
_fields_ = [("TYPE", ctypes.c_uint32, 4),
("IPV4_COUNT", ctypes.c_uint32, 1),
("IPV6_COUNT", ctypes.c_uint32, 2),
("TX_COUNT", ctypes.c_uint32, 24),
("GW_MODE", ctypes.c_uint32, 1),
("IPV4_ADDRESS", ctypes.c_uint32, 32)
]
## 64-bit mask constant.
max_int64 = 0xFFFFFFFFFFFFFFFF
## 32-bit mask constant.
max_int32 = 0xFFFFFFFF
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param hello_message The Messages.HelloMessage object.
# @return A header binary string in hex representation.
def pack(self, hello_message):
args = [hello_message.type, hello_message.ipv4_count, hello_message.ipv6_count,
hello_message.tx_count, hello_message.gw_mode]
# Add fields in the structure, depending on the given hello_message
if hello_message.ipv4_count and hello_message.ipv6_count == 0:
ipv4_address = struct.unpack("!I", inet_aton(hello_message.ipv4_address))[0]
args.append(ipv4_address)
header = self.OnlyIpv4Header(*args)
elif hello_message.ipv6_count:
fields = list(self.FixedHeader._fields_)
if hello_message.ipv4_count:
fields.append(("IPV4_ADDRESS", ctypes.c_uint32, 32))
ipv4_address = struct.unpack("!I", inet_aton(hello_message.ipv4_address))[0]
args.append(ipv4_address)
for i in range(hello_message.ipv6_count):
fields.append(("IPV6_ADDRESS_%s_1" % i, ctypes.c_uint32, 32))
fields.append(("IPV6_ADDRESS_%s_2" % i, ctypes.c_uint32, 32))
fields.append(("IPV6_ADDRESS_%s_3" % i, ctypes.c_uint32, 32))
fields.append(("IPV6_ADDRESS_%s_4" % i, ctypes.c_uint32, 32))
# Pack ipv6 addresses
ipv6_address = int(binascii.hexlify(inet_pton(AF_INET6, hello_message.ipv6_addresses[i])), 16)
ipv6_address_left = (ipv6_address >> 64) & self.max_int64
ipv6_address_right = ipv6_address & self.max_int64
args.extend([(ipv6_address_left >> 32) & self.max_int32, ipv6_address_left & self.max_int32,
(ipv6_address_right >> 32) & self.max_int32, ipv6_address_right & self.max_int32])
# Define header structure
class Header(ctypes.Structure):
_fields_ = fields
header = Header(*args)
# Else, construct an empty header without IP addresses
else:
header = self.FixedHeader(*args)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Hello Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Get the first fixed part of the header
fixed_header_unpacked = self.FixedHeader.from_buffer_copy(binary_header)
# Create and write to a message object
message = HelloMessage()
# Generate the rest of the part
if fixed_header_unpacked.IPV4_COUNT and fixed_header_unpacked.IPV6_COUNT == 0:
header_unpacked = self.OnlyIpv4Header.from_buffer_copy(binary_header)
message.ipv4_address = inet_ntoa(struct.pack("!I", header_unpacked.IPV4_ADDRESS))
elif fixed_header_unpacked.IPV6_COUNT:
fields = list(self.FixedHeader._fields_)
if fixed_header_unpacked.IPV4_COUNT:
fields.append(("IPV4_ADDRESS", ctypes.c_uint32, 32))
for i in range(fixed_header_unpacked.IPV6_COUNT):
fields.append(("IPV6_ADDRESS_%s_1" % i, ctypes.c_uint32, 32))
fields.append(("IPV6_ADDRESS_%s_2" % i, ctypes.c_uint32, 32))
fields.append(("IPV6_ADDRESS_%s_3" % i, ctypes.c_uint32, 32))
fields.append(("IPV6_ADDRESS_%s_4" % i, ctypes.c_uint32, 32))
# Define header structure
class Header(ctypes.Structure):
_fields_ = fields
header_unpacked = Header.from_buffer_copy(binary_header)
if header_unpacked.IPV4_COUNT:
message.ipv4_address = inet_ntoa(struct.pack("!I", header_unpacked.IPV4_ADDRESS))
for i in range(header_unpacked.IPV6_COUNT):
# Merge the parts of 128-bit IPv6 address together
ipv6_left = (getattr(header_unpacked, "IPV6_ADDRESS_%s_1" % i) << 32 |
getattr(header_unpacked, "IPV6_ADDRESS_%s_2" % i))
ipv6_right = (getattr(header_unpacked, "IPV6_ADDRESS_%s_3" % i) << 32 |
getattr(header_unpacked, "IPV6_ADDRESS_%s_4" % i))
ipv6_packed_value = struct.pack(b"!QQ", ipv6_left, ipv6_right)
message.ipv6_addresses.append(inet_ntop(AF_INET6, ipv6_packed_value))
# Else, create a message without ip addresses
else:
message.tx_count = fixed_header_unpacked.TX_COUNT
message.gw_mode = fixed_header_unpacked.GW_MODE
message.ipv4_count = fixed_header_unpacked.IPV4_COUNT
message.ipv6_count = fixed_header_unpacked.IPV6_COUNT
# Return the message
return message, len(bytearray(fixed_header_unpacked))
message.ipv4_count = header_unpacked.IPV4_COUNT
message.ipv6_count = header_unpacked.IPV6_COUNT
message.tx_count = header_unpacked.TX_COUNT
message.gw_mode = header_unpacked.GW_MODE
# Return the message
return message, len(bytearray(header_unpacked))
## ACK header.
class AckHeader:
## ACK header structure.
# This sub-class describes a header structure for ACK message.
# Fields structure:
# TYPE: 4 bits, ID: 20 bits, TX_COUNT: 8 bits, MSG_HASH: 32 bits. Total length: 64 bits.
class Header(ctypes.LittleEndianStructure):
_fields_ = [
("TYPE", ctypes.c_uint32, 4),
("ID", ctypes.c_uint32, 20),
("TX_COUNT", ctypes.c_uint32, 8),
("MSG_HASH", ctypes.c_uint32, 32)
]
## Constructor.
# @param self The object pointer.
# @return None
def __init__(self):
pass
## Pack the message object into the given structure.
# @param self The object pointer.
# @param ack_message The Messages.AckMessage object.
# @return A header binary string in hex representation.
def pack(self, ack_message):
header = self.Header(ack_message.type, ack_message.id, ack_message.tx_count, ack_message.msg_hash)
# Return the array in byte representation
return bytearray(header)
## Unpack the message object from the binary string.
# @param self The object pointer.
# @param binary_header Binary string with the Header structure.
# @return (message object, created from the binary string), (length of the unpacked header structure)
def unpack(self, binary_header):
# Cast the byte_array into the structure
header_unpacked = self.Header.from_buffer_copy(binary_header)
# Get values and create message object and fill up the fields
message = AckMessage()
message.id = header_unpacked.ID
message.tx_count = header_unpacked.TX_COUNT
message.msg_hash = header_unpacked.MSG_HASH
# Return the message
return message, len(bytearray(header_unpacked))