Permalink
Browse files

Major overhaul

  • Loading branch information...
steffann committed Jan 11, 2013
1 parent 181d525 commit 603620253be7b8354d949860a7cbdefa63c6f755
Showing with 880 additions and 488 deletions.
  1. +27 −20 ip_test.py
  2. +96 −0 kladblok.txt
  3. +2 −0 pylisp/packet/__init__.py
  4. +3 −0 pylisp/packet/ip/__init__.py
  5. +15 −8 pylisp/packet/ip/ipv4.py
  6. +0 −413 pylisp/packet/ip/ipv6.py
  7. +11 −0 pylisp/packet/ip/ipv6/__init__.py
  8. +153 −0 pylisp/packet/ip/ipv6/base.py
  9. +82 −0 pylisp/packet/ip/ipv6/destination_options_header.py
  10. +99 −0 pylisp/packet/ip/ipv6/fragment_header.py
  11. +82 −0 pylisp/packet/ip/ipv6/hop_by_hop_options_header.py
  12. +54 −0 pylisp/packet/ip/ipv6/no_next_header.py
  13. +97 −0 pylisp/packet/ip/ipv6/routing_header.py
  14. +52 −0 pylisp/packet/ip/protocol.py
  15. +44 −0 pylisp/packet/ip/protocol_registry.py
  16. +21 −8 pylisp/packet/ip/udp.py
  17. +2 −0 pylisp/packet/lisp/__init__.py
  18. 0 pylisp/packet/{ → lisp}/control/__init__.py
  19. +1 −1 pylisp/packet/{ → lisp}/control/base.py
  20. 0 pylisp/packet/{ → lisp}/control/constants.py
  21. +1 −2 pylisp/packet/{ → lisp}/control/encapsulated_control_message.py
  22. 0 pylisp/packet/{ → lisp}/control/locator_record.py
  23. +1 −2 pylisp/packet/{ → lisp}/control/map_notify.py
  24. +3 −4 pylisp/packet/{ → lisp}/control/map_referral.py
  25. +1 −1 pylisp/packet/{ → lisp}/control/map_referral_record.py
  26. +3 −5 pylisp/packet/{ → lisp}/control/map_register.py
  27. +7 −7 pylisp/packet/{ → lisp}/control/map_reply.py
  28. +1 −1 pylisp/packet/{ → lisp}/control/map_reply_record.py
  29. +3 −4 pylisp/packet/{ → lisp}/control/map_request.py
  30. +1 −1 pylisp/packet/{ → lisp}/control/type_registry.py
  31. +13 −7 pylisp/packet/{ → lisp}/data.py
  32. +2 −0 pylisp/utils/checksum.py
  33. +1 −1 unittests/test_packet_control_encapsulated_control_message.py
  34. +1 −1 unittests/test_packet_control_map_request.py
  35. +1 −2 unittests/test_packet_control_type_registry.py
View
@@ -1,6 +1,6 @@
#!/usr/bin/env python
-from pylisp.packet.ip.udp import UDPMessage
-from pylisp.packet.control.base import LISPControlMessage
+from pylisp.packet.ip.ipv4 import IPv4Packet
+from pylisp.packet.ip.ipv6 import IPv6HopByHopOptionsHeader, IPv6Packet
ip_packet_hex = ('45c00078000a0000201173c85cfe1cbdd413d814'
'10f610f6006437b6'
@@ -11,27 +11,25 @@
'0064ff00000500015cfe1cbd')
ip_packet = ip_packet_hex.decode('hex')
-from pylisp.packet.ip.ipv4 import IPv4Packet
-
packet = IPv4Packet.from_bytes(ip_packet)
-print packet
+print repr(packet)
-pkt_bytes = packet.to_bytes()
+pkt_bytes = bytes(packet)
pkt_bytes_hex = pkt_bytes.encode('hex')
print ip_packet == pkt_bytes
-udp_message = UDPMessage.from_bytes(packet.payload)
-print udp_message
+udp_message = packet.payload
+print repr(udp_message)
print udp_message.calculate_checksum(packet.source, packet.destination)
udp_msg_bytes = udp_message.to_bytes()
udp_msg_bytes_hex = udp_msg_bytes.encode('hex')
print packet.payload == udp_msg_bytes
-lisp_message = LISPControlMessage.from_bytes(udp_message.payload)
-print lisp_message
+lisp_message = udp_message.payload
+print repr(lisp_message)
ipv6_packet_hex = ('6000000000d611fffe80000000000000'
'aafad8fffeec0ea3ff02000000000000'
@@ -52,30 +50,39 @@
'0004aafad8ec0ea3a8fad8ec0ea3')
ipv6_packet = ipv6_packet_hex.decode('hex')
-from pylisp.packet.ip.ipv6 import IPv6Packet
-
packet = IPv6Packet.from_bytes(ipv6_packet)
-print packet
+print repr(packet)
pkt_bytes = packet.to_bytes()
pkt_bytes_hex = pkt_bytes.encode('hex')
print ipv6_packet == pkt_bytes
-udp_message = UDPMessage.from_bytes(packet.payload)
-print udp_message
+udp_message = packet.payload
+print repr(udp_message)
print udp_message.calculate_checksum(packet.source, packet.destination)
udp_msg_bytes = udp_message.to_bytes()
udp_msg_bytes_hex = udp_msg_bytes.encode('hex')
print packet.payload == udp_msg_bytes
-from pylisp.packet.ip.ipv6 import IPv6HopByHopOptionsHeader
-
-hdrbytes_hex = ('00010000000000000000000000000000')
+hdrbytes_hex = ('3b010000000000000000000000000000')
hdrbytes = hdrbytes_hex.decode('hex')
hdr = IPv6HopByHopOptionsHeader.from_bytes(hdrbytes)
-print hdr
+print repr(hdr)
print hdrbytes_hex
-print hdr.to_bytes().encode('hex')
+print bytes(hdr).encode('hex')
+
+multilayer_hex = ('45000073f7734000ff1173f95f61535d'
+ '254d380104ce10f5005f0000c0ac1912'
+ '00000007600000000027113e2a008640'
+ '0001000002204afffec8259920010503'
+ '0c2700000000000000020030d21d0035'
+ '0027af7b942700000001000000000000'
+ '08686f73746e616d650462696e640000'
+ '100003')
+multilayer = multilayer_hex.decode('hex')
+
+outer = IPv4Packet.from_bytes(multilayer)
+print repr(outer)
View
@@ -0,0 +1,96 @@
+# from pylisp.packet.control import LISPControlPacket
+# from pylisp.packet.data import LISPDataPacket
+# import doctest
+# import pylisp.packet.control.encapsulated_control_message
+#
+# print '-------------------------------------------------'
+# print 'Testing parsing and re-constructing a data packet'
+# print '-------------------------------------------------'
+#
+# data_packet_hex = ('c033d3c10000000745c0005835400000' +
+# 'ff06094a254d38204d45d1a30016f597' +
+# 'a1c3c7406718bf1b50180ff0793f0000' +
+# 'b555e59ff5ba6aad33d875c600fd8c1f' +
+# 'c5268078f365ee199179fbd09d09d690' +
+# '193622a6b70bcbc7bf5f20dda4258801')
+#
+# data_packet = data_packet_hex.decode('hex')
+#
+# print 'Parsing...'
+# packet = LISPDataPacket.from_bytes(data_packet)
+# print 'Parsed', packet.__class__.__name__, packet
+# print 'Reconstructing...'
+# packet_bin = packet.to_bytes()
+# assert(data_packet == packet_bin)
+# print 'Reconstructed packet matches original'
+#
+#
+# control_packets_hex = [('13000001ae92b5574f849cd00001ac10'
+# '1f0300015cfe1cbd00200001ac101f01'),
+# ('28000001ae92b5574f849cd0000005a0'
+# '0120100000000001ac101f010064ff00'
+# '00070001d41ac503'),
+# ('30000003'
+# '3a59ec0af1f32f480001001469719fe8'
+# '91f6705e111e912200856438d38c1c96'
+# '000005a0031f100000000001254d3820'
+# '3264ff000005000157c3c44d0a64ff00'
+# '000500015f61535d3264ff0000050002'
+# '200109e085000b000000000000000001'
+# '000005a0031a100000000001254d3840'
+# '3264ff000005000157c3c44d0a64ff00'
+# '000500015f61535d3264ff0000050002'
+# '200109e085000b000000000000000001'
+# '000005a0031e100000000001254d3938'
+# '3264ff000005000157c3c44d0a64ff00'
+# '000500015f61535d3264ff0000050002'
+# '200109e085000b000000000000000001'),
+# ('40000003'
+# '3a59ec0af1f32f4800010014cfa7ab7f'
+# '3cfea95378ac6efbb0f548930944592d'
+# '000005a0031f100000000001254d3820'
+# '3264ff000005000157c3c44d0a64ff00'
+# '000500015f61535d3264ff0000050002'
+# '200109e085000b000000000000000001'
+# '000005a0031a100000000001254d3840'
+# '3264ff000005000157c3c44d0a64ff00'
+# '000500015f61535d3264ff0000050002'
+# '200109e085000b000000000000000001'
+# '000005a0031e100000000001254d3938'
+# '3264ff000005000157c3c44d0a64ff00'
+# '000500015f61535d3264ff0000050002'
+# '200109e085000b000000000000000001'),
+# ('80000000'
+# '6e000000004811402a0086400001ffff'
+# '000000000000000a2a02000000000000'
+# '0000000000000000'
+# '10f610f600487396'
+# '10000201ee924adef97a97d700000001'
+# '57c3c44d00015f61535d0002200109e0'
+# '85000b000000000000000001000f0002'
+# '2a020000000000000000000000000000')]
+#
+# key = 'pylisp-demonstration-key'
+#
+# for control_packet_hex in control_packets_hex:
+# print '----------------------------------------------------'
+# print 'Testing parsing and re-constructing a control packet'
+# print '----------------------------------------------------'
+#
+# control_packet = control_packet_hex.decode('hex')
+#
+# print 'Parsing...'
+# message = LISPControlPacket.from_bytes(control_packet)
+# print 'Parsed', message.__class__.__name__, message
+# print 'Reconstructing...'
+# message_bin = message.to_bytes()
+# assert(control_packet == message_bin)
+# print 'Reconstructed packet matches original'
+#
+# if hasattr(message, 'verify_authentication_data'):
+# print 'Verifying authentication data...'
+# assert(message.verify_authentication_data(key))
+# print 'Authentication data is correct'
+#
+# doctest.testmod(pylisp.packet.control.encapsulated_control_message,
+# verbose=True)
@@ -0,0 +1,2 @@
+import ip
+import lisp
@@ -0,0 +1,3 @@
+import ipv4
+import ipv6
+import udp
View
@@ -3,18 +3,21 @@
@author: sander
'''
-from bitstring import ConstBitStream, BitStream
from IPy import IP
+from bitstring import ConstBitStream, BitStream
+from pylisp.packet.ip import protocol_registry
from pylisp.utils import checksum
import math
+from pylisp.packet.ip.protocol import Protocol
-class IPv4Packet(object):
+class IPv4Packet(Protocol):
'''
Minimal IPv4 implementation to use in LISP Encapsulated Control Messages.
Options are not interpreted.
'''
+ header_type = 4
version = 4
def __init__(self, tos=0, identification=0, dont_fragment=False,
@@ -126,6 +129,10 @@ def from_bytes(cls, bitstream):
payload_bytes = (total_length) - (ihl * 4)
packet.payload = bitstream.read('bytes:%d' % payload_bytes)
+ payload_class = protocol_registry.get_type_class(packet.protocol)
+ if payload_class:
+ packet.payload = payload_class.from_bytes(packet.payload)
+
# There should be no remaining bits
if bitstream.pos != bitstream.len:
raise ValueError('Bits remaining after processing packet')
@@ -153,7 +160,8 @@ def to_bytes(self):
bitstream += BitStream('uint:8=%d' % self.tos)
# Write the total length
- total_length = 20 + len(self.payload)
+ payload_bytes = bytes(self.payload)
+ total_length = 20 + len(payload_bytes)
bitstream += BitStream('uint:16=%d' % total_length)
# Write the identification
@@ -190,9 +198,8 @@ def to_bytes(self):
my_checksum = checksum.ones_complement(bitstream.bytes)
bitstream[80:96] = BitStream('uint:16=%d' % my_checksum)
- # Determine payload
- payload = self.payload
- if hasattr(payload, 'to_bytes'):
- payload = payload.to_bytes()
+ return bitstream.bytes + payload_bytes
+
- return bitstream.bytes + payload
+# Register this header type
+protocol_registry.register_type_class(IPv4Packet)
Oops, something went wrong.

0 comments on commit 6036202

Please sign in to comment.