1 2 23 24 package net.fenyo.gnetwatch.actions; 25 26 import net.fenyo.gnetwatch.*; 27 import net.fenyo.gnetwatch.activities.Background; 28 import net.fenyo.gnetwatch.data.*; 29 import net.fenyo.gnetwatch.targets.*; 30 31 import java.io.*; 32 import java.net.*; 33 import java.util.Arrays ; 34 import java.util.Date ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 45 46 public class ActionFlood extends Action { 47 private static Log log = LogFactory.getLog(ActionPing.class); 48 49 private boolean interrupted = false; 50 51 56 public ActionFlood(final Target target, final Background background) { 59 super(target, background); 60 setItem("flood"); 61 } 62 63 67 public ActionFlood() { 70 setItem("flood"); 71 } 72 73 78 public String getQueueName() { 80 return "flood"; 81 } 82 83 88 public long getMaxDelay() { 91 return 30000000; 92 } 93 94 100 public void interrupt(final InterruptCause cause) { 103 interrupted = true; 104 } 105 106 113 public void invoke() throws IOException, InterruptedException { 116 if (isDisposed() == true) return; 117 118 try { 119 super.invoke(); 120 121 final IPQuerier querier; 123 if (TargetIPv4.class.isInstance(getTarget())) { 124 querier = ((TargetIPv4) getTarget()).getIPQuerier(); 125 } else if (TargetIPv6.class.isInstance(getTarget())) { 126 querier = ((TargetIPv6) getTarget()).getIPQuerier(); 127 } else return; 128 129 DatagramSocket socket = new DatagramSocket(querier.getPortSrc()); 130 socket.setTrafficClass(querier.getTOS() << 2); 131 socket.setBroadcast(true); 132 133 final byte [] buf = new byte [querier.getPDUMaxSize()]; 134 Arrays.fill(buf, (byte) 0); 135 final DatagramPacket packet = new DatagramPacket(buf, buf.length, 136 new InetSocketAddress(querier.getAddress(), querier.getPortDst())); 137 138 long last_time = System.currentTimeMillis(); 139 int bytes_sent = 0; 140 while (true) { 141 socket.send(packet); 142 bytes_sent += buf.length; 143 144 if (System.currentTimeMillis() - last_time > 1000) { 145 getTarget().addEvent(new EventFlood(bytes_sent)); 146 setDescription("" + new Double (((double) 8 * 1000 * bytes_sent) / (System.currentTimeMillis() - last_time)).intValue() + " bit/s"); 147 148 getGUI().setStatus(getGUI().getConfig().getPattern("bytes_flooded", bytes_sent, querier.getAddress().toString().substring(1))); 150 151 last_time = System.currentTimeMillis(); 152 bytes_sent = 0; 153 } 154 155 if (interrupted == true) { 156 socket.close(); 157 return; 158 } 159 } 160 } catch (final InterruptedException ex) {} 161 } 162 163 168 protected void disposed() { 169 super.disposed(); 171 172 interrupt(InterruptCause.removed); 174 } 175 } 176 | Popular Tags |