Skip to content

Commit

Permalink
Raw path and IP parsing (#77)
Browse files Browse the repository at this point in the history
* Raw path and IP parsing

---------

Co-authored-by: Tilmann Zäschke <tilmann.zaeschke@inf.ethz.ch>
  • Loading branch information
tzaeschke and Tilmann Zäschke authored May 27, 2024
1 parent 983eece commit 79c3490
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#72](https://github.com/scionproto-contrib/jpan/issues/72)
- Fixed path lookup error when destination isn't reachable.
[#70](https://github.com/scionproto-contrib/jpan/issues/70)
- Fixed internal raw path parsing and IP parsing.
[#77](https://github.com/scionproto-contrib/jpan/pull/77)

## [0.1.1] - 2024-05-10

Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/scion/jpan/ScionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.scion.jpan;

import java.nio.ByteBuffer;
import org.scion.jpan.internal.PathRawParser;

/** Scion utility functions. */
Expand Down Expand Up @@ -103,11 +102,7 @@ public static String toStringIA(int isd, long as) {
* @return The sequence or border router interface IDs.
*/
public static String toStringPath(byte[] raw) {
if (raw.length == 0) {
return "[]";
}
PathRawParser ph = new PathRawParser();
ph.read(ByteBuffer.wrap(raw));
PathRawParser ph = PathRawParser.create(raw);
StringBuilder sb = new StringBuilder();
sb.append("[");
int[] segLen = {ph.getSegLen(0), ph.getSegLen(1), ph.getSegLen(2)};
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/scion/jpan/internal/IPHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public static byte[] toByteArray(String s) {
if ("localhost".equals(s)) {
return new byte[] {127, 0, 0, 1};
}
if ("localhost-ip6".equals(s)) {
return new byte[] {127, 0, 0, 1};
if ("ip6-localhost".equals(s)) {
return new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
}
if (s.startsWith("[")) {
if (s.endsWith("]")) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/scion/jpan/internal/PathRawParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ public class PathRawParser {

public static PathRawParser create(byte[] rawPath) {
PathRawParser p = new PathRawParser();
p.read(ByteBuffer.wrap(rawPath));
if (rawPath.length != 0) {
p.read(ByteBuffer.wrap(rawPath));
}
return p;
}

public PathRawParser() {
private PathRawParser() {
this.info[0] = new InfoField();
this.info[1] = new InfoField();
this.info[2] = new InfoField();
Arrays.setAll(hops, value -> new HopField());
}

public void read(ByteBuffer data) {
private void read(ByteBuffer data) {
int start = data.position();
// 2 bit : (C)urrINF : 2-bits index (0-based) pointing to the current info field (see offset
// calculations below).
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/scion/jpan/api/ScionUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,6 @@ private RequestPath createRequestPathRemote() throws UnknownHostException {
void toStringPath_raw() {
assertEquals("[]", ScionUtil.toStringPath(new byte[] {}));
assertEquals("[2>1]", ScionUtil.toStringPath(ExamplePacket.PATH_RAW_TINY_110_112));
assertEquals("[1>5 30>5 7>3 6>1]", ScionUtil.toStringPath(ExamplePacket.PATH_RAW_UP_CORE_DOWN));
}
}
84 changes: 84 additions & 0 deletions src/test/java/org/scion/jpan/internal/IPHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.scion.jpan.internal;

import static org.junit.jupiter.api.Assertions.*;

import java.net.UnknownHostException;
import org.junit.jupiter.api.Test;

class IPHelperTest {

@Test
void isLocalhost() {
assertTrue(IPHelper.isLocalhost("localhost"));
assertTrue(IPHelper.isLocalhost("127.0.0.42"));

assertTrue(IPHelper.isLocalhost("::1"));
assertTrue(IPHelper.isLocalhost("0:0:0:0:0:0:0:1"));
assertTrue(IPHelper.isLocalhost("ip6-localhost"));

assertFalse(IPHelper.isLocalhost("dummy.com"));
assertFalse(IPHelper.isLocalhost("192.168.0.1"));
}

@Test
void lookupLocalhost() {
assertArrayEquals(new byte[] {127, 0, 0, 1}, IPHelper.lookupLocalhost("localhost"));
assertArrayEquals(new byte[] {127, 0, 0, 42}, IPHelper.lookupLocalhost("127.0.0.42"));

byte[] ipv6 = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertArrayEquals(ipv6, IPHelper.lookupLocalhost("::1"));
assertArrayEquals(ipv6, IPHelper.lookupLocalhost("0:0:0:0:0:0:0:1"));
assertArrayEquals(ipv6, IPHelper.lookupLocalhost("ip6-localhost"));

assertNull(IPHelper.lookupLocalhost("dummy.com"));
assertNull(IPHelper.lookupLocalhost("192.168.0.1"));
}

@Test
void toByteArray() {
assertArrayEquals(new byte[] {127, 0, 0, 1}, IPHelper.toByteArray("localhost"));
assertArrayEquals(new byte[] {127, 0, 0, 1}, IPHelper.toByteArray("127.0.0.1"));
assertArrayEquals(new byte[] {127, 0, 0, 42}, IPHelper.toByteArray("127.0.0.42"));

byte[] ipv6 = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertArrayEquals(ipv6, IPHelper.toByteArray("::1"));
assertArrayEquals(ipv6, IPHelper.toByteArray("[::1]"));
assertArrayEquals(ipv6, IPHelper.toByteArray("0:0:0:0:0:0:0:1"));
assertArrayEquals(ipv6, IPHelper.toByteArray("[0:0:0:0:0:0:0:1]"));
assertArrayEquals(ipv6, IPHelper.toByteArray("ip6-localhost"));

assertNull(IPHelper.toByteArray("dummy.com"));
assertArrayEquals(new byte[] {1, 2, 3, 4}, IPHelper.toByteArray("1.2.3.4"));
assertArrayEquals(new byte[] {192 - 256, 168 - 256, 0, 1}, IPHelper.toByteArray("192.168.0.1"));

assertNull(IPHelper.toByteArray("[::1"));
assertNull(IPHelper.toByteArray("::1]"));

assertNull(IPHelper.toByteArray("127.0.0.1x"));
assertNull(IPHelper.toByteArray("[::1x]"));
}

@Test
void toInetAddress() throws UnknownHostException {
assertArrayEquals(new byte[] {127, 0, 0, 1}, IPHelper.toInetAddress("localhost").getAddress());
assertArrayEquals(
new byte[] {127, 0, 0, 42}, IPHelper.toInetAddress("127.0.0.42").getAddress());

byte[] ipv6 = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertArrayEquals(ipv6, IPHelper.toInetAddress("::1").getAddress());
}
}
9 changes: 9 additions & 0 deletions src/test/java/org/scion/jpan/testutil/ExamplePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ public class ExamplePacket {
86, -46, 89, 0, 31, -112, -83, -100, 0, 19, 68, -82, 72, 101, 108, 108,
111, 32, 115, 99, 105, 111, 110,
};

/** A real world long path from 64-2:0:9 to 71-2:0:48. */
public static final byte[] PATH_RAW_UP_CORE_DOWN = {
0, 0, 32, -62, 0, 0, 33, 38, 102, 84, -122, 69, 0, 0, -103, -70, 102, 84, -122, -64, 1, 0, -11,
54, 102, 84, -122, -64, 0, 63, 0, 1, 0, 0, 117, -46, 17, -106, -11, -104, 0, 63, 0, 0, 0, 5, -2,
-52, -56, 123, -91, 72, 0, 63, 0, 30, 0, 0, 77, -86, 93, 42, -83, -82, 0, 63, 0, 7, 0, 5, 21,
107, -89, 61, 117, 32, 0, 63, 0, 0, 0, 3, 83, -69, 122, -45, 58, -111, 0, 63, 0, 0, 0, 6, -46,
-42, 67, 69, 117, 2, 0, 63, 0, 1, 0, 0, 85, -55, 20, 110, -37, 90
};
}

0 comments on commit 79c3490

Please sign in to comment.