aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/zeroconf/jmdns/DNSIncoming.java
blob: 3dcedc48b06df1b0102ee9b6234f49d3cc9f73a8 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright 2003-2005 Arthur van Hoff Rick Blair
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.zeroconf.jmdns;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;

/**
 * Parse an incoming DNS message into its components.
 *
 * @version %I%, %G%
 * @author  Arthur van Hoff, Werner Randelshofer, Pierre Frisch, Daniel Bobbert
 */
final class DNSIncoming
{
    private static Logger logger = Logger.getLogger(DNSIncoming.class.toString());
    // Implementation note: This vector should be immutable.
    // If a client of DNSIncoming changes the contents of this vector,
    // we get undesired results. To fix this, we have to migrate to
    // the Collections API of Java 1.2. i.e we replace Vector by List.
    // final static Vector EMPTY = new Vector();

    private DatagramPacket packet;
    private int off;
    private int len;
    private byte data[];

    int id;
    private int flags;
    private int numQuestions;
    int numAnswers;
    private int numAuthorities;
    private int numAdditionals;
    private long receivedTime;

    List<DNSEntry> questions;
    List<DNSRecord> answers;

    /**
     * Parse a message from a datagram packet.
     */
    DNSIncoming(DatagramPacket packet) throws IOException
    {
        String SLevel = System.getProperty("jmdns.debug");
        if (SLevel == null) SLevel = "INFO";
        logger.setLevel(Level.parse(SLevel));

        this.packet = packet;
        this.data = packet.getData();
        this.len = packet.getLength();
        this.off = packet.getOffset();
        this.questions = new LinkedList<DNSEntry>();
        this.answers = new LinkedList<DNSRecord>();
        this.receivedTime = System.currentTimeMillis();

        try
        {
            id = readUnsignedShort();
            flags = readUnsignedShort();
            numQuestions = readUnsignedShort();
            numAnswers = readUnsignedShort();
            numAuthorities = readUnsignedShort();
            numAdditionals = readUnsignedShort();

            // parse questions
            if (numQuestions > 0)
            {
                questions =
                    Collections.synchronizedList(
                                    new ArrayList<DNSEntry>(numQuestions));
                for (int i = 0; i < numQuestions; i++)
                {
                    DNSQuestion question =
                        new DNSQuestion(
                            readName(),
                            readUnsignedShort(),
                            readUnsignedShort());

                    questions.add(question);
                }
            }

            // parse answers
            int n = numAnswers + numAuthorities + numAdditionals;
            if (n > 0)
            {
                //System.out.println("JMDNS received "+n+" answers!");
                answers = Collections.synchronizedList(
                                new ArrayList<DNSRecord>(n));
                for (int i = 0; i < n; i++)
                {
                    String domain = readName();
                    int type = readUnsignedShort();
                    int clazz = readUnsignedShort();
                    int ttl = readInt();
                    int len = readUnsignedShort();
                    int end = off + len;
                    DNSRecord rec = null;

                    switch (type)
                    {
                        case DNSConstants.TYPE_A:       // IPv4
                        case DNSConstants.TYPE_AAAA:    // IPv6 FIXME [PJYF Oct 14 2004] This has not been tested
                            rec = new DNSRecord.Address(
                                domain, type, clazz, ttl, readBytes(off, len));
                            break;
                        case DNSConstants.TYPE_CNAME:
                        case DNSConstants.TYPE_PTR:
                            rec = new DNSRecord.Pointer(
                                domain, type, clazz, ttl, readName());
                            break;
                        case DNSConstants.TYPE_TXT:
                            rec = new DNSRecord.Text(
                                domain, type, clazz, ttl, readBytes(off, len));
                            break;
                        case DNSConstants.TYPE_SRV:
                            //System.out.println("JMDNS: One is a SRV field!!");
                            rec = new DNSRecord.Service(    domain,
                                                            type,
                                                            clazz,
                                                            ttl,
                                                            readUnsignedShort(),
                                                            readUnsignedShort(),
                                                            readUnsignedShort(),
                                                            readName());
                            break;
                        case DNSConstants.TYPE_HINFO:
                            // Maybe we should do something with those
                            break;
                        default :
                            logger.finer("DNSIncoming() unknown type:" + type);
                            break;
                    }

                    if (rec != null)
                    {
                        // Add a record, if we were able to create one.
                        answers.add(rec);
                    }
                    else
                    {
                        // Addjust the numbers for the skipped record
                        if (answers.size() < numAnswers)
                        {
                            numAnswers--;
                        }
                        else
                        {
                            if (answers.size() < numAnswers + numAuthorities)
                            {
                                numAuthorities--;
                            }
                            else
                            {
                                if (answers.size() < numAnswers +
                                                     numAuthorities +
                                                     numAdditionals)
                                {
                                    numAdditionals--;
                                }
                            }
                        }
                    }
                    off = end;
                }
            }
        }
        catch (IOException e)
        {
            logger.log(Level.WARNING,
                "DNSIncoming() dump " + print(true) + "\n exception ", e);
            throw e;
        }
    }

    /**
     * Check if the message is a query.
     */
    boolean isQuery()
    {
        return (flags & DNSConstants.FLAGS_QR_MASK) ==
                DNSConstants.FLAGS_QR_QUERY;
    }

    /**
     * Check if the message is truncated.
     */
    boolean isTruncated()
    {
        return (flags & DNSConstants.FLAGS_TC) != 0;
    }

    /**
     * Check if the message is a response.
     */
    boolean isResponse()
    {
        return (flags & DNSConstants.FLAGS_QR_MASK) ==
                DNSConstants.FLAGS_QR_RESPONSE;
    }

    private int get(int off) throws IOException
    {
        if ((off < 0) || (off >= len))
        {
            throw new IOException("parser error: offset=" + off);
        }
        return data[off] & 0xFF;
    }

    private int readUnsignedShort() throws IOException
    {
        return (get(off++) << 8) + get(off++);
    }

    private int readInt() throws IOException
    {
        return (readUnsignedShort() << 16) + readUnsignedShort();
    }

    private byte[] readBytes(int off, int len) throws IOException
    {
        byte bytes[] = new byte[len];
        System.arraycopy(data, off, bytes, 0, len);
        return bytes;
    }

    private void readUTF(StringBuffer buf, int off, int len) throws IOException
    {
        for (int end = off + len; off < end;)
        {
            int ch = get(off++);
            switch (ch >> 4)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    // 0xxxxxxx
                    break;
                case 12:
                case 13:
                    // 110x xxxx   10xx xxxx
                    ch = ((ch & 0x1F) << 6) | (get(off++) & 0x3F);
                    break;
                case 14:
                    // 1110 xxxx  10xx xxxx  10xx xxxx
                    ch =    ((ch & 0x0f) << 12) |
                            ((get(off++) & 0x3F) << 6) |
                            (get(off++) & 0x3F);
                    break;
                default:
                    // 10xx xxxx,  1111 xxxx
                    ch = ((ch & 0x3F) << 4) | (get(off++) & 0x0f);
                    break;
            }
            buf.append((char) ch);
        }
    }

    private String readName() throws IOException
    {
        StringBuffer buf = new StringBuffer();
        int off = this.off;
        int next = -1;
        int first = off;

        while (true)
        {
            int len = get(off++);
            if (len == 0)
            {
                break;
            }
            switch (len & 0xC0)
            {
                case 0x00:
                    //buf.append("[" + off + "]");
                    readUTF(buf, off, len);
                    off += len;
                    buf.append('.');
                    break;
                case 0xC0:
                    //buf.append("<" + (off - 1) + ">");
                    if (next < 0)
                    {
                        next = off + 1;
                    }
                    off = ((len & 0x3F) << 8) | get(off++);
                    if (off >= first)
                    {
                        throw new IOException(
                            "bad domain name: possible circular name detected");
                    }
                    first = off;
                    break;
                default:
                    throw new IOException(
                        "bad domain name: '" + buf + "' at " + off);
            }
        }
        this.off = (next >= 0) ? next : off;
        return buf.toString();
    }

    /**
     * Debugging.
     */
    String print(boolean dump)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(toString() + "\n");
        for (Iterator<DNSEntry> iterator = questions.iterator();
                iterator.hasNext();)
        {
            buf.append("    ques:" + iterator.next() + "\n");
        }
        int count = 0;
        for (Iterator<DNSRecord> iterator = answers.iterator();
                iterator.hasNext();
                count++)
        {
            if (count < numAnswers)
            {
                buf.append("    answ:");
            }
            else
            {
                if (count < numAnswers + numAuthorities)
                {
                    buf.append("    auth:");
                }
                else
                {
                    buf.append("    addi:");
                }
            }
            buf.append(iterator.next() + "\n");
        }
        if (dump)
        {
            for (int off = 0, len = packet.getLength(); off < len; off += 32)
            {
                int n = Math.min(32, len - off);
                if (off < 10)
                {
                    buf.append(' ');
                }
                if (off < 100)
                {
                    buf.append(' ');
                }
                buf.append(off);
                buf.append(':');
                for (int i = 0; i < n; i++)
                {
                    if ((i % 8) == 0)
                    {
                        buf.append(' ');
                    }
                    buf.append(Integer.toHexString((data[off + i] & 0xF0) >> 4));
                    buf.append(Integer.toHexString((data[off + i] & 0x0F) >> 0));
                }
                buf.append("\n");
                buf.append("    ");
                for (int i = 0; i < n; i++)
                {
                    if ((i % 8) == 0)
                    {
                        buf.append(' ');
                    }
                    buf.append(' ');
                    int ch = data[off + i] & 0xFF;
                    buf.append(((ch > ' ') && (ch < 127)) ? (char) ch : '.');
                }
                buf.append("\n");

                // limit message size
                if (off + 32 >= 256)
                {
                    buf.append("....\n");
                    break;
                }
            }
        }
        return buf.toString();
    }

    @Override
    public String toString()
    {
        StringBuffer buf = new StringBuffer();
        buf.append(isQuery() ? "dns[query," : "dns[response,");
        if (packet.getAddress() != null)
        {
            buf.append(packet.getAddress().getHostAddress());
        }
        buf.append(':');
        buf.append(packet.getPort());
        buf.append(",len=");
        buf.append(packet.getLength());
        buf.append(",id=0x");
        buf.append(Integer.toHexString(id));
        if (flags != 0)
        {
            buf.append(",flags=0x");
            buf.append(Integer.toHexString(flags));
            if ((flags & DNSConstants.FLAGS_QR_RESPONSE) != 0)
            {
                buf.append(":r");
            }
            if ((flags & DNSConstants.FLAGS_AA) != 0)
            {
                buf.append(":aa");
            }
            if ((flags & DNSConstants.FLAGS_TC) != 0)
            {
                buf.append(":tc");
            }
        }
        if (numQuestions > 0)
        {
            buf.append(",questions=");
            buf.append(numQuestions);
        }
        if (numAnswers > 0)
        {
            buf.append(",answers=");
            buf.append(numAnswers);
        }
        if (numAuthorities > 0)
        {
            buf.append(",authorities=");
            buf.append(numAuthorities);
        }
        if (numAdditionals > 0)
        {
            buf.append(",additionals=");
            buf.append(numAdditionals);
        }
        buf.append("]");
        return buf.toString();
    }

    /**
     * Appends answers to this Incoming.
     *
     * @throws IllegalArgumentException If not a query or if Truncated.
     */
    void append(DNSIncoming that)
    {
        if (this.isQuery() && this.isTruncated() && that.isQuery())
        {
            if (that.numQuestions > 0) {
                if (Collections.EMPTY_LIST.equals(this.questions))
                    this.questions =
                        Collections.synchronizedList(
                            new ArrayList<DNSEntry>(that.numQuestions));

                this.questions.addAll(that.questions);
                this.numQuestions += that.numQuestions;
            }

            if (Collections.EMPTY_LIST.equals(answers))
            {
                answers = Collections.synchronizedList(
                                    new ArrayList<DNSRecord>());
            }

            if (that.numAnswers > 0)
            {
                this.answers.addAll(this.numAnswers,
                                    that.answers.subList(0, that.numAnswers));
                this.numAnswers += that.numAnswers;
            }
            if (that.numAuthorities > 0)
            {
                this.answers.addAll(this.numAnswers + this.numAuthorities,
                                    that.answers.subList(
                                        that.numAnswers,
                                        that.numAnswers + that.numAuthorities));
                this.numAuthorities += that.numAuthorities;
            }
            if (that.numAdditionals > 0)
            {
                this.answers.addAll(
                    that.answers.subList(
                        that.numAnswers + that.numAuthorities,
                        that.numAnswers + that.numAuthorities + that.numAdditionals));
                this.numAdditionals += that.numAdditionals;
            }
        }
        else
        {
            throw new IllegalArgumentException();
        }
    }

    int elapseSinceArrival()
    {
        return (int) (System.currentTimeMillis() - receivedTime);
    }
}