-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSQuestionImpl.java
More file actions
141 lines (122 loc) · 3.69 KB
/
DNSQuestionImpl.java
File metadata and controls
141 lines (122 loc) · 3.69 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
/**
* Basic implementation of DNSQuestion.
*
* @author Chris Blades
* @version 2/25/10
*/
public class DNSQuestionImpl extends AbstractDNSObject implements DNSQuestion {
/**
* Name of this DNSQuestion
*/
private DNSUrl questionName;
/** the type of this question */
private DNSRecordType questionType;
/** the class of this question */
private DNSRecordClass questionClass;
/**
* Returns the name of the record.
*
* @return the name of the record
*/
public DNSUrl getName() {
return this.questionName;
}
/**
* Returns the type of the record.
*
* @return the type of the record.
*/
public DNSRecordType getType(){
return this.questionType;
}
/**
* Returns the DNS class of the record.
*
* @return the DNS class of the record.
*/
public DNSRecordClass getRecordClass(){
return this.questionClass;
}
/**
* Changes the name of the record.
*
* @param name the new name of the record
*/
public void setName(DNSUrl questionName){
this.questionName = questionName;
changed = true;
}
/**
* Changes the type of the record.
*
* @param type the new type of the record
*/
public void setType(DNSRecordType questionType){
this.questionType = questionType;
changed = true;
}
/**
* Changes the class of the record.
*
* @param recordClass the new class of the record
*/
public void setClass(DNSRecordClass questionClass){
this.questionClass = questionClass;
changed = true;
}
/**
* Serialize should translate the DNSObject into an array of bytes
* as per the DNS protocol.
*
* @return a byte array representing this DNSObject
*/
public byte[] serialize() {
generateTransient();
return serialized;
}
/**
* Should return a description of the state of this DNSObject in the
* format [label][value]
* @return descriptions of the state of this DNSObject
*/
public Map stateValues() {
generateTransient();
return values;
}
/**
* Generates serialized and values based on current state of this
* object.
*/
protected void generateTransient() {
// if there's been no change, don't bother regenerating anything
if (changed) {
//
// construct serialized
//
// length will equal length of name + 4 bytes for type and class
serialized = new byte[questionName.getLength() + 4];
// find length of the url
byte[] urlBytes = questionName.serialize();
// copy url bytes
for (int i = 0; i < urlBytes.length; i++) {
serialized[i] = urlBytes[i];
}
// copy type bytes, only copy 2 bytes (shouldn't be anymore)
byte[] typeBytes = questionType.serialize();
serialized[serialized.length - 4] = typeBytes[0];
serialized[serialized.length - 3] = typeBytes[1];
// copy class bytes, only copy 2 bytes (shouldn't be anymore)
byte[] classBytes = questionClass.serialize();
serialized[serialized.length - 2] = classBytes[0];
serialized[serialized.length - 1] = classBytes[1];
//
// construct values
//
values.clear();
values.put("Name", questionName.stateValues());
values.put("Type", questionType.stateValues());
values.put("Class", questionClass.stateValues());
changed = false;
}
}
}