Skip to content

Commit

Permalink
Avoid InetAddress.getHostName() (#64)
Browse files Browse the repository at this point in the history
* InetAddress.getHostName()

---------

Co-authored-by: Tilmann Zäschke <tilmann.zaeschke@inf.ethz.ch>
  • Loading branch information
tzaeschke and Tilmann Zäschke authored May 7, 2024
1 parent 2f8a637 commit 6ad14f3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- Fixed spurious CI failures, esp. Windows. [#61](https://github.com/tzaeschke/phtree-cpp/pull/61)
- Fix SCM references in pom file + clean up. [#60](https://github.com/tzaeschke/phtree-cpp/pull/60)
- DNS lookup caused by `InetAddress.getByName()`. [#63](https://github.com/tzaeschke/phtree-cpp/pull/63)
- DNS lookup caused by `InetAddress.getByName()`. [#63](https://github.com/scionproto-contrib/jpan/pull/63)
- DNS lookup caused by `InetAddress.getHostName()`. [#64](https://github.com/scionproto-contrib/jpan/pull/64)

## [0.1.0] - 2024-04-29

### Added
- Code coverage. [#11](https://github.com/tzaeschke/phtree-cpp/pull/11)
- Code coverage. [#11](https://github.com/netsec-ethz/scion-java-client/pull/11)
- Global JUnit callback for initial setup. THis allows setting global properties before centrally
before running any tests.
[#38](https://github.com/netsec-ethz/scion-java-client/pull/38)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/scion/jpan/ScionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ List<Daemon.Path> getPathListDaemon(long srcIsdAs, long dstIsdAs) {
* @throws IOException if an errors occurs while querying paths.
*/
public List<RequestPath> getPaths(InetSocketAddress dstAddress) throws IOException {
ScionAddress sa = getScionAddress(dstAddress.getHostName());
// Use getHostString() to avoid DNS reverse lookup.
ScionAddress sa = getScionAddress(dstAddress.getHostString());
return getPaths(sa.getIsdAs(), sa.getInetAddress(), dstAddress.getPort());
}

Expand Down
39 changes: 15 additions & 24 deletions src/main/java/org/scion/jpan/internal/HostsFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,25 @@ public class HostsFileParser {

private static final Logger LOG = LoggerFactory.getLogger(HostsFileParser.class);
private static final String PATH_LINUX = "/etc/scion/hosts";
private final String HOSTS_FILES =
private final String propHostsFiles =
ScionUtil.getPropertyOrEnv(Constants.PROPERTY_HOSTS_FILES, Constants.ENV_HOSTS_FILES);

// We use hostName/addressString as key.
private final Map<String, HostEntry> entries = new HashMap<>();

public static class HostEntry {
private final long isdAs;
private final String hostName;
private final InetAddress address;

HostEntry(long isdAs, InetAddress address, String hostName) {
HostEntry(long isdAs, InetAddress address) {
this.isdAs = isdAs;
this.hostName = hostName;
this.address = address;
}

public long getIsdAs() {
return isdAs;
}

public String getHostName() {
return hostName;
}

public InetAddress getAddress() {
return address;
}
Expand All @@ -69,8 +64,8 @@ public HostsFileParser() {
private void init() {
String hostsFiles;
String os = System.getProperty("os.name");
if (HOSTS_FILES != null && !HOSTS_FILES.isEmpty()) {
hostsFiles = HOSTS_FILES;
if (propHostsFiles != null && !propHostsFiles.isEmpty()) {
hostsFiles = propHostsFiles;
} else if ("Linux".equals(os)) {
hostsFiles = PATH_LINUX;
} else {
Expand All @@ -81,18 +76,18 @@ private void init() {
Path path = Paths.get(file);
// On Windows /etc/hosts is reported as ¨: not a file"
if (!Files.exists(path) || !Files.isRegularFile(path)) {
LOG.info(PATH_LINUX + " not found.");
LOG.info("{} not found.", path);
return;
}
try (Stream<String> lines = Files.lines(path)) {
lines.forEach(this::parseLine);
lines.forEach((line) -> parseLine(line, path));
} catch (IOException e) {
throw new ScionRuntimeException(e);
}
}
}

private void parseLine(String line) {
private void parseLine(String line, Path path) {
try {
String s = line.trim();
if (s.isEmpty() || s.startsWith("#")) {
Expand All @@ -109,27 +104,23 @@ private void parseLine(String line) {
check(!addrStr.isEmpty(), "Address is empty");

byte[] addrBytes = IPHelper.toByteArray(addrStr);

// TODO
// 4) Singleton ?!?!?!?!!!

check(addrBytes != null, "Address string is not a legal address");
for (int i = 1; i < lineParts.length; i++) {
String hostName = lineParts[i];
if (hostName.startsWith("#")) {
// ignore comments
break;
}
InetAddress inetAddr = InetAddress.getByAddress(hostName, addrBytes);
entries.put(hostName, new HostEntry(isdIa, inetAddr, hostName));
entries.put(hostName, new HostEntry(isdIa, inetAddr));
}
// The following may differ, e.g. for IPv6
// TODO find a better way, i.e. use InetAddress instances as keys?
InetAddress inetAddr = InetAddress.getByAddress(addrStr, addrBytes);
entries.put(inetAddr.getHostName(), new HostEntry(isdIa, inetAddr, inetAddr.getHostName()));
entries.put(
inetAddr.getHostAddress(), new HostEntry(isdIa, inetAddr, inetAddr.getHostAddress()));
// Use original address string as key
entries.put(addrStr, new HostEntry(isdIa, inetAddr));
// Use "normalized" address string as key (these may differ fo IPv6)
entries.put(inetAddr.getHostAddress(), new HostEntry(isdIa, inetAddr));
} catch (IndexOutOfBoundsException | IllegalArgumentException | UnknownHostException e) {
LOG.info("ERROR {} while parsing file {}: {}", e.getMessage(), PATH_LINUX, line);
LOG.info("ERROR parsing file {}: error=\"{}\" line=\"{}\"", path, e.getMessage(), line);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/scion/jpan/testutil/JUnitSetUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void close() {

@Override
public void beforeEach(ExtensionContext context) {
System.setProperty(Constants.PROPERTY_HOSTS_FILES, "....some-invalid-file");
Scion.closeDefault();
if (failed) {
System.exit(1);
Expand Down

0 comments on commit 6ad14f3

Please sign in to comment.