<s> """<STR_LIT>""" <EOL> import struct , copy , socket <EOL> from . config import conf <EOL> from . volatile import * <EOL> from . data import * <EOL> from . utils import * <EOL> from . base_classes import BasePacket , Gen , Net <EOL> class Field : <EOL> """<STR_LIT>""" <EOL> islist = <NUM_LIT:0> <EOL> holds_packets = <NUM_LIT:0> <EOL> def __init__ ( self , name , default , fmt = "<STR_LIT:H>" ) : <EOL> self . name = name <EOL> if fmt [ <NUM_LIT:0> ] in "<STR_LIT>" : <EOL> self . fmt = fmt <EOL> else : <EOL> self . fmt = "<STR_LIT:!>" + fmt <EOL> self . default = self . any2i ( None , default ) <EOL> self . sz = struct . calcsize ( self . fmt ) <EOL> self . owners = [ ] <EOL> def register_owner ( self , cls ) : <EOL> self . owners . append ( cls ) <EOL> def i2len ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> return self . sz <EOL> def i2count ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> <EOL> def i2b ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> if type ( x ) is str : <EOL> x = bytes ( [ ord ( i ) for i in x ] ) <EOL> return x <EOL> def i2dict ( self , pkt , x ) : <EOL> return { self . name : x } <EOL> def h2i ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> if type ( x ) is str : <EOL> x = bytes ( [ ord ( i ) for i in x ] ) <EOL> return x <EOL> def i2h ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> return x <EOL> def m2i ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> return x <EOL> def i2m ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> if x is None : <EOL> x = <NUM_LIT:0> <EOL> return x <EOL> def any2i ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> return self . h2i ( pkt , x ) <EOL> def i2repr ( self , pkt , x ) : <EOL> """<STR_LIT>""" <EOL> return repr ( self . i2h ( pkt , x ) ) <EOL> def addfield ( self , pkt , s , val ) : <EOL> """<STR_LIT>""" <EOL> return s + struct . pack ( self . fmt , self . i2m ( pkt , val ) ) <EOL> def getfield ( self , pkt , s ) : <EOL> """<STR_LIT>""" <EOL> return s [ self . sz : ] , self . m2i ( pkt , struct . unpack ( self . fmt , s [ : self . sz ] ) [ <NUM_LIT:0> ] ) <EOL> def do_copy ( self , x ) : <EOL> if hasattr ( x , "<STR_LIT>" ) : <EOL> return x . copy ( ) <EOL> if type ( x ) is list : <EOL> x = x [ : ] <EOL> for i in range ( len ( x ) ) : <EOL> if isinstance ( x [ i ] , BasePacket ) : <EOL> x [ i ] = x [ i ] . copy ( ) <EOL> return x <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( "<STR_LIT:U+002C>" . join ( x . __name__ for x in self . owners ) , self . name ) <EOL> def copy ( self ) : <EOL> return copy . deepcopy ( self ) <EOL> def randval ( self ) : <EOL> """<STR_LIT>""" <EOL> fmtt = self . fmt [ - <NUM_LIT:1> ] <EOL> if fmtt in "<STR_LIT>" : <EOL> return { "<STR_LIT:B>" : RandByte , "<STR_LIT:H>" : RandShort , "<STR_LIT:I>" : RandInt , "<STR_LIT>" : RandLong } [ fmtt ] ( ) <EOL> elif fmtt == "<STR_LIT:s>" : <EOL> if self . fmt [ <NUM_LIT:0> ] in "<STR_LIT>" : <EOL> l = int ( self . fmt [ : - <NUM_LIT:1> ] ) <EOL> else : <EOL> l = int ( self . fmt [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> return RandBin ( l ) <EOL> else : <EOL> warning ( "<STR_LIT>" % ( self . name , self . fmt ) ) <EOL> class Emph : <EOL> fld = b"<STR_LIT>" <EOL> def __init__ ( self , fld ) : <EOL> self . fld = fld <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . fld , attr ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . fld ) <EOL> def __eq__ ( self , other ) : <EOL> return self . fld == other <EOL> class ActionField : <EOL> _fld = None <EOL> def __init__ ( self , fld , action_method , ** kargs ) : <EOL> self . _fld = fld <EOL> self . _action_method = action_method <EOL> self . _privdata = kargs <EOL> def any2i ( self , pkt , val ) : <EOL> getattr ( pkt , self . _action_method ) ( val , self . _fld , ** self . _privdata ) <EOL> return getattr ( self . _fld , "<STR_LIT>" ) ( pkt , val ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . _fld , attr ) <EOL> class ConditionalField : <EOL> fld = None <EOL> def __init__ ( self , fld , cond ) : <EOL> self . fld = fld <EOL> self . cond = cond <EOL> def _evalcond ( self , pkt ) : <EOL> return self . cond ( pkt ) <EOL> def getfield ( self , pkt , s ) : <EOL> if self . _evalcond ( pkt ) : <EOL> return self . fld . getfield ( pkt , s ) <EOL> else : <EOL> return s , None <EOL> def addfield ( self , pkt , s , val ) : <EOL> if self . _evalcond ( pkt ) : <EOL> return self . fld . addfield ( pkt , s , val ) <EOL> else : <EOL> return s <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . fld , attr ) <EOL> class PadField : <EOL> """<STR_LIT>""" <EOL> _fld = None <EOL> def __init__ ( self , fld , align , padwith = None ) : <EOL> self . _fld = fld <EOL> self . _align = align <EOL> self . _padwith = padwith or b"<STR_LIT>" <EOL> def padlen ( self , flen ) : <EOL> return - flen % self . _align <EOL> def getfield ( self , pkt , s ) : <EOL> remain , val = self . _fld . getfield ( pkt , s ) <EOL> padlen = self . padlen ( len ( s ) - len ( remain ) ) <EOL> return remain [ padlen : ] , val <EOL> def addfield ( self , pkt , s , val ) : <EOL> sval = self . _fld . addfield ( pkt , b"<STR_LIT>" , val ) <EOL> return s + sval + struct . pack ( "<STR_LIT>" % ( self . padlen ( len ( sval ) ) ) , self . _padwith ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . _fld , attr ) <EOL> class MACField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> def i2m ( self , pkt , x ) : <EOL> if x is None : <EOL> return b"<STR_LIT>" <EOL> return mac2str ( x ) <EOL> def m2i ( self , pkt , x ) : <EOL> return str2mac ( x ) <EOL> def any2i ( self , pkt , x ) : <EOL> if type ( x ) is bytes and len ( x ) is <NUM_LIT:6> : <EOL> x = self . m2i ( pkt , x ) <EOL> return x <EOL> def i2repr ( self , pkt , x ) : <EOL> x = self . i2h ( pkt , x ) <EOL> if self in conf . resolve : <EOL> x = conf . manufdb . _resolve_MAC ( x ) <EOL> return x <EOL> def randval ( self ) : <EOL> return RandMAC ( ) <EOL> class IPField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> def h2i ( self , pkt , x ) : <EOL> if type ( x ) is str : <EOL> try : <EOL> inet_aton ( x ) <EOL> except socket . error : <EOL> x = Net ( x ) <EOL> elif type ( x ) is list : <EOL> x = [ self . h2i ( pkt , n ) for n in x ] <EOL> return x <EOL> def resolve ( self , x ) : <EOL> if self in conf . resolve : <EOL> try : <EOL> ret = socket . gethostbyaddr ( x ) [ <NUM_LIT:0> ] <EOL> except : <EOL> pass <EOL> else : <EOL> if ret : <EOL> return ret <EOL> return x <EOL> def i2m ( self , pkt , x ) : <EOL> return inet_aton ( x ) <EOL> def m2i ( self , pkt , x ) : <EOL> return inet_ntoa ( x ) <EOL> def any2i ( self , pkt , x ) : <EOL> return self . h2i ( pkt , x ) <EOL> def i2repr ( self , pkt , x ) : <EOL> return self . resolve ( self . i2h ( pkt , x ) ) <EOL> def randval ( self ) : <EOL> return RandIP ( ) <EOL> class SourceIPField ( IPField ) : <EOL> def __init__ ( self , name , dstname ) : <EOL> IPField . __init__ ( self , name , None ) <EOL> self . dstname = dstname <EOL> def i2m ( self , pkt , x ) : <EOL> if x is None : <EOL> iff , x , gw = pkt . route ( ) <EOL> if x is None : <EOL> x = "<STR_LIT>" <EOL> return IPField . i2m ( self , pkt , x ) <EOL> def i2h ( self , pkt , x ) : <EOL> if x is None : <EOL> dst = getattr ( pkt , self . dstname ) <EOL> if isinstance ( dst , Gen ) : <EOL> r = [ conf . route . route ( i ) for i in dst ] <EOL> r . sort ( ) <EOL> if r [ <NUM_LIT:0> ] != r [ - <NUM_LIT:1> ] : <EOL> warning ( "<STR_LIT>" % repr ( dst ) ) <EOL> iff , x , gw = r [ <NUM_LIT:0> ] <EOL> else : <EOL> iff , x , gw = conf . route . route ( dst ) <EOL> return IPField . i2h ( self , pkt , x ) <EOL> class ByteField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:B>" ) <EOL> class SignedByteField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:b>" ) <EOL> class XByteField ( ByteField ) : <EOL> def i2repr ( self , pkt , x ) : <EOL> return lhex ( self . i2h ( pkt , x ) ) <EOL> class OByteField ( ByteField ) : <EOL> def i2repr ( self , pkt , x ) : <EOL> return "<STR_LIT>" % self . i2h ( pkt , x ) <EOL> class X3BytesField ( XByteField ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> def addfield ( self , pkt , s , val ) : <EOL> return s + struct . pack ( self . fmt , self . i2m ( pkt , val ) ) [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> def getfield ( self , pkt , s ) : <EOL> return s [ <NUM_LIT:3> : ] , self . m2i ( pkt , struct . unpack ( self . fmt , b"<STR_LIT:\x00>" + s [ : <NUM_LIT:3> ] ) [ <NUM_LIT:0> ] ) <EOL> class ShortField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:H>" ) <EOL> class LEShortField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> class XShortField ( ShortField ) : <EOL> def i2repr ( self , pkt , x ) : <EOL> return lhex ( self . i2h ( pkt , x ) ) <EOL> class IntField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:I>" ) <EOL> class SignedIntField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:i>" ) <EOL> def randval ( self ) : <EOL> return RandSInt ( ) <EOL> class LEIntField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> class LESignedIntField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> def randval ( self ) : <EOL> return RandSInt ( ) <EOL> class XIntField ( IntField ) : <EOL> def i2repr ( self , pkt , x ) : <EOL> return lhex ( self . i2h ( pkt , x ) ) <EOL> class LongField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> class LELongField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> class XLongField ( LongField ) : <EOL> def i2repr ( self , pkt , x ) : <EOL> return lhex ( self . i2h ( pkt , x ) ) <EOL> class IEEEFloatField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:f>" ) <EOL> class IEEEDoubleField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT:d>" ) <EOL> class StrField ( Field ) : <EOL> def __init__ ( self , name , default , fmt = "<STR_LIT:H>" , remain = <NUM_LIT:0> ) : <EOL> Field . __init__ ( self , name , default , fmt ) <EOL> self . remain = remain <EOL> def i2repr ( self , pkt , x ) : <EOL> try : <EOL> if type ( x ) is bytes : <EOL> x = x . decode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> return repr ( x ) <EOL> def i2len ( self , pkt , i ) : <EOL> return len ( i ) <EOL> def i2m ( self , pkt , x ) : <EOL> if x is None : <EOL> x = b"<STR_LIT>" <EOL> elif type ( x ) is not bytes : <EOL> x = str ( x ) . encode ( '<STR_LIT:ascii>' ) <EOL> return x <EOL> def addfield ( self , pkt , s , val ) : <EOL> return s + self . i2m ( pkt , val ) <EOL> def getfield ( self , pkt , s ) : <EOL> if self . remain == <NUM_LIT:0> : <EOL> return b"<STR_LIT>" , self . m2i ( pkt , s ) <EOL> else : <EOL> return s [ - self . remain : ] , self . m2i ( pkt , s [ : - self . remain ] ) <EOL> def randval ( self ) : <EOL> return RandBin ( RandNum ( <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> class PacketField ( StrField ) : <EOL> holds_packets = <NUM_LIT:1> <EOL> def __init__ ( self , name , default , cls , remain = <NUM_LIT:0> ) : <EOL> StrField . __init__ ( self , name , default , remain = remain ) <EOL> self . cls = cls <EOL> def i2m ( self , pkt , i ) : <EOL> return bytes ( i ) <EOL> def m2i ( self , pkt , m ) : <EOL> return self . cls ( m ) <EOL> def getfield ( self , pkt , s ) : <EOL> i = self . m2i ( pkt , s ) <EOL> remain = b"<STR_LIT>" <EOL> if conf . padding_layer in i : <EOL> r = i [ conf . padding_layer ] <EOL> del ( r . underlayer . payload ) <EOL> remain = r . load <EOL> return remain , i <EOL> class PacketLenField ( PacketField ) : <EOL> holds_packets = <NUM_LIT:1> <EOL> def __init__ ( self , name , default , cls , length_from = None ) : <EOL> PacketField . __init__ ( self , name , default , cls ) <EOL> self . length_from = length_from <EOL> def getfield ( self , pkt , s ) : <EOL> l = self . length_from ( pkt ) <EOL> try : <EOL> i = self . m2i ( pkt , s [ : l ] ) <EOL> except Exception : <EOL> if conf . debug_dissector : <EOL> raise <EOL> i = conf . raw_layer ( load = s [ : l ] ) <EOL> return s [ l : ] , i <EOL> class PacketListField ( PacketField ) : <EOL> islist = <NUM_LIT:1> <EOL> holds_packets = <NUM_LIT:1> <EOL> def __init__ ( self , name , default , cls , count_from = None , length_from = None ) : <EOL> if default is None : <EOL> default = [ ] <EOL> PacketField . __init__ ( self , name , default , cls ) <EOL> self . count_from = count_from <EOL> self . length_from = length_from <EOL> def any2i ( self , pkt , x ) : <EOL> if type ( x ) is not list : <EOL> return [ x ] <EOL> else : <EOL> return x <EOL> def i2count ( self , pkt , val ) : <EOL> if type ( val ) is list : <EOL> return len ( val ) <EOL> return <NUM_LIT:1> <EOL> def i2len ( self , pkt , val ) : <EOL> return sum ( len ( p ) for p in val ) <EOL> def do_copy ( self , x ) : <EOL> return [ i . copy ( ) for i in x ] <EOL> def getfield ( self , pkt , s ) : <EOL> c = l = None <EOL> if self . length_from is not None : <EOL> l = self . length_from ( pkt ) <EOL> elif self . count_from is not None : <EOL> c = self . count_from ( pkt ) <EOL> lst = [ ] <EOL> ret = b"<STR_LIT>" <EOL> remain = s <EOL> if l is not None : <EOL> remain , ret = s [ : l ] , s [ l : ] <EOL> while remain : <EOL> if c is not None : <EOL> if c <= <NUM_LIT:0> : <EOL> break <EOL> c -= <NUM_LIT:1> <EOL> try : <EOL> p = self . m2i ( pkt , remain ) <EOL> except Exception : <EOL> if conf . debug_dissector : <EOL> raise <EOL> p = conf . raw_layer ( load = remain ) <EOL> remain = b"<STR_LIT>" <EOL> else : <EOL> if conf . padding_layer in p : <EOL> pad = p [ conf . padding_layer ] <EOL> remain = pad . load <EOL> del ( pad . underlayer . payload ) <EOL> else : <EOL> remain = b"<STR_LIT>" <EOL> lst . append ( p ) <EOL> return remain + ret , lst <EOL> def addfield ( self , pkt , s , val ) : <EOL> return s + b"<STR_LIT>" . join ( [ bytes ( i ) for i in val ] ) <EOL> class StrFixedLenField ( StrField ) : <EOL> def __init__ ( self , name , default , length = None , length_from = None ) : <EOL> StrField . __init__ ( self , name , default ) <EOL> self . length_from = length_from <EOL> if length is not None : <EOL> self . length_from = lambda pkt , length = length : length <EOL> def i2repr ( self , pkt , v ) : <EOL> if type ( v ) is bytes : <EOL> v = v . rstrip ( b"<STR_LIT>" ) <EOL> return repr ( v ) <EOL> def getfield ( self , pkt , s ) : <EOL> l = self . length_from ( pkt ) <EOL> return s [ l : ] , self . m2i ( pkt , s [ : l ] ) <EOL> def addfield ( self , pkt , s , val ) : <EOL> l = self . length_from ( pkt ) <EOL> return s + struct . pack ( "<STR_LIT>" % l , self . i2m ( pkt , val ) ) <EOL> def randval ( self ) : <EOL> try : <EOL> l = self . length_from ( None ) <EOL> except : <EOL> l = RandNum ( <NUM_LIT:0> , <NUM_LIT:200> ) <EOL> return RandBin ( l ) <EOL> class StrFixedLenEnumField ( StrFixedLenField ) : <EOL> def __init__ ( self , name , default , length = None , enum = None , length_from = None ) : <EOL> StrFixedLenField . __init__ ( self , name , default , length = length , length_from = length_from ) <EOL> self . enum = enum <EOL> def i2repr ( self , pkt , v ) : <EOL> r = v . rstrip ( "<STR_LIT>" ) <EOL> rr = repr ( r ) <EOL> if v in self . enum : <EOL> rr = "<STR_LIT>" % ( rr , self . enum [ v ] ) <EOL> elif r in self . enum : <EOL> rr = "<STR_LIT>" % ( rr , self . enum [ r ] ) <EOL> return rr <EOL> class NetBIOSNameField ( StrFixedLenField ) : <EOL> def __init__ ( self , name , default , length = <NUM_LIT> ) : <EOL> StrFixedLenField . __init__ ( self , name , default , length ) <EOL> def i2m ( self , pkt , x ) : <EOL> l = self . length_from ( pkt ) // <NUM_LIT:2> <EOL> if x is None : <EOL> x = b"<STR_LIT>" <EOL> x += b"<STR_LIT:U+0020>" * ( l ) <EOL> x = x [ : l ] <EOL> x = b"<STR_LIT>" . join ( [ bytes ( [ <NUM_LIT> + ( i >> <NUM_LIT:4> ) , <NUM_LIT> + ( i & <NUM_LIT> ) ] ) for i in x ] ) <EOL> x = b"<STR_LIT:U+0020>" + x <EOL> return x <EOL> def m2i ( self , pkt , x ) : <EOL> x = x . strip ( b"<STR_LIT:\x00>" ) . strip ( b"<STR_LIT:U+0020>" ) <EOL> return b"<STR_LIT>" . join ( map ( lambda x , y : bytes ( [ ( ( ( x - <NUM_LIT:1> ) & <NUM_LIT> ) << <NUM_LIT:4> ) + ( ( y - <NUM_LIT:1> ) & <NUM_LIT> ) ] ) , x [ : : <NUM_LIT:2> ] , x [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) ) <EOL> class StrLenField ( StrField ) : <EOL> def __init__ ( self , name , default , fld = None , length_from = None ) : <EOL> StrField . __init__ ( self , name , default ) <EOL> self . length_from = length_from <EOL> def getfield ( self , pkt , s ) : <EOL> l = self . length_from ( pkt ) <EOL> return s [ l : ] , self . m2i ( pkt , s [ : l ] ) <EOL> class FieldListField ( Field ) : <EOL> islist = <NUM_LIT:1> <EOL> def __init__ ( self , name , default , field , length_from = None , count_from = None ) : <EOL> if default is None : <EOL> default = [ ] <EOL> Field . __init__ ( self , name , default ) <EOL> self . count_from = count_from <EOL> self . length_from = length_from <EOL> self . field = field <EOL> def i2count ( self , pkt , val ) : <EOL> if type ( val ) is list : <EOL> return len ( val ) <EOL> return <NUM_LIT:1> <EOL> def i2len ( self , pkt , val ) : <EOL> return sum ( self . field . i2len ( pkt , v ) for v in val ) <EOL> def i2m ( self , pkt , val ) : <EOL> if val is None : <EOL> val = [ ] <EOL> return val <EOL> def any2i ( self , pkt , x ) : <EOL> if type ( x ) is not list : <EOL> return [ x ] <EOL> else : <EOL> return x <EOL> def addfield ( self , pkt , s , val ) : <EOL> val = self . i2m ( pkt , val ) <EOL> for v in val : <EOL> s = self . field . addfield ( pkt , s , v ) <EOL> return s <EOL> def getfield ( self , pkt , s ) : <EOL> c = l = None <EOL> if self . length_from is not None : <EOL> l = self . length_from ( pkt ) <EOL> elif self . count_from is not None : <EOL> c = self . count_from ( pkt ) <EOL> val = [ ] <EOL> ret = b"<STR_LIT>" <EOL> if l is not None : <EOL> s , ret = s [ : l ] , s [ l : ] <EOL> while s : <EOL> if c is not None : <EOL> if c <= <NUM_LIT:0> : <EOL> break <EOL> c -= <NUM_LIT:1> <EOL> s , v = self . field . getfield ( pkt , s ) <EOL> val . append ( v ) <EOL> return s + ret , val <EOL> class FieldLenField ( Field ) : <EOL> def __init__ ( self , name , default , length_of = None , fmt = "<STR_LIT:H>" , count_of = None , adjust = lambda pkt , x : x , fld = None ) : <EOL> Field . __init__ ( self , name , default , fmt ) <EOL> self . length_of = length_of <EOL> self . count_of = count_of <EOL> self . adjust = adjust <EOL> if fld is not None : <EOL> FIELD_LENGTH_MANAGEMENT_DEPRECATION ( self . __class__ . __name__ ) <EOL> self . length_of = fld <EOL> def i2m ( self , pkt , x ) : <EOL> if x is None : <EOL> if self . length_of is not None : <EOL> fld , fval = pkt . getfield_and_val ( self . length_of ) <EOL> f = fld . i2len ( pkt , fval ) <EOL> else : <EOL> fld , fval = pkt . getfield_and_val ( self . count_of ) <EOL> f = fld . i2count ( pkt , fval ) <EOL> x = self . adjust ( pkt , f ) <EOL> return x <EOL> class StrNullField ( StrField ) : <EOL> def addfield ( self , pkt , s , val ) : <EOL> return s + self . i2m ( pkt , val ) + b"<STR_LIT:\x00>" <EOL> def getfield ( self , pkt , s ) : <EOL> l = s . find ( b"<STR_LIT:\x00>" ) <EOL> if l < <NUM_LIT:0> : <EOL> return "<STR_LIT>" , s <EOL> return s [ l + <NUM_LIT:1> : ] , self . m2i ( pkt , s [ : l ] ) <EOL> def randval ( self ) : <EOL> return RandTermString ( RandNum ( <NUM_LIT:0> , <NUM_LIT> ) , b"<STR_LIT:\x00>" ) <EOL> class StrStopField ( StrField ) : <EOL> def __init__ ( self , name , default , stop , additionnal = <NUM_LIT:0> ) : <EOL> Field . __init__ ( self , name , default ) <EOL> self . stop = stop <EOL> self . additionnal = additionnal <EOL> def getfield ( self , pkt , s ) : <EOL> l = s . find ( self . stop ) <EOL> if l < <NUM_LIT:0> : <EOL> return b"<STR_LIT>" , s <EOL> l += len ( self . stop ) + self . additionnal <EOL> return s [ l : ] , s [ : l ] <EOL> def randval ( self ) : <EOL> return RandTermString ( RandNum ( <NUM_LIT:0> , <NUM_LIT> ) , self . stop ) <EOL> class LenField ( Field ) : <EOL> def i2m ( self , pkt , x ) : <EOL> if x is None : <EOL> x = len ( pkt . payload ) <EOL> return x <EOL> class BCDFloatField ( Field ) : <EOL> def i2m ( self , pkt , x ) : <EOL> return int ( <NUM_LIT> * x ) <EOL> def m2i ( self , pkt , x ) : <EOL> return x / <NUM_LIT> <EOL> class BitField ( Field ) : <EOL> def __init__ ( self , name , default , size ) : <EOL> Field . __init__ ( self , name , default ) <EOL> self . rev = size < <NUM_LIT:0> <EOL> self . size = abs ( size ) <EOL> def reverse ( self , val ) : <EOL> if self . size == <NUM_LIT:16> : <EOL> val = socket . ntohs ( val ) <EOL> elif self . size == <NUM_LIT:32> : <EOL> val = socket . ntohl ( val ) <EOL> return val <EOL> def addfield ( self , pkt , s , val ) : <EOL> val = self . i2m ( pkt , val ) <EOL> if type ( s ) is tuple : <EOL> s , bitsdone , v = s <EOL> else : <EOL> bitsdone = <NUM_LIT:0> <EOL> v = <NUM_LIT:0> <EOL> if self . rev : <EOL> val = self . reverse ( val ) <EOL> v <<= self . size <EOL> v |= val & ( ( <NUM_LIT:1> << self . size ) - <NUM_LIT:1> ) <EOL> bitsdone += self . size <EOL> while bitsdone >= <NUM_LIT:8> : <EOL> bitsdone -= <NUM_LIT:8> <EOL> s = s + struct . pack ( "<STR_LIT>" , v >> bitsdone ) <EOL> v &= ( <NUM_LIT:1> << bitsdone ) - <NUM_LIT:1> <EOL> if bitsdone : <EOL> return s , bitsdone , v <EOL> else : <EOL> return s <EOL> def getfield ( self , pkt , s ) : <EOL> if type ( s ) is tuple : <EOL> s , bn = s <EOL> else : <EOL> bn = <NUM_LIT:0> <EOL> nb_bytes = ( self . size + bn - <NUM_LIT:1> ) // <NUM_LIT:8> + <NUM_LIT:1> <EOL> w = s [ : nb_bytes ] <EOL> bs = struct . unpack ( '<STR_LIT>' % nb_bytes , w ) <EOL> b = <NUM_LIT:0> <EOL> for c in range ( nb_bytes ) : <EOL> b |= int ( bs [ c ] ) << ( nb_bytes - c - <NUM_LIT:1> ) * <NUM_LIT:8> <EOL> b &= ( <NUM_LIT:1> << ( nb_bytes * <NUM_LIT:8> - bn ) ) - <NUM_LIT:1> <EOL> b = b >> ( nb_bytes * <NUM_LIT:8> - self . size - bn ) <EOL> if self . rev : <EOL> b = self . reverse ( b ) <EOL> bn += self . size <EOL> s = s [ bn // <NUM_LIT:8> : ] <EOL> bn = bn % <NUM_LIT:8> <EOL> b = self . m2i ( pkt , b ) <EOL> if bn : <EOL> return ( s , bn ) , b <EOL> else : <EOL> return s , b <EOL> def randval ( self ) : <EOL> return RandNum ( <NUM_LIT:0> , <NUM_LIT:2> ** self . size - <NUM_LIT:1> ) <EOL> class BitFieldLenField ( BitField ) : <EOL> def __init__ ( self , name , default , size , length_of = None , count_of = None , adjust = lambda pkt , x : x ) : <EOL> BitField . __init__ ( self , name , default , size ) <EOL> self . length_of = length_of <EOL> self . count_of = count_of <EOL> self . adjust = adjust <EOL> def i2m ( self , pkt , x ) : <EOL> return FieldLenField . i2m ( self , pkt , x ) <EOL> class XBitField ( BitField ) : <EOL> def i2repr ( self , pkt , x ) : <EOL> return lhex ( self . i2h ( pkt , x ) ) <EOL> class EnumField ( Field ) : <EOL> def __init__ ( self , name , default , enum , fmt = "<STR_LIT:H>" ) : <EOL> i2s = self . i2s = { } <EOL> s2i = self . s2i = { } <EOL> if type ( enum ) is list : <EOL> keys = range ( len ( enum ) ) <EOL> else : <EOL> keys = enum . keys ( ) <EOL> if list ( filter ( lambda x : type ( x ) is str , keys ) ) : <EOL> i2s , s2i = s2i , i2s <EOL> for k in keys : <EOL> i2s [ k ] = enum [ k ] <EOL> s2i [ enum [ k ] ] = k <EOL> Field . __init__ ( self , name , default , fmt ) <EOL> def any2i_one ( self , pkt , x ) : <EOL> if type ( x ) is str : <EOL> x = self . s2i [ x ] <EOL> return x <EOL> def i2repr_one ( self , pkt , x ) : <EOL> if self not in conf . noenum and not isinstance ( x , VolatileValue ) and x in self . i2s : <EOL> return self . i2s [ x ] <EOL> return repr ( x ) <EOL> def any2i ( self , pkt , x ) : <EOL> if type ( x ) is list : <EOL> return list ( map ( lambda z , pkt = pkt : self . any2i_one ( pkt , z ) , x ) ) <EOL> else : <EOL> return self . any2i_one ( pkt , x ) <EOL> def i2repr ( self , pkt , x ) : <EOL> if type ( x ) is list : <EOL> return list ( map ( lambda z , pkt = pkt : self . i2repr_one ( pkt , z ) , x ) ) <EOL> else : <EOL> return self . i2repr_one ( pkt , x ) <EOL> class CharEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum , fmt = "<STR_LIT>" ) : <EOL> EnumField . __init__ ( self , name , default , enum , fmt ) <EOL> k = list ( self . i2s . keys ( ) ) <EOL> if k and len ( k [ <NUM_LIT:0> ] ) != <NUM_LIT:1> : <EOL> self . i2s , self . s2i = self . s2i , self . i2s <EOL> def any2i_one ( self , pkt , x ) : <EOL> if len ( x ) != <NUM_LIT:1> : <EOL> x = self . s2i [ x ] <EOL> return x <EOL> class BitEnumField ( BitField , EnumField ) : <EOL> def __init__ ( self , name , default , size , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum ) <EOL> self . rev = size < <NUM_LIT:0> <EOL> self . size = abs ( size ) <EOL> def any2i ( self , pkt , x ) : <EOL> return EnumField . any2i ( self , pkt , x ) <EOL> def i2repr ( self , pkt , x ) : <EOL> return EnumField . i2repr ( self , pkt , x ) <EOL> class ShortEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum , "<STR_LIT:H>" ) <EOL> class LEShortEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum , "<STR_LIT>" ) <EOL> class ByteEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum , "<STR_LIT:B>" ) <EOL> class IntEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum , "<STR_LIT:I>" ) <EOL> class SignedIntEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum , "<STR_LIT:i>" ) <EOL> def randval ( self ) : <EOL> return RandSInt ( ) <EOL> class LEIntEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum ) : <EOL> EnumField . __init__ ( self , name , default , enum , "<STR_LIT>" ) <EOL> class XShortEnumField ( ShortEnumField ) : <EOL> def i2repr_one ( self , pkt , x ) : <EOL> if self not in conf . noenum and not isinstance ( x , VolatileValue ) and x in self . i2s : <EOL> return self . i2s [ x ] <EOL> return lhex ( x ) <EOL> class MultiEnumField ( EnumField ) : <EOL> def __init__ ( self , name , default , enum , depends_on , fmt = "<STR_LIT:H>" ) : <EOL> self . depends_on = depends_on <EOL> self . i2s_multi = enum <EOL> self . s2i_multi = { } <EOL> self . s2i_all = { } <EOL> for m in enum : <EOL> self . s2i_multi [ m ] = s2i = { } <EOL> for k , v in enum [ m ] . items ( ) : <EOL> s2i [ v ] = k <EOL> self . s2i_all [ v ] = k <EOL> Field . __init__ ( self , name , default , fmt ) <EOL> def any2i_one ( self , pkt , x ) : <EOL> if type ( x ) is str : <EOL> v = self . depends_on ( pkt ) <EOL> if v in self . s2i_multi : <EOL> s2i = self . s2i_multi [ v ] <EOL> if x in s2i : <EOL> return s2i [ x ] <EOL> return self . s2i_all [ x ] <EOL> return x <EOL> def i2repr_one ( self , pkt , x ) : <EOL> v = self . depends_on ( pkt ) <EOL> if v in self . i2s_multi : <EOL> return self . i2s_multi [ v ] . get ( x , x ) <EOL> return x <EOL> class BitMultiEnumField ( BitField , MultiEnumField ) : <EOL> def __init__ ( self , name , default , size , enum , depends_on ) : <EOL> MultiEnumField . __init__ ( self , name , default , enum ) <EOL> self . rev = size < <NUM_LIT:0> <EOL> self . size = abs ( size ) <EOL> def any2i ( self , pkt , x ) : <EOL> return MultiEnumField . any2i ( self , pkt , x ) <EOL> def i2repr ( self , pkt , x ) : <EOL> return MultiEnumField . i2repr ( self , pkt , x ) <EOL> class LELongField ( Field ) : <EOL> def __init__ ( self , name , default ) : <EOL> Field . __init__ ( self , name , default , "<STR_LIT>" ) <EOL> class LEFieldLenField ( FieldLenField ) : <EOL> def __init__ ( self , name , default , length_of = None , fmt = "<STR_LIT>" , count_of = None , adjust = lambda pkt , x : x , fld = None ) : <EOL> FieldLenField . __init__ ( self , name , default , length_of = length_of , fmt = fmt , fld = fld , adjust = adjust ) <EOL> class FlagsField ( BitField ) : <EOL> def __init__ ( self , name , default , size , names ) : <EOL> self . multi = type ( names ) is list or type ( names ) is tuple <EOL> if self . multi : <EOL> self . names = names <EOL> else : <EOL> self . names = names <EOL> BitField . __init__ ( self , name , default , size ) <EOL> def any2i ( self , pkt , x ) : <EOL> if type ( x ) is str : <EOL> if self . multi : <EOL> x = x . split ( "<STR_LIT:+>" ) <EOL> y = <NUM_LIT:0> <EOL> for i in x : <EOL> y |= <NUM_LIT:1> << self . names . index ( i ) <EOL> x = y <EOL> return x <EOL> def i2repr ( self , pkt , x ) : <EOL> if type ( x ) is list or type ( x ) is tuple : <EOL> return repr ( x ) <EOL> if self . multi : <EOL> r = [ ] <EOL> else : <EOL> r = "<STR_LIT>" <EOL> i = <NUM_LIT:0> <EOL> while x : <EOL> if x & <NUM_LIT:1> : <EOL> if self . multi : <EOL> r += [ self . names [ i ] ] <EOL> else : <EOL> r += self . names [ i ] <EOL> i += <NUM_LIT:1> <EOL> x >>= <NUM_LIT:1> <EOL> if self . multi : <EOL> r = "<STR_LIT:+>" . join ( r ) <EOL> return r <EOL> def i2dict ( self , pkt , x ) : <EOL> if self . multi : <EOL> r = { } <EOL> else : <EOL> return { self . names : x } <EOL> for i , n in enumerate ( self . names ) : <EOL> if x & <NUM_LIT:1> : <EOL> r [ n ] = True <EOL> else : <EOL> r [ n ] = False <EOL> x >>= <NUM_LIT:1> <EOL> return r <EOL> class FixedPointField ( BitField ) : <EOL> def __init__ ( self , name , default , size , frac_bits = <NUM_LIT:16> ) : <EOL> self . frac_bits = frac_bits <EOL> BitField . __init__ ( self , name , default , size ) <EOL> def any2i ( self , pkt , val ) : <EOL> if val is None : <EOL> return val <EOL> ival = int ( val ) <EOL> fract = int ( ( val - ival ) * <NUM_LIT:2> ** self . frac_bits ) <EOL> return ( ival << self . frac_bits ) | fract <EOL> def i2h ( self , pkt , val ) : <EOL> int_part = val >> self . frac_bits <EOL> frac_part = val & ( <NUM_LIT:1> << self . frac_bits ) - <NUM_LIT:1> <EOL> frac_part /= <NUM_LIT> ** self . frac_bits <EOL> return int_part + frac_part <EOL> def i2repr ( self , pkt , val ) : <EOL> return self . i2h ( pkt , val ) </s>
