- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.2k
 
Add BPv7 and TCPCL layers #4824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            T-recks
  wants to merge
  18
  commits into
  secdev:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
T-recks:hdtn
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            18 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      7be9728
              
                initial import
              
              
                T-recks 91dd675
              
                fix typos and add author info
              
              
                T-recks 10392aa
              
                Merge branch 'master' into hdtn
              
              
                T-recks 56fb175
              
                add module info to dtn.common
              
              
                T-recks 79f3d4c
              
                fix whitespace
              
              
                T-recks d14c746
              
                more pep8 fixes
              
              
                T-recks 1121357
              
                revert pyproject changes
              
              
                T-recks 5e26d68
              
                translate bpv7 tests to uts
              
              
                T-recks fee5a51
              
                update comments
              
              
                T-recks 3371d84
              
                translate tcpcl tests to uts
              
              
                T-recks f736de6
              
                fix pep8 whitespace errors
              
              
                T-recks db3e983
              
                Merge branch 'master' into hdtn
              
              
                T-recks 9f525b6
              
                add spdx
              
              
                T-recks 700caae
              
                reformat with black
              
              
                T-recks 4a6cb57
              
                ignore E231 inside of string
              
              
                T-recks df5c0bf
              
                fix E501 line too long
              
              
                T-recks 7211647
              
                add contrib.dtn runtime deps
              
              
                T-recks 4812c62
              
                fix spell err
              
              
                T-recks File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,293 @@ | ||
| # SPDX-License-Identifier: GPL-2.0-only | ||
| # This file is part of Scapy | ||
| # See https://scapy.net/ for more information | ||
| 
     | 
||
| # scapy.contrib.description = Concise Binary Object Representation (CBOR) | ||
| # scapy.contrib.status = library | ||
| 
     | 
||
| """ | ||
| Concise Binary Object Representation (CBOR) utility | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 
     | 
||
| :authors: Timothy Recker, timothy.recker@nasa.gov | ||
| Tad Kollar, tad.kollar@nasa.gov | ||
| """ | ||
| 
     | 
||
| from scapy.fields import Field, BitField, BitEnumField, PacketField | ||
| from scapy.packet import Packet | ||
| from scapy.all import raw | ||
| import flynn | ||
| from typing import Tuple, List, Union | ||
| 
     | 
||
| MajorTypes = { | ||
| 0: "unsigned", | ||
| 1: "negative", | ||
| 2: "byte string", | ||
| 3: "text string", | ||
| 4: "array", | ||
| 5: "map", | ||
| 6: "tag", | ||
| 7: "simple/float", | ||
| } | ||
| 
     | 
||
| 
     | 
||
| class MajorTypeException(Exception): | ||
| """This exception indicates that a CBOR object has an unexpected | ||
| value for its Major Type. | ||
| 
     | 
||
| Attributes: | ||
| actual -- the integer value of the actual Major Type | ||
| expected -- an integer or list indicating the acceptable Major Type values | ||
| """ | ||
| 
     | 
||
| def __init__(self, actual: int, expected: Union[int, List[int]]): | ||
| 
     | 
||
| message = f"[Error] Major type {actual} does not refer to a(n)" | ||
| if isinstance(expected, int): | ||
| typ = MajorTypes[expected] | ||
| message += f" {typ}." | ||
| else: | ||
| typ = MajorTypes[expected[0]] | ||
| message += f" {typ}" | ||
| for val in expected[1:]: | ||
| typ = MajorTypes[val] | ||
| message += f" or {typ}" | ||
| message += "." | ||
| super().__init__(message) | ||
| 
     | 
||
| 
     | 
||
| class StopCodeException(Exception): | ||
| def __init__(self, value): | ||
| super().__init__(f"[Error] Major type {value} does not refer to a stop code.") | ||
| 
     | 
||
| 
     | 
||
| class AdditionalInfoException(Exception): | ||
| """This exception indicates that a CBOR object has an unexpected value for | ||
| its Additional Info.""" | ||
| 
     | 
||
| def __init__(self): | ||
| super().__init__("[Error] Invalid additional info.") | ||
| 
     | 
||
| 
     | 
||
| class UnhandledTypeException(Exception): | ||
| def __init__(self, value, cls): | ||
| super().__init__(f"[Error] Major type {value} is not handled by {cls}.") | ||
| 
     | 
||
| 
     | 
||
| # CBOR definitions | ||
| class CBORNull(Field): | ||
| """This class exists so that it can be used in a MultipleTypeField containing CBOR | ||
| values. Every option given to a MultipleTypeField must be a field with at least a | ||
| name. Thus, if one of the MultipleType options should be that no field is present, | ||
| you need a field that produces no bytes when added to the packet. | ||
| CBORNull can serve this purpose.""" | ||
| 
     | 
||
| 
     | 
||
| class CBORBase(Field): | ||
| @staticmethod | ||
| def static_get_head_info(b): | ||
| if len(b) == 0: | ||
| return None, None | ||
| 
     | 
||
| head = b[0] | ||
| major_type = head >> 5 | ||
| add_info = head & 0b00011111 | ||
| 
     | 
||
| return major_type, add_info | ||
| 
     | 
||
| def get_head_info(self, b): | ||
| return CBORBase.static_get_head_info(b) | ||
| 
     | 
||
| def addfield(self, pkt, s, val): | ||
| return s + flynn.dumps(val) | ||
| 
     | 
||
| 
     | 
||
| class CBORInteger(CBORBase): | ||
| @staticmethod | ||
| def get_value(add_info, b): | ||
| if add_info < 24: | ||
| val_length = 1 # 1 byte head, argument=add_info | ||
| val = add_info | ||
| elif add_info == 24: | ||
| val_length = 2 # 1 byte head + 1 byte argument | ||
| val = b[1] | ||
| elif add_info == 25: | ||
| val_length = 3 # 1 byte head + 2 byte argument | ||
| val = int.from_bytes(b[1:3], byteorder="big") | ||
| elif add_info == 26: | ||
| val_length = 5 # 4 byte argument | ||
| val = int.from_bytes(b[1:5], byteorder="big") | ||
| elif add_info == 27: | ||
| val_length = 9 # 8 byte argument | ||
| val = int.from_bytes(b[1:9], byteorder="big") | ||
| else: | ||
| raise AdditionalInfoException() | ||
| 
     | 
||
| return b[val_length:], val | ||
| 
     | 
||
| def getfield(self, pkt, s): | ||
| major_type, add_info = self.get_head_info(s) | ||
| 
     | 
||
| if major_type != 0: | ||
| raise MajorTypeException(major_type, 0) | ||
| 
     | 
||
| return CBORInteger.get_value(add_info, s) | ||
| 
     | 
||
| 
     | 
||
| class CBORStringBase(CBORBase): | ||
| @staticmethod | ||
| def get_value(add_info, b): | ||
| if add_info < 24: | ||
| arg_size = 0 | ||
| data_size = add_info # argument = data size = additional info | ||
| else: | ||
| if add_info == 24: | ||
| arg_size = 1 | ||
| elif add_info == 25: | ||
| arg_size = 2 | ||
| elif add_info == 26: | ||
| arg_size = 4 | ||
| elif add_info == 27: | ||
| arg_size = 8 | ||
| else: | ||
| raise AdditionalInfoException() | ||
| 
     | 
||
| # size of argument is known now, so | ||
| # get value of the argument, which contains the size of the data | ||
| data_size = int.from_bytes(b[1 : 1 + arg_size], byteorder="big") | ||
| val_length = 1 + arg_size + data_size | ||
| val = b[1 + arg_size : val_length] | ||
| 
     | 
||
| return b[val_length:], val | ||
| 
     | 
||
| 
     | 
||
| class CBORByteString(CBORStringBase): | ||
| def getfield(self, pkt, s): | ||
| major_type, add_info = self.get_head_info(s) | ||
| 
     | 
||
| if major_type != 2: | ||
| raise MajorTypeException(major_type, 2) | ||
| 
     | 
||
| return CBORStringBase.get_value(add_info, s) | ||
| 
     | 
||
| 
     | 
||
| class CBORTextString(CBORStringBase): | ||
| def getfield(self, pkt, s): | ||
| major_type, add_info = self.get_head_info(s) | ||
| 
     | 
||
| if major_type != 3: | ||
| raise MajorTypeException(major_type, 3) | ||
| 
     | 
||
| return CBORStringBase.get_value(add_info, s) | ||
| 
     | 
||
| 
     | 
||
| class CBORIntOrText(CBORBase): | ||
| def getfield(self, pkt, s): | ||
| major_type, add_info = self.get_head_info(s) | ||
| 
     | 
||
| if major_type == 0: | ||
| return CBORInteger.get_value(add_info, s) | ||
| if major_type == 3: | ||
| return CBORStringBase.get_value(add_info, s) | ||
| 
     | 
||
| raise MajorTypeException(major_type, [0, 3]) | ||
| 
     | 
||
| 
     | 
||
| class CBORStopCode(CBORBase): | ||
| def addfield(self, pkt, s, val): | ||
| return s + b"\xff" | ||
| 
     | 
||
| @staticmethod | ||
| def get_value(add_info, b): | ||
| return b[1:], add_info | ||
| 
     | 
||
| def getfield(self, pkt, s): | ||
| major_type, add_info = self.get_head_info(s) | ||
| if major_type != 7: | ||
| raise StopCodeException(major_type) | ||
| 
     | 
||
| return CBORStopCode.get_value(add_info, s) | ||
| 
     | 
||
| 
     | 
||
| class CBORAny(CBORBase): | ||
| def getfield(self, pkt, s): | ||
| major_type, add_info = self.get_head_info(s) | ||
| 
     | 
||
| if major_type == 0: | ||
| return CBORInteger.get_value(add_info, s) | ||
| if major_type in [2, 3]: | ||
| return CBORStringBase.get_value(add_info, s) | ||
| if major_type == 7: | ||
| return CBORStopCode.get_value(add_info, s) | ||
| 
     | 
||
| raise UnhandledTypeException(major_type, CBORAny) | ||
| 
     | 
||
| 
     | 
||
| class CBORArray(Packet): | ||
| _major_type = BitEnumField("major_type", 4, 3, MajorTypes) | ||
| _add = BitField("add", 0, 5) # additional information = length | ||
| 
     | 
||
| # head fields | ||
| fields_desc = [_major_type, _add] | ||
| 
     | 
||
| def count_additional_fields(self) -> int: | ||
| """ | ||
| Return the number of fields other than the two head fields. This method does | ||
| not work correctly with ConditionalFields and should be overridden when that | ||
| field type is in use. | ||
| """ | ||
| head_field_count = 2 | ||
| return len(self.default_fields) - head_field_count | ||
| 
     | 
||
| def set_additional_fields(self, pkt: Packet) -> bytes: | ||
| """For an array, the add field is set to the number of elements minus | ||
| the two head fields.""" | ||
| # pylint: disable=W0201 | ||
| # field (instance variable) initialization is handled via "fields_desc" | ||
| self.add = self.count_additional_fields() | ||
| head = (self.major_type << 5) | self.add | ||
| 
     | 
||
| return head.to_bytes(1, "big") + pkt[1:] | ||
| 
     | 
||
| def post_build(self, pkt: bytes, pay: bytes) -> bytes: | ||
| return self.set_additional_fields(pkt) + pay | ||
| 
     | 
||
| def extract_padding(self, s): | ||
| return "", s | ||
| 
     | 
||
| 
     | 
||
| class CBORPacketField(PacketField): | ||
| def i2m(self, pkt: Packet, i) -> bytes: | ||
| if i is None: | ||
| return b"" | ||
| 
     | 
||
| return flynn.dumps(raw(i)) | ||
| 
     | 
||
| def m2i(self, pkt: Packet, m): | ||
| _, add_info = CBORBase.static_get_head_info(m) | ||
| remain, decoded_m = CBORStringBase.get_value(add_info, m) | ||
| try: | ||
| # we want to set parent wherever possible | ||
| return self.cls(decoded_m + remain, _parent=pkt) # type: ignore | ||
| except TypeError: | ||
| return self.cls(decoded_m + remain) | ||
| 
     | 
||
| 
     | 
||
| class CBORPacketFieldWithRemain(CBORPacketField): | ||
| """ | ||
| The regular Packet.getfield() never returns the remaining bytes, so the CRC or | ||
| other following fields get lost. This getfield does return the remaining bytes. | ||
| """ | ||
| 
     | 
||
| def m2i(self, pkt: Packet, m): | ||
| _, add_info = CBORBase.static_get_head_info(m) | ||
| remain, decoded_m = CBORStringBase.get_value(add_info, m) | ||
| try: | ||
| # we want to set parent wherever possible | ||
| return remain, self.cls(decoded_m + remain, _parent=pkt) # type: ignore | ||
| except TypeError: | ||
| return remain, self.cls(decoded_m + remain) | ||
| 
     | 
||
| def getfield(self, pkt: Packet, s: bytes) -> Tuple[bytes, Packet]: | ||
| remain, i = self.m2i(pkt, s) | ||
| return remain, i | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # SPDX-License-Identifier: GPL-2.0-only | ||
| # This file is part of Scapy | ||
| # See https://scapy.net/ for more information | ||
| 
     | 
||
| # scapy.contrib.description = utility functions and classes for DTN module | ||
| # scapy.contrib.status = library | ||
| 
     | 
||
| from scapy.packet import Packet, Raw | ||
| from scapy.fields import Field | ||
| from typing import Dict, List | ||
| 
     | 
||
| 
     | 
||
| class NoPayloadPacket(Packet): | ||
| """A packet with no payload layer to bind.""" | ||
| 
     | 
||
| def extract_padding(self, s): | ||
| return "", s | ||
| 
     | 
||
| def post_dissect(self, s): | ||
| try: | ||
| if self[Raw].load is not None: | ||
| raise ValueError(f"found payload in {Packet} when none was expected") | ||
| except IndexError: # No Raw layer found, i.e. no unparsed payload is present | ||
| pass | ||
| return s | ||
| 
     | 
||
| 
     | 
||
| class ControlPacket(NoPayloadPacket): | ||
| """A packet containing control data, rather than user data.""" | ||
| 
     | 
||
| 
     | 
||
| class FieldPacket(NoPayloadPacket): | ||
| """A packet intended for use as a field (i.e. PacketField or PacketListField) | ||
| in another Packet, rather than one sent or received on the wire. Useful when you | ||
| need heterogeneous, compound data similar to a record/struct within | ||
| another Packet.""" | ||
| 
     | 
||
| 
     | 
||
| FieldsTemplate = Dict[str, Field] | ||
| 
     | 
||
| 
     | 
||
| def template_replace( | ||
| template: FieldsTemplate, new_values: FieldsTemplate | ||
| ) -> FieldsTemplate: | ||
| return {**template, **new_values} | ||
| 
     | 
||
| 
     | 
||
| def make_fields_desc(template: FieldsTemplate) -> List[Field]: | ||
| return list(template.values()) | 
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, I think this could be worked around