bitstream += BitStream('uint:13=%d' % self.fragment_offset) # Write the TTL bitstream += BitStream('uint:8=%d' % self.ttl) # Write the protocol number bitstream += BitStream('uint:8=%d' % self.protocol) # Write the header checksum as 0 for now, we calculate it later bitstream += BitStream('uint:16=0') # Write the source and destination addresses bitstream += BitStream('uint:32=%d, ' 'uint:32=%d' % (int(self.source), int(self.destination))) # Add the options bitstream += BitStream(bytes=self.options) padding_len = (4 - (len(self.options) % 4)) % 4 bitstream += BitStream(padding_len * 8) # Calculate the header checksum and fill it in my_checksum = checksum.ones_complement(bitstream.bytes) bitstream[80:96] = BitStream('uint:16=%d' % my_checksum) return bitstream.bytes + payload_bytes # Register this header type protocol_registry.register_type_class(IPv4Packet)
# Verify that the properties make sense packet.sanitize() return packet def to_bytes(self): ''' Create bytes from properties ''' # Verify that the properties make sense self.sanitize() # Write the source and destination ports bitstream = BitStream('uint:16=%d, ' 'uint:16=%d' % (self.source_port, self.destination_port)) # Write the length payload_bytes = bytes(self.payload) length = len(payload_bytes) + 8 bitstream += BitStream('uint:16=%d' % length) # Write the checksum bitstream += BitStream('uint:16=%d' % self.checksum) return bitstream.bytes + payload_bytes # Register this header type protocol_registry.register_type_class(UDPMessage)
# Verify that the properties make sense packet.sanitize() return packet def to_bytes(self): ''' Create bytes from properties ''' # Verify that the properties make sense self.sanitize() # Write the next header type bitstream = BitStream('uint:8=%d' % self.next_header) # Write the header length header_length_unpadded = len(self.options) + 2 header_length = math.ceil(header_length_unpadded / 8.0) bitstream += BitStream('uint:8=%d' % (header_length - 1)) # Add the options bitstream += BitStream(bytes=self.options) padding_len = (8 - (header_length_unpadded % 8)) % 8 bitstream += BitStream(padding_len * 8) return bitstream.bytes + bytes(self.payload) # Register this header type protocol_registry.register_type_class(IPv6HopByHopOptionsHeader)
""" Create bytes from properties """ # Verify that the properties make sense self.sanitize() # Write the next header type bitstream = BitStream("uint:8=%d" % self.next_header) # Write the header length header_length_unpadded = len(self.data) + 4 header_length = math.ceil(header_length_unpadded / 8.0) bitstream += BitStream("uint:8=%d" % (header_length - 1)) # Add the routing type bitstream += BitStream("uint:8=%d" % self.routing_type) # Add the segments left bitstream += BitStream("uint:8=%d" % self.segments_left) # Add the data bitstream += BitStream(bytes=self.data) padding_len = (8 - (header_length_unpadded % 8)) % 8 bitstream += BitStream(padding_len * 8) return bitstream.bytes + bytes(self.payload) # Register this header type protocol_registry.register_type_class(IPv6RoutingHeader)
def to_bytes(self): ''' Create bytes from properties ''' # Verify that the properties make sense self.sanitize() # Write the next header type bitstream = BitStream('uint:8=%d' % self.next_header) # Add the reserved bits bitstream += BitStream(8) # Add the fragment offset bitstream += BitStream('uint:13=%d' % self.fragment_offset) # Add the reserved bits bitstream += BitStream(2) # Add the flags bitstream += BitStream('bool=%d' % self.more_fragments) # Add the identification bitstream += BitStream('uint:32=%d' % self.identification) return bitstream.bytes + bytes(self.payload) # Register this header type protocol_registry.register_type_class(IPv6FragmentHeader)
# Verify that the properties make sense packet.sanitize() return packet def to_bytes(self): ''' Create bytes from properties ''' # Verify that the properties make sense self.sanitize() # Write the next header type bitstream = BitStream('uint:8=%d' % self.next_header) # Write the header length header_length_unpadded = len(self.options) + 2 header_length = math.ceil(header_length_unpadded / 8.0) bitstream += BitStream('uint:8=%d' % (header_length - 1)) # Add the options bitstream += BitStream(bytes=self.options) padding_len = (8 - (header_length_unpadded % 8)) % 8 bitstream += BitStream(padding_len * 8) return bitstream.bytes + bytes(self.payload) # Register this header type protocol_registry.register_type_class(IPv6DestinationOptionsHeader)
def from_bytes(cls, bitstream): packet = cls() # Convert to ConstBitStream (if not already provided) if not isinstance(bitstream, ConstBitStream): if isinstance(bitstream, Bits): bitstream = ConstBitStream(auto=bitstream) else: bitstream = ConstBitStream(bytes=bitstream) # Everything is payload remaining = bitstream[bitstream.pos:] packet.payload = remaining.bytes # Verify that the properties make sense packet.sanitize() return packet def to_bytes(self): ''' Create bytes from properties ''' # Verify that the properties make sense self.sanitize() return bytes(self.payload) # Register this header type protocol_registry.register_type_class(IPv6NoNextHeader)