<s> import socket <EOL> import struct <EOL> from types import FunctionType <EOL> from broccoli_intern import * <EOL> bro_init ( None ) <EOL> class Connection : <EOL> def __init__ ( self , destination , broclass = "<STR_LIT>" , flags = BRO_CFLAG_RECONNECT | BRO_CFLAG_ALWAYS_QUEUE , connect = True ) : <EOL> self . bc = bro_conn_new_str ( destination , flags ) <EOL> self . destination = destination <EOL> if not self . bc : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> self . _registerHandlers ( ) <EOL> if broclass : <EOL> bro_conn_set_class ( self . bc , broclass ) <EOL> if connect : <EOL> self . connect ( ) ; <EOL> def connect ( self ) : <EOL> if not bro_conn_connect ( self . bc ) : <EOL> raise IOError ( "<STR_LIT>" % self . destination ) <EOL> def processInput ( self ) : <EOL> bro_conn_process_input ( self . bc ) ; <EOL> return bro_event_queue_length ( self . bc ) > <NUM_LIT:0> <EOL> def connAlive ( self ) : <EOL> if bro_conn_alive ( self . bc ) == <NUM_LIT:1> : <EOL> return True <EOL> return False <EOL> def connDelete ( self ) : <EOL> return bro_conn_delete ( self . bc ) <EOL> def send ( self , name , * args ) : <EOL> ev = bro_event_new ( name ) <EOL> for arg in args : <EOL> bro_event_add_val ( ev , _getInternalVal ( arg ) ) <EOL> bro_event_send ( self . bc , ev ) ; <EOL> bro_event_free ( ev ) ; <EOL> self . processInput ( ) <EOL> def subscribe ( self , event_name , callback ) : <EOL> ev = event ( callback ) <EOL> bro_event_registry_add_compact ( self . bc , event_name , ev ) ; <EOL> def _registerHandlers ( self ) : <EOL> for ev in _Events : <EOL> bro_event_registry_add_compact ( self . bc , ev . __name__ , ev ) ; <EOL> def current_time ( ) : <EOL> return bro_util_current_time ( ) <EOL> _Events = [ ] <EOL> def event ( * types ) : <EOL> def make_wrapper ( func ) : <EOL> def wrapped_f ( * args ) : <EOL> new_args = [ ] <EOL> ptypes = types <EOL> if not ptypes : <EOL> ptypes = [ None ] * len ( args ) <EOL> for ( arg , type ) in zip ( args , ptypes ) : <EOL> ( btype , val ) = arg <EOL> new_args += [ instantiate ( btype , val , type ) ] <EOL> return func ( * new_args ) ; <EOL> wrapped_f . func_name = func . func_name <EOL> global _Events <EOL> _Events += [ wrapped_f ] <EOL> return wrapped_f <EOL> if len ( types ) == <NUM_LIT:1> and type ( types [ <NUM_LIT:0> ] ) == FunctionType : <EOL> func = types [ <NUM_LIT:0> ] <EOL> types = ( ) <EOL> return make_wrapper ( func ) <EOL> else : <EOL> return make_wrapper <EOL> class Val : <EOL> def __init__ ( self , type , val ) : <EOL> self . type = type <EOL> self . val = val <EOL> self . __class__ . _bro_type = type <EOL> def __str__ ( self ) : <EOL> return str ( self . val ) <EOL> def internalVal ( self ) : <EOL> return ( self . type , self . val ) <EOL> class count ( Val ) : <EOL> def __init__ ( self , val ) : <EOL> Val . __init__ ( self , BRO_TYPE_COUNT , int ( val ) ) <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = count ( val ) <EOL> if dst_type == int or not dst_type : <EOL> return v . val <EOL> _typeCheck ( dst_type , count ) <EOL> return v <EOL> class interval ( Val ) : <EOL> def __init__ ( self , val ) : <EOL> Val . __init__ ( self , BRO_TYPE_INTERVAL , float ( val ) ) <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = interval ( val ) <EOL> if dst_type == float or not dst_type : <EOL> return v . val <EOL> _typeCheck ( dst_type , interval ) <EOL> return v <EOL> class time ( Val ) : <EOL> def __init__ ( self , val ) : <EOL> Val . __init__ ( self , BRO_TYPE_TIME , float ( val ) ) <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = time ( val ) <EOL> if dst_type == float or not dst_type : <EOL> return v . val <EOL> _typeCheck ( dst_type , time ) <EOL> return v <EOL> class port ( Val ) : <EOL> protos_by_name = { "<STR_LIT>" : <NUM_LIT:6> , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT:1> } <EOL> protos_by_num = { <NUM_LIT:6> : "<STR_LIT>" , <NUM_LIT> : "<STR_LIT>" , <NUM_LIT:1> : "<STR_LIT>" } <EOL> def __init__ ( self , str = None , internal = None ) : <EOL> v = internal and internal or self . _parse ( str ) <EOL> Val . __init__ ( self , BRO_TYPE_PORT , v ) <EOL> def __str__ ( self ) : <EOL> ( port , proto ) = self . val <EOL> try : <EOL> return "<STR_LIT>" % ( port , self . protos_by_num [ proto ] ) <EOL> except IndexError : <EOL> return "<STR_LIT>" % port <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = port ( internal = val ) <EOL> if dst_type == str or not dst_type : <EOL> return str ( v ) <EOL> if dst_type == int : <EOL> return v [ <NUM_LIT:0> ] <EOL> _typeCheck ( dst_type , port ) <EOL> return v <EOL> def _parse ( self , str ) : <EOL> ( port , proto ) = str . split ( "<STR_LIT:/>" ) <EOL> try : <EOL> return ( int ( port ) , self . protos_by_name [ proto . lower ( ) ] ) <EOL> except ( IndexError , ValueError ) : <EOL> return ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> class addr ( Val ) : <EOL> def __init__ ( self , str = None , internal = None ) : <EOL> v = internal and internal or self . _parse ( str ) <EOL> Val . __init__ ( self , BRO_TYPE_IPADDR , v ) <EOL> def __str__ ( self ) : <EOL> return _addrTupleToString ( self . val ) <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = addr ( internal = val ) <EOL> if dst_type == str or not dst_type : <EOL> return str ( v ) <EOL> _typeCheck ( dst_type , addr ) <EOL> return v <EOL> def _parse ( self , str ) : <EOL> return _addrStringToTuple ( str ) <EOL> class subnet ( Val ) : <EOL> def __init__ ( self , str = None , internal = None ) : <EOL> v = internal and internal or self . _parse ( str ) <EOL> Val . __init__ ( self , BRO_TYPE_SUBNET , v ) <EOL> def __str__ ( self ) : <EOL> ( net , mask ) = self . val <EOL> return "<STR_LIT>" % ( _addrTupleToString ( net ) , mask ) <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = subnet ( internal = val ) <EOL> if dst_type == str or not dst_type : <EOL> return str ( v ) <EOL> _typeCheck ( dst_type , subnet ) <EOL> return v <EOL> def _parse ( self , str ) : <EOL> ( net , mask ) = str . split ( "<STR_LIT:/>" ) <EOL> return ( _addrStringToTuple ( net ) , int ( mask ) ) <EOL> class enum ( Val ) : <EOL> def __init__ ( self , str = None , internal = None ) : <EOL> v = internal and internal or self . _parse ( str ) <EOL> Val . __init__ ( self , BRO_TYPE_ENUM , v ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> @ staticmethod <EOL> def _factory ( val , dst_type ) : <EOL> v = enum ( internal = val ) <EOL> _typeCheck ( dst_type , enum ) <EOL> return v <EOL> def _parse ( self , str ) : <EOL> return <NUM_LIT:0> <EOL> class unknown ( Val ) : <EOL> def __init__ ( self ) : <EOL> Val . __init__ ( self , BRO_TYPE_UNKNOWN , None ) <EOL> class record_type : <EOL> def __init__ ( self , * fields ) : <EOL> self . fields = fields <EOL> @ classmethod <EOL> def _factory ( self , vals , dst_type ) : <EOL> vals = [ instantiate ( btype , val , None ) for ( btype , val ) in vals ] <EOL> return record ( dst_type , vals ) <EOL> class record ( Val ) : <EOL> def __init__ ( self , type , vals = None ) : <EOL> Val . __init__ ( self , BRO_TYPE_RECORD , { } ) <EOL> self . _type = type <EOL> if not vals : <EOL> vals = [ None ] * len ( type . fields ) <EOL> for ( key , val ) in zip ( type . fields , vals ) : <EOL> self . val [ key ] = val <EOL> def internalVal ( self ) : <EOL> vals = [ ] <EOL> for f in self . _type . fields : <EOL> v = self . val . get ( f , None ) <EOL> if v != None : <EOL> vals . append ( ( f , _getInternalVal ( v ) ) ) <EOL> return ( BRO_TYPE_RECORD , vals ) <EOL> def __getattr__ ( self , key ) : <EOL> if "<STR_LIT>" in self . __dict__ and key in self . _type . fields : <EOL> return self . val [ key ] <EOL> raise AttributeError <EOL> def __setattr__ ( self , key , val ) : <EOL> try : <EOL> if key in self . _type . fields : <EOL> self . val [ key ] = val <EOL> return <EOL> except AttributeError : <EOL> pass <EOL> self . __dict__ [ key ] = val <EOL> def _addrTupleToString ( a ) : <EOL> if len ( a ) == <NUM_LIT:1> : <EOL> return socket . inet_ntop ( socket . AF_INET , struct . pack ( '<STR_LIT>' , a [ <NUM_LIT:0> ] ) ) ; <EOL> else : <EOL> return socket . inet_ntop ( socket . AF_INET6 , struct . pack ( '<STR_LIT>' , a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] , a [ <NUM_LIT:2> ] , a [ <NUM_LIT:3> ] ) ) <EOL> def _addrStringToTuple ( s ) : <EOL> if '<STR_LIT::>' in s : <EOL> return struct . unpack ( '<STR_LIT>' , socket . inet_pton ( socket . AF_INET6 , s ) ) <EOL> else : <EOL> return struct . unpack ( '<STR_LIT>' , socket . inet_pton ( socket . AF_INET , s ) ) <EOL> def _typeCheck ( type1 , type2 ) : <EOL> def typeToBro ( type ) : <EOL> if type == int : <EOL> return BRO_TYPE_INT ; <EOL> if type == bool : <EOL> return BRO_TYPE_BOOL ; <EOL> if type == float : <EOL> return BRO_TYPE_DOUBLE ; <EOL> if type == str : <EOL> return BRO_TYPE_STRING ; <EOL> return type . _bro_type <EOL> if type1 and type2 and typeToBro ( type1 ) != typeToBro ( type2 ) : <EOL> raise TypeError <EOL> def _getInternalVal ( arg ) : <EOL> if arg == None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if type ( arg ) == int : <EOL> return ( BRO_TYPE_INT , arg ) <EOL> elif type ( arg ) == bool : <EOL> return ( BRO_TYPE_BOOL , arg ) <EOL> elif type ( arg ) == str : <EOL> return ( BRO_TYPE_STRING , arg ) <EOL> elif type ( arg ) == float : <EOL> return ( BRO_TYPE_DOUBLE , arg ) <EOL> else : <EOL> return arg . internalVal ( ) <EOL> def _long_factory ( val , dst_type ) : <EOL> return long ( val ) <EOL> def _bool_factory ( val , dst_type ) : <EOL> return bool ( val ) <EOL> def _string_factory ( val , dst_type ) : <EOL> return str ( val ) <EOL> def _float_factory ( val , dst_type ) : <EOL> return float ( val ) <EOL> string = str <EOL> double = float <EOL> _Factories = { <EOL> BRO_TYPE_INT : _long_factory , <EOL> BRO_TYPE_BOOL : _bool_factory , <EOL> BRO_TYPE_COUNT : count . _factory , <EOL> BRO_TYPE_TIME : time . _factory , <EOL> BRO_TYPE_INTERVAL : interval . _factory , <EOL> BRO_TYPE_DOUBLE : _float_factory , <EOL> BRO_TYPE_STRING : _string_factory , <EOL> BRO_TYPE_PORT : port . _factory , <EOL> BRO_TYPE_IPADDR : addr . _factory , <EOL> BRO_TYPE_SUBNET : subnet . _factory , <EOL> BRO_TYPE_ENUM : enum . _factory , <EOL> BRO_TYPE_RECORD : record_type . _factory , <EOL> } <EOL> def instantiate ( src_type , val , dst_type ) : <EOL> return _Factories [ src_type ] ( val , dst_type ) </s>
