MCPcopy Create free account
hub / github.com/ActiveState/code / DNSQuery

Class DNSQuery

recipes/Python/491264_Mini_Fake_DNS_server/recipe-491264.py:3–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import socket
2
3class DNSQuery:
4 def __init__(self, data):
5 self.data=data
6 self.dominio=''
7
8 tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
9 if tipo == 0: # Standard query
10 ini=12
11 lon=ord(data[ini])
12 while lon != 0:
13 self.dominio+=data[ini+1:ini+lon+1]+'.'
14 ini+=lon+1
15 lon=ord(data[ini])
16
17 def respuesta(self, ip):
18 packet=''
19 if self.dominio:
20 packet+=self.data[:2] + "\x81\x80"
21 packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00' # Questions and Answers Counts
22 packet+=self.data[12:] # Original Domain Name Question
23 packet+='\xc0\x0c' # Pointer to domain name
24 packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04' # Response type, ttl and resource data length -> 4 bytes
25 packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
26 return packet
27
28if __name__ == '__main__':
29 ip='192.168.1.1'

Callers 1

recipe-491264.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected