<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> import warnings <EOL> import struct , random , types , socket <EOL> try : <EOL> import cStringIO as StringIO <EOL> except ImportError : <EOL> import StringIO <EOL> AF_INET6 = socket . AF_INET6 <EOL> from zope . interface import implements , Interface , Attribute <EOL> from twisted . internet import protocol , defer <EOL> from twisted . internet . error import CannotListenError <EOL> from twisted . python import log , failure <EOL> from twisted . python import util as tputil <EOL> from twisted . python import randbytes <EOL> def randomSource ( ) : <EOL> """<STR_LIT>""" <EOL> return struct . unpack ( '<STR_LIT:H>' , randbytes . secureRandom ( <NUM_LIT:2> , fallback = True ) ) [ <NUM_LIT:0> ] <EOL> PORT = <NUM_LIT> <EOL> ( A , NS , MD , MF , CNAME , SOA , MB , MG , MR , NULL , WKS , PTR , HINFO , MINFO , MX , TXT , <EOL> RP , AFSDB ) = range ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> AAAA = <NUM_LIT> <EOL> SRV = <NUM_LIT> <EOL> NAPTR = <NUM_LIT> <EOL> A6 = <NUM_LIT> <EOL> DNAME = <NUM_LIT> <EOL> SPF = <NUM_LIT> <EOL> QUERY_TYPES = { <EOL> A : '<STR_LIT:A>' , <EOL> NS : '<STR_LIT>' , <EOL> MD : '<STR_LIT>' , <EOL> MF : '<STR_LIT>' , <EOL> CNAME : '<STR_LIT>' , <EOL> SOA : '<STR_LIT>' , <EOL> MB : '<STR_LIT>' , <EOL> MG : '<STR_LIT>' , <EOL> MR : '<STR_LIT>' , <EOL> NULL : '<STR_LIT>' , <EOL> WKS : '<STR_LIT>' , <EOL> PTR : '<STR_LIT>' , <EOL> HINFO : '<STR_LIT>' , <EOL> MINFO : '<STR_LIT>' , <EOL> MX : '<STR_LIT>' , <EOL> TXT : '<STR_LIT>' , <EOL> RP : '<STR_LIT>' , <EOL> AFSDB : '<STR_LIT>' , <EOL> AAAA : '<STR_LIT>' , <EOL> SRV : '<STR_LIT>' , <EOL> NAPTR : '<STR_LIT>' , <EOL> A6 : '<STR_LIT>' , <EOL> DNAME : '<STR_LIT>' , <EOL> SPF : '<STR_LIT>' <EOL> } <EOL> IXFR , AXFR , MAILB , MAILA , ALL_RECORDS = range ( <NUM_LIT> , <NUM_LIT> ) <EOL> EXT_QUERIES = { <EOL> IXFR : '<STR_LIT>' , <EOL> AXFR : '<STR_LIT>' , <EOL> MAILB : '<STR_LIT>' , <EOL> MAILA : '<STR_LIT>' , <EOL> ALL_RECORDS : '<STR_LIT>' <EOL> } <EOL> REV_TYPES = dict ( [ <EOL> ( v , k ) for ( k , v ) in QUERY_TYPES . items ( ) + EXT_QUERIES . items ( ) <EOL> ] ) <EOL> IN , CS , CH , HS = range ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> ANY = <NUM_LIT:255> <EOL> QUERY_CLASSES = { <EOL> IN : '<STR_LIT>' , <EOL> CS : '<STR_LIT>' , <EOL> CH : '<STR_LIT>' , <EOL> HS : '<STR_LIT>' , <EOL> ANY : '<STR_LIT>' <EOL> } <EOL> REV_CLASSES = dict ( [ <EOL> ( v , k ) for ( k , v ) in QUERY_CLASSES . items ( ) <EOL> ] ) <EOL> OP_QUERY , OP_INVERSE , OP_STATUS = range ( <NUM_LIT:3> ) <EOL> OP_NOTIFY = <NUM_LIT:4> <EOL> OP_UPDATE = <NUM_LIT:5> <EOL> OK , EFORMAT , ESERVER , ENAME , ENOTIMP , EREFUSED = range ( <NUM_LIT:6> ) <EOL> class IRecord ( Interface ) : <EOL> """<STR_LIT>""" <EOL> TYPE = Attribute ( "<STR_LIT>" ) <EOL> from twisted . names . error import DomainError , AuthoritativeDomainError <EOL> from twisted . names . error import DNSQueryTimeoutError <EOL> def str2time ( s ) : <EOL> suffixes = ( <EOL> ( '<STR_LIT:S>' , <NUM_LIT:1> ) , ( '<STR_LIT:M>' , <NUM_LIT> ) , ( '<STR_LIT:H>' , <NUM_LIT> * <NUM_LIT> ) , ( '<STR_LIT:D>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> ) , ( '<STR_LIT:Y>' , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> ) <EOL> if isinstance ( s , types . StringType ) : <EOL> s = s . upper ( ) . strip ( ) <EOL> for ( suff , mult ) in suffixes : <EOL> if s . endswith ( suff ) : <EOL> return int ( float ( s [ : - <NUM_LIT:1> ] ) * mult ) <EOL> try : <EOL> s = int ( s ) <EOL> except ValueError : <EOL> raise ValueError , "<STR_LIT>" + s <EOL> return s <EOL> def readPrecisely ( file , l ) : <EOL> buff = file . read ( l ) <EOL> if len ( buff ) < l : <EOL> raise EOFError <EOL> return buff <EOL> class IEncodable ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def encode ( strio , compDict = None ) : <EOL> """<STR_LIT>""" <EOL> def decode ( strio , length = None ) : <EOL> """<STR_LIT>""" <EOL> class Charstr ( object ) : <EOL> implements ( IEncodable ) <EOL> def __init__ ( self , string = '<STR_LIT>' ) : <EOL> if not isinstance ( string , str ) : <EOL> raise ValueError ( "<STR_LIT>" % ( string , ) ) <EOL> self . string = string <EOL> def encode ( self , strio , compDict = None ) : <EOL> """<STR_LIT>""" <EOL> string = self . string <EOL> ind = len ( string ) <EOL> strio . write ( chr ( ind ) ) <EOL> strio . write ( string ) <EOL> def decode ( self , strio , length = None ) : <EOL> """<STR_LIT>""" <EOL> self . string = '<STR_LIT>' <EOL> l = ord ( readPrecisely ( strio , <NUM_LIT:1> ) ) <EOL> self . string = readPrecisely ( strio , l ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Charstr ) : <EOL> return self . string == other . string <EOL> return False <EOL> def __hash__ ( self ) : <EOL> return hash ( self . string ) <EOL> def __str__ ( self ) : <EOL> return self . string <EOL> class Name : <EOL> implements ( IEncodable ) <EOL> def __init__ ( self , name = '<STR_LIT>' ) : <EOL> assert isinstance ( name , types . StringTypes ) , "<STR_LIT>" % ( name , ) <EOL> self . name = name <EOL> def encode ( self , strio , compDict = None ) : <EOL> """<STR_LIT>""" <EOL> name = self . name <EOL> while name : <EOL> if compDict is not None : <EOL> if name in compDict : <EOL> strio . write ( <EOL> struct . pack ( "<STR_LIT>" , <NUM_LIT> | compDict [ name ] ) ) <EOL> return <EOL> else : <EOL> compDict [ name ] = strio . tell ( ) + Message . headerSize <EOL> ind = name . find ( '<STR_LIT:.>' ) <EOL> if ind > <NUM_LIT:0> : <EOL> label , name = name [ : ind ] , name [ ind + <NUM_LIT:1> : ] <EOL> else : <EOL> label , name = name , '<STR_LIT>' <EOL> ind = len ( label ) <EOL> strio . write ( chr ( ind ) ) <EOL> strio . write ( label ) <EOL> strio . write ( chr ( <NUM_LIT:0> ) ) <EOL> def decode ( self , strio , length = None ) : <EOL> """<STR_LIT>""" <EOL> self . name = '<STR_LIT>' <EOL> off = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> l = ord ( readPrecisely ( strio , <NUM_LIT:1> ) ) <EOL> if l == <NUM_LIT:0> : <EOL> if off > <NUM_LIT:0> : <EOL> strio . seek ( off ) <EOL> return <EOL> if ( l >> <NUM_LIT:6> ) == <NUM_LIT:3> : <EOL> new_off = ( ( l & <NUM_LIT> ) << <NUM_LIT:8> <EOL> | ord ( readPrecisely ( strio , <NUM_LIT:1> ) ) ) <EOL> if off == <NUM_LIT:0> : <EOL> off = strio . tell ( ) <EOL> strio . seek ( new_off ) <EOL> continue <EOL> label = readPrecisely ( strio , l ) <EOL> if self . name == '<STR_LIT>' : <EOL> self . name = label <EOL> else : <EOL> self . name = self . name + '<STR_LIT:.>' + label <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Name ) : <EOL> return str ( self ) == str ( other ) <EOL> return <NUM_LIT:0> <EOL> def __hash__ ( self ) : <EOL> return hash ( str ( self ) ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> class Query : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable ) <EOL> name = None <EOL> type = None <EOL> cls = None <EOL> def __init__ ( self , name = '<STR_LIT>' , type = A , cls = IN ) : <EOL> """<STR_LIT>""" <EOL> self . name = Name ( name ) <EOL> self . type = type <EOL> self . cls = cls <EOL> def encode ( self , strio , compDict = None ) : <EOL> self . name . encode ( strio , compDict ) <EOL> strio . write ( struct . pack ( "<STR_LIT>" , self . type , self . cls ) ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . name . decode ( strio ) <EOL> buff = readPrecisely ( strio , <NUM_LIT:4> ) <EOL> self . type , self . cls = struct . unpack ( "<STR_LIT>" , buff ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( str ( self . name ) . lower ( ) , self . type , self . cls ) ) <EOL> def __cmp__ ( self , other ) : <EOL> return isinstance ( other , Query ) and cmp ( <EOL> ( str ( self . name ) . lower ( ) , self . type , self . cls ) , <EOL> ( str ( other . name ) . lower ( ) , other . type , other . cls ) <EOL> ) or cmp ( self . __class__ , other . __class__ ) <EOL> def __str__ ( self ) : <EOL> t = QUERY_TYPES . get ( self . type , EXT_QUERIES . get ( self . type , '<STR_LIT>' % self . type ) ) <EOL> c = QUERY_CLASSES . get ( self . cls , '<STR_LIT>' % self . cls ) <EOL> return '<STR_LIT>' % ( self . name , t , c ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( str ( self . name ) , self . type , self . cls ) <EOL> class RRHeader ( tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable ) <EOL> compareAttributes = ( '<STR_LIT:name>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fmt = "<STR_LIT>" <EOL> name = None <EOL> type = None <EOL> cls = None <EOL> ttl = None <EOL> payload = None <EOL> rdlength = None <EOL> cachedResponse = None <EOL> def __init__ ( self , name = '<STR_LIT>' , type = A , cls = IN , ttl = <NUM_LIT:0> , payload = None , auth = False ) : <EOL> """<STR_LIT>""" <EOL> assert ( payload is None ) or ( payload . TYPE == type ) <EOL> self . name = Name ( name ) <EOL> self . type = type <EOL> self . cls = cls <EOL> self . ttl = ttl <EOL> self . payload = payload <EOL> self . auth = auth <EOL> def encode ( self , strio , compDict = None ) : <EOL> self . name . encode ( strio , compDict ) <EOL> strio . write ( struct . pack ( self . fmt , self . type , self . cls , self . ttl , <NUM_LIT:0> ) ) <EOL> if self . payload : <EOL> prefix = strio . tell ( ) <EOL> self . payload . encode ( strio , compDict ) <EOL> aft = strio . tell ( ) <EOL> strio . seek ( prefix - <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> strio . write ( struct . pack ( '<STR_LIT>' , aft - prefix ) ) <EOL> strio . seek ( aft , <NUM_LIT:0> ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . name . decode ( strio ) <EOL> l = struct . calcsize ( self . fmt ) <EOL> buff = readPrecisely ( strio , l ) <EOL> r = struct . unpack ( self . fmt , buff ) <EOL> self . type , self . cls , self . ttl , self . rdlength = r <EOL> def isAuthoritative ( self ) : <EOL> return self . auth <EOL> def __str__ ( self ) : <EOL> t = QUERY_TYPES . get ( self . type , EXT_QUERIES . get ( self . type , '<STR_LIT>' % self . type ) ) <EOL> c = QUERY_CLASSES . get ( self . cls , '<STR_LIT>' % self . cls ) <EOL> return '<STR_LIT>' % ( self . name , t , c , self . ttl , self . auth and '<STR_LIT:True>' or '<STR_LIT:False>' ) <EOL> __repr__ = __str__ <EOL> class SimpleRecord ( tputil . FancyStrMixin , tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> showAttributes = ( ( '<STR_LIT:name>' , '<STR_LIT:name>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> compareAttributes = ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> TYPE = None <EOL> name = None <EOL> def __init__ ( self , name = '<STR_LIT>' , ttl = None ) : <EOL> self . name = Name ( name ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> self . name . encode ( strio , compDict ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . name = Name ( ) <EOL> self . name . decode ( strio ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . name ) <EOL> class Record_NS ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = NS <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_MD ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = MD <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_MF ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = MF <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_CNAME ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = CNAME <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_MB ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = MB <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_MG ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = MG <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_MR ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = MR <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_PTR ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = PTR <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_DNAME ( SimpleRecord ) : <EOL> """<STR_LIT>""" <EOL> TYPE = DNAME <EOL> fancybasename = '<STR_LIT>' <EOL> class Record_A ( tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> compareAttributes = ( '<STR_LIT:address>' , '<STR_LIT>' ) <EOL> TYPE = A <EOL> address = None <EOL> def __init__ ( self , address = '<STR_LIT>' , ttl = None ) : <EOL> address = socket . inet_aton ( address ) <EOL> self . address = address <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( self . address ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . address = readPrecisely ( strio , <NUM_LIT:4> ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . address ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . dottedQuad ( ) , self . ttl ) <EOL> __repr__ = __str__ <EOL> def dottedQuad ( self ) : <EOL> return socket . inet_ntoa ( self . address ) <EOL> class Record_SOA ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> fancybasename = '<STR_LIT>' <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> showAttributes = ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> TYPE = SOA <EOL> def __init__ ( self , mname = '<STR_LIT>' , rname = '<STR_LIT>' , serial = <NUM_LIT:0> , refresh = <NUM_LIT:0> , retry = <NUM_LIT:0> , expire = <NUM_LIT:0> , minimum = <NUM_LIT:0> , ttl = None ) : <EOL> self . mname , self . rname = Name ( mname ) , Name ( rname ) <EOL> self . serial , self . refresh = str2time ( serial ) , str2time ( refresh ) <EOL> self . minimum , self . expire = str2time ( minimum ) , str2time ( expire ) <EOL> self . retry = str2time ( retry ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> self . mname . encode ( strio , compDict ) <EOL> self . rname . encode ( strio , compDict ) <EOL> strio . write ( <EOL> struct . pack ( <EOL> '<STR_LIT>' , <EOL> self . serial , self . refresh , self . retry , self . expire , <EOL> self . minimum <EOL> ) <EOL> ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . mname , self . rname = Name ( ) , Name ( ) <EOL> self . mname . decode ( strio ) <EOL> self . rname . decode ( strio ) <EOL> r = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:20> ) ) <EOL> self . serial , self . refresh , self . retry , self . expire , self . minimum = r <EOL> def __hash__ ( self ) : <EOL> return hash ( ( <EOL> self . serial , self . mname , self . rname , <EOL> self . refresh , self . expire , self . retry <EOL> ) ) <EOL> class Record_NULL ( tputil . FancyStrMixin , tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> fancybasename = '<STR_LIT>' <EOL> showAttributes = compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> TYPE = NULL <EOL> def __init__ ( self , payload = None , ttl = None ) : <EOL> self . payload = payload <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( self . payload ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . payload = readPrecisely ( strio , length ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . payload ) <EOL> class Record_WKS ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> fancybasename = "<STR_LIT>" <EOL> compareAttributes = ( '<STR_LIT:address>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> showAttributes = [ ( '<STR_LIT>' , '<STR_LIT:address>' , '<STR_LIT:%s>' ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> TYPE = WKS <EOL> _address = property ( lambda self : socket . inet_ntoa ( self . address ) ) <EOL> def __init__ ( self , address = '<STR_LIT>' , protocol = <NUM_LIT:0> , map = '<STR_LIT>' , ttl = None ) : <EOL> self . address = socket . inet_aton ( address ) <EOL> self . protocol , self . map = protocol , map <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( self . address ) <EOL> strio . write ( struct . pack ( '<STR_LIT>' , self . protocol ) ) <EOL> strio . write ( self . map ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . address = readPrecisely ( strio , <NUM_LIT:4> ) <EOL> self . protocol = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> self . map = readPrecisely ( strio , length - <NUM_LIT:5> ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . address , self . protocol , self . map ) ) <EOL> class Record_AAAA ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = AAAA <EOL> fancybasename = '<STR_LIT>' <EOL> showAttributes = ( ( '<STR_LIT>' , '<STR_LIT:address>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> compareAttributes = ( '<STR_LIT:address>' , '<STR_LIT>' ) <EOL> _address = property ( lambda self : socket . inet_ntop ( AF_INET6 , self . address ) ) <EOL> def __init__ ( self , address = '<STR_LIT>' , ttl = None ) : <EOL> self . address = socket . inet_pton ( AF_INET6 , address ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( self . address ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . address = readPrecisely ( strio , <NUM_LIT:16> ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . address ) <EOL> class Record_A6 ( tputil . FancyStrMixin , tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = A6 <EOL> fancybasename = '<STR_LIT>' <EOL> showAttributes = ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> _suffix = property ( lambda self : socket . inet_ntop ( AF_INET6 , self . suffix ) ) <EOL> def __init__ ( self , prefixLen = <NUM_LIT:0> , suffix = '<STR_LIT>' , prefix = '<STR_LIT>' , ttl = None ) : <EOL> self . prefixLen = prefixLen <EOL> self . suffix = socket . inet_pton ( AF_INET6 , suffix ) <EOL> self . prefix = Name ( prefix ) <EOL> self . bytes = int ( ( <NUM_LIT> - self . prefixLen ) / <NUM_LIT> ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , self . prefixLen ) ) <EOL> if self . bytes : <EOL> strio . write ( self . suffix [ - self . bytes : ] ) <EOL> if self . prefixLen : <EOL> self . prefix . encode ( strio , None ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . prefixLen = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> self . bytes = int ( ( <NUM_LIT> - self . prefixLen ) / <NUM_LIT> ) <EOL> if self . bytes : <EOL> self . suffix = '<STR_LIT:\x00>' * ( <NUM_LIT:16> - self . bytes ) + readPrecisely ( strio , self . bytes ) <EOL> if self . prefixLen : <EOL> self . prefix . decode ( strio ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Record_A6 ) : <EOL> return ( self . prefixLen == other . prefixLen and <EOL> self . suffix [ - self . bytes : ] == other . suffix [ - self . bytes : ] and <EOL> self . prefix == other . prefix and <EOL> self . ttl == other . ttl ) <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . prefixLen , self . suffix [ - self . bytes : ] , self . prefix ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . prefix , <EOL> socket . inet_ntop ( AF_INET6 , self . suffix ) , <EOL> self . prefixLen , self . ttl <EOL> ) <EOL> class Record_SRV ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = SRV <EOL> fancybasename = '<STR_LIT>' <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT:port>' , '<STR_LIT>' ) <EOL> showAttributes = ( '<STR_LIT>' , '<STR_LIT>' , ( '<STR_LIT:target>' , '<STR_LIT:target>' , '<STR_LIT:%s>' ) , '<STR_LIT:port>' , '<STR_LIT>' ) <EOL> def __init__ ( self , priority = <NUM_LIT:0> , weight = <NUM_LIT:0> , port = <NUM_LIT:0> , target = '<STR_LIT>' , ttl = None ) : <EOL> self . priority = int ( priority ) <EOL> self . weight = int ( weight ) <EOL> self . port = int ( port ) <EOL> self . target = Name ( target ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , self . priority , self . weight , self . port ) ) <EOL> self . target . encode ( strio , None ) <EOL> def decode ( self , strio , length = None ) : <EOL> r = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , struct . calcsize ( '<STR_LIT>' ) ) ) <EOL> self . priority , self . weight , self . port = r <EOL> self . target = Name ( ) <EOL> self . target . decode ( strio ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . priority , self . weight , self . port , self . target ) ) <EOL> class Record_NAPTR ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = NAPTR <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> fancybasename = '<STR_LIT>' <EOL> showAttributes = ( '<STR_LIT>' , '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> def __init__ ( self , order = <NUM_LIT:0> , preference = <NUM_LIT:0> , flags = '<STR_LIT>' , service = '<STR_LIT>' , regexp = '<STR_LIT>' , <EOL> replacement = '<STR_LIT>' , ttl = None ) : <EOL> self . order = int ( order ) <EOL> self . preference = int ( preference ) <EOL> self . flags = Charstr ( flags ) <EOL> self . service = Charstr ( service ) <EOL> self . regexp = Charstr ( regexp ) <EOL> self . replacement = Name ( replacement ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , self . order , self . preference ) ) <EOL> self . flags . encode ( strio , None ) <EOL> self . service . encode ( strio , None ) <EOL> self . regexp . encode ( strio , None ) <EOL> self . replacement . encode ( strio , None ) <EOL> def decode ( self , strio , length = None ) : <EOL> r = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , struct . calcsize ( '<STR_LIT>' ) ) ) <EOL> self . order , self . preference = r <EOL> self . flags = Charstr ( ) <EOL> self . service = Charstr ( ) <EOL> self . regexp = Charstr ( ) <EOL> self . replacement = Name ( ) <EOL> self . flags . decode ( strio ) <EOL> self . service . decode ( strio ) <EOL> self . regexp . decode ( strio ) <EOL> self . replacement . decode ( strio ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( <EOL> self . order , self . preference , self . flags , <EOL> self . service , self . regexp , self . replacement ) ) <EOL> class Record_AFSDB ( tputil . FancyStrMixin , tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = AFSDB <EOL> fancybasename = '<STR_LIT>' <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> showAttributes = ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> def __init__ ( self , subtype = <NUM_LIT:0> , hostname = '<STR_LIT>' , ttl = None ) : <EOL> self . subtype = int ( subtype ) <EOL> self . hostname = Name ( hostname ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , self . subtype ) ) <EOL> self . hostname . encode ( strio , compDict ) <EOL> def decode ( self , strio , length = None ) : <EOL> r = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , struct . calcsize ( '<STR_LIT>' ) ) ) <EOL> self . subtype , = r <EOL> self . hostname . decode ( strio ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . subtype , self . hostname ) ) <EOL> class Record_RP ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = RP <EOL> fancybasename = '<STR_LIT>' <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> showAttributes = ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> def __init__ ( self , mbox = '<STR_LIT>' , txt = '<STR_LIT>' , ttl = None ) : <EOL> self . mbox = Name ( mbox ) <EOL> self . txt = Name ( txt ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> self . mbox . encode ( strio , compDict ) <EOL> self . txt . encode ( strio , compDict ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . mbox = Name ( ) <EOL> self . txt = Name ( ) <EOL> self . mbox . decode ( strio ) <EOL> self . txt . decode ( strio ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . mbox , self . txt ) ) <EOL> class Record_HINFO ( tputil . FancyStrMixin , tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = HINFO <EOL> fancybasename = '<STR_LIT>' <EOL> showAttributes = compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , cpu = '<STR_LIT>' , os = '<STR_LIT>' , ttl = None ) : <EOL> self . cpu , self . os = cpu , os <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , len ( self . cpu ) ) + self . cpu ) <EOL> strio . write ( struct . pack ( '<STR_LIT>' , len ( self . os ) ) + self . os ) <EOL> def decode ( self , strio , length = None ) : <EOL> cpu = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> self . cpu = readPrecisely ( strio , cpu ) <EOL> os = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> self . os = readPrecisely ( strio , os ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Record_HINFO ) : <EOL> return ( self . os . lower ( ) == other . os . lower ( ) and <EOL> self . cpu . lower ( ) == other . cpu . lower ( ) and <EOL> self . ttl == other . ttl ) <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . os . lower ( ) , self . cpu . lower ( ) ) ) <EOL> class Record_MINFO ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = MINFO <EOL> rmailbx = None <EOL> emailbx = None <EOL> fancybasename = '<STR_LIT>' <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> showAttributes = ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' ) , <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , rmailbx = '<STR_LIT>' , emailbx = '<STR_LIT>' , ttl = None ) : <EOL> self . rmailbx , self . emailbx = Name ( rmailbx ) , Name ( emailbx ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> self . rmailbx . encode ( strio , compDict ) <EOL> self . emailbx . encode ( strio , compDict ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . rmailbx , self . emailbx = Name ( ) , Name ( ) <EOL> self . rmailbx . decode ( strio ) <EOL> self . emailbx . decode ( strio ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . rmailbx , self . emailbx ) ) <EOL> class Record_MX ( tputil . FancyStrMixin , tputil . FancyEqMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = MX <EOL> fancybasename = '<STR_LIT>' <EOL> compareAttributes = ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> showAttributes = ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT:name>' , '<STR_LIT:%s>' ) , '<STR_LIT>' ) <EOL> def __init__ ( self , preference = <NUM_LIT:0> , name = '<STR_LIT>' , ttl = None , ** kwargs ) : <EOL> self . preference , self . name = int ( preference ) , Name ( kwargs . get ( '<STR_LIT>' , name ) ) <EOL> self . ttl = str2time ( ttl ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , self . preference ) ) <EOL> self . name . encode ( strio , compDict ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . preference = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] <EOL> self . name = Name ( ) <EOL> self . name . decode ( strio ) <EOL> def exchange ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> return self . name <EOL> exchange = property ( exchange ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . preference , self . name ) ) <EOL> class Record_TXT ( tputil . FancyEqMixin , tputil . FancyStrMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IEncodable , IRecord ) <EOL> TYPE = TXT <EOL> fancybasename = '<STR_LIT>' <EOL> showAttributes = compareAttributes = ( '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> def __init__ ( self , * data , ** kw ) : <EOL> self . data = list ( data ) <EOL> self . ttl = str2time ( kw . get ( '<STR_LIT>' , None ) ) <EOL> def encode ( self , strio , compDict = None ) : <EOL> for d in self . data : <EOL> strio . write ( struct . pack ( '<STR_LIT>' , len ( d ) ) + d ) <EOL> def decode ( self , strio , length = None ) : <EOL> soFar = <NUM_LIT:0> <EOL> self . data = [ ] <EOL> while soFar < length : <EOL> L = struct . unpack ( '<STR_LIT>' , readPrecisely ( strio , <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> self . data . append ( readPrecisely ( strio , L ) ) <EOL> soFar += L + <NUM_LIT:1> <EOL> if soFar != length : <EOL> log . msg ( <EOL> "<STR_LIT>" % ( <EOL> soFar , self . fancybasename , length <EOL> ) <EOL> ) <EOL> def __hash__ ( self ) : <EOL> return hash ( tuple ( self . data ) ) <EOL> class Record_SPF ( Record_TXT ) : <EOL> """<STR_LIT>""" <EOL> TYPE = SPF <EOL> fancybasename = '<STR_LIT>' <EOL> class Message : <EOL> """<STR_LIT>""" <EOL> headerFmt = "<STR_LIT>" <EOL> headerSize = struct . calcsize ( headerFmt ) <EOL> queries = answers = add = ns = None <EOL> def __init__ ( self , id = <NUM_LIT:0> , answer = <NUM_LIT:0> , opCode = <NUM_LIT:0> , recDes = <NUM_LIT:0> , recAv = <NUM_LIT:0> , <EOL> auth = <NUM_LIT:0> , rCode = OK , trunc = <NUM_LIT:0> , maxSize = <NUM_LIT> ) : <EOL> self . maxSize = maxSize <EOL> self . id = id <EOL> self . answer = answer <EOL> self . opCode = opCode <EOL> self . auth = auth <EOL> self . trunc = trunc <EOL> self . recDes = recDes <EOL> self . recAv = recAv <EOL> self . rCode = rCode <EOL> self . queries = [ ] <EOL> self . answers = [ ] <EOL> self . authority = [ ] <EOL> self . additional = [ ] <EOL> def addQuery ( self , name , type = ALL_RECORDS , cls = IN ) : <EOL> """<STR_LIT>""" <EOL> self . queries . append ( Query ( name , type , cls ) ) <EOL> def encode ( self , strio ) : <EOL> compDict = { } <EOL> body_tmp = StringIO . StringIO ( ) <EOL> for q in self . queries : <EOL> q . encode ( body_tmp , compDict ) <EOL> for q in self . answers : <EOL> q . encode ( body_tmp , compDict ) <EOL> for q in self . authority : <EOL> q . encode ( body_tmp , compDict ) <EOL> for q in self . additional : <EOL> q . encode ( body_tmp , compDict ) <EOL> body = body_tmp . getvalue ( ) <EOL> size = len ( body ) + self . headerSize <EOL> if self . maxSize and size > self . maxSize : <EOL> self . trunc = <NUM_LIT:1> <EOL> body = body [ : self . maxSize - self . headerSize ] <EOL> byte3 = ( ( ( self . answer & <NUM_LIT:1> ) << <NUM_LIT:7> ) <EOL> | ( ( self . opCode & <NUM_LIT> ) << <NUM_LIT:3> ) <EOL> | ( ( self . auth & <NUM_LIT:1> ) << <NUM_LIT:2> ) <EOL> | ( ( self . trunc & <NUM_LIT:1> ) << <NUM_LIT:1> ) <EOL> | ( self . recDes & <NUM_LIT:1> ) ) <EOL> byte4 = ( ( ( self . recAv & <NUM_LIT:1> ) << <NUM_LIT:7> ) <EOL> | ( self . rCode & <NUM_LIT> ) ) <EOL> strio . write ( struct . pack ( self . headerFmt , self . id , byte3 , byte4 , <EOL> len ( self . queries ) , len ( self . answers ) , <EOL> len ( self . authority ) , len ( self . additional ) ) ) <EOL> strio . write ( body ) <EOL> def decode ( self , strio , length = None ) : <EOL> self . maxSize = <NUM_LIT:0> <EOL> header = readPrecisely ( strio , self . headerSize ) <EOL> r = struct . unpack ( self . headerFmt , header ) <EOL> self . id , byte3 , byte4 , nqueries , nans , nns , nadd = r <EOL> self . answer = ( byte3 >> <NUM_LIT:7> ) & <NUM_LIT:1> <EOL> self . opCode = ( byte3 >> <NUM_LIT:3> ) & <NUM_LIT> <EOL> self . auth = ( byte3 >> <NUM_LIT:2> ) & <NUM_LIT:1> <EOL> self . trunc = ( byte3 >> <NUM_LIT:1> ) & <NUM_LIT:1> <EOL> self . recDes = byte3 & <NUM_LIT:1> <EOL> self . recAv = ( byte4 >> <NUM_LIT:7> ) & <NUM_LIT:1> <EOL> self . rCode = byte4 & <NUM_LIT> <EOL> self . queries = [ ] <EOL> for i in range ( nqueries ) : <EOL> q = Query ( ) <EOL> try : <EOL> q . decode ( strio ) <EOL> except EOFError : <EOL> return <EOL> self . queries . append ( q ) <EOL> items = ( ( self . answers , nans ) , ( self . authority , nns ) , ( self . additional , nadd ) ) <EOL> for ( l , n ) in items : <EOL> self . parseRecords ( l , n , strio ) <EOL> def parseRecords ( self , list , num , strio ) : <EOL> for i in range ( num ) : <EOL> header = RRHeader ( ) <EOL> try : <EOL> header . decode ( strio ) <EOL> except EOFError : <EOL> return <EOL> t = self . lookupRecordType ( header . type ) <EOL> if not t : <EOL> continue <EOL> header . payload = t ( ttl = header . ttl ) <EOL> try : <EOL> header . payload . decode ( strio , header . rdlength ) <EOL> except EOFError : <EOL> return <EOL> list . append ( header ) <EOL> _recordTypes = { } <EOL> for name in globals ( ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> _recordTypes [ globals ( ) [ name ] . TYPE ] = globals ( ) [ name ] <EOL> del name <EOL> def lookupRecordType ( self , type ) : <EOL> """<STR_LIT>""" <EOL> return self . _recordTypes . get ( type , None ) <EOL> def toStr ( self ) : <EOL> strio = StringIO . StringIO ( ) <EOL> self . encode ( strio ) <EOL> return strio . getvalue ( ) <EOL> def fromStr ( self , str ) : <EOL> strio = StringIO . StringIO ( str ) <EOL> self . decode ( strio ) <EOL> class DNSMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> id = None <EOL> liveMessages = None <EOL> def __init__ ( self , controller , reactor = None ) : <EOL> self . controller = controller <EOL> self . id = random . randrange ( <NUM_LIT:2> ** <NUM_LIT:10> , <NUM_LIT:2> ** <NUM_LIT:15> ) <EOL> if reactor is None : <EOL> from twisted . internet import reactor <EOL> self . _reactor = reactor <EOL> def pickID ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> id = randomSource ( ) <EOL> if id not in self . liveMessages : <EOL> return id <EOL> def callLater ( self , period , func , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . _reactor . callLater ( period , func , * args ) <EOL> def _query ( self , queries , timeout , id , writeMessage ) : <EOL> """<STR_LIT>""" <EOL> m = Message ( id , recDes = <NUM_LIT:1> ) <EOL> m . queries = queries <EOL> try : <EOL> writeMessage ( m ) <EOL> except : <EOL> return defer . fail ( ) <EOL> resultDeferred = defer . Deferred ( ) <EOL> cancelCall = self . callLater ( timeout , self . _clearFailed , resultDeferred , id ) <EOL> self . liveMessages [ id ] = ( resultDeferred , cancelCall ) <EOL> return resultDeferred <EOL> def _clearFailed ( self , deferred , id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . liveMessages [ id ] <EOL> except KeyError : <EOL> pass <EOL> deferred . errback ( failure . Failure ( DNSQueryTimeoutError ( id ) ) ) <EOL> class DNSDatagramProtocol ( DNSMixin , protocol . DatagramProtocol ) : <EOL> """<STR_LIT>""" <EOL> resends = None <EOL> def stopProtocol ( self ) : <EOL> """<STR_LIT>""" <EOL> self . liveMessages = { } <EOL> self . resends = { } <EOL> self . transport = None <EOL> def startProtocol ( self ) : <EOL> """<STR_LIT>""" <EOL> self . liveMessages = { } <EOL> self . resends = { } <EOL> def writeMessage ( self , message , address ) : <EOL> """<STR_LIT>""" <EOL> self . transport . write ( message . toStr ( ) , address ) <EOL> def startListening ( self ) : <EOL> self . _reactor . listenUDP ( <NUM_LIT:0> , self , maxPacketSize = <NUM_LIT> ) <EOL> def datagramReceived ( self , data , addr ) : <EOL> """<STR_LIT>""" <EOL> m = Message ( ) <EOL> try : <EOL> m . fromStr ( data ) <EOL> except EOFError : <EOL> log . msg ( "<STR_LIT>" % ( len ( data ) , addr ) ) <EOL> return <EOL> except : <EOL> log . err ( failure . Failure ( ) , "<STR_LIT>" ) <EOL> return <EOL> if m . id in self . liveMessages : <EOL> d , canceller = self . liveMessages [ m . id ] <EOL> del self . liveMessages [ m . id ] <EOL> canceller . cancel ( ) <EOL> try : <EOL> d . callback ( m ) <EOL> except : <EOL> log . err ( ) <EOL> else : <EOL> if m . id not in self . resends : <EOL> self . controller . messageReceived ( m , self , addr ) <EOL> def removeResend ( self , id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . resends [ id ] <EOL> except KeyError : <EOL> pass <EOL> def query ( self , address , queries , timeout = <NUM_LIT:10> , id = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . transport : <EOL> try : <EOL> self . startListening ( ) <EOL> except CannotListenError : <EOL> return defer . fail ( ) <EOL> if id is None : <EOL> id = self . pickID ( ) <EOL> else : <EOL> self . resends [ id ] = <NUM_LIT:1> <EOL> def writeMessage ( m ) : <EOL> self . writeMessage ( m , address ) <EOL> return self . _query ( queries , timeout , id , writeMessage ) <EOL> class DNSProtocol ( DNSMixin , protocol . Protocol ) : <EOL> """<STR_LIT>""" <EOL> length = None <EOL> buffer = '<STR_LIT>' <EOL> def writeMessage ( self , message ) : <EOL> """<STR_LIT>""" <EOL> s = message . toStr ( ) <EOL> self . transport . write ( struct . pack ( '<STR_LIT>' , len ( s ) ) + s ) <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> self . liveMessages = { } <EOL> self . controller . connectionMade ( self ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . controller . connectionLost ( self ) <EOL> def dataReceived ( self , data ) : <EOL> self . buffer += data <EOL> while self . buffer : <EOL> if self . length is None and len ( self . buffer ) >= <NUM_LIT:2> : <EOL> self . length = struct . unpack ( '<STR_LIT>' , self . buffer [ : <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] <EOL> self . buffer = self . buffer [ <NUM_LIT:2> : ] <EOL> if len ( self . buffer ) >= self . length : <EOL> myChunk = self . buffer [ : self . length ] <EOL> m = Message ( ) <EOL> m . fromStr ( myChunk ) <EOL> try : <EOL> d , canceller = self . liveMessages [ m . id ] <EOL> except KeyError : <EOL> self . controller . messageReceived ( m , self ) <EOL> else : <EOL> del self . liveMessages [ m . id ] <EOL> canceller . cancel ( ) <EOL> try : <EOL> d . callback ( m ) <EOL> except : <EOL> log . err ( ) <EOL> self . buffer = self . buffer [ self . length : ] <EOL> self . length = None <EOL> else : <EOL> break <EOL> def query ( self , queries , timeout = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> id = self . pickID ( ) <EOL> return self . _query ( queries , timeout , id , self . writeMessage ) </s>
