-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSPacketGuiDecorator.java
More file actions
212 lines (187 loc) · 5.95 KB
/
DNSPacketGuiDecorator.java
File metadata and controls
212 lines (187 loc) · 5.95 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
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.List;
/**
* A decorator for the DNSPacketGuiDecorator interface that uses a tree to
* create a representaiton of state that can be displayed graphically.
*
* @author chris Blades
* @version 21/3/10
*/
public class DNSPacketGuiDecorator extends DefaultMutableTreeNode
implements DNSPacket {
/**
* DNSPacket object that this decorator wraps
*/
private DNSPacket packet;
/**
* Creates a new DNSPacketGuiDecorator that contains the given
* DNSPacket.
*
* @param packet the DNSPacket this object should wrap and whose state
* it should reflect
*/
public DNSPacketGuiDecorator(DNSPacket packet) {
this.packet = packet;
setupGui();
}
/**
* Create a tree that reflects packet's state.
*/
public void setupGui() {
// since this will be rebuilt everytime the state of answer changes,
// scratch everything
super.removeAllChildren();
// set name of root node for this tree
super.setUserObject("Domain Name System");
// add header
super.add(new DNSHeaderGuiDecorator(packet.getHeader()));
// add questions
DefaultMutableTreeNode questions = new DefaultMutableTreeNode(
"Queries");
for (DNSQuestion q : packet.getQuestions()) {
questions.add(new DNSQuestionGuiDecorator(q));
}
// only add sub-tree if there are 1 or more nodes
if (packet.getQuestions().size() > 0) {
super.add(questions);
}
// add answers
DefaultMutableTreeNode answers = new DefaultMutableTreeNode(
"Answers");
for (DNSAnswer a : packet.getAnswers()) {
answers.add(new DNSAnswerGuiDecorator(a));
}
// only add sub-tree if there are 1 or more nodes
if (packet.getAnswers().size() > 0) {
super.add(answers);
}
// add authority answers
answers = new DefaultMutableTreeNode(
"Authority Answers");
for (DNSAnswer a : packet.getAuthoritativeAnswers()) {
answers.add(new DNSAnswerGuiDecorator(a));
}
// only add sub-tree if there are 1 or more nodes
if (packet.getAuthoritativeAnswers().size() > 0) {
super.add(answers);
}
// add additional answers
answers = new DefaultMutableTreeNode(
"Additional Answers");
for (DNSAnswer a : packet.getAdditionalAnswers()) {
answers.add(new DNSAnswerGuiDecorator(a));
}
// only add sub-tree if there are 1 or more nodes
if (packet.getAdditionalAnswers().size() > 0) {
super.add(answers);
}
}
//
// 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 serialized version of the underlying
* DNSPacket.
*
* @return the length of the serialized version of this packet
*/
public int getLength() {
return packet.getLength();
}
/**
* Returns a mapping of the state values of this DNSpacket.
*
* @return the state values of this DNSPacket
*/
public Map stateValues() {
return packet.stateValues();
}
/**
* Returns the serialized version of this DNSPacket.
*
* @return this packet serialized as per the DNS protocol
*/
public byte[] serialize() {
return packet.serialize();
}
}