-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSPacketParserDecorator.java
More file actions
198 lines (173 loc) · 5.24 KB
/
DNSPacketParserDecorator.java
File metadata and controls
198 lines (173 loc) · 5.24 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
import java.util.List;
import java.util.ArrayList;
/**
* A decorator for DNSPacket that parses a byte buffer to create a
* DNSPacket.
*
* @author Chris Blades
* @version 20/3/10
*/
public class DNSPacketParserDecorator implements DNSPacket {
/** The DNSPacket object this object wraps */
private DNSPacket packet;
/** byte buffer to parse and pass to other objects */
private IndexedArrayList<Byte> bytes;
/**
* Creates a new DNSPacketParserDecorator with the given byte
* buffer.
*
* @param bytes the byte buffer to parse.
*/
public DNSPacketParserDecorator(IndexedArrayList<Byte> bytes) {
this.packet = new DNSPacketImpl();
this.bytes = bytes;
}
/**
* Parses the byte buffer to form a DNSPacket object.
*/
public void parse() {
DNSHeaderParserDecorator head = new DNSHeaderParserDecorator(bytes);
head.parse();
packet.setHeader(head);
// parse questions
for (int i = 0; i < head.getNumQuestions(); i++) {
DNSQuestionParserDecorator current =
new DNSQuestionParserDecorator(bytes);
current.parse();
packet.setQuestion(current);
}
// parse answers
for (int i = 0; i < head.getNumAnswers(); i++) {
DNSAnswerParserDecorator current =
new DNSAnswerParserDecorator(bytes);
current.parse();
packet.addAnswer(current);
}
// parse authoritative answers
for (int i = 0; i < head.getNumAuthorityAnswers(); i++) {
DNSAnswerParserDecorator current =
new DNSAnswerParserDecorator(bytes);
current.parse();
packet.addAuthoritativeAnswer(current);
}
// parse additional answers
for (int i = 0; i < head.getNumAdditionalAnswers(); i++) {
DNSAnswerParserDecorator current =
new DNSAnswerParserDecorator(bytes);
current.parse();
packet.addAdditionalAnswer(current);
}
}
public DNSPacket getPacket() {
return this.packet;
}
//
// DNSPacket
//
/**
* Returns the header of this DNSPacket.
*
* @return This packet's header
*/
public DNSHeader getHeader() {
return packet.getHeader();
}
/**
* Returns a list of all the questions contained in this packet.
*
* @return a list of all the questions in this packet
*/
public List<DNSQuestion> getQuestions() {
return packet.getQuestions();
}
/**
* Returns a list of all the answers contained in this packet.
*
* @return a list of all the answers in this packet
*/
public List<DNSAnswer> getAnswers() {
return packet.getAnswers();
}
/**
* Returns a list of all the authority answers contained in this packet.
*
* @return a list of all the authority answers in this packet
*/
public List<DNSAnswer> getAuthoritativeAnswers() {
return packet.getAuthoritativeAnswers();
}
/**
* Returns a list of all the additional answers contained in this packet.
*
* @return a list of all the additional answers in this packet
*/
public List<DNSAnswer> getAdditionalAnswers() {
return packet.getAdditionalAnswers();
}
/**
* Sets the header of this DNSPacket.
*
* @param header the new header of this DNSpacket.
*/
public void setHeader(DNSHeader header) {
packet.setHeader(header);
}
/**
* Sets the question contained in this packet.
*
* @param question the question contained in this packet.
*/
public void setQuestion(DNSQuestion question) {
packet.setQuestion(question);
}
/**
* adds an answer to this packet
*
* @param answer the answer to add
*/
public void addAnswer(DNSAnswer answer) {
packet.addAnswer(answer);
}
/**
* adds an authority answer to this packet
*
* @param answer the authority answer to add
*/
public void addAuthoritativeAnswer(DNSAnswer answer) {
packet.addAuthoritativeAnswer(answer);
}
/**
* adds an additional answer to this packet
*
* @param answer the additional answer to add
*/
public void addAdditionalAnswer(DNSAnswer answer) {
packet.addAdditionalAnswer(answer);
}
/**
* Returns the length of the parsed DNSPacket object.
*
* @return the length of the parsed DNSPacket object.
*/
public int getLength() {
return packet.getLength();
}
/**
* Returns a mapping of the state values of the parsed DNSPacket object.
*
* @return the state values of the parsed DNSPacket object.
*/
public Map stateValues() {
return packet.stateValues();
}
/**
* 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 packet.serialize();
}
}