1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
10 * =============================================================================
12 * For the license of bayes/sort.h and bayes/sort.c, please see the header
15 * ------------------------------------------------------------------------
17 * For the license of kmeans, please see kmeans/LICENSE.kmeans
19 * ------------------------------------------------------------------------
21 * For the license of ssca2, please see ssca2/COPYRIGHT
23 * ------------------------------------------------------------------------
25 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26 * header of the files.
28 * ------------------------------------------------------------------------
30 * For the license of lib/rbtree.h and lib/rbtree.c, please see
31 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33 * ------------------------------------------------------------------------
35 * Unless otherwise noted, the following license applies to STAMP files:
37 * Copyright (c) 2007, Stanford University
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions are
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
47 * * Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in
49 * the documentation and/or other materials provided with the
52 * * Neither the name of Stanford University nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66 * THE POSSIBILITY OF SUCH DAMAGE.
68 * =============================================================================
70 #define PARAM_ATTACK 'a'
71 #define PARAM_LENGTH 'l'
73 #define PARAM_SEED 's'
74 #define PARAM_THREAD 't'
76 #define PARAM_DEFAULT_ATTACK 10
77 #define PARAM_DEFAULT_LENGTH 16
78 #define PARAM_DEFAULT_NUM (1 << 20)
79 #define PARAM_DEFAULT_SEED 1
80 #define PARAM_DEFAULT_THREAD 1
82 public class Intruder extends Thread {
94 public Intruder(String[] argv)
99 public Intruder(int myID,Arg a)
105 private void setDefaultParams() {
106 percentAttack = PARAM_DEFAULT_ATTACK;
107 maxDataLength = PARAM_DEFAULT_LENGTH;
108 numFlow = PARAM_DEFAULT_NUM;
109 randomSeed = PARAM_DEFAULT_SEED;
110 numThread = PARAM_DEFAULT_THREAD;
114 /* =============================================================================
116 * =============================================================================
118 private void displayUsage()
120 System.out.print ("Usage: Intruder [options]\n");
121 System.out.println("\nOptions: (defaults)\n");
122 System.out.print (" a <UINT> Percent [a]ttack ");
123 System.out.print (" l <UINT> Max data [l]ength ");
124 System.out.print (" n <UINT> [n]umber of flows ");
125 System.out.print (" s <UINT> Random [s]eed ");
126 System.out.print (" t <UINT> Number of [t]hreads ");
131 /* =============================================================================
133 * =============================================================================
135 private void parseArg(String[] argv)
139 boolean opterr = false;
143 while ( i< argv.length) {
145 if(argv[i].charAt(0) == '-') {
148 if(arg.equals("-a")) {
149 percentAttack = Integer.parseInt(argv[i++]);
151 else if(arg.equals("-l")) {
152 maxDataLength = Integer.parseInt(argv[i++]);
154 else if(arg.equals("-n")) {
155 numFlow = Integer.parseInt(argv[i++]);
157 else if(arg.equals("-s")) {
158 randomSeed = Integer.parseInt(argv[i++]);
160 else if(arg.equals("-t")) {
161 numThread = Integer.parseInt(argv[i++]);
164 System.out.println("Non-option argument: " + argv[i]);
177 /* =============================================================================
179 * =============================================================================
181 public void processPackets(Arg argPtr)
183 // TM_THREAD_ENTER();
185 Stream streamPtr = argPtr.streamPtr;
186 Decoder decoderPtr = argPtr.decoderPtr;
187 Vector_t[] errorVectors = argPtr.errorVectors;
190 Detector detectorPtr = new Detector();
191 detectorPtr.addPreprocessor(2);
193 Vector_t errorVectorPtr = errorVectors[threadID];
199 packetPtr = streamPtr.getPacket();
202 if(packetPtr == null) {
205 int flowId = packetPtr.flowId;
208 error = decoderPtr.process(packetPtr,(packetPtr.length));
213 * Currently, stream_generate() does not create these errors.
215 System.out.println("Here?"+error);
218 int[] decodedFlowId = new int[1];
221 data = decoderPtr.getComplete(decodedFlowId);
225 int err = detectorPtr.process(data);
228 boolean status = errorVectorPtr.vector_pushBack(new Integer(decodedFlowId[0]));
230 System.out.println("Assertion in Intruder.processPacket");
242 Barrier.enterBarrier();
243 processPackets(argument);
244 Barrier.enterBarrier();
247 /* =============================================================================
249 * =============================================================================
252 public static void main(String[] argv)
259 ERROR er = new ERROR();
262 Intruder in = new Intruder(argv); // parsing argv
264 Barrier.setBarrier(in.numThread);
266 System.out.println("Percent attack = " + in.percentAttack);
267 System.out.println("Max data length = " + in.maxDataLength);
268 System.out.println("Num flow = " + in.numFlow);
269 System.out.println("Random seed = " + in.randomSeed);
271 Dictionary dictionaryPtr = new Dictionary();
272 Stream streamPtr = new Stream(in.percentAttack);
273 int numAttack = streamPtr.generate(dictionaryPtr,in.numFlow,in.randomSeed,in.maxDataLength);
275 System.out.println("Num Attack = " + numAttack);
277 Decoder decoderPtr = new Decoder();
278 Vector_t[] errorVectors = new Vector_t[in.numThread];
282 for(i =0;i< in.numThread;i++) {
283 errorVectors[i] = new Vector_t(in.numFlow);
288 arg.streamPtr = streamPtr;
289 arg.decoderPtr = decoderPtr;
290 arg.errorVectors = errorVectors;
296 Intruder[] intruders = new Intruder[in.numThread];
298 for(i=1; i<in.numThread;i++) {
299 intruders[i] = new Intruder(i,arg);
304 for(i = 1; i< in.numThread;i++) {
305 intruders[i].start();
308 long start = System.currentTimeMillis();
310 Barrier.enterBarrier();
311 in.processPackets(in.argument);
312 Barrier.enterBarrier();
314 long finish = System.currentTimeMillis();
315 long elapsed = finish - start;
317 System.out.println("TIME=" + elapsed);
325 for(i =0;i<in.numThread;i++) {
326 Vector_t errorVectorPtr = errorVectors[i];
328 int numError = errorVectorPtr.vector_getSize();
329 //System.out.println("numError = " + numError);
330 numFound += numError;
331 for (e = 0; e< numError; e++) {
332 int flowId = ((Integer)errorVectorPtr.vector_at(e)).intValue();
333 boolean status = streamPtr.isAttack(flowId);
335 if(status == false) {
336 System.out.println("Assertion in check solution");
342 System.out.println("Num found = " + numFound);
344 if(numFound != numAttack) {
345 System.out.println("Assertion in check solution");
349 System.out.println("Finished");
355 /* =============================================================================
357 * End of intruder.java
359 * =============================================================================