Skip to content

Commit

Permalink
Avoid /etc/hosts warning (#155)
Browse files Browse the repository at this point in the history
* Avoid /etc/hosts warning

---------

Co-authored-by: Tilmann Zäschke <tilmann.zaeschke@inf.ethz.ch>
  • Loading branch information
tzaeschke and Tilmann Zäschke authored Jan 8, 2025
1 parent 9471818 commit d493711
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ For example: Path.getFirstHopAddress(), DatagramChannel.setPathPolicy()
- Fixed disabled SHIM tests and general test cleanup.
[#149](https://github.com/scionproto-contrib/jpan/pull/149)
- Fixed parsing of /etc/hosts [#150](https://github.com/scionproto-contrib/jpan/pull/150)
- Fixed warning when dnsjava parses /etc/hosts with SCION adresses
[#155](https://github.com/scionproto-contrib/jpan/pull/155)

## [0.4.1] - 2024-11-22

Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/scion/jpan/internal/DNSHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static <R> R queryTXT(String hostName, String key, Function<String, R> va
}

public static <R> R queryTXT(Name name, String key, Function<String, R> valueParser) {
org.xbill.DNS.Record[] records = new Lookup(name, Type.TXT).run();
org.xbill.DNS.Record[] records = newLookup(name, Type.TXT).run();
if (records == null) {
return null;
}
Expand All @@ -84,7 +84,7 @@ public static <R> R queryTXT(Name name, String key, Function<String, R> valuePar
}

public static InetAddress queryA(Name hostName) {
org.xbill.DNS.Record[] recordsA = new Lookup(hostName, Type.A).run();
org.xbill.DNS.Record[] recordsA = newLookup(hostName, Type.A).run();
if (recordsA == null) {
throw new ScionRuntimeException("No DNS A entry found for host: " + hostName);
}
Expand All @@ -93,7 +93,7 @@ public static InetAddress queryA(Name hostName) {
}

public static InetAddress queryAAAA(Name hostName) {
org.xbill.DNS.Record[] recordsA = new Lookup(hostName, Type.AAAA).run();
org.xbill.DNS.Record[] recordsA = newLookup(hostName, Type.AAAA).run();
if (recordsA == null) {
throw new ScionRuntimeException("No DNS AAAA entry found for host: " + hostName);
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public static String getScionDiscoveryAddress(String hostName) throws IOExceptio
}

private static String getScionDiscoveryAddress(Name hostName) {
org.xbill.DNS.Record[] records = new Lookup(hostName, Type.NAPTR).run();
org.xbill.DNS.Record[] records = newLookup(hostName, Type.NAPTR).run();
if (records == null) {
LOG.debug("Checking discovery service NAPTR: no records found");
return null;
Expand Down Expand Up @@ -211,7 +211,7 @@ static Name findSearchDomainViaReverseLookup() {
Resolver resolver = new SimpleResolver("zh.akamaitech.net");

// IPv4
Lookup lookup4 = new Lookup(reverseLookupHost, Type.A);
Lookup lookup4 = newLookup(reverseLookupHost, Type.A);
lookup4.setResolver(resolver);
org.xbill.DNS.Record[] records4 = lookup4.run();
for (org.xbill.DNS.Record record4 : records4) {
Expand All @@ -223,7 +223,7 @@ static Name findSearchDomainViaReverseLookup() {
}
}

Lookup lookup6 = new Lookup(reverseLookupHost, Type.AAAA);
Lookup lookup6 = newLookup(reverseLookupHost, Type.AAAA);
lookup6.setResolver(resolver);
org.xbill.DNS.Record[] records6 = lookup6.run();
for (org.xbill.DNS.Record record6 : records6) {
Expand All @@ -243,15 +243,15 @@ static Name findSearchDomainViaReverseLookup() {
private static Name findSearchDomainViaReverseLookup(InetAddress address)
throws TextParseException {
Name name = Name.fromString(reverseAddressForARPA(address));
org.xbill.DNS.Record[] records = new Lookup(name, Type.PTR).run();
org.xbill.DNS.Record[] records = newLookup(name, Type.PTR).run();
if (records == null) {
return null;
}
for (org.xbill.DNS.Record record2 : records) {
PTRRecord ptrRecord = (PTRRecord) record2;
Name domain = ptrRecord.getTarget();
while (true) {
if (new Lookup(domain, Type.NAPTR).run() != null) {
if (newLookup(domain, Type.NAPTR).run() != null) {
return domain;
}

Expand Down Expand Up @@ -288,4 +288,12 @@ static String reverseAddressForARPA(InetAddress address) {
}
return sb.toString();
}

private static Lookup newLookup(Name name, int type) {
Lookup lookup = new Lookup(name, type);
// Avoid parsing /etc/hosts because this would print a WARNING, see
// https://github.com/dnsjava/dnsjava/issues/361
lookup.setHostsFileParser(null);
return lookup;
}
}
1 change: 0 additions & 1 deletion src/test/resources/simplelogger.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
org.slf4j.simpleLogger.defaultLogLevel = WARN
# org.slf4j.simpleLogger.defaultLogLevel = INFO
# org.slf4j.simpleLogger.defaultLogLevel = DEBUG
# org.slf4j.simpleLogger.log.org.xbill.DNS.hosts.HostsFileParser = ERROR

0 comments on commit d493711

Please sign in to comment.