diff options
4 files changed, 81 insertions, 3 deletions
diff --git a/src/main/java/com/lumaserv/bgp/protocol/IPAddress.java b/src/main/java/com/lumaserv/bgp/protocol/IPAddress.java new file mode 100644 index 0000000..025e392 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/IPAddress.java @@ -0,0 +1,76 @@ +package com.lumaserv.bgp.protocol; + +import java.net.InetAddress; +import java.net.Inet6Address; + +public class IPAddress { + + static int toInt(byte[] array, int pos) { + return (((int)array[pos] & 0xff) << 8) + ((int)array[pos + 1] & 0xff); + } + + public static String toString(InetAddress address) { + byte[] raw = address.getAddress(); + + if (!(raw.length == 16 && address instanceof Inet6Address)) + return address.getHostAddress(); + + int maxPos = -1; + int maxCount = 0; + int pos = -1; + int count = 0; + + for (int i=0; i < 8; i++) { + int value = toInt(raw, i * 2); + if (value == 0) { + if (pos < 0) { + pos = i; + count = 0; + } + count++; + } else { + if (count > maxCount) { + maxPos = pos; + maxCount = count; + } + pos = -1; + count = 0; + } + } + + if (count > maxCount) { + maxPos = pos; + maxCount = count; + } + + if (maxCount < 2) { + maxPos = -1; + maxCount = 0; + } + + StringBuffer buf = new StringBuffer(8 * (4+1)); + + pos = -1; + count = 0; + for (int i=0; i < 8; i++) { + if (count > 0) { + count--; + if (count == 0) { + if (i == 7) + buf.append("::"); + else + buf.append(":"); + } + } else if (i == maxPos) { + count = maxCount - 1; + pos = maxPos; + } else { + if (i > 0) + buf.append(":"); + int value = toInt(raw, i * 2); + buf.append(Integer.toString(value, 16)); + } + } + return buf.toString(); + } +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java b/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java index f68677c..5d38cf6 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java +++ b/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java @@ -17,7 +17,7 @@ public class IPPrefix { public String toString() { try { - return InetAddress.getByAddress(address).getHostAddress() + "/" + length; + return IPAddress.toString(InetAddress.getByAddress(address)) + "/" + length; } catch (UnknownHostException e) { e.printStackTrace(); return null; diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java index 0cb2e49..392b2de 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java @@ -10,6 +10,7 @@ import lombok.Getter; import lombok.Setter; import com.lumaserv.bgp.protocol.AFI; +import com.lumaserv.bgp.protocol.IPAddress; import com.lumaserv.bgp.protocol.IPPrefix; import com.lumaserv.bgp.protocol.SAFI; @@ -60,6 +61,6 @@ public class MPReachableNLRIAttribute implements PathAttribute { } public String toString() { - return "{AFI:" + afi + " SAFI:" + safi + " NextHop:" + nextHop.getHostAddress() + " NLRIs:" + nlriPrefixes + "}"; + return "{AFI:" + afi + " SAFI:" + safi + " NextHop:" + IPAddress.toString(nextHop) + " NLRIs:" + nlriPrefixes + "}"; } } diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/TunnelEncapsAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/TunnelEncapsAttribute.java index 747b923..0cfd9bc 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/TunnelEncapsAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/TunnelEncapsAttribute.java @@ -1,6 +1,7 @@ package com.lumaserv.bgp.protocol.attribute; import com.lumaserv.bgp.protocol.AFI; +import com.lumaserv.bgp.protocol.IPAddress; import lombok.AllArgsConstructor; import lombok.Getter; @@ -291,7 +292,7 @@ public class TunnelEncapsAttribute implements PathAttribute { @Override public String toString() { - return "IP:" + address.getHostAddress(); + return "IP:" + IPAddress.toString(address); } } |