Skip to content

Commit 81b0df6

Browse files
author
bwelling
committed
Store rcodes as ints, use the Mnemonic class in the Rcode class.
git-svn-id: http://svn.code.sf.net/p/dnsjava/code/trunk@1179 c76caeb1-94fd-44dd-870f-0c9d92034fc1
1 parent 073fc19 commit 81b0df6

9 files changed

Lines changed: 93 additions & 75 deletions

File tree

jnamed.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ else if (queryOPT != null)
470470
}
471471

472472
byte []
473-
buildErrorMessage(Header header, short rcode, Record question) {
473+
buildErrorMessage(Header header, int rcode, Record question) {
474474
Message response = new Message();
475475
response.setHeader(header);
476476
for (int i = 0; i < 4; i++)
@@ -494,7 +494,7 @@ else if (queryOPT != null)
494494
}
495495

496496
public byte []
497-
errorMessage(Message query, short rcode) {
497+
errorMessage(Message query, int rcode) {
498498
return buildErrorMessage(query.getHeader(), rcode,
499499
query.getQuestion());
500500
}

org/xbill/DNS/Cache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ else if (cr == null)
601601
int qtype;
602602
int qclass;
603603
byte cred;
604-
short rcode = in.getHeader().getRcode();
604+
int rcode = in.getHeader().getRcode();
605605
boolean haveAnswer = false;
606606
boolean completed = false;
607607
RRset [] answers, auth, addl;

org/xbill/DNS/Header.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Header {
1717

1818
private int id;
1919
private boolean [] flags;
20-
private short rcode;
20+
private int rcode;
2121
private int opcode;
2222
private int [] counts;
2323

@@ -69,7 +69,7 @@ public class Header {
6969
out.writeShort(getID());
7070
writeFlags(out);
7171
for (int i = 0; i < counts.length; i++)
72-
out.writeShort((short)counts[i]);
72+
out.writeShort(counts[i]);
7373
}
7474

7575
public byte []
@@ -124,31 +124,37 @@ public class Header {
124124
*/
125125
public void
126126
setID(int id) {
127+
if (opcode > 0xFF)
128+
throw new IllegalArgumentException("DNS message ID " + id +
129+
"is out of range");
127130
this.id = id;
128131
}
129132

130133
/**
131134
* Generates a random number suitable for use as a message ID
132135
*/
133-
static short
136+
static int
134137
randomID() {
135-
return (short) (random.nextInt(0xffff));
138+
return (random.nextInt(0xffff));
136139
}
137140

138141
/**
139142
* Sets the message's rcode
140143
* @see Rcode
141144
*/
142145
public void
143-
setRcode(short value) {
146+
setRcode(int value) {
147+
if (opcode > 0xF)
148+
throw new IllegalArgumentException("DNS Rcode " + value +
149+
"is out of range");
144150
rcode = value;
145151
}
146152

147153
/**
148154
* Retrieves the mesasge's rcode
149155
* @see Rcode
150156
*/
151-
public short
157+
public int
152158
getRcode() {
153159
return rcode;
154160
}
@@ -176,6 +182,10 @@ public class Header {
176182

177183
void
178184
setCount(int field, int value) {
185+
if (value > 0xFF)
186+
throw new IllegalArgumentException("DNS section count " +
187+
value +
188+
"is out of range");
179189
counts[field] = value;
180190
}
181191

@@ -200,7 +210,7 @@ public class Header {
200210

201211
private void
202212
writeFlags(DataByteOutputStream out) {
203-
short flags1 = 0, flags2 = 0;
213+
int flags1 = 0, flags2 = 0;
204214
for (int i = 0; i < 8; i++) {
205215
if (flags[i]) flags1 |= (1 << (7-i));
206216
if (flags[i+8]) flags2 |= (1 << (7-i));
@@ -213,8 +223,8 @@ public class Header {
213223

214224
private void
215225
readFlags(DataByteInputStream in) throws IOException {
216-
short flags1 = (short)in.readUnsignedByte();
217-
short flags2 = (short)in.readUnsignedByte();
226+
int flags1 = in.readUnsignedByte();
227+
int flags2 = in.readUnsignedByte();
218228
for (int i = 0; i < 8; i++) {
219229
flags[i] = ((flags1 & (1 << (7-i))) != 0);
220230
flags[i+8] = ((flags2 & (1 << (7-i))) != 0);
@@ -238,7 +248,7 @@ public class Header {
238248
}
239249

240250
String
241-
toStringWithRcode(short newrcode) {
251+
toStringWithRcode(int newrcode) {
242252
StringBuffer sb = new StringBuffer();
243253

244254
sb.append(";; ->>HEADER<<- ");

org/xbill/DNS/Lookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public final class Lookup {
416416
networkerror = true;
417417
return;
418418
}
419-
short rcode = response.getHeader().getRcode();
419+
int rcode = response.getHeader().getRcode();
420420
if (rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) {
421421
// The server we contacted is broken or otherwise unhelpful.
422422
// Press on.

org/xbill/DNS/Message.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public class Message implements Cloneable {
175175
public void
176176
removeAllRecords(int section) {
177177
sections[section] = null;
178-
header.setCount(section, (short)0);
178+
header.setCount(section, 0);
179179
}
180180

181181
/**
@@ -305,12 +305,12 @@ public class Message implements Cloneable {
305305
* Returns the message's rcode (error code). This incorporates the EDNS
306306
* extended rcode.
307307
*/
308-
public short
308+
public int
309309
getRcode() {
310-
short rcode = header.getRcode();
310+
int rcode = header.getRcode();
311311
OPTRecord opt = getOPT();
312312
if (opt != null)
313-
rcode += (short)(opt.getExtendedRcode() << 4);
313+
rcode += (opt.getExtendedRcode() << 4);
314314
return rcode;
315315
}
316316

org/xbill/DNS/Mnemonic.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ else if (wordcase == CASE_LOWER)
149149
strings.put(str, value);
150150
}
151151

152+
/**
153+
* Copies all mnemonics from one table into another.
154+
* @param val The numeric value
155+
* @param string The text string
156+
*/
157+
public void
158+
addAll(Mnemonic source) {
159+
strings.putAll(source.strings);
160+
values.putAll(source.values);
161+
}
162+
152163
/**
153164
* Gets the text mnemonic corresponding to a numeric value.
154165
* @param val The numeric value

org/xbill/DNS/OPTRecord.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public class OPTRecord extends Record {
4545
* SimpleResolver, but can also be called by a server.
4646
*/
4747
public
48-
OPTRecord(int payloadSize, byte xrcode, byte version, int flags) {
49-
this(Name.root, payloadSize,
50-
((int)xrcode << 24) + ((int)version << 16) + flags);
48+
OPTRecord(int payloadSize, int xrcode, int version, int flags) {
49+
this(Name.root, payloadSize, 0);
5150
checkU16("payloadSize", payloadSize);
5251
checkU8("xrcode", xrcode);
5352
checkU8("version", version);
5453
checkU16("flags", flags);
54+
ttl = ((long)xrcode << 24) + ((long)version << 16) + flags;
5555
options = null;
5656
}
5757

@@ -60,7 +60,7 @@ public class OPTRecord extends Record {
6060
* SimpleResolver, but can also be called by a server.
6161
*/
6262
public
63-
OPTRecord(int payloadSize, byte xrcode, byte version) {
63+
OPTRecord(int payloadSize, int xrcode, int version) {
6464
this(payloadSize, xrcode, version, 0);
6565
}
6666

org/xbill/DNS/Rcode.java

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,87 @@
1212

1313
public final class Rcode {
1414

15-
private static StringValueTable rcodes = new StringValueTable();
16-
private static StringValueTable tsigrcodes = new StringValueTable();
15+
private static Mnemonic rcodes = new Mnemonic("DNS Rcode",
16+
Mnemonic.CASE_UPPER);
17+
18+
private static Mnemonic tsigrcodes = new Mnemonic("TSIG rcode",
19+
Mnemonic.CASE_UPPER);
1720

1821
/** No error */
19-
public static final byte NOERROR = 0;
22+
public static final int NOERROR = 0;
2023

2124
/** Format error */
22-
public static final byte FORMERR = 1;
25+
public static final int FORMERR = 1;
2326

2427
/** Server failure */
25-
public static final byte SERVFAIL = 2;
28+
public static final int SERVFAIL = 2;
2629

2730
/** The name does not exist */
28-
public static final byte NXDOMAIN = 3;
31+
public static final int NXDOMAIN = 3;
2932

3033
/** The operation requested is not implemented */
31-
public static final byte NOTIMPL = 4;
34+
public static final int NOTIMPL = 4;
3235

3336
/** The operation was refused by the server */
34-
public static final byte REFUSED = 5;
37+
public static final int REFUSED = 5;
3538

3639
/** The name exists */
37-
public static final byte YXDOMAIN = 6;
40+
public static final int YXDOMAIN = 6;
3841

3942
/** The RRset (name, type) exists */
40-
public static final byte YXRRSET = 7;
43+
public static final int YXRRSET = 7;
4144

4245
/** The RRset (name, type) does not exist */
43-
public static final byte NXRRSET = 8;
46+
public static final int NXRRSET = 8;
4447

4548
/** The requestor is not authorized to perform this operation */
46-
public static final byte NOTAUTH = 9;
49+
public static final int NOTAUTH = 9;
4750

4851
/** The zone specified is not a zone */
49-
public static final byte NOTZONE = 10;
52+
public static final int NOTZONE = 10;
5053

5154
/* EDNS extended rcodes */
5255
/** Unsupported EDNS level */
53-
public static final byte BADVERS = 16;
56+
public static final int BADVERS = 16;
5457

5558
/* TSIG/TKEY only rcodes */
5659
/** The signature is invalid (TSIG/TKEY extended error) */
57-
public static final byte BADSIG = 16;
60+
public static final int BADSIG = 16;
5861

5962
/** The key is invalid (TSIG/TKEY extended error) */
60-
public static final byte BADKEY = 17;
63+
public static final int BADKEY = 17;
6164

6265
/** The time is out of range (TSIG/TKEY extended error) */
63-
public static final byte BADTIME = 18;
66+
public static final int BADTIME = 18;
6467

6568
/** The mode is invalid (TKEY extended error) */
66-
public static final byte BADMODE = 19;
69+
public static final int BADMODE = 19;
6770

6871
static {
69-
rcodes.put2(NOERROR, "NOERROR");
70-
rcodes.put2(FORMERR, "FORMERR");
71-
rcodes.put2(SERVFAIL, "SERVFAIL");
72-
rcodes.put2(NXDOMAIN, "NXDOMAIN");
73-
rcodes.put2(NOTIMPL, "NOTIMPL");
74-
rcodes.put2(REFUSED, "REFUSED");
75-
rcodes.put2(YXDOMAIN, "YXDOMAIN");
76-
rcodes.put2(YXRRSET, "YXRRSET");
77-
rcodes.put2(NXRRSET, "NXRRSET");
78-
rcodes.put2(NOTAUTH, "NOTAUTH");
79-
rcodes.put2(NOTZONE, "NOTZONE");
80-
rcodes.put2(BADVERS, "BADVERS");
81-
82-
tsigrcodes.put2(BADSIG, "BADSIG");
83-
tsigrcodes.put2(BADKEY, "BADKEY");
84-
tsigrcodes.put2(BADTIME, "BADTIME");
85-
tsigrcodes.put2(BADMODE, "BADMODE");
72+
rcodes.setMaximum(0xFFF);
73+
rcodes.setPrefix("RESERVED");
74+
75+
rcodes.add(NOERROR, "NOERROR");
76+
rcodes.add(FORMERR, "FORMERR");
77+
rcodes.add(SERVFAIL, "SERVFAIL");
78+
rcodes.add(NXDOMAIN, "NXDOMAIN");
79+
rcodes.add(NOTIMPL, "NOTIMPL");
80+
rcodes.add(REFUSED, "REFUSED");
81+
rcodes.add(YXDOMAIN, "YXDOMAIN");
82+
rcodes.add(YXRRSET, "YXRRSET");
83+
rcodes.add(NXRRSET, "NXRRSET");
84+
rcodes.add(NOTAUTH, "NOTAUTH");
85+
rcodes.add(NOTZONE, "NOTZONE");
86+
rcodes.add(BADVERS, "BADVERS");
87+
88+
tsigrcodes.setMaximum(0xFFFF);
89+
tsigrcodes.setPrefix("RESERVED");
90+
tsigrcodes.addAll(rcodes);
91+
92+
tsigrcodes.add(BADSIG, "BADSIG");
93+
tsigrcodes.add(BADKEY, "BADKEY");
94+
tsigrcodes.add(BADTIME, "BADTIME");
95+
tsigrcodes.add(BADMODE, "BADMODE");
8696
}
8797

8898
private
@@ -91,32 +101,19 @@ public final class Rcode {
91101
/** Converts a numeric Rcode into a String */
92102
public static String
93103
string(int i) {
94-
String s = rcodes.getString(i);
95-
return (s != null) ? s : Integer.toString(i);
104+
return rcodes.getText(i);
96105
}
97106

98107
/** Converts a numeric TSIG extended Rcode into a String */
99108
public static String
100109
TSIGstring(int i) {
101-
String s = tsigrcodes.getString(i);
102-
if (s != null)
103-
return s;
104-
s = rcodes.getString(i);
105-
return (s != null) ? s : Integer.toString(i);
110+
return tsigrcodes.getText(i);
106111
}
107112

108113
/** Converts a String representation of an Rcode into its numeric value */
109-
public static byte
114+
public static int
110115
value(String s) {
111-
byte i = (byte) rcodes.getValue(s.toUpperCase());
112-
if (i >= 0)
113-
return i;
114-
try {
115-
return Byte.parseByte(s);
116-
}
117-
catch (Exception e) {
118-
return (-1);
119-
}
116+
return rcodes.getValue(s);
120117
}
121118

122119
}

update.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
483483
return true;
484484
}
485485
if (field.equalsIgnoreCase("rcode")) {
486-
short rcode = response.getHeader().getRcode();
486+
int rcode = response.getHeader().getRcode();
487487
if (rcode != Rcode.value(expected)) {
488488
value = Rcode.string(rcode);
489489
flag = false;

0 commit comments

Comments
 (0)