updated on Mon Jan 23 16:10:15 UTC 2012
[aur-mirror.git] / pyminifakedns / pyminifakedns.py
blob02e026bc511c996b94b94fd12397214687e570cf
1 #!/usr/bin/python2
3 import socket
5 class DNSQuery:
6 def __init__(self, data):
7 self.data=data
8 self.dominio=''
10 tipo = (ord(data[2]) >> 3) & 15
11 if tipo == 0:
12 ini=12
13 lon=ord(data[ini])
14 while lon != 0:
15 self.dominio+=data[ini+1:ini+lon+1]+'.'
16 ini+=lon+1
17 lon=ord(data[ini])
19 def respuesta(self, ip):
20 packet=''
21 if self.dominio:
22 packet+=self.data[:2] + "\x81\x80"
23 packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00'
24 packet+=self.data[12:]
25 packet+='\xc0\x0c'
26 packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'
27 packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.')))
28 return packet
30 if __name__ == '__main__':
31 ip='127.0.0.1'
32 print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip
34 udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
35 udps.bind(('',53))
37 try:
38 while 1:
39 data, addr = udps.recvfrom(1024)
40 p=DNSQuery(data)
41 udps.sendto(p.respuesta(ip), addr)
42 print 'Response: %s -> %s' % (p.dominio, ip)
43 except KeyboardInterrupt:
44 print 'Finalizing ...'
45 udps.close()