Skip to content

Commit

Permalink
toStringIP (#105)
Browse files Browse the repository at this point in the history
Co-authored-by: Tilmann Zäschke <tilmann.zaeschke@inf.ethz.ch>
  • Loading branch information
tzaeschke and Tilmann Zäschke authored Jul 1, 2024
1 parent 45a5136 commit 3e77c32
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 139 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- Remove use of 0.0.0.0 and "::". [#103](https://github.com/scionproto-contrib/jpan/pull/103)

### Removed
- Removed some useless IP printing functions.
[#105](https://github.com/scionproto-contrib/jpan/pull/105)

## [0.2.0] - 2024-06-24

### Added
Expand Down
20 changes: 8 additions & 12 deletions src/test/java/org/scion/jpan/demo/inspector/ScionHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,16 @@ public String toString() {
sb.append(" dstIsdAs=").append(ScionUtil.toStringIA(dstIsdAs));
sb.append(" srcIsdAs=").append(ScionUtil.toStringIA(srcIsdAs));
sb.append(" dstHost=").append(dt).append("/");
if (dl == 0) {
sb.append(ToStringUtil.toStringIPv4(dstHost));
} else if (dl == 3) {
sb.append(ToStringUtil.toStringIPv6(dstHost));
if (dl == 0 || dl == 3) {
sb.append(ToStringUtil.toStringIP(dstHost));
} else {
sb.append("Format not recognized: ").append(ToStringUtil.toStringIPv6(dstHost));
sb.append("Format not recognized: ").append(ToStringUtil.toStringByte(dstHost));
}
sb.append(" srcHost=").append(st).append("/");
if (sl == 0) {
sb.append(ToStringUtil.toStringIPv4(srcHost));
} else if (sl == 3) {
sb.append(ToStringUtil.toStringIPv6(srcHost));
if (sl == 0 || sl == 3) {
sb.append(ToStringUtil.toStringIP(srcHost));
} else {
sb.append("Format not recognized: ").append(ToStringUtil.toStringIPv6(srcHost));
sb.append("Format not recognized: ").append(ToStringUtil.toStringByte(srcHost));
}
return sb.toString();
}
Expand All @@ -266,9 +262,9 @@ public void setDstIA(long dstIsdAs) {

public String getSrcHostString() {
if (sl == 0 && (st == 0 || st == 1)) {
return ToStringUtil.toStringIPv4(srcHost);
return ToStringUtil.toStringIP(srcHost);
} else if (sl == 3 && st == 0) {
return ToStringUtil.toStringIPv6(srcHost);
return ToStringUtil.toStringIP(srcHost);
} else {
throw new UnsupportedOperationException("Src address not supported: ST/SL=" + st + "/" + sl);
}
Expand Down
80 changes: 6 additions & 74 deletions src/test/java/org/scion/jpan/demo/util/ToStringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,20 @@
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import org.scion.jpan.demo.inspector.HopField;
import org.scion.jpan.demo.inspector.InfoField;
import org.scion.jpan.demo.inspector.PathHeaderScion;

public class ToStringUtil {

public static String toStringIPv4(int ip) {
int mask = 0xFF000000;
String s = "";
s += ((ip & mask) >>> 24) + ".";
s += ((ip & (mask >>> 8)) >>> 16) + ".";
s += ((ip & (mask >>> 16)) >>> 8) + ".";
s += (ip & (mask >>> 24));
return s;
}

public static String toStringIPv6(int len, int... ips) {
String s = "";
for (int i = 0; i < len; i++) {
String s2 = Integer.toHexString(ips[i] >>> 16);
String s3 = Integer.toHexString(ips[i] & 0xFFFF);
s += s2 + ":" + s3;
if (i < len - 1) {
s += ":";
}
}
// TODO not quite correct, we should replace the LONGEST sequence of 0:0 instead of the FIRST
// one.
int pos = s.indexOf("0:0:");
if (pos >= 0) {
int pos2 = pos + 4;
for (; pos2 + 1 < s.length(); pos2 += 2) {
if (s.charAt(pos2) != '0' || s.charAt(pos2 + 1) != ':') {
break;
}
}
s = s.substring(0, pos) + s.substring(pos2 - 1);
if (pos == 0) {
s = ":" + s;
}
}
return s;
}

public static String toStringIPv4(byte[] bytes) {
return bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3];
}

public static String toStringIPv6(byte[] b) {
// TODO use custom fix-length builder with loop
Integer[] ints = new Integer[8];
for (int i = 0; i < ints.length; i++) {
ints[i] = b[i * 2] * 256 + b[i * 2 + 1];
public static String toStringIP(byte[] b) {
try {
return InetAddress.getByAddress(b).getHostAddress();
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e);
}
String s = String.format("%x:%x:%x:%x:%x:%x:%x:%x", (Object[]) ints);
// String s = String.format("%x%02x:%x%02x:%x%02x:%x%02x:%x%02x:%x%02x:%x%02x:%x%02x",
// b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
// b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
// String s = Integer.toHexString(bytes[0]) + Integer.toHexString(bytes[1]) + ":" +
// Integer.toHexString(bytes[2]) + Integer.toHexString(bytes[3]) + ":" +
// Integer.toHexString(bytes[4]) + Integer.toHexString(bytes[5]) + ":" +
// Integer.toHexString(bytes[6]) + Integer.toHexString(bytes[7]) + ":" +
// Integer.toHexString(bytes[8]) + Integer.toHexString(bytes[9]) + ":" +
// Integer.toHexString(bytes[10]) + Integer.toHexString(bytes[11]) + ":" +
// Integer.toHexString(bytes[12]) + Integer.toHexString(bytes[13]) + ":" +
// Integer.toHexString(bytes[14]) + Integer.toHexString(bytes[15]);
// TODO not quite correct, we should replace the LONGEST sequence of 0:0 instead of the FIRST
// one.
// int pos = s.indexOf(":0:");
// if (pos >= 0) {
// int pos2 = pos + 2;
// while (pos2 + 2 < s.length() && s.charAt(pos2 + 1) == '0' && s.charAt(pos2 + 2) == ':')
// {
// pos2 += 2;
// }
// s = s.substring(0, pos + 1) + s.substring(pos2);
// if (s.startsWith("0:")) {
// // TODO
// }
// }
return s;
}

/**
Expand Down
53 changes: 0 additions & 53 deletions src/test/java/org/scion/jpan/demo/util/ToStringUtilTest.java

This file was deleted.

0 comments on commit 3e77c32

Please sign in to comment.