-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSAnswerParserDecorator.java
More file actions
214 lines (185 loc) · 5.4 KB
/
DNSAnswerParserDecorator.java
File metadata and controls
214 lines (185 loc) · 5.4 KB
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
import java.nio.ByteBuffer;
/**
* A decorator for DNSAnswer that parses a byte buffer to create a
* DNSAnswer.
*
* @author Chris Blades
* @version 20/3/10
*/
public class DNSAnswerParserDecorator implements DNSAnswer {
/** The DNSAnswer object this object wraps */
private DNSAnswer answer;
/** byte buffer to parse and pass to other objects */
private IndexedArrayList<Byte> bytes;
/**
* Creates a new DNSAnswerParserDecorator with the given byte
* buffer.
*
* @param bytes the byte buffer to parse.
*/
public DNSAnswerParserDecorator(IndexedArrayList<Byte> bytes) {
this.bytes = bytes;
this.answer = new DNSAnswerImpl();
}
/**
* Parses the byte buffer to form a DNSAnswer object.
*
*/
public void parse() {
// parse name
DNSUrlParserDecorator url = new DNSUrlParserDecorator(bytes);
url.parse();
answer.setName(url);
// parse type
DNSRecordTypeParserDecorator type =
new DNSRecordTypeParserDecorator(bytes);
type.parse();
answer.setType(type);
// parse class
DNSRecordClassParserDecorator clss =
new DNSRecordClassParserDecorator(bytes);
clss.parse();
answer.setClass(clss);
// parse TTL
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(bytes.getNext());
bb.put(bytes.getNext());
bb.put(bytes.getNext());
bb.put(bytes.getNext());
answer.setTTL(bb.getInt(0));
//
// do not parse Data Length, let DNSAnswerImpl handle it.
// But be sure to forward buffer.
//
bytes.getNext();
bytes.getNext();
if (answer.getType().stateValues().equals(
DNSRecordTypeEnum.A.stateValues())) {
DNSIPParserDecorator ip = new DNSIPParserDecorator(bytes);
ip.parse();
answer.setData(ip);
} else {
url = new DNSUrlParserDecorator(bytes);
url.parse();
answer.setData(url);
}
}
//
// DNSAnswer
//
/**
* Returns the name of the parsed DNSAnswer object.
*
* @return the name of the parsed DNSAnswer object.
*/
public DNSUrl getName() {
return answer.getName();
}
/**
* Returns the type of the parsed DNSAnswer object.
*
* @return the type of the parsed DNSAnswer object.
*/
public DNSRecordType getType() {
return answer.getType();
}
/**
* Returns the class of the parsed DNSAnswer object.
*
* @return the class of the parsed DNSAnswer object.
*/
public DNSRecordClass getRecordClass() {
return answer.getRecordClass();
}
/**
* Sets the name of the parsed DNSAnswer object.
*
* @param name the new name of the parsed DNSAnswer object.
*/
public void setName(DNSUrl name) {
answer.setName(name);
}
/**
* Sets the type of the parsed DNSAnswer object.
*
* @param type the new type of the parsed DNSAnswer object.
*/
public void setType(DNSRecordType type) {
answer.setType(type);
}
/**
* Sets the DNS class of the parsed DNSAnswer object.
*
* @param recordClass the new class of the parsed DNSAnswer object.
*/
public void setClass(DNSRecordClass recordClass) {
answer.setClass(recordClass);
}
/**
* Returns the time to live of the parsed DNSAnswer object.
*
* @return the time to live of the parsed DNSAnswer object.
*/
public int getTTL() {
return answer.getTTL();
}
/**
* Returns the data length of the parsed DNSAnswer object.
*
* @return the data length of the parsed DNSAnswer object.
*/
public int getDataLength() {
return answer.getDataLength();
}
/**
* Returns the data field of the parsed DNSAnswer object.
*
* @return the data field of the parsed DNSAnswer object.
*/
public DNSResource getData() {
return answer.getData();
}
/**
* Sets the time to live of the parsed DNSAnswer object.
*
* @param TTL the new time to live of the parsed DNSAnswer object.
*/
public void setTTL(int TTL) {
answer.setTTL(TTL);
}
/**
* Sets the contents of the data field of the parsed DNSAnswer object.
*
* @param data the new contents of the data field
* of the parsed DNSAnswer object.
*/
public void setData(DNSResource data) {
answer.setData(data);
}
/**
* Returns the length of the parsed DNSAnswer object.
*
* @return the length of the parsed DNSAnswer object.
*/
public int getLength() {
return answer.getLength();
}
/**
* returns the serialized version of the parsed dnsanswer object.
* can't just return the byte buffer because we don't know what else is in
* it.
*
* @return the serialized version of the parsed dnsanswer object.
*/
public byte[] serialize() {
return answer.serialize();
}
/**
* Returns a mapping of the state values of the parsed DNSAnswer object.
*
* @return the state values of the parsed DNSAnswer object.
*/
public Map stateValues() {
return answer.stateValues();
}
}