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;
024:
025: import net.fenyo.gnetwatch.activities.*;
026: import net.fenyo.gnetwatch.targets.*;
027: import net.fenyo.gnetwatch.GUI.*;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.dom4j.Document;
032: import org.dom4j.Node;
033:
034: import java.util.*;
035:
036: /**
037: * This class is dedicated to maintain main operations.
038: * @author Alexandre Fenyo
039: * @version $Id: Main.java,v 1.16 2007/03/03 00:38:19 fenyo Exp $
040: */
041:
042: public class Main {
043: private static Log log = LogFactory.getLog(Main.class);
044: private final Config config;
045: private GUI gui = null;
046: private final CaptureManager capture_mgr;
047: private CaptureManager.HandlePacket handler = null;
048:
049: /**
050: * Constructor.
051: * main thread
052: * @param config configuration.
053: */
054: public Main(final Config config, final CaptureManager capture_mgr) {
055: this .config = config;
056: this .capture_mgr = capture_mgr;
057: }
058:
059: /**
060: * Defines the GUI instance.
061: * @param GUI gui.
062: * @return void.
063: */
064: public void setGUI(final GUI gui) {
065: this .gui = gui;
066: }
067:
068: /**
069: * Ask to spawn tethereal instances and add targets for new discovered IP targets.
070: * @param none.
071: * @return void.
072: */
073: // GUI thread
074: public void startDiscover() {
075: handler = new CaptureManager.HandlePacket() {
076: public void document(final Document packet) {
077: final List<Node> nodes = packet
078: .selectNodes("/packet/proto[@name='ip']/field[@name='ip.addr']");
079: if (nodes.size() == 2) {
080: final String srcaddr = nodes.get(0)
081: .valueOf("@show");
082: final String dstaddr = nodes.get(1)
083: .valueOf("@show");
084: // shorter but slower way :
085: // final String srcaddr = (String) packet.selectObject("string(//field[@name='ip.addr'][1]/@show)");
086:
087: // log.debug("ipv4 packet captured: " + srcaddr + " -> " + dstaddr);
088: TargetIPv4.addTargetIPv4(gui, srcaddr);
089: TargetIPv4.addTargetIPv4(gui, dstaddr);
090: }
091:
092: final List<Node> nodes_ipv6 = packet
093: .selectNodes("/packet/proto[@name='ipv6']/field[@name='ipv6.addr']");
094: if (nodes_ipv6.size() == 2) {
095: final String srcaddr = nodes_ipv6.get(0).valueOf(
096: "@show");
097: final String dstaddr = nodes_ipv6.get(1).valueOf(
098: "@show");
099:
100: // log.debug("ipv6 packet captured: " + srcaddr + " -> " + dstaddr);
101: TargetIPv6.addTargetIPv6(gui, srcaddr);
102: TargetIPv6.addTargetIPv6(gui, dstaddr);
103: }
104: }
105: };
106: try {
107: capture_mgr.registerListener("ip", handler);
108: } catch (final InterruptedException ex) {
109: log.error("Exception", ex);
110: }
111: }
112:
113: /**
114: * Ask to kill tethereal instances if this is the last listener.
115: * @param none.
116: * @return void.
117: */
118: // GUI thread
119: public void stopDiscover() {
120: try {
121: capture_mgr.unRegisterListener("ip", handler);
122: } catch (final InterruptedException ex) {
123: log.error("Exception", ex);
124: }
125: }
126: }
|