KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > targets > TargetIPv4


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.targets;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.actions.*;
28 import net.fenyo.gnetwatch.data.*;
29 import net.fenyo.gnetwatch.GUI.GUI;
30 import net.fenyo.gnetwatch.GUI.VisualElement;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.snmp4j.PDU;
35 import org.snmp4j.event.ResponseEvent;
36 import org.snmp4j.event.ResponseListener;
37
38 import java.net.*;
39
40 /**
41  * TargetIPv4 implements an IPv4 capable target.
42  * @author Alexandre Fenyo
43  * @version $Id: TargetIPv4.java,v 1.27 2007/03/09 22:44:20 fenyo Exp $
44  */

45
46 public class TargetIPv4 extends Target {
47   private static Log log = LogFactory.getLog(TargetIPv4.class);
48
49   private Inet4Address address; // not null
50
private SNMPQuerier snmp_querier;
51   private IPQuerier ip_querier;
52
53   /**
54    * Constructor.
55    * @param name target name.
56    * @param address IPv4 address.
57    * @param SNMPManager snmp manager.
58    * @throws AlgorithmException exception.
59    */

60   // GUI thread
61
public TargetIPv4(final String JavaDoc name, final Inet4Address address, final SNMPManager snmp_manager) throws AlgorithmException {
62     super(name);
63     if (address == null) throw new AlgorithmException("address is null");
64     this.address = address;
65     snmp_querier = snmp_manager != null ? snmp_manager.getQuerier(address) : null;
66     setItem(address.getHostAddress());
67     ip_querier = new IPQuerier(address);
68     // may last a long time (DNS resolver)
69
// setDescription(address.getCanonicalHostName());
70
}
71
72   /**
73    * Checks that this host is SNMP capable.
74    * @param none.
75    * @return void.
76    */

77 // 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
78
// faire drop action nmap et drop view nmap
79
public void checkSNMPAwareness() {
80     snmp_querier.getSysDescr(new SNMPQuerier.QuerierListener() {
81       public void onResponse(ResponseEvent event) {
82         getGUI().setStatus(getGUI().getConfig().getPattern("discovered_snmp", getAddress().toString().substring(1)));
83         getGUI().appendConsole(getGUI().getConfig().getPattern("discovered_snmp", getAddress().toString().substring(1)) + "<BR/>");
84         setImageHostSNMP();
85
86         if (event != null && event.getResponse() != null && event.getResponse().size() > 0 &&
87             event.getResponse().get(0) != null && event.getResponse().get(0).getVariable() != null)
88           setType(event.getResponse().get(0).getVariable().toString());
89         else log.error("got a bad SNMP response");
90       }
91
92       public void onTimeout(ResponseEvent event) {
93         getGUI().setStatus(getGUI().getConfig().getPattern("snmp_timeout", getAddress().toString().substring(1)));
94       }
95     });
96   }
97
98   /**
99    * Returns the SNMP querier.
100    * @param none.
101    * @return SNMPQuerier querier instance.
102    */

103   public SNMPQuerier getSNMPQuerier() {
104     return snmp_querier;
105   }
106
107   /**
108    * Returns the IP querier.
109    * @param none.
110    * @return IPQuerier querier instance.
111    */

112   public IPQuerier getIPQuerier() {
113     return ip_querier;
114   }
115
116   /**
117    * Returns the IP address.
118    * @param none.
119    * @return Inet4Address IP address.
120    */

121   // any thread
122
public Inet4Address getAddress() {
123     return address;
124   }
125
126   /**
127    * Checks that the parameter can be attached to this target.
128    * @param visual_element parameter to check.
129    * @return true if the parameter can be attached to this target.
130    */

131   public boolean canManageThisChild(final VisualElement visual_element) {
132     if (ActionPing.class.isInstance(visual_element)) return true;
133     if (ActionFlood.class.isInstance(visual_element)) return true;
134     if (ActionSNMP.class.isInstance(visual_element)) return true;
135     if (EventReachable.class.isInstance(visual_element)) return true;
136     if (TargetInterface.class.isInstance(visual_element)) return true;
137     if (ActionHTTP.class.isInstance(visual_element)) return true;
138     if (ActionNmap.class.isInstance(visual_element)) return true;
139     return false;
140   }
141
142   /**
143    * Initializes this target.
144    * @param gui current GUI instance.
145    * @return void.
146    */

147   // final because called by constructor (by means of setItem())
148
public final void initialize(final GUI gui) {
149     super.initialize(gui);
150     if (gui != null) setImageHost();
151   }
152
153   /**
154    * Attaches this target to a specific parent.
155    * @param gui current GUI instance.
156    * @param parent parent.
157    * @return true if this target has been succesfully attached.
158    */

159   public boolean addTarget(final GUI gui, final VisualElement parent) {
160     initialize(gui);
161
162     if (!canAddTarget(parent)) return false;
163     if (parent != null && !parent.canManageThisChild(this)) return false;
164
165     final boolean is_new = !getGUI().containsCanonicalInstance(this);
166     final TargetIPv4 target_ipv4 = (TargetIPv4) getGUI().getCanonicalInstance(this);
167
168     if (!getGUI().getVisualTransientAll().contains(target_ipv4)) target_ipv4.setParent(getGUI(), getGUI().getVisualTransientAll());
169
170     final String JavaDoc addr_str = GenericTools.inet4AddressToString(target_ipv4.getAddress());
171     final String JavaDoc net_str = GenericTools.getNetFromAddress(addr_str);
172
173     if (addr_str == null || net_str == null) {
174       if (is_new) getGUI().dropTargetInstance(target_ipv4);
175       log.error("addr_str or net_str is null - " + getItem());
176       return false;
177     }
178
179     Inet4Address net_addr = null;
180     Inet4Address net_netmask = null;
181
182     try {
183       if (net_str.endsWith("/4")) {
184         
185         net_addr = (Inet4Address)
186         InetAddress.getByName(GenericTools.unsignedByteToShort(address.getAddress()[0]) < 240 ?
187             "224.0.0.0" : "240.0.0.0");
188         net_netmask = (Inet4Address) InetAddress.getByName("240.0.0.0");
189       }
190
191       if (net_str.endsWith("/8")) {
192         net_addr = (Inet4Address) InetAddress.getByName(net_str.substring(0, net_str.length() - 2));
193         net_netmask = (Inet4Address) InetAddress.getByName("255.0.0.0");
194       }
195
196       if (net_str.endsWith("/16")) {
197         net_addr = (Inet4Address) InetAddress.getByName(net_str.substring(0, net_str.length() - 3));
198         net_netmask = (Inet4Address) InetAddress.getByName("255.255.0.0");
199       }
200
201       if (net_str.endsWith("/24")) {
202         net_addr = (Inet4Address) InetAddress.getByName(net_str.substring(0, net_str.length() - 3));
203         net_netmask = (Inet4Address) InetAddress.getByName("255.255.255.0");
204       }
205     } catch (final UnknownHostException ex) {
206       if (is_new) getGUI().dropTargetInstance(target_ipv4);
207       log.error("Exception", ex);
208       return false;
209     }
210
211     TargetIPv4Subnet subnet;
212     try {
213       subnet = new TargetIPv4Subnet(addr_str, net_addr, net_netmask);
214     } catch (final AlgorithmException ex) {
215       if (is_new) getGUI().dropTargetInstance(target_ipv4);
216       log.error("Exception", ex);
217       return false;
218     }
219
220     subnet = (TargetIPv4Subnet) getGUI().getCanonicalInstance(subnet);
221     if (!getGUI().getVisualTransientNetworks().contains(subnet)) subnet.setParent(getGUI(), getGUI().getVisualTransientNetworks());
222     if (!subnet.contains(target_ipv4)) target_ipv4.setParent(getGUI(), subnet);
223
224     if (parent != null) target_ipv4.setParent(getGUI(), parent);
225
226     return is_new;
227   }
228
229   /**
230    * Attaches this target to a specific parent defined by its address.
231    * @param gui current GUI instance.
232    * @param addr_str parent address.
233    * @return true if this target has been succesfully attached.
234    */

235   public static void addTargetIPv4(final GUI gui, final String JavaDoc addr_str) {
236     try {
237       synchronized (gui.sync_tree) {
238         final TargetIPv4 foo = new TargetIPv4(addr_str, (Inet4Address) InetAddress.getByName(addr_str), gui.getSNMPManager());
239         if (gui.containsCanonicalInstance(foo)) return;
240         gui.asyncExecIfNeeded(new Runnable JavaDoc() {
241           public void run() {
242             synchronized (gui.sync_tree) {
243               if (gui.containsCanonicalInstance(foo)) return;
244               if (foo.addTarget(gui, (VisualElement) null) == true) {
245                 gui.setStatus(gui.getConfig().getPattern("adding_target", foo.getAddress().toString().substring(1)));
246                 foo.checkSNMPAwareness();
247               }
248             }
249           }
250         });
251       }
252     } catch (final AlgorithmException ex) {
253       log.error("Exception", ex);
254     } catch (final UnknownHostException ex) {
255       log.error("Exception", ex);
256     }
257   }
258
259   /**
260    * Compares two targets.
261    * @param o target to compare to.
262    * @return true if the targets are equal.
263    */

264   // any thread
265
public boolean equals(final Object JavaDoc o) {
266     if (this == o) return true;
267     if ((o == null) || (o.getClass() != getClass())) return false;
268     final TargetIPv4 target = (TargetIPv4) o;
269     return getAddress().equals(target.getAddress());
270   }
271   
272   /**
273    * Returns the hashcode for this target.
274    * @param none.
275    * @return int hashcode.
276    */

277   // any thread
278
public int hashCode() {
279     return getAddress().hashCode();
280   }
281 }
282
Popular Tags