1 2 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 45 46 public class TargetIPv4 extends Target { 47 private static Log log = LogFactory.getLog(TargetIPv4.class); 48 49 private Inet4Address address; private SNMPQuerier snmp_querier; 51 private IPQuerier ip_querier; 52 53 60 public TargetIPv4(final String 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 } 71 72 77 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 103 public SNMPQuerier getSNMPQuerier() { 104 return snmp_querier; 105 } 106 107 112 public IPQuerier getIPQuerier() { 113 return ip_querier; 114 } 115 116 121 public Inet4Address getAddress() { 123 return address; 124 } 125 126 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 147 public final void initialize(final GUI gui) { 149 super.initialize(gui); 150 if (gui != null) setImageHost(); 151 } 152 153 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 addr_str = GenericTools.inet4AddressToString(target_ipv4.getAddress()); 171 final String 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 235 public static void addTargetIPv4(final GUI gui, final String 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 () { 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 264 public boolean equals(final Object 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 277 public int hashCode() { 279 return getAddress().hashCode(); 280 } 281 } 282 | Popular Tags |