Skip to content
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

Path should return InetAddress #44

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Cleaned up `DatagramChannel`: Fixed connect()/disconnect(), improved concurrency,
fixed buffer resizing wrt MTU, general clean up.
[#35](https://github.com/netsec-ethz/scion-java-client/pull/35)
- **BREAKING CHANGE**: Renamed project to `jpan`.
[#43](https://github.com/netsec-ethz/scion-java-client/pull/43)
- **BREAKING CHANGE**: `Path` now returns `InetAddress` instead of `byte[]`
[#44](https://github.com/netsec-ethz/scion-java-client/pull/44)

### Fixed
- Fixed: SCMP problem when pinging local AS.
Expand All @@ -54,7 +58,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Some cleanup related to hists file parser. [#42](https://github.com/netsec-ethz/scion-java-client/pull/42)

### Removed

- Removed all code related to DatagramSockets
[#21](https://github.com/netsec-ethz/scion-java-client/pull/21)

Expand Down
22 changes: 9 additions & 13 deletions src/main/java/org/scion/jpan/AbstractDatagramChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,10 @@ public InetSocketAddress getLocalAddress() throws IOException {
}
}

public SocketAddress getRemoteAddress() throws UnknownHostException {
public SocketAddress getRemoteAddress() {
Path path = getConnectionPath();
if (path != null) {
InetAddress ip = InetAddress.getByAddress(path.getDestinationAddress());
return new InetSocketAddress(ip, path.getDestinationPort());
return new InetSocketAddress(path.getDestinationAddress(), path.getDestinationPort());
}
return null;
}
Expand Down Expand Up @@ -501,7 +500,7 @@ protected void buildHeader(
ensureBound();
buffer.clear();
long srcIA;
byte[] srcAddress;
InetAddress srcAddress;
int srcPort;
if (path instanceof ResponsePath) {
// We could get source IA, address and port locally, but it seems cleaner
Expand All @@ -519,9 +518,9 @@ protected void buildHeader(
// elsewhere (from the service).

// TODO cache this or add it to path object?
srcAddress = getOrCreateService().getExternalIP(path.getFirstHopAddress()).getAddress();
srcAddress = getOrCreateService().getExternalIP(path.getFirstHopAddress());
} else {
srcAddress = localAddress.getAddress();
srcAddress = localAddress;
}
srcPort = ((InetSocketAddress) channel.getLocalAddress()).getPort();
if (srcPort == 0) {
Expand All @@ -532,24 +531,21 @@ protected void buildHeader(
}
}

long dstIA = path.getDestinationIsdAs();
byte[] dstAddress = path.getDestinationAddress();
int dstPort = path.getDestinationPort();

byte[] rawPath = path.getRawPath();
ScionHeaderParser.write(
buffer,
payloadLength,
rawPath.length,
srcIA,
srcAddress,
dstIA,
dstAddress,
srcAddress.getAddress(),
path.getDestinationIsdAs(),
path.getDestinationAddress().getAddress(),
hdrType,
cfgTrafficClass);
ScionHeaderParser.writePath(buffer, rawPath);

if (hdrType == InternalConstants.HdrTypes.UDP) {
int dstPort = path.getDestinationPort();
ScionHeaderParser.writeUdpOverlayHeader(buffer, payloadLength, srcPort, dstPort);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/scion/jpan/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
public abstract class Path {
private final byte[] pathRaw;
private final long dstIsdAs;
private final byte[] dstAddress;
private final InetAddress dstAddress;
private final int dstPort;

protected Path(byte[] rawPath, long dstIsdAs, byte[] dstIP, int dstPort) {
protected Path(byte[] rawPath, long dstIsdAs, InetAddress dstIP, int dstPort) {
this.pathRaw = rawPath;
this.dstIsdAs = dstIsdAs;
this.dstAddress = dstIP;
Expand All @@ -45,7 +45,7 @@ public int getDestinationPort() {
return dstPort;
}

public byte[] getDestinationAddress() {
public InetAddress getDestinationAddress() {
return dstAddress;
}

Expand All @@ -59,7 +59,7 @@ public String toString() {
+ "dstIsdAs="
+ ScionUtil.toStringIA(dstIsdAs)
+ ", dstAddress="
+ Arrays.toString(dstAddress)
+ dstAddress
+ ", dstPort="
+ dstPort
+ ", pathRaw="
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/org/scion/jpan/RequestPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,16 @@ public class RequestPath extends Path {
// We store the first hop separately to void creating unnecessary objects.
private final InetSocketAddress firstHop;

static RequestPath create(Daemon.Path path, long dstIsdAs, byte[] dstIP, int dstPort) {
static RequestPath create(Daemon.Path path, long dstIsdAs, InetAddress dstIP, int dstPort) {
return new RequestPath(path, dstIsdAs, dstIP, dstPort);
}

private RequestPath(Daemon.Path path, long dstIsdAs, byte[] dstIP, int dstPort) {
private RequestPath(Daemon.Path path, long dstIsdAs, InetAddress dstIP, int dstPort) {
super(path.getRaw().toByteArray(), dstIsdAs, dstIP, dstPort);
this.pathProtoc = path;
if (getRawPath().length == 0) {
// local AS has path length 0
try {
InetAddress address = InetAddress.getByAddress(getDestinationAddress());
firstHop = new InetSocketAddress(address, getDestinationPort());
} catch (UnknownHostException e) {
// This is impossible, an IP address cannot be unknown
throw new UncheckedIOException(e);
}
firstHop = new InetSocketAddress(getDestinationAddress(), getDestinationPort());
} else {
firstHop = getFirstHopAddress(pathProtoc);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/scion/jpan/ResponsePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package org.scion.jpan;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Arrays;

/**
* A ResponsePath is created/returned when receiving a packet. Besides being a Path, it contains
Expand All @@ -28,16 +28,16 @@ public class ResponsePath extends Path {
private final InetSocketAddress firstHopAddress;
// The ResponsePath gets source information from the incoming packet.
private final long srcIsdAs;
private final byte[] srcAddress;
private final InetAddress srcAddress;
private final int srcPort;

public static ResponsePath create(
byte[] rawPath,
long srcIsdAs,
byte[] srcIP,
InetAddress srcIP,
int srcPort,
long dstIsdAs,
byte[] dstIP,
InetAddress dstIP,
int dstPort,
InetSocketAddress firstHopAddress) {
return new ResponsePath(
Expand All @@ -47,10 +47,10 @@ public static ResponsePath create(
private ResponsePath(
byte[] rawPath,
long srcIsdAs,
byte[] srcIP,
InetAddress srcIP,
int srcPort,
long dstIsdAs,
byte[] dstIP,
InetAddress dstIP,
int dstPort,
InetSocketAddress firstHopAddress) {
super(rawPath, dstIsdAs, dstIP, dstPort);
Expand All @@ -69,7 +69,7 @@ public long getSourceIsdAs() {
return srcIsdAs;
}

public byte[] getSourceAddress() {
public InetAddress getSourceAddress() {
return srcAddress;
}

Expand All @@ -86,7 +86,7 @@ public String toString() {
+ ", srcIsdAs="
+ srcIsdAs
+ ", srcAddress="
+ Arrays.toString(srcAddress)
+ srcAddress
+ ", srcPort="
+ srcPort
+ '}';
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/scion/jpan/ScionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public List<RequestPath> getPaths(InetSocketAddress dstAddress) throws IOExcepti
* @return All paths returned by the path service.
*/
public List<RequestPath> getPaths(long dstIsdAs, InetSocketAddress dstAddress) {
return getPaths(dstIsdAs, dstAddress.getAddress().getAddress(), dstAddress.getPort());
return getPaths(dstIsdAs, dstAddress.getAddress(), dstAddress.getPort());
}

/**
Expand All @@ -333,7 +333,7 @@ public List<RequestPath> getPaths(RequestPath path) {
* @param dstPort Destination port
* @return All paths returned by the path service.
*/
public List<RequestPath> getPaths(long dstIsdAs, byte[] dstAddress, int dstPort) {
public List<RequestPath> getPaths(long dstIsdAs, InetAddress dstAddress, int dstPort) {
long srcIsdAs = getLocalIsdAs();
List<Daemon.Path> paths = getPathList(srcIsdAs, dstIsdAs);
if (paths.isEmpty()) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/scion/jpan/internal/ScionHeaderParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,21 @@ public static ResponsePath extractResponsePath(
int srcPort = Short.toUnsignedInt(data.getShort());
int dstPort = Short.toUnsignedInt(data.getShort());

InetAddress srcIP;
InetAddress dstIP;
try {
srcIP = InetAddress.getByAddress(bytesSrc);
dstIP = InetAddress.getByAddress(bytesDst);
} catch (UnknownHostException e) {
// this cannot happen
throw new IllegalStateException(e);
}

// rewind to original offset
data.position(pos);
// Swap src and dst.
return ResponsePath.create(
path, dstIsdAs, bytesDst, dstPort, srcIsdAs, bytesSrc, srcPort, firstHopAddress);
path, dstIsdAs, dstIP, dstPort, srcIsdAs, srcIP, srcPort, firstHopAddress);
}

/**
Expand Down
26 changes: 23 additions & 3 deletions src/test/java/org/scion/jpan/PackageVisibilityHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,25 @@ public static ResponsePath getResponsePath(ByteBuffer packet, InetSocketAddress

public static RequestPath createDummyPath() {
InetSocketAddress dstAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 12345);
return createDummyPath(0, ExamplePacket.SRC_HOST, 55555, new byte[0], dstAddr);
try {
InetAddress dstIP = InetAddress.getByAddress(ExamplePacket.SRC_HOST);
return createDummyPath(0, dstIP, 55555, new byte[0], dstAddr);
} catch (UnknownHostException e) {
throw new IllegalStateException(e);
}
}

public static RequestPath createDummyPath(
long dstIsdAs, byte[] dstHost, int dstPort, byte[] raw, InetSocketAddress firstHop) {
try {
return createDummyPath(dstIsdAs, InetAddress.getByAddress(dstHost), dstPort, raw, firstHop);
} catch (UnknownHostException e) {
throw new IllegalStateException(e);
}
}

public static RequestPath createDummyPath(
long dstIsdAs, InetAddress dstHost, int dstPort, byte[] raw, InetSocketAddress firstHop) {
ByteString bs = ByteString.copyFrom(raw);
String firstHopString = firstHop.getHostString() + ":" + firstHop.getPort();
Daemon.Interface inter =
Expand All @@ -86,13 +100,19 @@ public static ResponsePath createDummyResponsePath(
byte[] dstIP,
int dstPort,
InetSocketAddress firstHop) {
return ResponsePath.create(raw, srcIsdAs, srcIP, srcPort, dstIsdAs, dstIP, dstPort, firstHop);
try {
InetAddress src = InetAddress.getByAddress(srcIP);
InetAddress dst = InetAddress.getByAddress(dstIP);
return ResponsePath.create(raw, srcIsdAs, src, srcPort, dstIsdAs, dst, dstPort, firstHop);
} catch (UnknownHostException e) {
throw new IllegalStateException(e);
}
}

public static RequestPath createRequestPath110_112(
Daemon.Path.Builder builder,
long dstIsdAs,
byte[] dstHost,
InetAddress dstHost,
int dstPort,
InetSocketAddress firstHop) {
ByteString bs = ByteString.copyFrom(ExamplePacket.PATH_RAW_TINY_110_112);
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/org/scion/jpan/api/DatagramChannelApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ void getLocalAddress_notLocalhost() throws IOException {

RequestPath path =
PackageVisibilityHelper.createDummyPath(
sAddr.getIsdAs(),
sAddr.getInetAddress().getAddress(),
dummyPort,
new byte[100],
firstHop);
sAddr.getIsdAs(), sAddr.getInetAddress(), dummyPort, new byte[100], firstHop);

try (DatagramChannel channel = DatagramChannel.open()) {
channel.connect(path);
Expand Down Expand Up @@ -327,7 +323,7 @@ void isConnected_InetSocket() throws IOException {
@Test
void isConnected_Path() throws IOException {
RequestPath path = PackageVisibilityHelper.createDummyPath();
InetAddress ip = InetAddress.getByAddress(path.getDestinationAddress());
InetAddress ip = path.getDestinationAddress();
InetSocketAddress address = new InetSocketAddress(ip, path.getDestinationPort());
try (DatagramChannel channel = DatagramChannel.open()) {
assertFalse(channel.isConnected());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void client(DatagramChannel channel, Path serverAddress, int id) throws
String message = PingPongHelper.MSG + "-" + id;
ByteBuffer sendBuf = ByteBuffer.wrap(message.getBytes());
// Test send() with InetAddress
InetAddress inetServerAddress = InetAddress.getByAddress(serverAddress.getDestinationAddress());
InetAddress inetServerAddress = serverAddress.getDestinationAddress();
InetSocketAddress inetServerSocketAddress =
new InetSocketAddress(inetServerAddress, serverAddress.getDestinationPort());
channel.send(sendBuf, inetServerSocketAddress);
Expand All @@ -58,7 +58,7 @@ private void client(DatagramChannel channel, Path serverAddress, int id) throws
ByteBuffer response = ByteBuffer.allocate(512);
Path address = channel.receive(response);
assertNotNull(address);
assertArrayEquals(serverAddress.getDestinationAddress(), address.getDestinationAddress());
assertEquals(serverAddress.getDestinationAddress(), address.getDestinationAddress());
assertEquals(serverAddress.getDestinationPort(), address.getDestinationPort());

response.flip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void client(DatagramChannel channel, Path serverAddress, int id) throws
ByteBuffer response = ByteBuffer.allocate(512);
Path address = channel.receive(response);
assertNotNull(address);
assertArrayEquals(serverAddress.getDestinationAddress(), address.getDestinationAddress());
assertEquals(serverAddress.getDestinationAddress(), address.getDestinationAddress());
assertEquals(serverAddress.getDestinationPort(), address.getDestinationPort());

response.flip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void client(DatagramChannel channel, Path serverAddress, int id) throws
ByteBuffer sendBuf = ByteBuffer.wrap(message.getBytes());
channel.disconnect();
// Test send() with InetAddress
InetAddress inetAddress = InetAddress.getByAddress(serverAddress.getDestinationAddress());
InetAddress inetAddress = serverAddress.getDestinationAddress();
InetSocketAddress inetServerSocketAddress =
new InetSocketAddress(inetAddress, serverAddress.getDestinationPort());
channel.connect(inetServerSocketAddress);
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/org/scion/jpan/api/SCMPTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,13 @@ void traceroute_SCMP_error() throws IOException {
private RequestPath getPathTo112() {
ScionService service = Scion.defaultService();
long dstIA = ScionUtil.parseIA("1-ff00:0:112");
List<RequestPath> paths = service.getPaths(dstIA, new byte[] {0, 0, 0, 0}, 12345);
return paths.get(0);
try {
InetAddress zero = InetAddress.getByAddress(new byte[] {0, 0, 0, 0});
List<RequestPath> paths = service.getPaths(dstIA, zero, 12345);
return paths.get(0);
} catch (UnknownHostException e) {
throw new IllegalStateException(e);
}
}

private RequestPath getPathToLocalAS() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/scion/jpan/api/ScionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void getPaths_localAS() throws IOException {
// raw: {}
assertEquals(1, paths.size());
RequestPath path = paths.get(0);
InetAddress addr = InetAddress.getByAddress(path.getDestinationAddress());
InetAddress addr = path.getDestinationAddress();
InetSocketAddress sAddr = new InetSocketAddress(addr, path.getDestinationPort());
assertEquals(sAddr, path.getFirstHopAddress());
assertEquals(dstIA, path.getDestinationIsdAs());
Expand Down
Loading