-
Notifications
You must be signed in to change notification settings - Fork 100
Add support for 0x80 prefix compression in DNS domain name encoding #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ public string ReadDomainName() | |
| // top 2 bits set denotes domain name compression and to reference elsewhere | ||
| if ((length & 0xc0) == 0xc0) | ||
| { | ||
| // Standard suffix compression (11xxxxxx) | ||
| // work out the existing domain name, copy this pointer | ||
| var newRecordReader = new RecordReader(m_Data, (length & 0x3f) << 8 | ReadByte()); | ||
| if (bytes.Count > 0) | ||
|
|
@@ -87,6 +88,37 @@ public string ReadDomainName() | |
| } | ||
| return newRecordReader.ReadDomainName(); | ||
| } | ||
| // Check for 0x80 pattern (10xxxxxx) - prefix compression | ||
| else if ((length & 0xc0) == 0x80) | ||
| { | ||
| // Prefix compression (10xxxxxx) | ||
| // Read the pointer to the prefix | ||
| var offset = (length & 0x3f) << 8 | ReadByte(); | ||
| var newRecordReader = new RecordReader(m_Data, offset); | ||
| var prefix = newRecordReader.ReadDomainName(); | ||
|
|
||
| // Continue reading suffix at current position | ||
| var suffix = ReadDomainName(); | ||
|
|
||
| // Build the result, handling empty suffix (just ".") | ||
| string result; | ||
| if (suffix == ".") | ||
| { | ||
| // No suffix, just return prefix | ||
| result = prefix; | ||
| } | ||
| else | ||
| { | ||
| // Combine prefix and suffix, removing the trailing dot from prefix | ||
| result = prefix.TrimEnd('.') + "." + suffix; | ||
|
||
| } | ||
|
|
||
| if (bytes.Count > 0) | ||
| { | ||
| return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count) + result; | ||
| } | ||
|
Comment on lines
+116
to
+119
|
||
| return result; | ||
| } | ||
|
|
||
| // if not using compression, copy a char at a time to the domain name | ||
| while (length > 0) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recursive call to ReadDomainName() could lead to infinite recursion or stack overflow if the DNS packet contains circular references with 0x80 pointers. Consider adding depth tracking or visited offset checking to prevent infinite loops, similar to how the 0xc0 compression should be protected.