def lookup (self, qname, qtype, timeout=10, retries=3, port=53): m = new_packer() while 1: qid = random.randrange (65536) # avoid collisions if qid not in self.inflight_ids: break m.addHeader(qid, 0, packet.OPCODE.QUERY, 0, 0, 1, 0, 0,0,1,0, 0, 0) m.addQuestion (qname, qtype, packet.CLASS.IN) p = m.getbuf() for addr in self.nameservers: for i in range (retries): self.inflight.acquire (1) self.inflight_ids.add (qid) try: s = coro.udp_sock() s.connect ((addr, port)) s.send (p) try: reply = coro.with_timeout (timeout, s.recv, 3000) if not reply: continue r = unpack_reply(reply) if r.id != qid: raise QueryFailed ("bad id in reply") else: return r except coro.TimeoutError: pass finally: self.inflight.release (1) self.inflight_ids.remove (qid) raise QueryFailed ("no reply from nameservers")
def t1(qname, qtype): p = t0(qname, getattr(dns.TYPE, qtype)) s = coro.udp_sock() s.connect(('192.168.200.1', 53)) s.send(p) r = s.recv(8192) coro.write_stderr('reply=%r\n' % (r, )) u = dns.Unpacker(r) return u.unpack()
def t1 (qname, qtype): p = t0 (qname, getattr (dns.TYPE, qtype)) s = coro.udp_sock() s.connect (('192.168.200.1', 53)) s.send (p) r = s.recv (8192) coro.write_stderr ('reply=%r\n' % (r,)) u = dns.Unpacker (r) return u.unpack()
def lookup (self, qname, qtype, timeout=10, retries=3): m = packet.Packer() h = packet.Header() while 1: qid = random.randrange (65536) # avoid collisions if qid not in self.inflight_ids: break h.id = qid h.opcode = packet.OPCODE.QUERY h.rd = 1 h.qdcount = 1 m.addHeader (h) m.addQuestion (qname, qtype, packet.CLASS.IN) p = m.getbuf() for addr in self.nameservers: for i in range (retries): self.inflight.acquire (1) self.inflight_ids.add (qid) try: s = coro.udp_sock() s.connect ((addr, 53)) s.send (p) try: reply = coro.with_timeout (timeout, s.recv, 1000) u = packet.Unpacker (reply) result = u.unpack() rh = result[0] if rh.id != qid: raise QueryFailed ("bad id in reply") else: return result except coro.TimeoutError: pass finally: self.inflight.release (1) self.inflight_ids.remove (qid) raise QueryFailed ("no reply from nameservers")
def lookup(self, qname, qtype, timeout=10, retries=3): m = packet.Packer() h = packet.Header() while 1: qid = random.randrange(65536) # avoid collisions if qid not in self.inflight_ids: break h.id = qid h.opcode = packet.OPCODE.QUERY h.rd = 1 h.qdcount = 1 m.addHeader(h) m.addQuestion(qname, qtype, packet.CLASS.IN) p = m.getbuf() for addr in self.nameservers: for i in range(retries): self.inflight.acquire(1) self.inflight_ids.add(qid) try: s = coro.udp_sock() s.connect((addr, 53)) s.send(p) try: reply = coro.with_timeout(timeout, s.recv, 1000) u = packet.Unpacker(reply) result = u.unpack() rh = result[0] if rh.id != qid: raise QueryFailed("bad id in reply") else: return result except coro.TimeoutError: pass finally: self.inflight.release(1) self.inflight_ids.remove(qid) raise QueryFailed("no reply from nameservers")