A simple DNS server instance written by Python

Source: Internet
Author: User
Tags unpack nslookup
Because suddenly there is an evil idea, want to build a DNS server on their own Android tablet, because the tablet installed before the sl4a and Python interpreter, but also want to continue to learn python so, it is intended to be implemented in Python.

Looking for a bit on Google, the Python implementation of DNS, did not find the answer I want, so I decided to come true.

Now the implementation of nothing advanced, just can be a record query simple matching and reply.

The implementation code is as follows:
Copy the Code code as follows:


'''
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 = da Ta[i]
If d = = 0:
break;
If D <:
Self.name = Self.name + '. '
Else:
Self.name = Self.name + chr (d)
i = i + 1
self.querybytes = data[0:i + 1]
(Self.type, Self.class ify) = 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 being 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 the 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 was 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 the "nslookup" command to test it
# Such as "nslookup www.aa.com"

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.