-
Notifications
You must be signed in to change notification settings - Fork 61
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
WIP: Add SVCB record #110
Draft
Ch4t4r
wants to merge
6
commits into
MiniDNS:master
Choose a base branch
from
Ch4t4r:add_svcb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: Add SVCB record #110
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ed1edb
Add SVCB record TYPE (RR type 64)
Ch4t4r c7ec8fb
Add SVCB data class
Ch4t4r c3f7210
Add test for type
Ch4t4r 28def2c
Use @see for links
Ch4t4r c10a3f5
Add correct wireformat, add possible ServiceKey values, save params a…
Ch4t4r 5c4b8c4
Current (broken) state
Ch4t4r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
minidns-core/src/main/java/org/minidns/constants/SVCBConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.minidns.constants; | ||
|
||
import org.minidns.constants.svcbservicekeys.ALPNServiceKey; | ||
import org.minidns.constants.svcbservicekeys.ServiceKeySpecification; | ||
import org.minidns.constants.svcbservicekeys.UnrecognizedServiceKey; | ||
|
||
|
||
public class SVCBConstants { | ||
public static ServiceKeySpecification<?> findServiceKeyByNumber(int number, byte[] blob) { | ||
switch (number) { | ||
case 1: return new ALPNServiceKey(blob); | ||
default: return new UnrecognizedServiceKey(blob, number); | ||
} | ||
} | ||
|
||
// ALPN(1, "alpn"), | ||
// NO_DEFAULT_ALPN(2, "no-default-alpn"), | ||
// PORT(3, "port"), | ||
// IPV4HINT(4, "ipv4hint"), | ||
// ECHOCONFIG(5, "echoconfig"), | ||
// IPV6HINT(6, "ipv6hint"), | ||
// INVALID_KEY(65535, "key65535"); | ||
} |
50 changes: 50 additions & 0 deletions
50
minidns-core/src/main/java/org/minidns/constants/svcbservicekeys/ALPNServiceKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.minidns.constants.svcbservicekeys; | ||
|
||
import org.minidns.util.RRTextUtil; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ALPNServiceKey extends ServiceKeySpecification<List<String>> { | ||
private List<String> value; | ||
|
||
public ALPNServiceKey(byte[] blob) { | ||
super(blob, 1); | ||
} | ||
|
||
@Override | ||
public List<String> value() throws IOException { | ||
if(value == null) { | ||
List<String> values = new ArrayList<>(); | ||
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(blob)); | ||
while(dis.available() > 0) { | ||
byte[] blob = new byte[dis.readUnsignedShort()]; | ||
dis.readFully(blob); | ||
values.add(RRTextUtil.getTextFrom(blob)); | ||
} | ||
value = Collections.unmodifiableList(values); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public String getTextualRepresentation() { | ||
return "alpn"; | ||
} | ||
|
||
@Override | ||
public String valueAsString() throws IOException { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String s : value()) { | ||
if(sb.length() > 0) { | ||
sb.append(","); | ||
} | ||
sb.append(s.replaceAll(",", "\\\\,")); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...dns-core/src/main/java/org/minidns/constants/svcbservicekeys/ServiceKeySpecification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.minidns.constants.svcbservicekeys; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class ServiceKeySpecification<ValueType> implements Comparable<ServiceKeySpecification<?>> { | ||
public final byte[] blob; | ||
public final int number; | ||
|
||
public ServiceKeySpecification(byte[] blob, int number) { | ||
this.blob = blob; | ||
this.number = number; | ||
} | ||
|
||
public final int getNumber() { | ||
return number; | ||
} | ||
|
||
abstract public ValueType value() throws IOException; | ||
abstract public String getTextualRepresentation(); | ||
abstract public String valueAsString() throws IOException; | ||
|
||
@Override | ||
public int compareTo(ServiceKeySpecification<?> other) { | ||
return getNumber() - other.getNumber(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getTextualRepresentation(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
minidns-core/src/main/java/org/minidns/constants/svcbservicekeys/UnrecognizedServiceKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.minidns.constants.svcbservicekeys; | ||
|
||
import java.util.Arrays; | ||
|
||
public class UnrecognizedServiceKey extends ServiceKeySpecification<byte[]>{ | ||
public UnrecognizedServiceKey(byte[] blob, int number) { | ||
super(blob, number); | ||
} | ||
|
||
@Override | ||
public byte[] value() { | ||
return blob; | ||
} | ||
|
||
@Override | ||
public String getTextualRepresentation() { | ||
return "key" + number; | ||
} | ||
|
||
@Override | ||
public String valueAsString() { | ||
return Arrays.toString(blob); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
minidns-core/src/main/java/org/minidns/record/SVCB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.minidns.record; | ||
|
||
import org.minidns.constants.SVCBConstants; | ||
import org.minidns.constants.svcbservicekeys.ServiceKeySpecification; | ||
import org.minidns.dnsname.DnsName; | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* SVCB Record Type (Service binding) | ||
* | ||
* @see <a href="https://tools.ietf.org/html/draft-ietf-dnsop-svcb-https-01">draft-ietf-dnsop-svcb-https-01: Service binding and parameter specification via the DNS (DNS SVCB and HTTPS RRs)</a> | ||
*/ | ||
class SVCB extends RRWithTarget { | ||
|
||
/** | ||
* The priority indicates the SvcPriority. | ||
* A SvcPriority of 0 puts this RR in AliasMode (otherwise ServiceMode). | ||
* | ||
* @see <a href="https://tools.ietf.org/html/draft-ietf-dnsop-svcb-https-01#page-12>SvcPriority</a> | ||
*/ | ||
public final int priority; | ||
|
||
/** | ||
* A set of key=value pairs (SvcFieldValue). | ||
* The key is an ID for the parameter. | ||
* | ||
* This is a sorted map to follow specification. | ||
* | ||
* @see <a href="https://tools.ietf.org/html/draft-ietf-dnsop-svcb-https-01#section-12.3.2">Possible parameter IDs</a> | ||
*/ | ||
public final Set<ServiceKeySpecification<?>> params; | ||
|
||
/** | ||
* @param priority SvcPriority | ||
* @param target TargetName | ||
* @param params SvcParams | ||
*/ | ||
public SVCB(int priority, DnsName target, Set<ServiceKeySpecification<?>> params) { | ||
super(target); | ||
this.priority = priority; | ||
TreeSet<ServiceKeySpecification<?>> sorted = new TreeSet<>(params); | ||
this.params = Collections.unmodifiableSortedSet(sorted); | ||
} | ||
|
||
/** | ||
* Parses the wireformat data according to the spec. | ||
* | ||
* @see <a href="https://tools.ietf.org/html/draft-ietf-dnsop-svcb-https-01#section-2.2">RDATA wire format specification</a> | ||
*/ | ||
public static SVCB parse(DataInputStream dis, int length, byte[] data) | ||
throws IOException { | ||
int priority = dis.readUnsignedShort(); | ||
DnsName target = DnsName.parse(dis, data); | ||
Set<ServiceKeySpecification<?>> params; | ||
|
||
int paramBlobSize = length - 2 - target.getRawBytes().length; | ||
if(paramBlobSize == 0) { | ||
params = Collections.emptySet(); | ||
} else { | ||
params = parseParamsBlob(dis, length); | ||
} | ||
|
||
return new SVCB(priority, target, params); | ||
} | ||
|
||
private static Set<ServiceKeySpecification<?>> parseParamsBlob(DataInputStream dis, int paramBlobSize) throws IOException { | ||
int remainingBytes = paramBlobSize; | ||
int lastKey = Integer.MIN_VALUE; | ||
Set<ServiceKeySpecification<?>> params = new HashSet<>(); | ||
|
||
while(remainingBytes > 0) { | ||
int key = dis.readUnsignedShort(); | ||
if(key < lastKey) throw new IllegalArgumentException("SVCB ServiceKeys must be in ascending order (" + key + "<" + lastKey + ")"); | ||
else if(key == lastKey) throw new IllegalArgumentException("SVCB ServiceKeys must not be duplicate (" + key + "=" + lastKey + ")"); | ||
lastKey = key; | ||
|
||
int valueLength = dis.readUnsignedShort(); | ||
byte[] valueBlob = new byte[valueLength]; | ||
if(valueLength != 0) { | ||
dis.readFully(valueBlob); | ||
} | ||
|
||
ServiceKeySpecification<?> detectedKey = SVCBConstants.findServiceKeyByNumber(key, valueBlob); | ||
params.add(detectedKey); | ||
remainingBytes = remainingBytes - 4 - valueLength; | ||
} | ||
return params; | ||
} | ||
|
||
@Override | ||
public void serialize(DataOutputStream dos) throws IOException { | ||
dos.writeShort(priority); | ||
super.serialize(dos); | ||
for (ServiceKeySpecification<?> param: params) { | ||
dos.writeShort(param.blob.length); | ||
dos.write(param.blob); | ||
} | ||
} | ||
|
||
@Override | ||
public Record.TYPE getType() { | ||
return Record.TYPE.SVCB; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
try { | ||
return priority + " " + target + createValuesString(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private String createValuesString() throws IOException { | ||
StringBuilder builder = new StringBuilder(); | ||
for (ServiceKeySpecification<?> param : params) { | ||
builder.append(" "); | ||
builder.append(param.getTextualRepresentation()); | ||
builder.append("="); | ||
builder.append("\""); | ||
builder.append(param.valueAsString()); | ||
builder.append("\""); | ||
} | ||
return builder.toString(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
minidns-core/src/main/java/org/minidns/util/RRTextUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.minidns.util; | ||
|
||
public class RRTextUtil { | ||
|
||
public static String getTextFrom(byte[] blob) { | ||
StringBuilder sb = new StringBuilder(); | ||
return sb.toString(); | ||
} | ||
|
||
public static byte[] textToByteArray(String s) { | ||
return new byte[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't there be some escaping for commas in this method?
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.
It seems like I have to update this regardless. There are two RFCs floating around for this RR and I based this on the older one. The one I linked in the PR description is the correct one.
Right now the wireformat is not valid.