1 2 23 24 package net.fenyo.gnetwatch; 25 26 import java.net.*; 27 import java.util.*; 28 import java.util.regex.*; 29 30 import org.eclipse.swt.graphics.*; 31 32 import java.net.*; 33 34 import net.fenyo.gnetwatch.GUI.VisualElement; 35 36 import org.apache.commons.logging.*; 37 import org.apache.log4j.xml.*; 38 39 44 45 public class GenericTools { 46 private static Log log = LogFactory.getLog(GenericTools.class); 47 48 53 static public void initLogEngine(final Config config) { 54 DOMConfigurator.configure(config.getProperty("log4j")); 56 } 57 58 63 static public Inet4Address stringToInet4Address(final String str) throws UnknownHostException { 64 76 return (Inet4Address) InetAddress.getByName(str); 78 } 79 80 85 static public Inet6Address stringToInet6Address(final String str) throws UnknownHostException { 86 try { 87 return (Inet6Address) InetAddress.getByName(str); 88 } catch (final ClassCastException ex) { 89 return null; 90 } 91 } 92 93 98 static public short unsignedByteToShort(final byte ub) { 99 final int foo = ub; 100 return (short) (foo < 0 ? foo + 256 : foo); 101 } 102 103 108 static public String inet4AddressToString(final Inet4Address addr) { 109 try { 110 byte bytes[] = addr.getAddress(); 111 return InetAddress.getByAddress(bytes).toString().substring(1); 112 } catch (final UnknownHostException ex) { 113 log.error("Exception", ex); 114 } 115 return ""; 116 } 117 118 123 static public String inet6AddressToString(final Inet6Address addr) { 124 try { 125 byte bytes[] = addr.getAddress(); 126 return InetAddress.getByAddress(bytes).toString().substring(1); 127 } catch (final UnknownHostException ex) { 128 log.error("Exception", ex); 129 } 130 return ""; 131 } 132 133 138 static public String getNetFromAddress(final String addr_str) { 139 try { 140 final InetAddress addr; 141 142 addr = InetAddress.getByName(addr_str); 143 144 if (Inet6Address.class.isInstance(addr)) { 145 return "IPv6 range"; 147 } else if (Inet4Address.class.isInstance(addr)) { 148 byte bytes[] = ((Inet4Address) addr).getAddress(); 149 if (unsignedByteToShort(bytes[0]) < 128) { 150 bytes[1] = 0; 152 bytes[2] = 0; 153 bytes[3] = 0; 154 return InetAddress.getByAddress(bytes).toString().substring(1) + "/8"; 155 } else if (unsignedByteToShort(bytes[0]) < 192) { 156 bytes[2] = 0; 158 bytes[3] = 0; 159 return InetAddress.getByAddress(bytes).toString().substring(1) + "/16"; 160 } else if (unsignedByteToShort(bytes[0]) < 224) { 161 bytes[3] = 0; 163 return InetAddress.getByAddress(bytes).toString().substring(1) + "/24"; 164 } else if (unsignedByteToShort(bytes[0]) < 248) { 165 return "224.0.0.0/4"; 167 } else { 168 return "248.0.0.0/4"; 170 } 171 } else return null; 172 } catch (final UnknownHostException ex) { 173 log.error("Exception (addr_str=" + addr_str + ")", ex); 174 return null; 175 } 176 } 177 178 185 static public void substractGraph(java.util.List <Pair<VisualElement, VisualElement>> g1, java.util.List <Pair<VisualElement, VisualElement>> g2) { 186 final java.util.List <Pair<VisualElement, VisualElement>> gtemp = new LinkedList<Pair<VisualElement, VisualElement>>(g1); 187 for (final Pair<VisualElement, VisualElement> p : gtemp) 188 if (g2.contains(p)) g1.remove(p); 189 } 190 } 191 | Popular Tags |