summaryrefslogtreecommitdiff
path: root/src/javax/jmdns/impl/DNSMessage.java
blob: 65377051d2c7c938345d35a96d4682276ccbcbeb (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
/**
 *
 */
package javax.jmdns.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.jmdns.impl.constants.DNSConstants;

/**
 * DNSMessage define a DNS message either incoming or outgoing.
 * 
 * @author Werner Randelshofer, Rick Blair, Pierre Frisch
 */
public abstract class DNSMessage {

    /**
     *
     */
    public static final boolean       MULTICAST = true;

    /**
     *
     */
    public static final boolean       UNICAST   = false;

    // protected DatagramPacket _packet;
    // protected int _off;
    // protected int _len;
    // protected byte[] _data;

    private int                       _id;

    boolean                           _multicast;

    private int                       _flags;

    protected final List<DNSQuestion> _questions;

    protected final List<DNSRecord>   _answers;

    protected final List<DNSRecord>   _authoritativeAnswers;

    protected final List<DNSRecord>   _additionals;

    /**
     * @param flags
     * @param id
     * @param multicast
     */
    protected DNSMessage(int flags, int id, boolean multicast) {
        super();
        _flags = flags;
        _id = id;
        _multicast = multicast;
        _questions = Collections.synchronizedList(new LinkedList<DNSQuestion>());
        _answers = Collections.synchronizedList(new LinkedList<DNSRecord>());
        _authoritativeAnswers = Collections.synchronizedList(new LinkedList<DNSRecord>());
        _additionals = Collections.synchronizedList(new LinkedList<DNSRecord>());
    }

    // public DatagramPacket getPacket() {
    // return _packet;
    // }
    //
    // public int getOffset() {
    // return _off;
    // }
    //
    // public int getLength() {
    // return _len;
    // }
    //
    // public byte[] getData() {
    // if ( _data == null ) _data = new byte[DNSConstants.MAX_MSG_TYPICAL];
    // return _data;
    // }

    /**
     * @return message id
     */
    public int getId() {
        return (_multicast ? 0 : _id);
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(int id) {
        this._id = id;
    }

    /**
     * @return message flags
     */
    public int getFlags() {
        return _flags;
    }

    /**
     * @param flags
     *            the flags to set
     */
    public void setFlags(int flags) {
        this._flags = flags;
    }

    /**
     * @return true if multicast
     */
    public boolean isMulticast() {
        return _multicast;
    }

    /**
     * @return list of questions
     */
    public Collection<? extends DNSQuestion> getQuestions() {
        return _questions;
    }

    /**
     * @return number of questions in the message
     */
    public int getNumberOfQuestions() {
        return this.getQuestions().size();
    }

    public Collection<? extends DNSRecord> getAllAnswers() {
        List<DNSRecord> aList = new ArrayList<DNSRecord>(_answers.size() + _authoritativeAnswers.size() + _additionals.size());
        aList.addAll(_answers);
        aList.addAll(_authoritativeAnswers);
        aList.addAll(_additionals);
        return aList;
    }

    /**
     * @return list of answers
     */
    public Collection<? extends DNSRecord> getAnswers() {
        return _answers;
    }

    /**
     * @return number of answers in the message
     */
    public int getNumberOfAnswers() {
        return this.getAnswers().size();
    }

    /**
     * @return list of authorities
     */
    public Collection<? extends DNSRecord> getAuthorities() {
        return _authoritativeAnswers;
    }

    /**
     * @return number of authorities in the message
     */
    public int getNumberOfAuthorities() {
        return this.getAuthorities().size();
    }

    /**
     * @return list of additional answers
     */
    public Collection<? extends DNSRecord> getAdditionals() {
        return _additionals;
    }

    /**
     * @return number of additional in the message
     */
    public int getNumberOfAdditionals() {
        return this.getAdditionals().size();
    }

    /**
     * Check if the message is truncated.
     * 
     * @return true if the message was truncated
     */
    public boolean isTruncated() {
        return (_flags & DNSConstants.FLAGS_TC) != 0;
    }

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

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

    /**
     * Check if the message is empty
     * 
     * @return true is the message is empty
     */
    public boolean isEmpty() {
        return (this.getNumberOfQuestions() + this.getNumberOfAnswers() + this.getNumberOfAuthorities() + this.getNumberOfAdditionals()) == 0;
    }

    /**
     * Debugging.
     */
    String print() {
        StringBuffer buf = new StringBuffer(200);
        buf.append(this.toString());
        buf.append("\n");
        for (DNSQuestion question : _questions) {
            buf.append("\tquestion:      ");
            buf.append(question);
            buf.append("\n");
        }
        for (DNSRecord answer : _answers) {
            buf.append("\tanswer:        ");
            buf.append(answer);
            buf.append("\n");
        }
        for (DNSRecord answer : _authoritativeAnswers) {
            buf.append("\tauthoritative: ");
            buf.append(answer);
            buf.append("\n");
        }
        for (DNSRecord answer : _additionals) {
            buf.append("\tadditional:    ");
            buf.append(answer);
            buf.append("\n");
        }
        return buf.toString();
    }

    /**
     * Debugging.
     * 
     * @param data
     * @return data dump
     */
    protected String print(byte[] data) {
        StringBuilder buf = new StringBuilder(4000);
        for (int off = 0, len = data.length; off < len; off += 32) {
            int n = Math.min(32, len - off);
            if (off < 0x10) {
                buf.append(' ');
            }
            if (off < 0x100) {
                buf.append(' ');
            }
            if (off < 0x1000) {
                buf.append(' ');
            }
            buf.append(Integer.toHexString(off));
            buf.append(':');
            int index = 0;
            for (index = 0; index < n; index++) {
                if ((index % 8) == 0) {
                    buf.append(' ');
                }
                buf.append(Integer.toHexString((data[off + index] & 0xF0) >> 4));
                buf.append(Integer.toHexString((data[off + index] & 0x0F) >> 0));
            }
            // for incomplete lines
            if (index < 32) {
                for (int i = index; i < 32; i++) {
                    if ((i % 8) == 0) {
                        buf.append(' ');
                    }
                    buf.append("  ");
                }
            }
            buf.append("    ");
            for (index = 0; index < n; index++) {
                if ((index % 8) == 0) {
                    buf.append(' ');
                }
                int ch = data[off + index] & 0xFF;
                buf.append(((ch > ' ') && (ch < 127)) ? (char) ch : '.');
            }
            buf.append("\n");

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

}