<s> from construct import * <EOL> from construct import adapters <EOL> from tensor . protocol . sflow . protocol import utils <EOL> class IPv4Header ( object ) : <EOL> def __init__ ( self , data ) : <EOL> ip = Struct ( "<STR_LIT>" , <EOL> EmbeddedBitStruct ( <EOL> Const ( Nibble ( "<STR_LIT:version>" ) , <NUM_LIT:4> ) , <EOL> Nibble ( "<STR_LIT>" ) , <EOL> ) , <EOL> BitStruct ( "<STR_LIT>" , <EOL> Bits ( "<STR_LIT>" , <NUM_LIT:3> ) , <EOL> Flag ( "<STR_LIT>" ) , <EOL> Flag ( "<STR_LIT>" ) , <EOL> Flag ( "<STR_LIT>" ) , <EOL> Flag ( "<STR_LIT>" ) , <EOL> Padding ( <NUM_LIT:1> ) , <EOL> ) , <EOL> UBInt16 ( "<STR_LIT>" ) , <EOL> UBInt16 ( "<STR_LIT:id>" ) , <EOL> UBInt16 ( "<STR_LIT>" ) , <EOL> UBInt8 ( "<STR_LIT>" ) , <EOL> Enum ( UBInt8 ( "<STR_LIT>" ) , <EOL> UDP = <NUM_LIT> , <EOL> TCP = <NUM_LIT> , <EOL> HOPOPT = <NUM_LIT> , <EOL> ICMP = <NUM_LIT> , <EOL> IGMP = <NUM_LIT> , <EOL> GGP = <NUM_LIT> , <EOL> IPoIP = <NUM_LIT> , <EOL> ST = <NUM_LIT> , <EOL> CBT = <NUM_LIT> , <EOL> EGP = <NUM_LIT> , <EOL> IGP = <NUM_LIT> , <EOL> NVPII = <NUM_LIT> , <EOL> PUP = <NUM_LIT> , <EOL> ARGUS = <NUM_LIT> , <EOL> EMCON = <NUM_LIT> , <EOL> XNET = <NUM_LIT> , <EOL> CHAOS = <NUM_LIT> , <EOL> MUX = <NUM_LIT> , <EOL> DCNMEAS = <NUM_LIT> , <EOL> HMP = <NUM_LIT> , <EOL> PRM = <NUM_LIT> , <EOL> RDP = <NUM_LIT> , <EOL> IRTP = <NUM_LIT> , <EOL> ISOTP4 = <NUM_LIT> , <EOL> DCCP = <NUM_LIT> , <EOL> XTP = <NUM_LIT> , <EOL> DDP = <NUM_LIT> , <EOL> IL = <NUM_LIT> , <EOL> IPv6 = <NUM_LIT> , <EOL> SDRP = <NUM_LIT> , <EOL> IPv6Route = <NUM_LIT> , <EOL> IPv6Frag = <NUM_LIT> , <EOL> IDRP = <NUM_LIT> , <EOL> RSVP = <NUM_LIT> , <EOL> GRE = <NUM_LIT> , <EOL> MHRP = <NUM_LIT> , <EOL> BNA = <NUM_LIT> , <EOL> ESP = <NUM_LIT> , <EOL> AH = <NUM_LIT> , <EOL> SWIPE = <NUM_LIT> , <EOL> MOBILE = <NUM_LIT> , <EOL> TLSP = <NUM_LIT> , <EOL> SKIP = <NUM_LIT> , <EOL> IPv6ICMP = <NUM_LIT> , <EOL> IPv6NoNxt = <NUM_LIT> , <EOL> IPv6Opts = <NUM_LIT> , <EOL> CFTP = <NUM_LIT> , <EOL> SATEXPAK = <NUM_LIT> , <EOL> KRYPTOLAN = <NUM_LIT> , <EOL> RVD = <NUM_LIT> , <EOL> IPPC = <NUM_LIT> , <EOL> SATMON = <NUM_LIT> , <EOL> VISA = <NUM_LIT> , <EOL> IPCU = <NUM_LIT> , <EOL> CPNX = <NUM_LIT> , <EOL> CPHB = <NUM_LIT> , <EOL> WSN = <NUM_LIT> , <EOL> PVP = <NUM_LIT> , <EOL> BRSATMON = <NUM_LIT> , <EOL> SUNND = <NUM_LIT> , <EOL> WBMON = <NUM_LIT> , <EOL> WBEXPAK = <NUM_LIT> , <EOL> ISOIP = <NUM_LIT> , <EOL> VMTP = <NUM_LIT> , <EOL> SECUREVMTP = <NUM_LIT> , <EOL> VINES = <NUM_LIT> , <EOL> TTP = <NUM_LIT> , <EOL> IPTM = <NUM_LIT> , <EOL> NSFNETIGP = <NUM_LIT> , <EOL> DGP = <NUM_LIT> , <EOL> TCF = <NUM_LIT> , <EOL> EIGRP = <NUM_LIT> , <EOL> OSPF = <NUM_LIT> , <EOL> LARP = <NUM_LIT> , <EOL> MTP = <NUM_LIT> , <EOL> IPIP = <NUM_LIT> , <EOL> MICP = <NUM_LIT> , <EOL> SCCSP = <NUM_LIT> , <EOL> ETHERIP = <NUM_LIT> , <EOL> ENCAP = <NUM_LIT> , <EOL> GMTP = <NUM_LIT> , <EOL> IFMP = <NUM_LIT> , <EOL> PNNI = <NUM_LIT> , <EOL> PIM = <NUM_LIT> , <EOL> ARIS = <NUM_LIT> , <EOL> SCPS = <NUM_LIT> , <EOL> QNX = <NUM_LIT> , <EOL> IPComp = <NUM_LIT> , <EOL> SNP = <NUM_LIT> , <EOL> VRRP = <NUM_LIT> , <EOL> PGM = <NUM_LIT> , <EOL> L2TP = <NUM_LIT> , <EOL> DDX = <NUM_LIT> , <EOL> IATP = <NUM_LIT> , <EOL> STP = <NUM_LIT> , <EOL> SRP = <NUM_LIT> , <EOL> UTI = <NUM_LIT> , <EOL> SMP = <NUM_LIT> , <EOL> SM = <NUM_LIT> , <EOL> PTP = <NUM_LIT> , <EOL> ISIS = <NUM_LIT> , <EOL> FIRE = <NUM_LIT> , <EOL> CRTP = <NUM_LIT> , <EOL> CRUDP = <NUM_LIT> , <EOL> SSCOPMCE = <NUM_LIT> , <EOL> IPLT = <NUM_LIT> , <EOL> SPS = <NUM_LIT> , <EOL> SCTP = <NUM_LIT> , <EOL> FC = <NUM_LIT> , <EOL> UDPLite = <NUM_LIT> , <EOL> MPLSoIP = <NUM_LIT> , <EOL> manet = <NUM_LIT> , <EOL> HIP = <NUM_LIT> , <EOL> Shim6 = <NUM_LIT> , <EOL> WESP = <NUM_LIT> , <EOL> ROHC = <NUM_LIT> , <EOL> ) , <EOL> UBInt16 ( "<STR_LIT>" ) , <EOL> UBInt32 ( "<STR_LIT:src>" ) , <EOL> UBInt32 ( "<STR_LIT>" ) , <EOL> ) <EOL> self . ip = ip . parse ( data [ : ip . sizeof ( ) ] ) <EOL> self . ip . src = utils . IPv4Address ( self . ip . src ) <EOL> self . ip . dst = utils . IPv4Address ( self . ip . dst ) <EOL> data = data [ ip . sizeof ( ) : ] <EOL> if self . ip . proto in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . proto = Struct ( "<STR_LIT>" , <EOL> UBInt16 ( "<STR_LIT>" ) , <EOL> UBInt16 ( "<STR_LIT>" ) , <EOL> ) . parse ( data ) <EOL> self . ip_sport = self . proto . sport <EOL> self . ip_dport = self . proto . dport <EOL> class ISO8023Header ( object ) : <EOL> def __init__ ( self , data ) : <EOL> frame = Struct ( "<STR_LIT>" , <EOL> Bytes ( "<STR_LIT>" , <NUM_LIT:6> ) , <EOL> Bytes ( "<STR_LIT:source>" , <NUM_LIT:6> ) , <EOL> Enum ( UBInt16 ( "<STR_LIT:type>" ) , <EOL> IPv4 = <NUM_LIT> , <EOL> ARP = <NUM_LIT> , <EOL> RARP = <NUM_LIT> , <EOL> X25 = <NUM_LIT> , <EOL> IPX = <NUM_LIT> , <EOL> IPv6 = <NUM_LIT> , <EOL> VLAN = <NUM_LIT> <EOL> ) <EOL> ) <EOL> try : <EOL> ethernet = frame . parse ( data [ : <NUM_LIT> ] ) <EOL> except adapters . MappingError : <EOL> print "<STR_LIT>" <EOL> self . frame = None <EOL> print repr ( data ) <EOL> return <EOL> data = data [ <NUM_LIT> : ] <EOL> self . src_mac = ethernet . destination <EOL> self . dst_mac = ethernet . source <EOL> if ethernet . type == '<STR_LIT>' : <EOL> d = ord ( data [ <NUM_LIT:0> ] ) <EOL> self . vlan = d & <NUM_LIT> <EOL> self . vlan_priority = d >> <NUM_LIT> <EOL> elif ethernet . type == '<STR_LIT>' : <EOL> ipv4 = IPv4Header ( data ) <EOL> self . ip = ipv4 . ip <EOL> self . ip_sport = ipv4 . ip_sport <EOL> self . ip_dport = ipv4 . ip_dport <EOL> else : <EOL> print ethernet . type , repr ( data ) <EOL> class IPv6Header ( object ) : <EOL> def __init__ ( self , u ) : <EOL> pass <EOL> class IEEE80211MACHeader ( object ) : <EOL> def __init__ ( self , u ) : <EOL> pass <EOL> class PPPHeader ( object ) : <EOL> def __init__ ( self , u ) : <EOL> pass <EOL> class HeaderSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . protocol = u . unpack_uint ( ) <EOL> self . frame_len = u . unpack_uint ( ) <EOL> self . payload_removed = u . unpack_uint ( ) <EOL> self . sample_header = u . unpack_string ( ) <EOL> self . samplers = { <EOL> <NUM_LIT:1> : ISO8023Header , <EOL> <NUM_LIT:7> : PPPHeader , <EOL> <NUM_LIT:11> : IPv4Header , <EOL> <NUM_LIT:12> : IPv6Header <EOL> } <EOL> if self . samplers . get ( self . protocol ) : <EOL> self . frame = self . samplers [ self . protocol ] ( <EOL> self . sample_header <EOL> ) <EOL> else : <EOL> print "<STR_LIT>" , self . protocol <EOL> self . frame = None <EOL> class EthernetSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . length = u . unpack_uint ( ) <EOL> self . src_mac = u . unpack_fopaque ( <NUM_LIT:6> ) <EOL> self . dst_mac = u . unpack_fopaque ( <NUM_LIT:6> ) <EOL> self . type = u . unpack_uint ( ) <EOL> class IPV4Sample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . length = u . unpack_uint ( ) <EOL> self . protocol = u . unpack_uint ( ) <EOL> self . src_ip = u . unpack_fstring ( <NUM_LIT:4> ) <EOL> self . dst_ip = u . unpack_fstring ( <NUM_LIT:4> ) <EOL> self . src_port = u . unpack_uint ( ) <EOL> self . dst_port = u . unpack_uint ( ) <EOL> self . tcp_flags = u . unpack_uint ( ) <EOL> self . tos = u . unpack_uint ( ) <EOL> class IPV6Sample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . length = u . unpack_uint ( ) <EOL> self . protocol = u . unpack_uint ( ) <EOL> self . src_ip = u . unpack_fstring ( <NUM_LIT:16> ) <EOL> self . dst_ip = u . unpack_fstring ( <NUM_LIT:16> ) <EOL> self . src_port = u . unpack_uint ( ) <EOL> self . dst_port = u . unpack_uint ( ) <EOL> self . tcp_flags = u . unpack_uint ( ) <EOL> self . priority = u . unpack_uint ( ) <EOL> class SwitchSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . src_vlan = u . unpack_uint ( ) <EOL> self . src_priority = u . unpack_uint ( ) <EOL> self . dst_vlan = u . unpack_uint ( ) <EOL> self . dst_priority = u . unpack_uint ( ) <EOL> class RouterSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . next_hop = utils . unpack_address ( u ) <EOL> self . src_mask_len = u . unpack_uint ( ) <EOL> self . dst_mask_len = u . unpack_uint ( ) <EOL> class GatewaySample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . next_hop = utils . unpack_address ( u ) <EOL> self . asn = u . unpack_uint ( ) <EOL> self . src_as = u . unpack_uint ( ) <EOL> self . src_peer_as = u . unpack_uint ( ) <EOL> self . as_path_type = u . unpack_uint ( ) <EOL> self . as_path = u . unpack_array ( u . unpack_uint ) <EOL> self . communities = u . unpack_array ( u . unpack_uint ) <EOL> self . localpref = u . unpack_uint ( ) <EOL> class UserSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . src_charset = u . unpack_uint ( ) <EOL> self . src_user = u . unpack_string ( ) <EOL> self . dst_charset = u . unpack_uint ( ) <EOL> self . dst_user = u . unpack_string ( ) <EOL> class URLSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . url_direction = u . unpack_uint ( ) <EOL> self . url = u . unpack_string ( ) <EOL> self . host = u . unpack_string ( ) <EOL> class MPLSSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . next_hop = utils . unpack_address ( u ) <EOL> self . in_stack = u . unpack_array ( u . unpack_uint ) <EOL> self . out_stack = u . unpack_array ( u . unpack_uint ) <EOL> class NATSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . src_address = utils . unpack_address ( u ) <EOL> self . dst_address = utils . unpack_address ( u ) <EOL> class MPLSTunnelSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . tunnel_lsp_name = u . unpack_string ( ) <EOL> self . tunnel_id = u . unpack_uint ( ) <EOL> self . tunnel_cos = u . unpack_uint ( ) <EOL> class MPLSVCSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . vc_instance_name = u . unpack_string ( ) <EOL> self . vc_id = u . unpack_uint ( ) <EOL> self . vc_cos = u . unpack_uint ( ) <EOL> class MPLSFTNSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . mplsFTNDescr = u . unpack_string ( ) <EOL> self . mplsFTNMask = u . unpack_uint ( ) <EOL> class MPLSLDPFECSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . mplsFecAddrPrefixLength = u . unpack_uint ( ) <EOL> class VLANTunnelSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . stack = u . unpack_array ( u . unpack_uint ) <EOL> def getDecoder ( format ) : <EOL> decoders = { <EOL> <NUM_LIT:1> : HeaderSample , <EOL> <NUM_LIT:2> : EthernetSample , <EOL> <NUM_LIT:3> : IPV4Sample , <EOL> <NUM_LIT:4> : IPV6Sample , <EOL> <NUM_LIT> : SwitchSample , <EOL> <NUM_LIT> : RouterSample , <EOL> <NUM_LIT> : GatewaySample , <EOL> <NUM_LIT> : UserSample , <EOL> <NUM_LIT> : URLSample , <EOL> <NUM_LIT> : MPLSSample , <EOL> <NUM_LIT> : NATSample , <EOL> <NUM_LIT> : MPLSTunnelSample , <EOL> <NUM_LIT> : MPLSVCSample , <EOL> <NUM_LIT> : MPLSFTNSample , <EOL> <NUM_LIT> : MPLSLDPFECSample , <EOL> <NUM_LIT> : VLANTunnelSample <EOL> } <EOL> return decoders . get ( format , None ) </s>
