Python写的一个简单DNS服务器实例
因为突然有个邪恶的想法,想在自己的android平板上面搭建一个dns服务器,因为平板上之前安装过sl4a和python的解释器,也想继续学学python因此,就打算用python实现了。
在google上面找了一下,python实现的dns,没找到我所希望的答案,因此就决定自己来实现了。
现在所实现的没什么高深的,只是能够对a记录查询进行简单的匹配和回复。
实现的代码如下:
'''
created on 2012-10-15
@author: robintang
'''
import socketserver
import struct
# dns query
class sindnsquery:
def __init__(self, data):
i = 1
self.name = ''
while true:
d = data[i]
if d == 0:
break;
if d < 32:
self.name = self.name + '.'
else:
self.name = self.name + chr(d)
i = i + 1
self.querybytes = data[0:i + 1]
(self.type, self.classify) = struct.unpack('>hh', data[i + 1:i + 5])
self.len = i + 5
def getbytes(self):
return self.querybytes + struct.pack('>hh', self.type, self.classify)
# dns answer rrs
# this class is also can be use as authority rrs or additional rrs
class sindnsanswer:
def __init__(self, ip):
self.name = 49164
self.type = 1
self.classify = 1
self.timetolive = 190
self.datalength = 4
self.ip = ip
def getbytes(self):
res = struct.pack('>hhhlh', self.name, self.type, self.classify, self.timetolive, self.datalength)
s = self.ip.split('.')
res = res + struct.pack('bbbb', int(s[0]), int(s[1]), int(s[2]), int(s[3]))
return res
# dns frame
# must initialized by a dns query frame
class sindnsframe:
def __init__(self, data):
(self.id, self.flags, self.quests, self.answers, self.author, self.addition) = struct.unpack('>hhhhhh', data[0:12])
self.query = sindnsquery(data[12:])
def getname(self):
return self.query.name
def setip(self, ip):
self.answer = sindnsanswer(ip)
self.answers = 1
self.flags = 33152
def getbytes(self):
res = struct.pack('>hhhhhh', self.id, self.flags, self.quests, self.answers, self.author, self.addition)
res = res + self.query.getbytes()
if self.answers != 0:
res = res + self.answer.getbytes()
return res
# a udphandler to handle dns query
class sindnsudphandler(socketserver.baserequesthandler):
def handle(self):
data = self.request[0].strip()
dns = sindnsframe(data)
socket = self.request[1]
namemap = sindnsserver.namemap
if(dns.query.type==1):
# if this is query a a record, then response it
name = dns.getname();
if namemap.__contains__(name):
# if have record, response it
dns.setip(namemap[name])
socket.sendto(dns.getbytes(), self.client_address)
elif namemap.__contains__('*'):
# response default address
dns.setip(namemap['*'])
socket.sendto(dns.getbytes(), self.client_address)
else:
# ignore it
socket.sendto(data, self.client_address)
else:
# if this is not query a a record, ignore it
socket.sendto(data, self.client_address)
# dns server
# it only support a record query
# user it, u can create a simple dns server
class sindnsserver:
def __init__(self, port=53):
sindnsserver.namemap = {}
self.port = port
def addname(self, name, ip):
sindnsserver.namemap[name] = ip
def start(self):
host, port = "0.0.0.0", self.port
server = socketserver.udpserver((host, port), sindnsudphandler)
server.serve_forever()
# now, test it
if __name__ == "__main__":
sev = sindnsserver()
sev.addname('www.aa.com', '192.168.0.1') # add a a record
sev.addname('www.bb.com', '192.168.0.2') # add a a record
sev.addname('*', '0.0.0.0') # default address
sev.start() # start dns server
# now, u can use "nslookup" command to test it
# such as "nslookup www.aa.com"