aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/measite/minidns/Record.java
blob: fb0b5d5f66dcf16e94e8a4f6bc68541e7d53a0a1 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package de.measite.minidns;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;

import de.measite.minidns.record.A;
import de.measite.minidns.record.AAAA;
import de.measite.minidns.record.CNAME;
import de.measite.minidns.record.Data;
import de.measite.minidns.record.NS;
import de.measite.minidns.record.SRV;
import de.measite.minidns.util.NameUtil;

/**
 * A generic DNS record.
 */
public class Record {

    /**
     * The record type.
     * {@see http://www.iana.org/assignments/dns-parameters}
     */
    public static enum TYPE {
        A(1),
        NS(2),
        MD(3),
        MF(4),
        CNAME(5),
        SOA(6),
        MB(7),
        MG(8),
        MR(9),
        NULL(10),
        WKS(11),
        PTR(12),
        HINFO(13),
        MINFO(14),
        MX(15),
        TXT(16),
        RP(17),
        AFSDB(18),
        X25(19),
        ISDN(20),
        RT(21),
        NSAP(22),
        NSAP_PTR(23),
        SIG(24),
        KEY(25),
        PX(26),
        GPOS(27),
        AAAA(28),
        LOC(29),
        NXT(30),
        EID(31),
        NIMLOC(32),
        SRV(33),
        ATMA(34),
        NAPTR(35),
        KX(36),
        CERT(37),
        A6(38),
        DNAME(39),
        SINK(40),
        OPT(41),
        APL(42),
        DS(43),
        SSHFP(44),
        IPSECKEY(45),
        RRSIG(46),
        NSEC(47),
        DNSKEY(48),
        DHCID(49),
        NSEC3(50),
        NSEC3PARAM(51),
        HIP(55),
        NINFO(56),
        RKEY(57),
        TALINK(58),
        SPF(99),
        UINFO(100),
        UID(101),
        GID(102),
        TKEY(249),
        TSIG(250),
        IXFR(251),
        AXFR(252),
        MAILB(253),
        MAILA(254),
        ANY(255),
        TA(32768),
        DLV(32769);

        /**
         * The value of this DNS record type.
         */
        private final int value;

        /**
         * Internal lookup table to map values to types.
         */
        private final static HashMap<Integer, TYPE> INVERSE_LUT =
                                        new HashMap<Integer, TYPE>();

        /**
         * Initialize the reverse lookup table.
         */
        static {
            for(TYPE t: TYPE.values()) {
                INVERSE_LUT.put(t.getValue(), t);
            }
        }

        /**
         * Create a new record type.
         * @param value The binary value of this type.
         */
        private TYPE(int value) {
            this.value = value;
        }

        /**
         * Retrieve the binary value of this type.
         * @return The binary value.
         */
        public int getValue() {
            return value;
        }

        /**
         * Retrieve the symbolic type of the binary value.
         * @param value The binary type value.
         * @return The symbolic tpye.
         */
        public static TYPE getType(int value) {
            return INVERSE_LUT.get(value);
        }
    };

    /**
     * The symbolic class of a DNS record (usually IN for Internet).
     */
    public static enum CLASS {
        IN(1),
        CH(3),
        HS(4),
        NONE(254),
        ANY(255);

        /**
         * Internal reverse lookup table to map binary class values to symbolic
         * names.
         */
        private final static HashMap<Integer, CLASS> INVERSE_LUT =
                                            new HashMap<Integer, CLASS>();

        /**
         * Initialize the interal reverse lookup table.
         */
        static {
            for(CLASS c: CLASS.values()) {
                INVERSE_LUT.put(c.getValue(), c);
            }
        }

        /**
         * The binary value of this dns class.
         */
        private final int value;

        /**
         * Create a new DNS class based on a binary value.
         * @param value The binary value of this DNS class.
         */
        private CLASS(int value) {
            this.value = value;
        }

        /**
         * Retrieve the binary value of this DNS class.
         * @return The binary value of this DNS class.
         */
        public int getValue() {
            return value;
        }

        /**
         * Retrieve the symbolic DNS class for a binary class value.
         * @param value The binary DNS class value.
         * @return The symbolic class instance.
         */
        public static CLASS getClass(int value) {
            return INVERSE_LUT.get(value);
        }

    }

    /**
     * The generic name of this record.
     */
    protected String name;

    /**
     * The type (and payload type) of this record.
     */
    protected TYPE type;

    /**
     * The record class (usually CLASS.IN).
     */
    protected CLASS clazz;

    /**
     * The ttl of this record.
     */
    protected long ttl;

    /**
     * The payload object of this record.
     */
    protected Data payloadData;

    /**
     * Parse a given record based on the full message data and the current
     * stream position.
     * @param dis The DataInputStream positioned at the first record byte.
     * @param data The full message data.
     * @throws IOException In case of malformed replies.
     */
    public void parse(DataInputStream dis, byte[] data) throws IOException {
        this.name = NameUtil.parse(dis, data);
        this.type = TYPE.getType(dis.readUnsignedShort());
        this.clazz = CLASS.getClass(dis.readUnsignedShort());
        this.ttl = (((long)dis.readUnsignedShort()) << 32) +
                   dis.readUnsignedShort();
        int payloadLength = dis.readUnsignedShort();
        switch (this.type) {
        case SRV:
            this.payloadData = new SRV();
            break;
        case AAAA:
            this.payloadData = new AAAA();
            break;
        case A:
            this.payloadData = new A();
            break;
        case NS:
            this.payloadData = new NS();
            break;
        case CNAME:
            this.payloadData = new CNAME();
            break;
        default:
            System.out.println("Unparsed type " + type);
            this.payloadData = null;
            for (int i = 0; i < payloadLength; i++) {
                dis.readByte();
            }
            break;
        }
        if (this.payloadData != null) {
            this.payloadData.parse(dis, data, payloadLength);
        }
    }

    /**
     * Retrieve a textual representation of this resource record.
     * @return String
     */
    @Override
    public String toString() {
        if (payloadData == null) {
            return "RR " + type + "/" + clazz;
        }
        return "RR " + type + "/" + clazz + ": " + payloadData.toString();
    };

    /**
     * Check if this record answers a given query.
     * @param q The query.
     * @return True if this record is a valid answer.
     */
    public boolean isAnswer(Question q) {
        return ((q.getType() == type) || (q.getType() == TYPE.ANY)) &&
               ((q.getClazz() == clazz) || (q.getClazz() == CLASS.ANY)) &&
               (q.getName().equals(name));
    }

    public String getName() {
        return name;
    }

    public Data getPayload() {
        return payloadData;
    }

}