daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 53 in 2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. """Header
  4.  
  5. """
  6.  
  7. import struct
  8. from enum import IntEnum
  9. from binascii import crc32
  10.  
  11. class Header:
  12.     # TODO: make it possible to add the checksum after the fact, as this may
  13.     # become necessary.
  14.     # TODO: add value/format errors for all of the header fields.
  15.  
  16.     def __init__(self,
  17.                 # NOTE: I chose for 32-bit seq/ack numbers, since you will
  18.                 # already overflow 16-bit numbers within a window size of 66.
  19.                 # This way you only have to bother with modulo calculations
  20.                 # when you receive acknowledgment packets.
  21.                 streamId=None,
  22.                 seqNr=None,
  23.                 ackNr=None,
  24.                 flags=None,
  25.                 window=None,
  26.                 dataLength=0,
  27.                 buffer_=None):
  28.         """Initialization of Header object.
  29.  
  30.        See specifiation of bTCP header in project description.
  31.  
  32.        Args:
  33.            streamId (int): identifies the stream of bTCP packets.
  34.            seqNr (int): the packet sequence number.
  35.            ackNr (int): the packet acknowledgement number.
  36.            flags (Flags): flags indicating the type of bTCP packet.
  37.            window (int): max amount of packets allowed to be in transit.
  38.            dataLength (int): length of packet payload without padding.
  39.            checksum (int): checksum of header and payload data.
  40.        """
  41.  
  42.         if streamId:
  43.             self.streamId = streamId
  44.             self.seqNr = seqNr
  45.             self.ackNr = ackNr
  46.             self.flags = flags
  47.             self.window = window
  48.             self.dataLength = dataLength
  49.             self.checksum = 0
  50.         elif buffer_:
  51.            
  52.             self._from_bytes(buffer_)
  53.        
  54.     def to_bytes(self):
  55.         """Convert Header to bytes.
  56.  
  57.        Returns:
  58.            (bytes): the header converted to a bytes object.
  59.        """
  60.         return struct.pack('!3I2BHI',
  61.                 self.streamId,
  62.                 self.seqNr,
  63.                 self.ackNr,
  64.                 self.flags,
  65.                 self.window,
  66.                 self.dataLength,
  67.                 self.checksum)
  68.  
  69.     def _from_bytes(cls, buffer_):
  70.         """Convert Packet in bytes containing header back to Header object.
  71.  
  72.        Args:
  73.            Buffer (bytes): the buffer to extract the header from.
  74.        """
  75.         if len(buffer_) < 20:
  76.             raise ValueError('Too little data left in buffer to make packet')
  77.  
  78.         headerBytes = buffer_[:20]
  79.         buffer_ = buffer_[20:]
  80.         self = cls(*struct.unpack_from('!3I2BHI', headerBytes))
  81.  
  82.     def set_checksum(self, payload):
  83.         # TODO: adapt this to actual crc, which may be different from the
  84.         # internet checksum.
  85.         """Compute the internet checksum and add it to the header.
  86.  
  87.        See rfc 1071 for the checksum algorithm.
  88.  
  89.        Args:
  90.            payload (bytes): the payload of the packet the header belongs to.
  91.        """
  92.         if payload != None:
  93.             data = self.to_bytes() + payload
  94.         else
  95.             data = self.to_bytes(
  96.         self.checksum = crc32(data)
  97.  
  98.     def check_checksum(self, data):
  99.         # TODO: adapt this to actual crc, which may be different from the
  100.         # internet checksum.
  101.         """Check wether the checksum of the packet in bytes is correct.
  102.  
  103.        It isn't strictly necessary for this function to be part of this class,
  104.        but it reads easier on higher abstraction levels.
  105.  
  106.        Args:
  107.            data (bytes): the packet in bytes.
  108.        """
  109.         return crc32(data) == 0xffffffff
  110.  
  111.  
  112. class Flags(IntEnum):
  113.     """bTCP packet type.
  114.    """
  115.  
  116.     SYN = 1
  117.     SYNACK = 3
  118.     ACK = 2
  119.     FIN = 4
  120.     FINACK = 6
  121.  
  122.     def __str__(self):
  123.         return self.name
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top