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

Improved ScmpChannel #48

Merged
merged 1 commit into from
Apr 23, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#46](https://github.com/netsec-ethz/scion-java-client/pull/46)
- Support for comments, multiple spaces and tabs in `/etc/scion/hosts`.
[#47](https://github.com/netsec-ethz/scion-java-client/pull/47)
- Added helper methods for `ScmpChannel` and `ScionUtil.toStringPath()`.
[#48](https://github.com/netsec-ethz/scion-java-client/pull/48)

### Changed
- BREAKING CHANGE: Changed maven artifactId to "client"
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/scion/jpan/AbstractDatagramChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ public InetSocketAddress getLocalAddress() throws IOException {
}
}

public SocketAddress getRemoteAddress() {
/**
* Returns the remote address.
*
* @see DatagramChannel#getRemoteAddress()
* @return The remote address.
* @throws IOException If an I/O error occurs
*/
public InetSocketAddress getRemoteAddress() throws IOException {
Path path = getConnectionPath();
if (path != null) {
return new InetSocketAddress(path.getDestinationAddress(), path.getDestinationPort());
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/org/scion/jpan/ScionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package org.scion.jpan;

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

/** Scion utility functions. */
public class ScionUtil {

Expand Down Expand Up @@ -95,6 +98,57 @@ public static String toStringIA(int isd, long as) {
return s;
}

public static String toStringPath(byte[] raw) {
if (raw.length == 0) {
return "[]";
}
PathRawParser ph = new PathRawParser();
ph.read(ByteBuffer.wrap(raw));
StringBuilder sb = new StringBuilder();
sb.append("[");
int[] segLen = {ph.getSegLen(0), ph.getSegLen(1), ph.getSegLen(2)};
int offset = 0;
for (int j = 0; j < segLen.length; j++) {
boolean flagC = ph.getInfoField(j).getFlagC();
for (int i = offset; i < offset + segLen[j] - 1; i++) {
PathRawParser.HopField hfE = ph.getHopField(i);
PathRawParser.HopField hfI = ph.getHopField(i + 1);
if (flagC) {
sb.append(hfE.getEgress()).append(">").append(hfI.getIngress());
} else {
sb.append(hfE.getIngress()).append(">").append(hfI.getEgress());
}
if (i < ph.getHopCount() - 2) {
sb.append(" ");
}
}
offset += segLen[j];
}
sb.append("]");
return sb.toString();
}

public static String toStringPath(RequestPath path) {
if (path.getInterfacesList().isEmpty()) {
return "[]";
}
StringBuilder sb = new StringBuilder();
sb.append("[");
int nInterfcaces = path.getInterfacesList().size();
for (int i = 0; i < nInterfcaces; i++) {
RequestPath.PathInterface pIf = path.getInterfacesList().get(i);
if (i % 2 == 0) {
sb.append(ScionUtil.toStringIA(pIf.getIsdAs())).append(" ");
sb.append(pIf.getId()).append(">");
} else {
sb.append(pIf.getId()).append(" ");
}
}
sb.append(ScionUtil.toStringIA(path.getInterfacesList().get(nInterfcaces - 1).getIsdAs()));
sb.append("]");
return sb.toString();
}

private static void checkLimits(int isd, long as) {
if (isd < 0 || isd > MaxISD) {
throw new IllegalArgumentException("ISD out of range: " + isd);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/scion/jpan/ScmpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,16 @@ public void close() throws IOException {
selector.close();
}
}

public RequestPath getConnectionPath() {
return (RequestPath) channel.getConnectionPath();
}

public InetSocketAddress getLocalAddress() throws IOException {
return channel.getLocalAddress();
}

public InetSocketAddress getRemoteAddress() throws IOException {
return channel.getRemoteAddress();
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/scion/jpan/internal/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public void set(long l) {
* @param bitCount number of bits to read
* @return extracted bits as int.
*/
static int readInt(int input, int bitOffset, int bitCount) {
public static int readInt(int input, int bitOffset, int bitCount) {
int mask = (-1) >>> (32 - bitCount);
int shift = 32 - bitOffset - bitCount;
return (input >>> shift) & mask;
}

static long readLong(long input, int bitOffset, int bitCount) {
public static long readLong(long input, int bitOffset, int bitCount) {
long mask = (-1L) >>> (64 - bitCount);
int shift = 64 - bitOffset - bitCount;
return (input >>> shift) & mask;
}

static boolean readBoolean(int input, int bitOffset) {
public static boolean readBoolean(int input, int bitOffset) {
int mask = 1;
int shift = 32 - bitOffset - 1;
return ((input >>> shift) & mask) != 0;
Expand Down
240 changes: 240 additions & 0 deletions src/main/java/org/scion/jpan/internal/PathRawParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// Copyright 2023 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.scion.jpan.internal.ByteUtil.readBoolean;
import static org.scion.jpan.internal.ByteUtil.readInt;
import static org.scion.jpan.internal.ByteUtil.readLong;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class PathRawParser {
private static final String NL = System.lineSeparator();

// 2 bit : (C)urrINF : 2-bits index (0-based) pointing to the current info field (see offset
// calculations below).
private int currINF;
// 6 bit : CurrHF : 6-bits index (0-based) pointing to the current hop field (see offset
// calculations below).
private int currHF;
// 6 bit : RSV
private int reserved;
// Up to 3 Info fields and up to 64 Hop fields
// The number of hop fields in a given segment. Seg,Len > 0 implies the existence of info field i.
// 6 bit : Seg0Len
// 6 bit : Seg1Len
// 6 bit : Seg2Len
private final int[] segLen = new int[3];
private final InfoField[] info = new InfoField[3];

private final HopField[] hops = new HopField[64];
private int nHops;

private int len;

public 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) {
int start = data.position();
// 2 bit : (C)urrINF : 2-bits index (0-based) pointing to the current info field (see offset
// calculations below).
// 6 bit : CurrHF : 6-bits index (0-based) pointing to the current hop field (see offset
// calculations below).
// 6 bit : RSV
// Up to 3 Info fields and up to 64 Hop fields
// The number of hop fields in a segment. SegLen > 0 implies the existence of info field i.
// 6 bit : Seg0Len
// 6 bit : Seg1Len
// 6 bit : Seg2Len

int i0 = data.getInt();
currINF = readInt(i0, 0, 2);
currHF = readInt(i0, 2, 6);
reserved = readInt(i0, 8, 6);
segLen[0] = readInt(i0, 14, 6);
segLen[1] = readInt(i0, 20, 6);
segLen[2] = readInt(i0, 26, 6);

for (int i = 0; i < segLen.length && segLen[i] > 0; i++) {
info[i].read(data);
}

nHops = segLen[0] + segLen[1] + segLen[2];
for (int i = 0; i < nHops; i++) {
hops[i].read(data);
}

len = data.position() - start;
}

@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("currINF=").append(currINF).append(" currHP=").append(currHF);
s.append(" reserved=").append(reserved);
for (int i = 0; i < segLen.length; i++) {
s.append(" seg").append(i).append("Len=").append(segLen[i]);
}
for (int i = 0; i < segLen.length; i++) {
if (segLen[i] > 0) {
s.append(NL).append(" info").append(i).append("=").append(info[i]);
}
}
for (int i = 0; i < nHops; i++) {
s.append(NL).append(" hop=").append(hops[i]);
}
return s.toString();
}

public int length() {
return len;
}

public InfoField getInfoField(int i) {
return info[i];
}

public int getHopCount() {
return nHops;
}

public HopField getHopField(int i) {
return hops[i];
}

public int getSegLen(int i) {
return segLen[i];
}

public static class HopField {

/**
* 1 bit : ConsIngress Router Alert. If the ConsIngress Router Alert is set, the ingress router
* (in construction direction) will process the L4 payload in the packet.
*/
private boolean flagI;

/**
* 1 bit : ConsEgress Router Alert. If the ConsEgress Router Alert is set, the egress router (in
* construction direction) will process the L4 payload in the packet.
*/
private boolean flagE;

/**
* 8 bits : Expiry time of a hop field. The expiration time expressed is relative. An absolute
* expiration time in seconds is computed in combination with the timestamp field (from the
* corresponding info field) as follows: abs_time = timestamp + (1+expiryTime)*24*60*60/256
*/
private int expiryTime;

// 16 bits : consIngress : The 16-bits ingress interface IDs in construction direction.
private int consIngress;
// 16 bits : consEgress : The 16-bits egress interface IDs in construction direction.
private int consEgress;
// 48 bits : MAC : 6-byte Message Authentication Code to authenticate the hop field.
// For details on how this MAC is calculated refer to Hop Field MAC Computation:
// https://scion.docs.anapaya.net/en/latest/protocols/scion-header.html#hop-field-mac-computation
private long mac;

HopField() {}

public void read(ByteBuffer data) {
int i0 = data.getInt();
long l1 = data.getLong();
flagI = readBoolean(i0, 6);
flagE = readBoolean(i0, 7);
expiryTime = readInt(i0, 8, 8);
consIngress = readInt(i0, 16, 16);
consEgress = (int) readLong(l1, 0, 16);
mac = readLong(l1, 16, 48);
}

@Override
public String toString() {
String s = "I=" + flagI + ", E=" + flagE + ", expiryTime=" + expiryTime;
s += ", consIngress=" + consIngress + ", consEgress=" + consEgress + ", mac=" + mac;
return s;
}

public int length() {
return 12;
}

public int getIngress() {
return consIngress;
}

public int getEgress() {
return consEgress;
}

public boolean hasIngressAlert() {
return flagI;
}

public boolean hasEgressAlert() {
return flagE;
}
}

public static class InfoField {

private boolean p;
private boolean c;
// 16 bits : segID
private int segID;
// 32 bits : timestamp (unsigned int)
private int timestampRaw; // "raw" because field type is "signed int"

InfoField() {}

public void read(ByteBuffer data) {
int i0 = data.getInt();
int i1 = data.getInt();
p = readBoolean(i0, 6);
c = readBoolean(i0, 7);
segID = readInt(i0, 16, 16);
timestampRaw = i1;
}

public int length() {
return 8;
}

@Override
public String toString() {
long ts = Integer.toUnsignedLong(timestampRaw);
return "P=" + p + ", C=" + c + ", segID=" + segID + ", timestamp=" + ts;
}

public long getTimestamp() {
return Integer.toUnsignedLong(timestampRaw);
}

public boolean hasConstructionDirection() {
return c;
}

public boolean getFlagC() {
return c;
}
}
}
6 changes: 6 additions & 0 deletions src/test/java/org/scion/jpan/PackageVisibilityHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public static ResponsePath createDummyResponsePath(
}
}

public static RequestPath createRequestPath110_110(
Daemon.Path.Builder builder, long isdAs, InetAddress dstHost, int dstPort) {
Daemon.Path path = builder.build();
return RequestPath.create(path, isdAs, dstHost, dstPort);
}

public static RequestPath createRequestPath110_112(
Daemon.Path.Builder builder,
long dstIsdAs,
Expand Down
Loading