Skip to content

"WireParseException: bad label type" when parsing Message from ByteBuffer #213

@maltalex

Description

@maltalex

Hi

I'm running into WireParseException when parsing most responses from a specific DNS server over TCP. There seems to be an issue with the way in which dnsjava parses messages from ByteBuffers. Specifically, I believe that dnsjava doesn't take into account original position of the ByteBuffer when it encounters label compression.

The following code demonstrates the issue:

byte[] arr = Base64.getDecoder().decode("EEuBgAABAAEABAAIA3d3dwZnb29nbGUDY29tAAABAAHADAABAAEAAAAaAASO+rokwBAAAgABAAFHCwAGA25zMcAQwBAAAgABAAFHCwAGA25zNMAQwBAAAgABAAFHCwAGA25zM8AQwBAAAgABAAFHCwAGA25zMsAQwDwAAQABAADObwAE2O8gCsByAAEAAQABrVEABNjvIgrAYAABAAEAAVqZAATY7yQKwE4AAQABAAK9RQAE2O8mCsA8ABwAAQAD4a0AECABSGBIAgAyAAAAAAAAAArAcgAcAAEAAtDgABAgAUhgSAIANAAAAAAAAAAKwGAAHAABAACSagAQIAFIYEgCADYAAAAAAAAACsBOABwAAQAErVoAECABSGBIAgA4AAAAAAAAAAo=");
		
ByteBuffer wrap = ByteBuffer.allocate(arr.length+2);
wrap.putShort((short) arr.length); //prepend length, like when reading a response from a TCP channel
wrap.put(arr);
wrap.flip();
wrap.getShort(); //read the prepended length
		
System.out.println(new Message(wrap)); //Exception in thread "main" org.xbill.DNS.WireParseException: bad label type

Without prepending (and reading) the length, the same message is parsed without any issues:

byte[] arr = Base64.getDecoder().decode("EEuBgAABAAEABAAIA3d3dwZnb29nbGUDY29tAAABAAHADAABAAEAAAAaAASO+rokwBAAAgABAAFHCwAGA25zMcAQwBAAAgABAAFHCwAGA25zNMAQwBAAAgABAAFHCwAGA25zM8AQwBAAAgABAAFHCwAGA25zMsAQwDwAAQABAADObwAE2O8gCsByAAEAAQABrVEABNjvIgrAYAABAAEAAVqZAATY7yQKwE4AAQABAAK9RQAE2O8mCsA8ABwAAQAD4a0AECABSGBIAgAyAAAAAAAAAArAcgAcAAEAAtDgABAgAUhgSAIANAAAAAAAAAAKwGAAHAABAACSagAQIAFIYEgCADYAAAAAAAAACsBOABwAAQAErVoAECABSGBIAgA4AAAAAAAAAAo=");
		
ByteBuffer wrap = ByteBuffer.allocate(arr.length);
wrap.put(arr);
wrap.flip();

System.out.println(new Message(wrap));

Output:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4171
;; flags: qr rd ra ; qd: 1 an: 1 au: 4 ad: 8 
;; QUESTIONS:
;;	www.google.com., type = A, class = IN

;; ANSWERS:
www.google.com.		26	IN	A	142.250.186.36

;; AUTHORITY RECORDS:
google.com.		83723	IN	NS	ns1.google.com.
google.com.		83723	IN	NS	ns4.google.com.
google.com.		83723	IN	NS	ns3.google.com.
google.com.		83723	IN	NS	ns2.google.com.

;; ADDITIONAL RECORDS:
ns1.google.com.		52847	IN	A	216.239.32.10
ns2.google.com.		109905	IN	A	216.239.34.10
ns3.google.com.		88729	IN	A	216.239.36.10
ns4.google.com.		179525	IN	A	216.239.38.10
ns1.google.com.		254381	IN	AAAA	2001:4860:4802:32:0:0:0:a
ns2.google.com.		184544	IN	AAAA	2001:4860:4802:34:0:0:0:a
ns3.google.com.		37482	IN	AAAA	2001:4860:4802:36:0:0:0:a
ns4.google.com.		306522	IN	AAAA	2001:4860:4802:38:0:0:0:a

;; Message size: 296 bytes

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions