From: Marcus <the...@us...> - 2004-03-30 05:43:57
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12147/junk/WEB-INF/classes/junk/plugin/scanner Added Files: ScanThread.java Log Message: initial release --- NEW FILE: ScanThread.java --- /* * juNK - a file search system for smb shares * * Copyright 2004 by * Marcus Proest (theevilflow at users dot sf dot net) * Uwe van Heesch (tyron_e at users dot sf dot net) * * This file is part of junk (java useful net kollektor). * * junk is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * junk is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with junk; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package junk.plugin.scanner; import jcifs.netbios.NbtAddress; import junk.util.NameOnline; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * thread to scann ip's */ class ScanThread implements Runnable { /** * ip iterator */ private Iterator i; /** log writer */ private Log log = LogFactory.getLog(ScanThread.class); /** * parent plugin */ private ScannerPlugin parent; /** * thread */ private Thread me = null; /** * id of the thread */ private int t; /** * constructor * @param sp calling plugin * @param i ip iterator * @param t thread id */ protected ScanThread(ScannerPlugin sp, Iterator i, int t) { this.parent = sp; this.i = i; this.t = t; //start the real thread if (this.me == null) { this.me = new Thread(this); this.me.start(); } } /** * run method of the thread */ public void run() { log.info("Starting Thread " + t); long start = System.currentTimeMillis(); //to get a scanningtime int c = 0; while (i.hasNext()) { c++; String ip = (String) i.next(); //get next IP if (this.isSmb(ip)) { //the host is handled as 'online' this.parent.getHosts().put(ip, new NameOnline(this.getNetBiosName(ip), Boolean.TRUE)); } else { //the host is handled as 'offline' //et the old values to preserve the name NameOnline no = (NameOnline) this.parent.getHosts().get(ip); no.setOnline(Boolean.FALSE); //set it offline this.parent.getHosts().put(ip, no); //save it } } log.info("Ended Thread " + t + " I scanned " + c + " ip's"); long time = System.currentTimeMillis() - start; this.parent.reportDone(time); } /** 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, parent.SMB_PORT), this.parent.getConf().getTcpTimeout()); //if it worked, close it again sock.close(); ret = true; } catch (Exception e) { //any exception will result in false ret = false; } return ret; } } |