import sqlite3, socket, struct
from twisted.internet import reactor, defer
from twisted.names import client, dns

conn = sqlite3.connect('dns.db')
c = conn.cursor()

#c.execute('''CREATE TABLE dns (name varchar(255), ip int)''')


sem = defer.DeferredSemaphore(112)

def dottedQuadToNum(ip):
    "convert decimal dotted quad string to long integer"
    return struct.unpack('L',socket.inet_aton(ip))[0]

def got_ptr(args, addr, level):
	(ans, auth, add) = args
	if level == 4:
		records = {}
		print '\n', addr
		print 'Additional:'
		for A in add:
			if A.type is dns.A:
				print A.name, A.payload.dottedQuad()
				records[A.name] = A.payload.dottedQuad()
		print 'Authoritative:'
		for NS in auth:
			if NS.type is dns.NS:
				if NS.payload.name not in records:
					records[NS.payload.name] = None
					print NS.payload.name, "No IP"
	else:
		lookup(postfix=addr, level=level + 1)

def no_ptr(*args):
	pass

def lookup(postfix=None, level=1):
	for octet in range(0,10):
		if postfix:
			addr = "%i.%s" % (octet, postfix)
		else:
			addr = "%i.in-addr.arpa" % octet

		f = lambda addr: client.lookupPointer(addr).addCallback(got_ptr, addr, level).addErrback(no_ptr)

		sem.run(f, addr)

lookup()
reactor.run()

@@ -163,9 +163,8 @@
 ()%0A%0A
-#
 c.ex
@@ -267,85 +267,84 @@
 def 
-dottedQuadToNum(ip):%0A    %22convert decimal dotted quad string to long integer%22
+ip2int(addr):                                                               
 %0A   
@@ -369,12 +369,14 @@
 ack(
-'L',
+%22!I%22, 
 sock
@@ -392,15 +392,178 @@
 ton(
-ip
+addr
 ))%5B0%5D
+                       %0A%0Adef int2ip(addr):                                                               %0A    return socket.inet_ntoa(struct.pack(%22!I%22, addr))   
 %0A%0Ade
@@ -783,24 +783,29 @@
 cords%5BA.name
+.name
 %5D = A.payloa
@@ -909,16 +909,21 @@
 oad.name
+.name
  not in 
@@ -955,24 +955,29 @@
 payload.name
+.name
 %5D = None%0A%09%09%09
@@ -999,16 +999,21 @@
 oad.name
+.name
 , %22No IP
@@ -1014,16 +1014,208 @@
 %22No IP%22%0A
+%09%09for name, ip in records.items():%0A%09%09%09if ip:%0A%09%09%09%09t = name, ip, ip2int(ip), int2ip(ip2int(ip))%0A%09%09%09else:%0A%09%09%09%09t = name,%0A%09%09%09print t%0A%09%09%09#c.execute(%22INSERT INTO dns VALUES (?,?)%22, t)%0A%09%09%09#c.commit()%0A
 %09else:%0A%09

encode ip