summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns/resource.py
blob: 57fc53e9fa05a57c7d954449ae3c380d03bf7bc6 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python2

""" A DNS resource record

This class contains classes for DNS resource records and record data. This
module is fully implemented. You will have this module in the implementation
of your resolver and server.
"""

import socket
import struct

from dns.classes import Class
from dns.types import Type


class ResourceRecord(object):
    """ DNS resource record """
    def __init__(self, name, type_, class_, ttl, rdata):
        """ Create a new resource record

        Args:
            name (str): domain name
            type_ (Type): the type
            class_ (Class): the class
            rdata (RecordData): the record data
        """
        self.name     = name
        self.type_    = type_
        self.class_   = class_
        self.ttl      = ttl
        self.rdata    = rdata

    def to_bytes(self, offset, composer):
        """ Convert ResourceRecord to bytes """
        name = composer.to_bytes(offset, [self.name])
        offset += len(name)
        rdata = self.rdata.to_bytes(offset, composer)
        return (name +
            struct.pack("!HHIH",
                self.type_,
                self.class_,
                self.ttl,
                len(rdata)) + 
            rdata)

    @classmethod
    def from_bytes(cls, packet, offset, parser):
        """ Convert ResourceRecord from bytes """
        names, offset = parser.from_bytes(packet, offset, 1)
        name = names[0]
        type_, class_, ttl, rdlength = struct.unpack_from("!HHIH", packet, offset)
        offset += 10
        rdata = RecordData.from_bytes(type_, packet, offset, rdlength, parser)
        offset += rdlength
        return cls(name, type_, class_, ttl, rdata), offset


class RecordData(object):
    """ Record Data """

    def __init__(self, data):
        """ Initialize the record data

        Args:
            data (str): data
        """
        self.data = data

    @staticmethod
    def create(type_, data):
        """ Create a RecordData object from bytes

        Args:
            type_ (Type): type
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        classdict = {
            Type.A: ARecordData,
            Type.CNAME: CNAMERecordData,
            Type.NS: NSRecordData,
            Type.AAAA: AAAARecordData
        }
        if type_ in classdict:
            return classdict[type_](data)
        else:
            return GenericRecordData(data)

    @staticmethod
    def from_bytes(type_, packet, offset, rdlength, parser):
        """ Create a RecordData object from bytes

        Args:
            type_ (Type): type
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        classdict = {
            Type.A: ARecordData,
            Type.CNAME: CNAMERecordData,
            Type.NS: NSRecordData,
            Type.AAAA: AAAARecordData
        }
        if type_ in classdict:
            return classdict[type_].from_bytes(
                packet, offset, rdlength, parser)
        else:
            return GenericRecordData.from_bytes(
                packet, offset, rdlength, parser)


class ARecordData(RecordData):
    def to_bytes(self, offset, composer):
        """ Convert to bytes

        Args:
            offset (int): offset in message
            composer (Composer): domain name composer
        """
        return socket.inet_aton(self.data)

    @classmethod
    def from_bytes(cls, packet, offset, rdlength, parser):
        """ Create a RecordData object from bytes

        Args:
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        data = socket.inet_ntoa(packet[offset:offset+4])
        return cls(data)


class CNAMERecordData(RecordData):
    def to_bytes(self, offset, composer):
        """ Convert to bytes

        Args:
            offset (int): offset in message
            composer (Composer): domain name composer
        """
        return composer.to_bytes(offset, [self.data])

    @classmethod
    def from_bytes(cls, packet, offset, rdlength, parser):
        """ Create a RecordData object from bytes

        Args:
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        names, offset = parser.from_bytes(packet, offset, 1)
        data = names[0]
        return cls(data)


class NSRecordData(RecordData):
    def to_bytes(self, offset, composer):
        """ Convert to bytes

        Args:
            offset (int): offset in message
            composer (Composer): domain name composer
        """
        return composer.to_bytes(offset, [self.data])

    @classmethod
    def from_bytes(cls, packet, offset, rdlength, parser):
        """ Create a RecordData object from bytes

        Args:
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        names, offset = parser.from_bytes(packet, offset, 1)
        data = names[0]
        return cls(data)


class AAAARecordData(RecordData):
    def to_bytes(self, offset, composer):
        """ Convert to bytes

        Args:
            offset (int): offset in message
            composer (Composer): domain name composer
        """
        return socket.inet_pton(socket.AF_INET6, self.data)

    @classmethod
    def from_bytes(packet, offset, rdlength, parser):
        """ Create a RecordData object from bytes

        Args:
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        data = socket.inet_ntop(socket.AF_INET6, packet[offset:offset+16])
        return cls(data)


class GenericRecordData(RecordData):
    def to_bytes(self, offset, composer):
        """ Convert to bytes

        Args:
            offset (int): offset in message
            composer (Composer): domain name composer
        """
        return self.data

    @classmethod
    def from_bytes(packet, offset, rdlength, parser):
        """ Create a RecordData object from bytes

        Args:
            packet (bytes): packet
            offset (int): offset in message
            rdlength (int): length of rdata
            parser (int): domain name parser
        """
        data = packet[offset:offset+rdlength]
        return cls(data)