Changeset 3136

Show
Ignore:
Timestamp:
07/11/08 10:28:59 (5 years ago)
Author:
getxsick
Message:

Field class is not abstract class anymore.
We can use it for "simple fields". I mean that getting bit-representation is just a converting of value to big-endian representation.

Also there are some improvements to Flags class (based on the same issue described above).

Location:
branch/UMPA/umpa/protocols
Files:
2 modified

Legend:

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

    r3128 r3136  
    2121 
    2222import base 
     23 
     24from base import Field, Flags 
    2325from umpa.utils.my_exceptions import UMPAAttributeException 
    2426 
    25 class HVersion(base.Field): 
    26     def fillout(self): 
    27         pass 
    28  
    2927class HIHL(base.Field): 
    30     def fillout(self): 
    31         pass 
    32  
    33 class HTypeOfService(base.Field): 
    3428    def fillout(self): 
    3529        pass 
     
    4337        pass 
    4438 
    45 class HFlags(base.Flags): 
    46     def fillout(self): 
    47         pass 
    48  
    4939class HFragmentOffset(base.Field): 
    50     def fillout(self): 
    51         pass 
    52  
    53 class HTimeToLive(base.Field): 
    5440    def fillout(self): 
    5541        pass 
     
    7157        pass 
    7258 
    73 class HOptions(base.Field): 
    74     def fillout(self): 
    75         pass 
    76  
    7759class HPadding(base.Field): 
    7860    def fillout(self): 
     
    8365class IP(base.Protocol): 
    8466    _ordered_fields = ('_version', '_ihl', 'type_of_service', '_total_length', 
    85                         'identification', 'flags', '_fragment_offset', 
    86                         'time_to_live', 'protocol', '_header_checksum', 
     67                        '_identification', 'flags', '_fragment_offset', 
     68                        'time_to_live', '_protocol', '_header_checksum', 
    8769                        'source_address', 'destination_address', 'options', 
    8870                        '_padding',) 
     
    9173        base.Protocol.__init__(self, kw) 
    9274 
    93         fields_list = [ HVersion(4, True), HIHL(4, True), 
    94                         HTypeOfService(8), HTotalLength(16, True), 
    95                         HIdentification(16, True), HFlags(3, True), # add names to flags later 
    96                         HFragmentOffset(13, True), HTimeToLive(8), 
    97                         HProtocol(8, True), HHeaderChecksum(16, True), 
    98                         HSourceAddress(16), HDestinationAddress(16), 
    99                         HOptions(0), HPadding(0, True), ] 
     75        tos = ('presedence0','presedence1', 'presedence2', 'delay', 
     76                'throughput', 'relibility', 'reserved0', 'reserverd1') 
     77        flags = ('reserved', 'df', 'mf') 
     78 
     79        fields_list = [ Field(4, 4, auto=True), HIHL(4, auto=True), 
     80                        Flags(tos, auto=False), HTotalLength(16, auto=True), 
     81                        HIdentification(16, auto=True), 
     82                        Flags(flags, auto=False, reserved=0), 
     83                        HFragmentOffset(13, auto=True), Field(8, 255), 
     84                        HProtocol(8, auto=True), 
     85                        HHeaderChecksum(16, auto=True), 
     86                        Field(16), Field(16), Flags((), auto=True), 
     87                        HPadding(0, auto=True) ] 
    10088 
    10189        # we pack objects of header's fields to the dict 
  • branch/UMPA/umpa/protocols/base.py

    r3133 r3136  
    2323 
    2424class Field(object): 
    25     def __init__(self, bits, auto=False): 
     25    def __init__(self, bits, value=None, auto=False): 
    2626        """Set auto if you wish to take care about the field 
    2727        by the library. Then you will have to write how 
     
    3030        self._bits = bits 
    3131        self._auto = auto 
    32         self._value = None   # default value of the field 
     32        self._value = value 
    3333 
    3434    def set(self, value): 
     
    4848        return True 
    4949 
     50    def fillout(self): 
     51        print "Not implemented yet." 
     52        return False 
     53 
    5054class Flags(Field): 
    5155    """Most of protocols have a special field with bit-flags. 
     
    5357    """ 
    5458 
    55     def __init__(self, auto=False, *names): 
    56         Field.__init__(self, len(names), auto) 
     59    def __init__(self, names, auto=False, **preset): 
     60        """Names has to be in correct order. 
     61        If you use **preset, check if keys are in names list as well 
     62        because of order issue. 
     63        """ 
     64        Field.__init__(self, len(names), auto=auto) 
    5765 
    5866        self._ordered_fields = names 
     
    6169        false_list = [ False for i in xrange(self._bits) ] 
    6270        self._value = dict(zip(self._ordered_fields, false_list)) 
     71 
     72        # if preset exists then we update values 
     73        for name in preset: 
     74            if preset[name] == False: 
     75                self.set(name) 
     76            else: 
     77                self.unset(name) 
    6378 
    6479    def _is_valid(self, name):