Skip to content

Commit e2d4fde

Browse files
author
bwelling
committed
Change Type.value() and DClass.value() to return an int, not a short, so that
the entire range of types/classes as well as an error value can be returned. Update all of the callers. git-svn-id: http://svn.code.sf.net/p/dnsjava/code/trunk@1046 c76caeb1-94fd-44dd-870f-0c9d92034fc1
1 parent 561d97b commit e2d4fde

File tree

8 files changed

+80
-55
lines changed

8 files changed

+80
-55
lines changed

dig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class dig {
1111

1212
static Name name = null;
13-
static short type = Type.A, _class = DClass.IN;
13+
static int type = Type.A, dclass = DClass.IN;
1414

1515
static void
1616
usage() {
@@ -82,7 +82,7 @@ public class dig {
8282
name = Name.fromString(dns.inaddrString(argv[arg++]),
8383
Name.root);
8484
type = Type.PTR;
85-
_class = DClass.IN;
85+
dclass = DClass.IN;
8686
}
8787
else {
8888
name = Name.fromString(nameString, Name.root);
@@ -92,9 +92,9 @@ public class dig {
9292
else
9393
arg++;
9494

95-
_class = DClass.value(argv[arg]);
96-
if (_class < 0)
97-
_class = DClass.IN;
95+
dclass = DClass.value(argv[arg]);
96+
if (dclass < 0)
97+
dclass = DClass.IN;
9898
else
9999
arg++;
100100
}
@@ -177,7 +177,7 @@ public class dig {
177177
usage();
178178
}
179179

180-
rec = Record.newRecord(name, type, _class);
180+
rec = Record.newRecord(name, (short) type, (short) dclass);
181181
query = Message.newQuery(rec);
182182
if (opt != null)
183183
query.addRecord(opt, Section.ADDITIONAL);

lookup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class lookup {
2929

3030
public static void
3131
main(String [] args) throws Exception {
32-
short type = Type.A;
32+
int type = Type.A;
3333
int start = 0;
3434
if (args.length > 2 && args[0].equals("-t")) {
3535
type = Type.value(args[1]);
@@ -38,7 +38,7 @@ public class lookup {
3838
start = 2;
3939
}
4040
for (int i = start; i < args.length; i++) {
41-
Lookup l = new Lookup(args[i], type);
41+
Lookup l = new Lookup(args[i], (short) type);
4242
l.run();
4343
printAnswer(args[i], l);
4444
}

org/xbill/DNS/DClass.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
* Constants and functions relating to DNS classes. This is called DClass
10-
* since Class was already taken.
10+
* to avoid confusion with Class.
1111
*
1212
* @author Brian Wellington
1313
*/
@@ -57,21 +57,28 @@ public final class DClass {
5757
return (s != null) ? s : ("CLASS" + i);
5858
}
5959

60-
/** Converts a String representation of an Class into its numeric value */
61-
public static short
60+
/**
61+
* Converts a String representation of a DClass into its numeric value
62+
* @return The class code, or -1 on error.
63+
*/
64+
public static int
6265
value(String s) {
63-
short i = (short) classes.getValue(s.toUpperCase());
66+
s = s.toUpperCase();
67+
int i = classes.getValue(s);
6468
if (i >= 0)
6569
return i;
66-
try {
67-
if (s.toUpperCase().startsWith("CLASS"))
68-
return (Short.parseShort(s.substring(5)));
69-
else
70-
return Short.parseShort(s);
71-
}
72-
catch (Exception e) {
73-
return (-1);
70+
if (s.startsWith("CLASS")) {
71+
try {
72+
int dclass = Integer.parseInt(s.substring(5));
73+
if (dclass < 0 || dclass > 0xFFFF)
74+
return -1;
75+
return dclass;
76+
}
77+
catch (NumberFormatException e) {
78+
return -1;
79+
}
7480
}
81+
return -1;
7582
}
7683

7784
/* Converts a class into a Short, for use in Hashmaps, etc. */

org/xbill/DNS/Master.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class Master {
8181
while (true) {
8282
Name name;
8383
int ttl;
84-
short type, dclass;
84+
int type, dclass;
8585

8686
token = st.get(true, false);
8787
if (token.type == Tokenizer.WHITESPACE) {
@@ -151,7 +151,8 @@ else if (defaultTTL >= 0)
151151
if ((type = Type.value(s)) < 0)
152152
throw st.exception("Invalid type '" + s + "'");
153153

154-
last = Record.fromString(name, type, dclass, ttl, st, origin);
154+
last = Record.fromString(name, (short) type, (short) dclass,
155+
ttl, st, origin);
155156
st.getEOL();
156157
return last;
157158
}

org/xbill/DNS/NXTRecord.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public class NXTRecord extends Record {
8282
Tokenizer.Token t = st.get();
8383
if (!t.isString())
8484
break;
85-
short type = Type.value(t.value);
85+
int type = Type.value(t.value);
8686
if (type <= 0 || type > 128)
87-
throw st.exception("Invalid type " + t.value);
87+
throw st.exception("Invalid type: " + t.value);
8888
rec.bitmap.set(type);
8989
}
9090
st.unget();

org/xbill/DNS/SIGRecord.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ public class SIGRecord extends Record {
102102
throws IOException
103103
{
104104
SIGRecord rec = new SIGRecord(name, dclass, ttl);
105-
rec.covered = Type.value(st.getString());
105+
String typeString = st.getString();
106+
int covered = Type.value(typeString);
107+
if (covered < 0)
108+
throw st.exception("Invalid type: " + typeString);
109+
rec.covered = (short) covered;
106110
rec.alg = (byte) st.getUInt8();
107111
rec.labels = (byte) st.getUInt8();
108112
rec.origttl = st.getTTL();

org/xbill/DNS/Type.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,28 @@ private static class DoubleHashMap {
256256
return (s != null) ? s : ("TYPE" + i);
257257
}
258258

259-
/** Converts a String representation of an Type into its numeric value */
260-
public static short
259+
/**
260+
* Converts a String representation of an Type into its numeric value
261+
* @return The type code, or -1 on error.
262+
*/
263+
public static int
261264
value(String s) {
262-
Short val = types.getValue(s.toUpperCase());
265+
s = s.toUpperCase();
266+
Short val = types.getValue(s);
263267
if (val != null)
264268
return val.shortValue();
265-
try {
266-
if (s.toUpperCase().startsWith("TYPE"))
267-
return (Short.parseShort(s.substring(4)));
268-
else
269-
return Short.parseShort(s);
270-
}
271-
catch (Exception e) {
272-
return (-1);
269+
if (s.startsWith("TYPE")) {
270+
try {
271+
int type = Integer.parseInt(s.substring(4));
272+
if (type < 0 || type > 0xFFFF)
273+
return -1;
274+
return type;
275+
}
276+
catch (NumberFormatException e) {
277+
return -1;
278+
}
273279
}
280+
return -1;
274281
}
275282

276283
/** Is this type valid for a record (a non-meta type)? */

update.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class update {
1616
String server = null;
1717
Name zone = Name.root;
1818
int defaultTTL;
19-
short defaultClass = DClass.IN;
19+
int defaultClass = DClass.IN;
2020
PrintStream log = null;
2121

2222
void
@@ -115,7 +115,7 @@ else if (operation.equals("tcp")) {
115115
}
116116

117117
else if (operation.equals("class")) {
118-
short newClass = DClass.value(st.getString());
118+
int newClass = DClass.value(st.getString());
119119
if (newClass > 0)
120120
defaultClass = newClass;
121121
else
@@ -251,7 +251,7 @@ else if (operation.equals("date")) {
251251
if (query.getHeader().getCount(Section.ZONE) == 0) {
252252
Name updzone;
253253
updzone = zone;
254-
short dclass = defaultClass;
254+
int dclass = defaultClass;
255255
if (updzone == null) {
256256
Record [] recs = query.getSectionArray(Section.UPDATE);
257257
for (int i = 0; i < recs.length; i++) {
@@ -266,7 +266,8 @@ else if (operation.equals("date")) {
266266
}
267267
}
268268
}
269-
Record soa = Record.newRecord(updzone, Type.SOA, dclass);
269+
Record soa = Record.newRecord(updzone, Type.SOA,
270+
(short) dclass);
270271
query.addRecord(soa, Section.ZONE);
271272
}
272273

@@ -281,12 +282,12 @@ else if (operation.equals("date")) {
281282
* Ignore the class, if present.
282283
*/
283284
Record
284-
parseRR(Tokenizer st, short classValue, int TTLValue)
285+
parseRR(Tokenizer st, int classValue, int TTLValue)
285286
throws IOException
286287
{
287288
Name name = st.getName(zone);
288289
int ttl;
289-
short type;
290+
int type;
290291
Record record;
291292

292293
String s = st.getString();
@@ -307,7 +308,8 @@ else if (operation.equals("date")) {
307308
if ((type = Type.value(s)) < 0)
308309
throw new IOException("Invalid type: " + s);
309310

310-
record = Record.fromString(name, type, classValue, ttl, st, zone);
311+
record = Record.fromString(name, (short) type, (short) classValue, ttl,
312+
st, zone);
311313
if (record != null)
312314
return (record);
313315
else
@@ -319,8 +321,7 @@ record = Record.fromString(name, type, classValue, ttl, st, zone);
319321
Tokenizer.Token token;
320322
Name name;
321323
Record record;
322-
short type;
323-
short dclass;
324+
int type;
324325

325326
name = st.getName(zone);
326327
token = st.get();
@@ -331,10 +332,12 @@ record = Record.fromString(name, type, classValue, ttl, st, zone);
331332
boolean iseol = token.isEOL();
332333
st.unget();
333334
if (!iseol) {
334-
record = Record.fromString(name, type, defaultClass,
335+
record = Record.fromString(name, (short) type,
336+
(short) defaultClass,
335337
0, st, zone);
336338
} else
337-
record = Record.newRecord(name, type, DClass.ANY, 0);
339+
record = Record.newRecord(name, (short) type,
340+
DClass.ANY, 0);
338341
} else
339342
record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
340343

@@ -348,7 +351,7 @@ record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
348351
String s;
349352
Name name;
350353
Record record;
351-
short type;
354+
int type;
352355

353356
name = st.getName(zone);
354357
token = st.get();
@@ -357,7 +360,7 @@ record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
357360
throw new IOException("Invalid type: " + token.value);
358361
} else
359362
type = Type.ANY;
360-
record = Record.newRecord(name, type, DClass.NONE, 0);
363+
record = Record.newRecord(name, (short) type, DClass.NONE, 0);
361364
query.addRecord(record, Section.PREREQ);
362365
print(record);
363366
}
@@ -375,8 +378,8 @@ record = Record.newRecord(name, type, DClass.NONE, 0);
375378
String s;
376379
Name name;
377380
Record record;
378-
short type;
379-
short dclass;
381+
int type;
382+
int dclass;
380383

381384
name = st.getName(zone);
382385
token = st.get();
@@ -391,10 +394,12 @@ record = Record.newRecord(name, type, DClass.NONE, 0);
391394
boolean iseol = token.isEOL();
392395
st.unget();
393396
if (!iseol) {
394-
record = Record.fromString(name, type, DClass.NONE,
397+
record = Record.fromString(name, (short) type,
398+
DClass.NONE,
395399
0, st, zone);
396400
} else
397-
record = Record.newRecord(name, type, DClass.ANY, 0);
401+
record = Record.newRecord(name, (short) type,
402+
DClass.ANY, 0);
398403
}
399404
else
400405
record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
@@ -416,7 +421,8 @@ record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
416421
Tokenizer.Token token;
417422

418423
Name name = null;
419-
short type = Type.A, dclass = defaultClass;
424+
int type = Type.A;
425+
int dclass = defaultClass;
420426

421427
name = st.getName(zone);
422428
token = st.get();
@@ -432,7 +438,7 @@ record = Record.newRecord(name, Type.ANY, DClass.ANY, 0);
432438
}
433439
}
434440

435-
rec = Record.newRecord(name, type, dclass);
441+
rec = Record.newRecord(name, (short) type, (short) dclass);
436442
Message newQuery = Message.newQuery(rec);
437443
if (res == null)
438444
res = new SimpleResolver(server);

0 commit comments

Comments
 (0)