summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns/server.py
blob: f01043d13e1c9b3ae92cd2a6dd2105aea8fb0c71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
""" A recursive DNS server

This module provides a recursive DNS server. You will have to implement this
server using the algorithm described in section 4.3.2 of RFC 1034.
"""

import re
from threading import Thread

import dns.regexes as rgx
from dns.classes import Class
from dns.types import Type


class RequestHandler(Thread):
    """ A handler for requests to the DNS server """

    def __init__(self):
        """ Initialize the handler thread """
        super(RequestHandler, self).__init__()
        self.daemon = True

    def run(self):
        """ Run the handler thread """
        # TODO: Handle DNS request
        pass


class Server(object):
    """ A recursive DNS server """

    def __init__(self, port, caching, ttl):
        """ Initialize the server

        Args:
            port (int): port that server is listening on
            caching (bool): server uses resolver with caching if true
            ttl (int): ttl for records (if > 0) of cache
        """
        self.caching = caching
        self.ttl = ttl
        self.port = port
        self.done = False
        # TODO: create socket

    def serve(self):
        """ Start serving request """
        # TODO: start listening
        while not self.done:
            # TODO: receive request and open handler
            pass

    def shutdown(self):
        """ Shutdown the server """
        self.done = True
        # TODO: shutdown socket

    def parse_zone_file(self, fname):
        with open(fname) as zonef:
            zone = zonef.read()
            for match in re.finditer(rgx.ZONE_LINE_DOMAIN, zone, re.MULTILINE):
                match = match.groups()
                name = match[0]
                ttl = int(match[1] or match[4] or ttl)
                class_ = Class.from_string(
                    match[2] or match[3] or Class.to_string(class_))
                type_ = Type.from_string(match[5])
                data = match[6]
                print match
                print name, ttl, Class.to_string(class_), Type.to_string(type_), data