Changeset 3088

Show
Ignore:
Timestamp:
07/05/08 16:15:14 (5 years ago)
Author:
getxsick
Message:

Updated IP class. Extended super class Protocol. Added new exception UMPAAttributeException.

Location:
branch/UMPA/umpa
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branch/UMPA/umpa/protocols/IP.py

    r3084 r3088  
    2121 
    2222import base 
     23from utils.my_exceptions import UMPAAttributeException 
     24 
     25class HVersion(base.Field): 
     26    def fillout(self): 
     27        pass 
     28 
     29class HIHL(base.Field): 
     30    def fillout(self): 
     31        pass 
     32 
     33class HTypeOfService(base.Field): 
     34    def fillout(self): 
     35        pass 
     36 
     37class HTotalLength(base.Field): 
     38    def fillout(self): 
     39        pass 
     40 
     41class HIdentification(base.Field): 
     42    def fillout(self): 
     43        pass 
     44 
     45class HFlags(base.Field): 
     46    def fillout(self): 
     47        pass 
     48 
     49class HFragmentOffset(base.Field): 
     50    def fillout(self): 
     51        pass 
     52 
     53class HTimeToLive(base.Field): 
     54    def fillout(self): 
     55        pass 
     56 
     57class HProtocol(base.Field): 
     58    def fillout(self): 
     59        pass 
     60 
     61class HHeaderChecksum(base.Field): 
     62    def fillout(self): 
     63        pass 
     64 
     65class HSourceAddress(base.Field): 
     66    def fillout(self): 
     67        pass 
     68 
     69class HDestinationAddress(base.Field): 
     70    def fillout(self): 
     71        pass 
     72 
     73class HOptions(base.Field): 
     74    def fillout(self): 
     75        pass 
     76 
     77class HPadding(base.Field): 
     78    def fillout(self): 
     79        pass 
     80 
     81# main IP class 
    2382 
    2483class IP(base.Protocol): 
     84    valid_fields = ['_version', '_ihl', 'type_of_service', '_total_length', 
     85                    'identification', 'flags', '_fragment_offset', 'time_to_live', 
     86                    'protocol', '_header_checksum', 'source_address', 
     87                    'destination_address', 'options', '_padding',] 
     88 
    2589    def __init__(self, **kw): 
     90        base.Protocol.__init__(kw) 
     91 
    2692        # attributes listed below shouldn't be modifed by user 
    2793        # they will be generated automatically 
    28         self._version = None 
    29         self._ihl = None 
    30         self._total_length = None 
    31         self._fragment_offset = None 
    32         self._header_checksum = None 
    33         self._padding = None 
     94        self._fields = [ HVersion(4, True), HIHL(4, True), 
     95                        HTypeOfService(8), HTotalLength(16, True), 
     96                        HIdentification(16, True), HFlags(3, True), 
     97                        HFragmentOffset(13, True), HTimeToLive(8), 
     98                        HProtocol(8, True), HHeaderChecksum(16, True), 
     99                        HSourceAddress(16), HDestinationAddress(16), 
     100                        HOptions(0), HPadding(0, True), ] 
    34101 
    35102        # setting up passed fields 
    36103        for field in kw: 
    37             setattr(self, field, kw[field]) 
     104            self.__setattr__(field, kw[field]) 
    38105 
    39     # fields 
    40106 
    41     def set_type_of_service(self, val): 
    42         pass 
    43     def get_type_of_service(self): 
    44         return self._type_of_service 
    45     type_of_service = property(get_type_of_service, set_type_of_service) 
     107    def _is_valid(self, name): 
     108        '''Check if attribute is allowed.''' 
     109        if name in valid_fields: 
     110            return True 
     111        return False 
    46112 
    47     def set_identification(self, val): 
    48         pass 
    49     def get_identification(self): 
    50         return self._identification 
    51     identification = property(get_identification, set_identification) 
     113    def __setattr__(self, attr, val): 
     114        '''Set value of the field.''' 
     115        if self._is_valid(attr): 
     116            self._fields[valid_fields.index(attr)].set(val) 
     117        else: 
     118            raise UMPAAttributeException, attr + ' not allowed' 
    52119 
    53     def set_time_to_live(self, val): 
    54         pass 
    55     def get_time_to_live(self): 
    56         return self._time_to_live 
    57     time_to_live = property(get_time_to_live, set_time_to_live) 
    58  
    59     def set_protocol(self, val): 
    60         pass 
    61     def get_protocol(self): 
    62         return self._protocol 
    63     protocol = property(get_protocol, set_protocol) 
    64  
    65     def set_source_address(self, val): 
    66         pass 
    67     def get_source_address(self): 
    68         return self._source_address 
    69     source_address = property(get_source_address, set_source_address) 
    70  
    71     def set_destination_address(self, val): 
    72         pass 
    73     def get_destination_address(self): 
    74         return self._destination_address 
    75     destination_address = property(get_destination_address, 
    76             set_destination_address) 
    77  
    78     def set_option(self, val): 
    79         pass 
    80     def get_option(self): 
    81         return self._option 
    82     option = property(get_option, set_option) 
     120    def __getattr__(self, attr): 
     121        '''Return value of the field.''' 
     122        if self._is_valid(attr): 
     123            return self._fields[valid_fields.index(attr)].get() 
     124        else: 
     125            raise UMPAAttributeException, attr + ' not allowed' 
  • branch/UMPA/umpa/protocols/base.py

    r3084 r3088  
    2222import utils 
    2323 
    24 #class Field: 
    25 #    def set(self, val): 
    26 #        if self.is_valid(val): 
    27 #            self.val = val 
    28 # 
    29 #    def is_valid(self, val): 
    30 #        return True 
     24class Field(object): 
     25    def __init__(self, bits, auto=False): 
     26        '''Set auto if you wish to take care about the field 
     27        by the library. Then you will have to write how 
     28        to manage the field. 
     29        ''' 
     30        self._auto = auto 
     31        self._bits = bits 
     32        self._value = None   # default value of the field 
    3133 
    32 class Protocol: 
    33     _valid_fields = [] 
     34    def set(self, value): 
     35        if self.is_valid(value): 
     36            self.value = value 
     37 
     38    def is_valid(self, val): 
     39        '''Should be overload by sub-classes. 
     40 
     41        Otherwise always return true. 
     42        ''' 
     43        return True 
     44 
     45class Protocol(object): 
     46    def __init__(self, **kw): 
     47        self._fields = [] 
    3448 
    3549    def set_fields(self, *args, **kwargs): 
     
    4155 
    4256        for key in kwargs: 
     57            if self._is_valid(key): 
     58                setattr(self, key, kwargs[key]) 
    4359            self.fields[key].set(kwargs[key]) 
    4460 
     
    4864        return False 
    4965 
    50     def set_flags(self, val): 
    51         pass 
     66    def _is_valid(self, field): 
     67        '''Overload it in subclasses.''' 
     68        raise NotImplementedError 
    5269 
    53     def get_flags(self): 
    54         return self._Flags 
    55  
    56     flags = property(get_flags, set_flags) 
     70    #def set_flags(self, val): 
     71    #    pass 
     72    #def get_flags(self): 
     73    #    return self._Flags 
     74    #flags = property(get_flags, set_flags) 
    5775 
    5876class Layer4(Protocol): 
  • branch/UMPA/umpa/utils/my_exceptions.py

    r3081 r3088  
    2525    def __str__(self): 
    2626        return repr(self.msg) 
     27 
     28class UMPAAttributeException(UMPAException): 
     29    pass