1 2 23 24 package net.fenyo.gnetwatch.activities; 25 26 import net.fenyo.gnetwatch.*; 27 import net.fenyo.gnetwatch.actions.ExternalCommand; 28 import net.fenyo.gnetwatch.data.EventReachable; 29 30 import java.util.*; 31 import java.util.regex.Matcher ; 32 import java.util.regex.Pattern ; 33 import java.io.*; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import org.dom4j.*; 39 import org.dom4j.io.*; 40 41 130 131 137 138 public class Capture implements Runnable { 139 private static Log log = LogFactory.getLog(Capture.class); 140 141 final SAXReader reader = new SAXReader(); 142 143 private final Config config; 144 private ExternalCommand cmd; 145 private CaptureManager manager; 146 147 private boolean forked = false; 148 149 private Thread capture_thread = null; 150 151 private boolean must_end = false; 152 153 161 public Capture(final Config config, final CaptureManager manager, final int device, final String filter) { 164 this.config = config; 165 this.manager = manager; 166 cmd = new ExternalCommand(new String [] { "tethereal", "-i", "" + device, "-T", "pdml", "-R", "\"" + filter + "\"" }, true); 169 } 170 171 177 public static String [] listDevices() throws InterruptedException { 179 final String devices = 180 new ExternalCommand(new String [] { "tethereal", "-D" }, true).runStdout(); 181 return devices == null ? null : devices.split("\n"); 182 } 183 184 189 public void createCaptureThread() { 191 capture_thread = new Thread (this, "Capture Thread"); 192 capture_thread.start(); 193 } 194 195 201 public void end() throws InterruptedException { 204 must_end = true; 205 if (capture_thread != null) { 206 capture_thread.interrupt(); 207 } 208 209 while (!forked) Thread.sleep(100); 210 211 try { 212 cmd.end(); 213 } catch (final IOException ex) { 214 log.error("Exception", ex); 215 } 216 217 capture_thread.join(); 218 } 219 220 226 private void handlePacket(final StringBuffer packet) throws DocumentException { 229 try { 230 final Document document = reader.read(new StringReader(packet.toString())); 231 manager.handlePacket(document); 232 } catch (final DocumentException ex) { 233 log.warn("packet: {" + packet + "}"); 234 throw ex; 235 } 236 } 237 238 243 public void run() { 245 final StringBuffer packet = new StringBuffer (); 246 247 try { 248 cmd.fork(); 249 forked = true; 250 251 while (!config.isEnd() && !must_end) { 252 final String str = cmd.readLineStdout(); 254 if (str == null) break; 255 packet.append(str); 256 for (int idx = 0; idx < packet.length(); idx++) 259 if (packet.charAt(idx) != 9 && packet.charAt(idx) != 10 && 261 packet.charAt(idx) != 13 && packet.charAt(idx) < 32) 262 packet.setCharAt(idx, ' '); 263 264 if (str.contains("</packet>")) { 265 try { 266 handlePacket(packet); 267 } catch (final DocumentException ex) { 268 log.warn("Exception", ex); 269 } 270 packet.setLength(0); 271 } 272 } 273 } catch (final IOException ex) { 274 log.warn("Exception", ex); 275 } catch (final InterruptedException ex) { 276 } finally { 278 forked = true; 279 } 280 } 281 } 282 | Popular Tags |