KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > actions > ActionNmap


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.actions;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.GUI.GUI;
28 import net.fenyo.gnetwatch.actions.Action.InterruptCause;
29 import net.fenyo.gnetwatch.activities.Background;
30 import net.fenyo.gnetwatch.data.*;
31 import net.fenyo.gnetwatch.targets.*;
32
33 import java.io.*;
34 import java.util.regex.Matcher JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.widgets.MessageBox;
41
42 /**
43  * Instances of this action class use NMAP to explore their target
44  * and create events of type EventReachable.
45  * @author Alexandre Fenyo
46  * @version $Id: ActionNmap.java,v 1.2 2007/03/12 05:04:14 fenyo Exp $
47  */

48
49 // on devrait utiliser l'option -oX file pour traiter l'output en XML...
50

51 public class ActionNmap extends Action {
52   private static Log log = LogFactory.getLog(ActionNmap.class);
53
54   // main, Background & Queue threads
55
// supports any thread
56
private ExternalCommand cmd_nmap = null;
57
58   private String JavaDoc address = "";
59   private boolean invoked = false;
60
61   /**
62    * Constructor.
63    * @param target target this action works on.
64    * @param background queue manager by which this action will add events.
65    */

66   // GUI thread
67
// supports any thread
68
public ActionNmap(final Target target, final Background background) {
69     super(target, background);
70     setItem("nmap");
71   }
72
73   /**
74    * Constructor.
75    * @param none.
76    */

77   // GUI thread
78
// supports any thread
79
public ActionNmap() {
80     setItem("nmap");
81   }
82
83   /**
84    * Called to inform about the current GUI.
85    * @param gui current GUI instance.
86    * @return void.
87    */

88   protected void initialize(final GUI gui) {
89     super.initialize(gui);
90   }
91
92   /**
93    * Returns the associated target.
94    * @param none.
95    * @return Target associated target.
96    */

97   // any thread
98
public String JavaDoc getQueueName() {
99     return "nmap";
100   }
101
102   /**
103    * Returns the timeout associated with this action.
104    * @param none.
105    * @return long timeout.
106    */

107   // any thread
108
public long getMaxDelay() {
109     return new Long JavaDoc(getGUI().getConfig().getProperty("nmap.timeout"));
110   }
111
112   /**
113    * Asks this action to stop rapidely.
114    * @param cause cause.
115    * @return void.
116    * @throws IOException IO exception.
117    */

118   // main & Background threads
119
// supports any thread
120
public void interrupt(final InterruptCause cause) throws IOException {
121     if (cmd_nmap != null) {
122       cmd_nmap.end();
123       if (cause == InterruptCause.timeout)
124         getGUI().appendConsole(getGUI().getConfig().getPattern("nmap_interrupted", address) + "<BR/>");
125     }
126   }
127
128   /**
129    * Computes the signature of the target.
130    * @param none.
131    * @return void.
132    * @throws IOException IO exception.
133    * @throws InterruptedException exception.
134    */

135   // Queue thread
136
// supports any thread
137
public void invoke() throws IOException, InterruptedException JavaDoc {
138     if (isDisposed() == true) return;
139
140     super.invoke();
141
142     if (invoked == true) return;
143     invoked = true;
144
145     try {
146       final String JavaDoc [] cmdLine;
147
148       if (TargetIPv4.class.isInstance(getTarget())) {
149         final TargetIPv4 target = (TargetIPv4) getTarget();
150         address = target.getAddress().toString();
151         address = address.substring(1 + address.indexOf('/'));
152         cmdLine = new String JavaDoc [] { "nmap", "-A", address };
153       } else if (TargetIPv6.class.isInstance(getTarget())) {
154         final TargetIPv6 target = (TargetIPv6) getTarget();
155         address = target.getAddress().toString();
156         address = address.substring(1 + address.indexOf('/'));
157         cmdLine = new String JavaDoc [] { "nmap", "-6", "-A", address };
158       } else return;
159
160       String JavaDoc cmd_line = "";
161       for (final String JavaDoc part : cmdLine) cmd_line += part + " ";
162       getGUI().setStatus(getGUI().getConfig().getPattern("forking", cmd_line));
163
164       cmd_nmap = new ExternalCommand(cmdLine, true);
165       try {
166         cmd_nmap.fork();
167       } catch (final IOException ex) {
168         getGUI().asyncExecIfNeeded(new Runnable JavaDoc() {
169           public void run() {
170             final MessageBox dialog = new MessageBox(getGUI().getShell(), SWT.ICON_ERROR | SWT.OK);
171             dialog.setText(getGUI().getConfig().getString("nmap_error"));
172             dialog.setMessage(getGUI().getConfig().getString("nmap_error_long"));
173             dialog.open();
174           }
175         });
176
177         throw ex;
178       }
179
180       final String JavaDoc cmd_output = cmd_nmap.readStdout();
181       cmd_nmap.end();
182
183       final Matcher JavaDoc match = Pattern.compile("(\r|\n)Running: (.*)").matcher(cmd_output);
184       if (match.find() == true) if (match.group(2) != null)
185         getParents().get(0).setType(match.group(2));
186       getTarget().addEvent(new EventNmap(cmd_output));
187
188     } finally {
189       getGUI().asyncExecIfNeeded(new Runnable JavaDoc() {
190         public void run() {
191           synchronized (getGUI().sync_tree) {
192             removeVisualElements(getParents().get(0));
193           }
194         }
195       });
196     }
197   }
198
199   /**
200    * Called when this element is being removed.
201    * @param none.
202    * @return void.
203    */

204   protected void disposed() {
205     // remove us from the action queue
206
super.disposed();
207
208     // interrupt if currently running
209
try {
210       interrupt(InterruptCause.removed);
211     } catch (final IOException ex) {
212       log.error("Exception", ex);
213     }
214   }
215 }
216
Popular Tags