diff --git a/CHANGELOG.md b/CHANGELOG.md index d4a2867d6..e4147cab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/test/java/org/scion/jpan/demo/inspector/ScionHeader.java b/src/test/java/org/scion/jpan/demo/inspector/ScionHeader.java index 181043c05..5cb205eac 100644 --- a/src/test/java/org/scion/jpan/demo/inspector/ScionHeader.java +++ b/src/test/java/org/scion/jpan/demo/inspector/ScionHeader.java @@ -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(); } @@ -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); } diff --git a/src/test/java/org/scion/jpan/demo/util/ToStringUtil.java b/src/test/java/org/scion/jpan/demo/util/ToStringUtil.java index 6884ab4e3..317d6dc27 100644 --- a/src/test/java/org/scion/jpan/demo/util/ToStringUtil.java +++ b/src/test/java/org/scion/jpan/demo/util/ToStringUtil.java @@ -17,6 +17,7 @@ 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; @@ -24,81 +25,12 @@ 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; } /** diff --git a/src/test/java/org/scion/jpan/demo/util/ToStringUtilTest.java b/src/test/java/org/scion/jpan/demo/util/ToStringUtilTest.java deleted file mode 100644 index 81cbc3513..000000000 --- a/src/test/java/org/scion/jpan/demo/util/ToStringUtilTest.java +++ /dev/null @@ -1,53 +0,0 @@ -// 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.demo.util; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -class ToStringUtilTest { - - @Test - void testIPv4_fromBytes() { - byte[] bytes1 = new byte[] {127, 0, 15, 23}; - String ip1 = ToStringUtil.toStringIPv4(bytes1); - assertEquals("127.0.15.23", ip1); - } - - @Test - void testIPv6_fromBytes() { - // For now without :: substitution - byte[] bytes1 = new byte[] {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2}; - String ip1 = ToStringUtil.toStringIPv6(bytes1); - assertEquals("100:0:100:0:100:0:100:2", ip1); - - // byte[] bytes1 = new byte[]{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,2}; - // String ip1 = ScionUtil.toStringIPv6(bytes1); - // assertEquals("100::100:0:100:0:100:2", ip1); - // - // byte[] bytes2 = new byte[]{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2}; - // String ip2 = ScionUtil.toStringIPv6(bytes2); - // assertEquals("100::100:2", ip2); - // - // byte[] bytes3 = new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; - // String ip3 = ScionUtil.toStringIPv6(bytes3); - // assertEquals("::1", ip3); - // - // byte[] bytes4 = new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - // String ip4 = ScionUtil.toStringIPv6(bytes4); - // assertEquals("::", ip4); - } -}