-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSOpcodeParserDecorator.java
More file actions
82 lines (73 loc) · 2.11 KB
/
DNSOpcodeParserDecorator.java
File metadata and controls
82 lines (73 loc) · 2.11 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
/**
* A decorator for DNSOpcode that parses a byte buffer to create a
* DNSOpcode.
*
* @author Chris Blades
* @version 20/3/10
*/
public class DNSOpcodeParserDecorator implements DNSOpcode {
/** The DNSOpcode object this object wraps */
private DNSOpcode opcode;
/** byte buffer to parse and pass to other objects */
private IndexedArrayList<Byte> bytes;
/**
* Creates a new DNSOpcodeParserDecorator with the given byte
* buffer.
*
* @param bytes the byte buffer to parse.
*/
public DNSOpcodeParserDecorator(IndexedArrayList<Byte> bytes) {
this.bytes = bytes;
}
/**
* Parses the byte buffer to form a DNSOpcode object.
*
*/
public void parse() {
// opcode is in the middle of the byte...
byte temp = bytes.getNext();
// ... so shift and mask
temp = (byte)(temp >>> 3);
temp = (byte)(temp & 0x0F);
// compare to known values
switch(temp) {
case 0: opcode = DNSOpcodeEnum.QUERY; break;
case 1: opcode = DNSOpcodeEnum.IQUERY; break;
case 2: opcode = DNSOpcodeEnum.STATUS; break;
}
}
/**
* Return a string representation of the opcode
*
* @return a string representation of the opcode
*/
public String getName() {
return opcode.getName();
}
/**
* Returns the length of the parsed DNSOpcode object.
*
* @return the length of the parsed DNSOpcode object.
*/
public int getLength() {
return opcode.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 opcode.serialize();
}
/**
* Returns a mapping of the state values of the parsed DNSOpcode object.
*
* @return the state values of the parsed DNSOpcode object.
*/
public Map stateValues() {
return opcode.stateValues();
}
}