Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def processTCPReply(self):
import time
import Lib
self.f = self.s.makefile('r')
header = self.f.read(2)
if len(header) < 2:
raise DNSError('EOF')
count = Lib.unpack16bit(header)
self.reply = self.f.read(count)
if len(self.reply) != count:
raise DNSError('incomplete reply')
self.time_finish=time.time()
self.args['server']=self.ns
return self.processReply()
" do the work of sending a TCP request "
self.response=None
for self.ns in server:
try:
self.socketInit(socket.AF_INET, socket.SOCK_STREAM)
self.time_start=time.time()
self.conn()
self.s.send(Lib.pack16bit(len(self.request))+self.request)
self.s.shutdown(1)
self.response=self.processTCPReply()
except socket.error:
continue
break
if not self.response:
raise DNSError('no working nameservers found')
def handle_request (self, name, **args):
global pool
if isinstance (name, str):
name = name.encode ("utf8")
args = self.argparse (name, args)
protocol = args ['protocol']
opcode = args ['opcode']
rd = args ['rd']
if type(args['qtype']) in (bytes, str):
try:
qtype = getattr (Type, args ['qtype'].upper ())
except AttributeError:
raise Base.DNSError('%s unknown query type' % name)
else:
qtype = args ['qtype']
qname = args ['name']
m = Lib.Mpacker()
m.addHeader(self.get_id (),
0, opcode, 0, 0, rd, 0, 0, 0,
1, 0, 0, 0)
m.addQuestion (qname, qtype, Class.IN)
request = m.getbuf ()
conn = getattr (pool, args ['protocol']) (args.get ('addr'))
conn.query (request, args, self.process_reply)
protocol = self.args['protocol']
self.port = self.args['port']
opcode = self.args['opcode']
rd = self.args['rd']
server=self.args['server']
if type(self.args['qtype']) == bytes:
try:
qtype = getattr(Type, self.args['qtype'].upper())
except AttributeError:
raise DNSError('unknown query type')
else:
qtype=self.args['qtype']
if 'name' not in self.args:
#print(self.args)
raise DNSError('nothing to lookup')
qname = self.args['name']
if qtype == Type.AXFR:
print('Query type AXFR, protocol forced to TCP')
protocol = 'tcp'
#print 'QTYPE %d(%s)' % (qtype, Type.typestr(qtype))
m = Lib.Mpacker()
# jesus. keywords and default args would be good. TODO.
m.addHeader(0,
0, opcode, 0, 0, rd, 0, 0, 0,
1, 0, 0, 0)
m.addQuestion(qname, qtype, Class.IN)
self.request = m.getbuf()
def processUDPReply(self):
import time,select
if self.args['timeout'] > 0:
r,w,e = select.select([self.s],[],[],self.args['timeout'])
if not len(r):
raise DNSError('Timeout')
self.reply = self.s.recv(1024)
self.time_finish=time.time()
self.args['server']=self.ns
return self.processReply()
for self.ns in server:
try:
# TODO. Handle timeouts &c correctly (RFC)
#self.s.connect((self.ns, self.port))
self.conn()
self.time_start=time.time()
if not self._async:
self.s.send(self.request)
self.response=self.processUDPReply()
#except socket.error:
except None:
continue
break
if not self.response:
if not self._async:
raise DNSError('no working nameservers found')