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.targets;
024:
025: import net.fenyo.gnetwatch.*;
026: import net.fenyo.gnetwatch.actions.*;
027: import net.fenyo.gnetwatch.data.*;
028: import net.fenyo.gnetwatch.GUI.GUI;
029: import net.fenyo.gnetwatch.GUI.VisualElement;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.snmp4j.PDU;
034: import org.snmp4j.event.ResponseEvent;
035: import org.snmp4j.event.ResponseListener;
036:
037: import java.net.*;
038:
039: /**
040: * TargetIPv4 implements an IPv4 capable target.
041: * @author Alexandre Fenyo
042: * @version $Id: TargetIPv4.java,v 1.27 2007/03/09 22:44:20 fenyo Exp $
043: */
044:
045: public class TargetIPv4 extends Target {
046: private static Log log = LogFactory.getLog(TargetIPv4.class);
047:
048: private Inet4Address address; // not null
049: private SNMPQuerier snmp_querier;
050: private IPQuerier ip_querier;
051:
052: /**
053: * Constructor.
054: * @param name target name.
055: * @param address IPv4 address.
056: * @param SNMPManager snmp manager.
057: * @throws AlgorithmException exception.
058: */
059: // GUI thread
060: public TargetIPv4(final String name, final Inet4Address address,
061: final SNMPManager snmp_manager) throws AlgorithmException {
062: super (name);
063: if (address == null)
064: throw new AlgorithmException("address is null");
065: this .address = address;
066: snmp_querier = snmp_manager != null ? snmp_manager
067: .getQuerier(address) : null;
068: setItem(address.getHostAddress());
069: ip_querier = new IPQuerier(address);
070: // may last a long time (DNS resolver)
071: // setDescription(address.getCanonicalHostName());
072: }
073:
074: /**
075: * Checks that this host is SNMP capable.
076: * @param none.
077: * @return void.
078: */
079: // PENSER a changer l'icone en cas d'action SNMP qui marche car si ici ca a fait timeout... ET a mettre a jour le type
080: // faire drop action nmap et drop view nmap
081: public void checkSNMPAwareness() {
082: snmp_querier.getSysDescr(new SNMPQuerier.QuerierListener() {
083: public void onResponse(ResponseEvent event) {
084: getGUI().setStatus(
085: getGUI().getConfig().getPattern(
086: "discovered_snmp",
087: getAddress().toString().substring(1)));
088: getGUI().appendConsole(
089: getGUI().getConfig().getPattern(
090: "discovered_snmp",
091: getAddress().toString().substring(1))
092: + "<BR/>");
093: setImageHostSNMP();
094:
095: if (event != null
096: && event.getResponse() != null
097: && event.getResponse().size() > 0
098: && event.getResponse().get(0) != null
099: && event.getResponse().get(0).getVariable() != null)
100: setType(event.getResponse().get(0).getVariable()
101: .toString());
102: else
103: log.error("got a bad SNMP response");
104: }
105:
106: public void onTimeout(ResponseEvent event) {
107: getGUI().setStatus(
108: getGUI().getConfig().getPattern("snmp_timeout",
109: getAddress().toString().substring(1)));
110: }
111: });
112: }
113:
114: /**
115: * Returns the SNMP querier.
116: * @param none.
117: * @return SNMPQuerier querier instance.
118: */
119: public SNMPQuerier getSNMPQuerier() {
120: return snmp_querier;
121: }
122:
123: /**
124: * Returns the IP querier.
125: * @param none.
126: * @return IPQuerier querier instance.
127: */
128: public IPQuerier getIPQuerier() {
129: return ip_querier;
130: }
131:
132: /**
133: * Returns the IP address.
134: * @param none.
135: * @return Inet4Address IP address.
136: */
137: // any thread
138: public Inet4Address getAddress() {
139: return address;
140: }
141:
142: /**
143: * Checks that the parameter can be attached to this target.
144: * @param visual_element parameter to check.
145: * @return true if the parameter can be attached to this target.
146: */
147: public boolean canManageThisChild(final VisualElement visual_element) {
148: if (ActionPing.class.isInstance(visual_element))
149: return true;
150: if (ActionFlood.class.isInstance(visual_element))
151: return true;
152: if (ActionSNMP.class.isInstance(visual_element))
153: return true;
154: if (EventReachable.class.isInstance(visual_element))
155: return true;
156: if (TargetInterface.class.isInstance(visual_element))
157: return true;
158: if (ActionHTTP.class.isInstance(visual_element))
159: return true;
160: if (ActionNmap.class.isInstance(visual_element))
161: return true;
162: return false;
163: }
164:
165: /**
166: * Initializes this target.
167: * @param gui current GUI instance.
168: * @return void.
169: */
170: // final because called by constructor (by means of setItem())
171: public final void initialize(final GUI gui) {
172: super .initialize(gui);
173: if (gui != null)
174: setImageHost();
175: }
176:
177: /**
178: * Attaches this target to a specific parent.
179: * @param gui current GUI instance.
180: * @param parent parent.
181: * @return true if this target has been succesfully attached.
182: */
183: public boolean addTarget(final GUI gui, final VisualElement parent) {
184: initialize(gui);
185:
186: if (!canAddTarget(parent))
187: return false;
188: if (parent != null && !parent.canManageThisChild(this ))
189: return false;
190:
191: final boolean is_new = !getGUI()
192: .containsCanonicalInstance(this );
193: final TargetIPv4 target_ipv4 = (TargetIPv4) getGUI()
194: .getCanonicalInstance(this );
195:
196: if (!getGUI().getVisualTransientAll().contains(target_ipv4))
197: target_ipv4.setParent(getGUI(), getGUI()
198: .getVisualTransientAll());
199:
200: final String addr_str = GenericTools
201: .inet4AddressToString(target_ipv4.getAddress());
202: final String net_str = GenericTools.getNetFromAddress(addr_str);
203:
204: if (addr_str == null || net_str == null) {
205: if (is_new)
206: getGUI().dropTargetInstance(target_ipv4);
207: log.error("addr_str or net_str is null - " + getItem());
208: return false;
209: }
210:
211: Inet4Address net_addr = null;
212: Inet4Address net_netmask = null;
213:
214: try {
215: if (net_str.endsWith("/4")) {
216:
217: net_addr = (Inet4Address) InetAddress
218: .getByName(GenericTools
219: .unsignedByteToShort(address
220: .getAddress()[0]) < 240 ? "224.0.0.0"
221: : "240.0.0.0");
222: net_netmask = (Inet4Address) InetAddress
223: .getByName("240.0.0.0");
224: }
225:
226: if (net_str.endsWith("/8")) {
227: net_addr = (Inet4Address) InetAddress.getByName(net_str
228: .substring(0, net_str.length() - 2));
229: net_netmask = (Inet4Address) InetAddress
230: .getByName("255.0.0.0");
231: }
232:
233: if (net_str.endsWith("/16")) {
234: net_addr = (Inet4Address) InetAddress.getByName(net_str
235: .substring(0, net_str.length() - 3));
236: net_netmask = (Inet4Address) InetAddress
237: .getByName("255.255.0.0");
238: }
239:
240: if (net_str.endsWith("/24")) {
241: net_addr = (Inet4Address) InetAddress.getByName(net_str
242: .substring(0, net_str.length() - 3));
243: net_netmask = (Inet4Address) InetAddress
244: .getByName("255.255.255.0");
245: }
246: } catch (final UnknownHostException ex) {
247: if (is_new)
248: getGUI().dropTargetInstance(target_ipv4);
249: log.error("Exception", ex);
250: return false;
251: }
252:
253: TargetIPv4Subnet subnet;
254: try {
255: subnet = new TargetIPv4Subnet(addr_str, net_addr,
256: net_netmask);
257: } catch (final AlgorithmException ex) {
258: if (is_new)
259: getGUI().dropTargetInstance(target_ipv4);
260: log.error("Exception", ex);
261: return false;
262: }
263:
264: subnet = (TargetIPv4Subnet) getGUI().getCanonicalInstance(
265: subnet);
266: if (!getGUI().getVisualTransientNetworks().contains(subnet))
267: subnet.setParent(getGUI(), getGUI()
268: .getVisualTransientNetworks());
269: if (!subnet.contains(target_ipv4))
270: target_ipv4.setParent(getGUI(), subnet);
271:
272: if (parent != null)
273: target_ipv4.setParent(getGUI(), parent);
274:
275: return is_new;
276: }
277:
278: /**
279: * Attaches this target to a specific parent defined by its address.
280: * @param gui current GUI instance.
281: * @param addr_str parent address.
282: * @return true if this target has been succesfully attached.
283: */
284: public static void addTargetIPv4(final GUI gui,
285: final String addr_str) {
286: try {
287: synchronized (gui.sync_tree) {
288: final TargetIPv4 foo = new TargetIPv4(addr_str,
289: (Inet4Address) InetAddress.getByName(addr_str),
290: gui.getSNMPManager());
291: if (gui.containsCanonicalInstance(foo))
292: return;
293: gui.asyncExecIfNeeded(new Runnable() {
294: public void run() {
295: synchronized (gui.sync_tree) {
296: if (gui.containsCanonicalInstance(foo))
297: return;
298: if (foo
299: .addTarget(gui,
300: (VisualElement) null) == true) {
301: gui.setStatus(gui.getConfig()
302: .getPattern(
303: "adding_target",
304: foo.getAddress()
305: .toString()
306: .substring(1)));
307: foo.checkSNMPAwareness();
308: }
309: }
310: }
311: });
312: }
313: } catch (final AlgorithmException ex) {
314: log.error("Exception", ex);
315: } catch (final UnknownHostException ex) {
316: log.error("Exception", ex);
317: }
318: }
319:
320: /**
321: * Compares two targets.
322: * @param o target to compare to.
323: * @return true if the targets are equal.
324: */
325: // any thread
326: public boolean equals(final Object o) {
327: if (this == o)
328: return true;
329: if ((o == null) || (o.getClass() != getClass()))
330: return false;
331: final TargetIPv4 target = (TargetIPv4) o;
332: return getAddress().equals(target.getAddress());
333: }
334:
335: /**
336: * Returns the hashcode for this target.
337: * @param none.
338: * @return int hashcode.
339: */
340: // any thread
341: public int hashCode() {
342: return getAddress().hashCode();
343: }
344: }
|