001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch;
024:
025: import java.net.*;
026: import java.util.*;
027: import java.util.regex.*;
028:
029: import org.eclipse.swt.graphics.*;
030:
031: import java.net.*;
032:
033: import net.fenyo.gnetwatch.GUI.VisualElement;
034:
035: import org.apache.commons.logging.*;
036: import org.apache.log4j.xml.*;
037:
038: /**
039: * General methods not dedicated to a particular application.
040: * @author Alexandre Fenyo
041: * @version $Id: GenericTools.java,v 1.13 2007/03/09 22:44:21 fenyo Exp $
042: */
043:
044: public class GenericTools {
045: private static Log log = LogFactory.getLog(GenericTools.class);
046:
047: /**
048: * Configure the global logging rules.
049: * @param config Reference to the general configuration instance.
050: * @return void.
051: */
052: static public void initLogEngine(final Config config) {
053: // configure log4j
054: DOMConfigurator.configure(config.getProperty("log4j"));
055: }
056:
057: /**
058: * Converts a string to an Inet4Address.
059: * @param str source string.
060: * @return Inet4Address address representing this string.
061: */
062: static public Inet4Address stringToInet4Address(final String str)
063: throws UnknownHostException {
064: /*
065: final Matcher match =
066: Pattern.compile("([0-9]*)\\.([0-9]*)\\.([0-9]*)\\.([0-9]*)").matcher(str);
067: if (match.find() == true) {
068: return (Inet4Address) InetAddress.getByAddress(new byte [] {
069: new Integer(match.group(1)).byteValue(),
070: new Integer(match.group(2)).byteValue(),
071: new Integer(match.group(3)).byteValue(),
072: new Integer(match.group(4)).byteValue(),
073: });
074: } else return stringToInet4Address("0.0.0.0");
075: */
076: // shortest alternative to the previous code:
077: return (Inet4Address) InetAddress.getByName(str);
078: }
079:
080: /**
081: * Converts a string to an Inet6Address.
082: * @param str source string.
083: * @return Inet6Address address representing this string.
084: */
085: static public Inet6Address stringToInet6Address(final String str)
086: throws UnknownHostException {
087: try {
088: return (Inet6Address) InetAddress.getByName(str);
089: } catch (final ClassCastException ex) {
090: return null;
091: }
092: }
093:
094: /**
095: * Converts a unsigned byte (encoded into a java signed byte) to a positive signed short.
096: * @param ub unsigned byte encoded into a java signed byte.
097: * @return short positive signed short.
098: */
099: static public short unsignedByteToShort(final byte ub) {
100: final int foo = ub;
101: return (short) (foo < 0 ? foo + 256 : foo);
102: }
103:
104: /**
105: * Converts an IP address to its string representation.
106: * @param addr ipv4 address.
107: * @return String ascii representation.
108: */
109: static public String inet4AddressToString(final Inet4Address addr) {
110: try {
111: byte bytes[] = addr.getAddress();
112: return InetAddress.getByAddress(bytes).toString()
113: .substring(1);
114: } catch (final UnknownHostException ex) {
115: log.error("Exception", ex);
116: }
117: return "";
118: }
119:
120: /**
121: * Converts an IP address to its string representation.
122: * @param addr ipv6 address.
123: * @return String ascii representation.
124: */
125: static public String inet6AddressToString(final Inet6Address addr) {
126: try {
127: byte bytes[] = addr.getAddress();
128: return InetAddress.getByAddress(bytes).toString()
129: .substring(1);
130: } catch (final UnknownHostException ex) {
131: log.error("Exception", ex);
132: }
133: return "";
134: }
135:
136: /**
137: * Returns the class A/B/C network address containing an IP address.
138: * @param addr_str IPv4 address.
139: * @return String network address.
140: */
141: static public String getNetFromAddress(final String addr_str) {
142: try {
143: final InetAddress addr;
144:
145: addr = InetAddress.getByName(addr_str);
146:
147: if (Inet6Address.class.isInstance(addr)) {
148: // rfc-4291
149: return "IPv6 range";
150: } else if (Inet4Address.class.isInstance(addr)) {
151: byte bytes[] = ((Inet4Address) addr).getAddress();
152: if (unsignedByteToShort(bytes[0]) < 128) {
153: // class A
154: bytes[1] = 0;
155: bytes[2] = 0;
156: bytes[3] = 0;
157: return InetAddress.getByAddress(bytes).toString()
158: .substring(1)
159: + "/8";
160: } else if (unsignedByteToShort(bytes[0]) < 192) {
161: // class B
162: bytes[2] = 0;
163: bytes[3] = 0;
164: return InetAddress.getByAddress(bytes).toString()
165: .substring(1)
166: + "/16";
167: } else if (unsignedByteToShort(bytes[0]) < 224) {
168: // class C
169: bytes[3] = 0;
170: return InetAddress.getByAddress(bytes).toString()
171: .substring(1)
172: + "/24";
173: } else if (unsignedByteToShort(bytes[0]) < 248) {
174: // class D
175: return "224.0.0.0/4";
176: } else {
177: // class E
178: return "248.0.0.0/4";
179: }
180: } else
181: return null;
182: } catch (final UnknownHostException ex) {
183: log.error("Exception (addr_str=" + addr_str + ")", ex);
184: return null;
185: }
186: }
187:
188: /**
189: * Removes the part of a graph that is covered by another graph.
190: * Note that the second graph need not to be a subgraph of the first one.
191: * @param addr g1 initial graph.
192: * @param addr g2 graph defining links to remove to the initial graph.
193: * @return void.
194: */
195: static public void substractGraph(
196: java.util.List<Pair<VisualElement, VisualElement>> g1,
197: java.util.List<Pair<VisualElement, VisualElement>> g2) {
198: final java.util.List<Pair<VisualElement, VisualElement>> gtemp = new LinkedList<Pair<VisualElement, VisualElement>>(
199: g1);
200: for (final Pair<VisualElement, VisualElement> p : gtemp)
201: if (g2.contains(p))
202: g1.remove(p);
203: }
204: }
|