From: Marcus <the...@us...> - 2004-03-30 05:44:23
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12196/junk/WEB-INF/classes/junk/plugin/scanner Modified Files: ScannerPlugin.java Log Message: multithreading Index: ScannerPlugin.java =================================================================== RCS file: /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner/ScannerPlugin.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** ScannerPlugin.java 27 Mar 2004 20:14:18 -0000 1.10 --- ScannerPlugin.java 30 Mar 2004 05:32:49 -0000 1.11 *************** *** 26,31 **** package junk.plugin.scanner; - import jcifs.netbios.NbtAddress; - import junk.util.HostMap; import junk.util.IpSubnet; --- 26,29 ---- *************** *** 39,46 **** import java.io.ObjectOutputStream; - import java.net.InetSocketAddress; - import java.net.Socket; - import java.net.UnknownHostException; - import java.util.ArrayList; import java.util.HashMap; --- 37,40 ---- *************** *** 63,66 **** --- 57,61 ---- * * @author Marcus Proest (theevilflow at users dot sf dot net) + *@todo there are some issues regarding multiple ip subnets... */ public class ScannerPlugin implements PlugIn, Runnable { *************** *** 75,79 **** /** the key under which the scann config is stored in the servlet context */ ! public static final String KEY_SCANNER_CONFIG = "scanner-config"; /** the key under which the scanning status --- 70,76 ---- /** the key under which the scann config is stored in the servlet context */ ! ! //i dont know why to save this.... ! //public static final String KEY_SCANNER_CONFIG = "scanner-config"; /** the key under which the scanning status *************** *** 86,90 **** /** smb scanport */ ! private static final int SMB_PORT = 139; /** the actionservlet of the struts application */ --- 83,87 ---- /** smb scanport */ ! protected static final int SMB_PORT = 139; /** the actionservlet of the struts application */ *************** *** 112,115 **** --- 109,116 ---- */ private boolean isScanning = false; + /** + * yet finished threads + */ + private int doneThreads = 0; /** destroys the plugin. */ *************** *** 151,154 **** --- 152,156 ---- } + //forgot why to do this // this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_SCANNER_CONFIG, // this.conf); *************** *** 162,167 **** this.scanThread.start(); } else { ! log.fatal( ! "Scanner thread could not be started. It seems it is already running."); return; --- 164,169 ---- this.scanThread.start(); } else { ! log.fatal("Scanner thread could not be started. " ! + "It seems it is already running."); return; *************** *** 197,253 **** /** ! * not sure if i need this anyway... ! * @param conf fnoc ! */ ! protected void setConf(ScannerConfig conf) { ! this.conf = conf; } ! /** retrieve net bios name via jcifs. ! * @return the net bios name of the host, or the ip of the host if no ! * net bios name could be determined. ! * @param ip the ip of the host wich name should be determined ! */ ! private String getNetBiosName(String ip) { ! String name = null; ! ! try { ! name = NbtAddress.getByName(ip).getHostName(); ! } catch (UnknownHostException e) { ! name = ip; //to make it easy for the caller ! } ! ! return name; } ! /** determine if smb is running on host <code>ip</code><br/>. ! * ! * This function serves some speed considerations. ! * Querying via jcifs will take a long time, so we ! * just make a quick check here, ! * and afterwards query only the online hosts. ! * ! * @param ip the ip address of the host to check ! * @return <code>true</code> if the smb port is reachable. ! * <CODE>false</CODE> if any error occurs ! */ ! private boolean isSmb(String ip) { ! Socket sock = new Socket(); ! boolean ret; ! ! try { ! //try to connect to the smb port ! sock.connect(new InetSocketAddress(ip, SMB_PORT), ! this.conf.getTcpTimeout()); ! //if it worked, close it again ! sock.close(); ! ret = true; ! } catch (Exception e) { ! //any exception will result in false ! ret = false; } ! return ret; } --- 199,232 ---- /** ! * returns the scanner configuration ! * @return the scanner configuration ! */ ! protected ScannerConfig getConf() { ! return this.conf; } ! /** ! * returns the hostmap ! * @return the hostmap ! */ ! protected HostMap getHosts() { ! return this.hosts; } ! /** ! * the scanner threads call this to maintain the isScanning property ! * @param time the time the thread took to finish ! */ ! protected synchronized void reportDone(long time) { ! this.doneThreads++; ! if (this.doneThreads >= this.conf.getNumOfThreads()) { ! this.isScanning = false; ! this.doneThreads = 0; ! log.info("Last Thread done! Time: " + time); } ! this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, ! new Boolean(this.isScanning)); } *************** *** 318,325 **** private void saveMap(HostMap h) { String path = this.conf.getSaveMapPath(); try { ! FileOutputStream f = new FileOutputStream(path); ! ObjectOutputStream o = new ObjectOutputStream(f); o.writeObject(h); o.close(); --- 297,306 ---- private void saveMap(HostMap h) { String path = this.conf.getSaveMapPath(); + FileOutputStream f = null; + ObjectOutputStream o = null; try { ! f = new FileOutputStream(path); ! o = new ObjectOutputStream(f); o.writeObject(h); o.close(); *************** *** 328,331 **** --- 309,325 ---- } catch (Exception e) { log.warn("Could not save hostmap (" + path + ")", e); + } finally { + try { //cleanup. don't care if this fails. + + if (o != null) { + o.close(); + } + + if (f != null) { + f.close(); + } + } catch (IOException e11) { + ; + } } } *************** *** 352,386 **** //iterate over the subnet while (iterator.hasNext()) { //get an IpSubnet clone IpSubnet i = (IpSubnet) ((IpSubnet) iterator.next()).clone(); ! long start = System.currentTimeMillis(); //to get a scanningtime ! ! while (i.hasNext()) { ! String ip = (String) i.next(); //get next IP ! ! if (this.isSmb(ip)) { //the host is handled as 'online' ! this.hosts.put(ip, ! new NameOnline(this.getNetBiosName(ip), Boolean.TRUE)); ! } else { //the host is handled as 'offline' ! ! //get the old values to preserve the name ! NameOnline no = (NameOnline) hosts.get(ip); ! ! no.setOnline(Boolean.FALSE); //set it offline ! log.debug(no.toString()); ! this.hosts.put(ip, no); //save it ! ! //maybe the thread wants to terminate while we are in here ! if ((this.scanThread == null)) { ! return false; ! } ! } } - - //calculate scanning time - long time = System.currentTimeMillis() - start; - log.debug(i.toString() + " scan completed in " + time + " ms"); } --- 346,357 ---- //iterate over the subnet + //this iterates over the configured subnets, not over ip's! while (iterator.hasNext()) { //get an IpSubnet clone IpSubnet i = (IpSubnet) ((IpSubnet) iterator.next()).clone(); ! for (int x = 0; x < this.conf.getNumOfThreads(); x++) { ! new ScanThread(this, i, x); } } *************** *** 388,563 **** this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, hosts); ! this.isScanning = false; ! this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, ! new Boolean(this.isScanning)); ! ! return true; ! } ! ! /** handles the config of the scanner plugin. ! * @author Marcus Proest (theevilflow at users dot sf dot net) ! */ ! class ScannerConfig { ! /** plugin configuration key defining the sleep time of the scanner thread */ ! protected static final String KEY_THREAD_SLEEP = "thread-sleep"; ! ! /** plugin configuration key defining the scanning interval */ ! protected static final String KEY_SCAN_INTERVAL = "scan-interval"; ! ! /** plugin configuration key defining the ip range */ ! protected static final String KEY_IP_RANGE = "ip-range"; ! ! /** plugin configuration key defining the tcp timeout */ ! protected static final String KEY_TCP_TO = "tcp-timeout"; ! ! /** plugin configuration key defining the save map path */ ! protected static final String KEY_SAVE_MAP = "map-save-file"; ! ! /** the ip subnets to scan over */ ! private ArrayList ipSubnets = null; ! ! /** log writer */ ! private Log log = LogFactory.getLog(ScannerPlugin.class); ! ! /** initial ipsubnetsString */ ! private String ipSubnetsString = null; ! ! /** the path to load and save the hostmap to */ ! private String savemappath = "data/hostmap"; ! ! /** the tcp timeout */ ! private int tcpTimeout = 100; ! ! /** the scanning interval */ ! private long interval = 300000; ! ! /** the time the scannerthread sleeps */ ! private long sleep = 10000; ! ! /** Creates a new instance of ScannerConfig. ! * protected standard constructor to prevent instantiation outside ! * the package. ! */ ! protected ScannerConfig() { ! } ! ! /** Creates a new instance of ScannerConfig. ! * @param m ModuleConfig ! * @param a ActionServlet ! * @throws IllegalArgumentException ! * thrown if reading the configuration failed ! */ ! protected ScannerConfig(ActionServlet a, ModuleConfig m) ! throws IllegalArgumentException ! { ! HashMap conf = junk.plugin.PluginTools.getPluginConfig(ScannerPlugin.class, ! m); ! ! String path = a.getServletContext().getRealPath("/WEB-INF"); ! ! this.applyConfig(conf, path); ! } ! ! /** Returns the interval value. ! * @return returns the interval value ! */ ! public long getInterval() { ! return this.interval; ! } ! ! /** Returns the path to the saved hostmap. ! * @return the path to the saved hostmap ! */ ! public String getSaveMapPath() { ! return this.savemappath; ! } ! ! /** Returns the sleep value. ! * @return returns the sleep value ! */ ! public long getSleep() { ! return this.sleep; ! } ! ! /** return the initial ipSubnets String ! * @return initial ipsubnets String ! */ ! public String getSubnets() { ! return this.ipSubnetsString; ! } ! ! /** Returns the tcp timeout value. ! * @return tcp timeout ! */ ! public int getTcpTimeout() { ! return this.tcpTimeout; ! } ! ! /** Returns an iterator over the configured ip subnets. ! * @return an iterator over the configured ip subnets ! */ ! protected Iterator getIpSubnetIterator() { ! return this.ipSubnets.iterator(); ! } ! /** Apply the config values. ! * @param conf configuration values ! * @throws IllegalArgumentException ! * thrown if reading the configuration failed ! * @param basepath global path to the web-inf directory */ ! private void applyConfig(HashMap conf, String basepath) ! throws IllegalArgumentException ! { ! try { ! this.sleep = Long.parseLong((String) conf.get( ! ScannerConfig.KEY_THREAD_SLEEP)); ! } catch (Exception e) { ! log.warn("Loading parameter " + ScannerConfig.KEY_THREAD_SLEEP ! + " FAILED, defaulting to " + this.sleep, e); ! } ! ! try { ! this.interval = Long.parseLong((String) conf.get( ! ScannerConfig.KEY_SCAN_INTERVAL)); ! } catch (Exception e) { ! log.warn("Loading parameter " + ScannerConfig.KEY_SCAN_INTERVAL ! + "FAILED, defaulting to " + this.interval, e); ! } ! ! try { ! String subnetConfigs = (String) conf.get(ScannerConfig.KEY_IP_RANGE); ! this.ipSubnetsString = subnetConfigs; ! ! String[] scArray = subnetConfigs.split(","); ! this.ipSubnets = new ArrayList(scArray.length); ! ! for (int x = 0; x < scArray.length; x++) { ! this.ipSubnets.add(x, new IpSubnet(scArray[x])); ! } ! } catch (Exception e) { ! //this value cannot be defaulted, so failing ist fatal. ! throw new IllegalArgumentException("Loading parameter " ! + ScannerConfig.KEY_IP_RANGE + " FAILED. (" ! + e.getMessage() + ")"); ! } ! ! try { ! this.tcpTimeout = Integer.parseInt((String) conf.get( ! ScannerConfig.KEY_TCP_TO)); ! } catch (Exception e) { ! log.warn("Loading parameter " + ScannerConfig.KEY_TCP_TO ! + " FAILED, defaulting to " + this.tcpTimeout, e); ! } ! ! try { ! String filename = (String) conf.get(ScannerConfig.KEY_SAVE_MAP); ! this.savemappath = basepath + File.separatorChar + ".." ! + File.separatorChar + filename; ! } catch (Exception e) { ! log.warn("Loading parameter " + ScannerConfig.KEY_SAVE_MAP ! + " FAILED, defaulting to " + this.savemappath, e); ! } ! } } } --- 359,369 ---- this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, hosts); ! //this is now maintained by the threads ! /* this.isScanning = false; ! this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, ! new Boolean(this.isScanning)); */ ! return true; } } |