From e227b5015138d461799e5a24261db9f331b8f4c6 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Fri, 3 Dec 2010 13:40:42 +0000 Subject: [PATCH] fixed parsing of sky ipv6 addresses --- .../se/sics/cooja/interfaces/IPAddress.java | 66 ++++++++++++------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java b/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java index a2aed5959..d027bfb32 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: IPAddress.java,v 1.8 2010/02/03 10:14:46 fros4943 Exp $ + * $Id: IPAddress.java,v 1.9 2010/12/03 13:40:42 fros4943 Exp $ */ package se.sics.cooja.interfaces; @@ -71,6 +71,7 @@ public class IPAddress extends MoteInterface { String ipString = getIPString(); ipString = ipString.replace(".", ""); + ipString = ipString.replace(":", ""); ipString = ipString.replace("0", ""); if (!ipString.isEmpty()) { setChanged(); @@ -104,31 +105,52 @@ public class IPAddress extends MoteInterface { ipString += (0xFF & ip[3]); return ipString; } else if (isVersion6()) { - String ipString = ""; - - /* XXX Assuming fixed offset in struct uip_netif (uip-netif.h) */ - int offset = - 4*4 /* 4 uint32_t */ + 2*moteMem.getIntegerLength() /* 2 uint8_t */; - byte[] tmp = moteMem.getByteArray( - "uip_netif_physical_if", - offset + 16); - byte[] ip = new byte[16]; - System.arraycopy(tmp, offset, ip, 0, 16); - - int i=0; - while (i < 14) { - int val = (0xFF&ip[i+1]) + ((0xFF&ip[i])<<8); - ipString += hex16ToString(val) + "."; - i+=2; - } - int val = (0xFF&ip[15]) + ((0xFF&ip[14])<<8); - ipString += hex16ToString(val); - + String ipString = getUncompressedIPv6Address(); + if (ipString != null) { + ipString = compressIPv6Address(ipString); + } return ipString; } return null; } + public String compressIPv6Address(String ipString) { + while (ipString.contains(":0000:")) { + ipString = ipString.replaceAll(":0000:", "::"); + } + while (ipString.contains("0000")) { + ipString = ipString.replaceAll("0000", "0"); + } + while (ipString.contains(":::")) { + ipString = ipString.replaceAll(":::", "::"); + } + while (ipString.contains(":0")) { + ipString = ipString.replaceAll(":0", ":"); + } + if (ipString.endsWith(":")) { + ipString = ipString + "0"; + } + return ipString; + } + public String getUncompressedIPv6Address() { + /* TODO cpu independent */ + String ipString = ""; + int offset = 102; + byte[] tmp = moteMem.getByteArray("uip_ds6_if", offset + 16); + byte[] ip = new byte[16]; + System.arraycopy(tmp, offset, ip, 0, 16); + + int i=0; + while (i < 14) { + int val = (0xFF&ip[i+1]) + ((0xFF&ip[i])<<8); + ipString += hex16ToString(val) + ":"; + i+=2; + } + int val = (0xFF&ip[15]) + ((0xFF&ip[14])<<8); + ipString += hex16ToString(val); + return ipString; + } + /** * @return True if mote has an IPv4 address */ @@ -140,7 +162,7 @@ public class IPAddress extends MoteInterface { * @return True if mote has an IPv6 address */ public boolean isVersion6() { - return moteMem.variableExists("uip_netif_physical_if"); + return moteMem.variableExists("uip_ds6_if"); } public JPanel getInterfaceVisualizer() {