From c80ae641906ecd2e576abc758578f139623cadac Mon Sep 17 00:00:00 2001 From: adash Date: Mon, 29 Jun 2009 05:41:55 +0000 Subject: [PATCH] Add new benchmark. Main still incomplete and missing few other files --- .../src/Benchmarks/SingleTM/Intruder/Arg.java | 2 + .../SingleTM/Intruder/Detector.java | 43 +++ .../SingleTM/Intruder/Dictionary.java | 98 +++++ .../SingleTM/Intruder/Intruder.java | 359 ++++++++++++++++++ .../Benchmarks/SingleTM/Intruder/Node.java | 90 +++++ .../Benchmarks/SingleTM/Intruder/Packet.java | 23 ++ .../SingleTM/Intruder/Preprocessor.java | 15 + .../Benchmarks/SingleTM/Intruder/RBTree.java | 88 +++++ .../Benchmarks/SingleTM/Intruder/Stream.java | 171 +++++++++ 9 files changed, 889 insertions(+) create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Arg.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Detector.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Dictionary.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Intruder.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Node.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Packet.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Preprocessor.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/RBTree.java create mode 100644 Robust/src/Benchmarks/SingleTM/Intruder/Stream.java diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Arg.java b/Robust/src/Benchmarks/SingleTM/Intruder/Arg.java new file mode 100644 index 00000000..8312c440 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Arg.java @@ -0,0 +1,2 @@ +public class Args { +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Detector.java b/Robust/src/Benchmarks/SingleTM/Intruder/Detector.java new file mode 100644 index 00000000..defc7c1c --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Detector.java @@ -0,0 +1,43 @@ +public class Detector { + Dictionary dictionaryPtr; + Vector_t preprocessorVectorPtr; + + public Detector() { + dictionaryPtr = new Dictionary(); + if (dictionaryPtr == null) { + System.out.printString("Error: Cannot allocate Dictionary"); + System.exit(0); + } + preprocessorVectorPtr = new Vector_t(1); + if (preprocessorVectorPtr == null) { + System.out.printString("Error: Cannot allocate preprocessorVectorPtr"); + System.exit(0); + } + } + + public void detector_addPreprocessor(String p) + { + boolean status = preprocessorVectorPtr.vector_pushBack(p); + if (status == false) { + System.out.print("Error: Cannot insert in vector\n"); + System.exit(0); + } + } + + public boolean detector_process(String str) + { + Vector_t v = this.preprocessorVectorPtr; + int p; + int numPreprocessor = v.vector_getSize(); + for (p = 0; p < numPreprocessor; p++) { + String preprocessor = (String) v.vector_at(p); + String newstr = p.toLowerCase(); + } + + String signature = dictionaryPtr.dictionary_match(str); + if (signature == null) { + return ERROR_SIGNATURE; + } + return ERROR_NONE; + } +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Dictionary.java b/Robust/src/Benchmarks/SingleTM/Intruder/Dictionary.java new file mode 100644 index 00000000..cfdb353a --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Dictionary.java @@ -0,0 +1,98 @@ +public class Dictionary { + String global_defaultSignatures[]; + long global_numDefaultSignature = 72; + + public Dictionary() { + global_defaultSignatures = new String[72]; + + global_defaultSignatures[0] = "about"; + global_defaultSignatures[1] = "after"; + global_defaultSignatures[2] = "all"; + global_defaultSignatures[3] = "also"; + global_defaultSignatures[4] = "and"; + global_defaultSignatures[5] = "any"; + global_defaultSignatures[6] = "back"; + global_defaultSignatures[7] = "because"; + global_defaultSignatures[8] = "but"; + global_defaultSignatures[9] = "can"; + global_defaultSignatures[10] = "come"; + global_defaultSignatures[11] = "could"; + global_defaultSignatures[12] = "day"; + global_defaultSignatures[13] = "even"; + global_defaultSignatures[14] = "first"; + global_defaultSignatures[15] = "for"; + global_defaultSignatures[16] = "from"; + global_defaultSignatures[17] = "get"; + global_defaultSignatures[18] = "give"; + global_defaultSignatures[20] = "good"; + global_defaultSignatures[21] = "have"; + global_defaultSignatures[22] = "him"; + global_defaultSignatures[23] = "how"; + global_defaultSignatures[24] = "into"; + global_defaultSignatures[25] = "its"; + global_defaultSignatures[26] = "just"; + global_defaultSignatures[27] = "know"; + global_defaultSignatures[28] = "like"; + global_defaultSignatures[29] = "look"; + global_defaultSignatures[30] = "make"; + global_defaultSignatures[31] = "most"; + global_defaultSignatures[32] = "new"; + global_defaultSignatures[33] = "not"; + global_defaultSignatures[34] = "now"; + global_defaultSignatures[35] = "one"; + global_defaultSignatures[36] = "only"; + global_defaultSignatures[37] = "other"; + global_defaultSignatures[38] = "out"; + global_defaultSignatures[39] = "over"; + global_defaultSignatures[40] = "people"; + global_defaultSignatures[41] = "say"; + global_defaultSignatures[42] = "see"; + global_defaultSignatures[43] = "she"; + global_defaultSignatures[44] = "some"; + global_defaultSignatures[45] = "take"; + global_defaultSignatures[46] = "than"; + global_defaultSignatures[47] = "that"; + global_defaultSignatures[48] = "their"; + global_defaultSignatures[49] = "them"; + global_defaultSignatures[50] = "then"; + global_defaultSignatures[51] = "there"; + global_defaultSignatures[52] = "these"; + global_defaultSignatures[53] = "they"; + global_defaultSignatures[54] = "think"; + global_defaultSignatures[55] = "this"; + global_defaultSignatures[56] = "time"; + global_defaultSignatures[57] = "two"; + global_defaultSignatures[58] = "use"; + global_defaultSignatures[59] = "want"; + global_defaultSignatures[60] = "way"; + global_defaultSignatures[61] = "well"; + global_defaultSignatures[62] = "what"; + global_defaultSignatures[63] = "when"; + global_defaultSignatures[64] = "which"; + global_defaultSignatures[65] = "who"; + global_defaultSignatures[66] = "will"; + global_defaultSignatures[67] = "with"; + global_defaultSignatures[68] = "work"; + global_defaultSignatures[69] = "would"; + global_defaultSignatures[70] = "year"; + global_defaultSignatures[71] = "your" + } + + String dictionary_get(long i) { + if (i < 0 || i >= global_numDefaultSignature) { + System.out.print("dictionary_get: Index out of bounds"); + } + return global_defaultSignatures[i]; + } + + String dictionary_match(String str) { + long i; + + for (i = 0; i < global_defaultSignatures; i++) { + if(global_defaultSignatures[i].equals(str)) { + return global_defaultSignatures[i]; + } + } + return null; + } +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Intruder.java b/Robust/src/Benchmarks/SingleTM/Intruder/Intruder.java new file mode 100644 index 00000000..dbef673a --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Intruder.java @@ -0,0 +1,359 @@ +/* ============================================================================= + * + * intruder.java + * + * ============================================================================= + * + * Copyright (C) Stanford University, 2006. All Rights Reserved. + * Author: Chi Cao Minh + * + * ============================================================================= + * + * For the license of bayes/sort.h and bayes/sort.c, please see the header + * of the files. + * + * ------------------------------------------------------------------------ + * + * For the license of kmeans, please see kmeans/LICENSE.kmeans + * + * ------------------------------------------------------------------------ + * + * For the license of ssca2, please see ssca2/COPYRIGHT + * + * ------------------------------------------------------------------------ + * + * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the + * header of the files. + * + * ------------------------------------------------------------------------ + * + * For the license of lib/rbtree.h and lib/rbtree.c, please see + * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree + * + * ------------------------------------------------------------------------ + * + * Unless otherwise noted, the following license applies to STAMP files: + * + * Copyright (c) 2007, Stanford University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * * Neither the name of Stanford University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + * + * ============================================================================= + */ + +#define PARAM_ATTACK 97 +#define PARAM_LENGTH 108 +#define PARAM_NUM 110 +#define PARAM_THREAD 116 + +#define PARAM_DEFAULT_ATTACK 10 +#define PARAM_DEFAULT_LENGTH 16 +#define PARAM_DEFAULT_NUM (1 << 20) +#define PARAM_DEFAULT_SEED 1 +#define PARAM_DEFAULT_THREAD 1 + + +public class Intruder extends Thread { + long global_params[256]; /* 256 = ascii limit */ + + + + public Intruder() { + global_params[PARAM_ATTACK] = PARAM_DEFAULT_ATTACK; + global_params[PARAM_LENGTH] = PARAM_DEFAULT_LENGTH; + global_params[PARAM_NUM] = PARAM_DEFAULT_NUM; + global_params[PARAM_SEED] = PARAM_DEFAULT_SEED; + global_params[PARAM_THREAD] = PARAM_DEFAULT_THREAD; + } +} + +typedef struct arg { + /* input: */ + stream_t* streamPtr; + decoder_t* decoderPtr; + /* output: */ + vector_t** errorVectors; +} arg_t; + + +/* ============================================================================= + * displayUsage + * ============================================================================= + */ +static void +displayUsage (const char* appName) +{ + printf("Usage: %s [options]\n", appName); + puts("\nOptions: (defaults)\n"); + printf(" a Percent [a]ttack (%i)\n", PARAM_DEFAULT_ATTACK); + printf(" l Max data [l]ength (%i)\n", PARAM_DEFAULT_LENGTH); + printf(" n [n]umber of flows (%i)\n", PARAM_DEFAULT_NUM); + printf(" s Random [s]eed (%i)\n", PARAM_DEFAULT_SEED); + printf(" t Number of [t]hreads (%i)\n", PARAM_DEFAULT_THREAD); + exit(1); +} + + +/* ============================================================================= + * parseArgs + * ============================================================================= + */ +static void +parseArgs (long argc, char* const argv[]) +{ + long i; + long opt; + + opterr = 0; + + while ((opt = getopt(argc, argv, "a:l:n:s:t:")) != -1) { + switch (opt) { + case 'a': + case 'l': + case 'n': + case 's': + case 't': + global_params[(unsigned char)opt] = atol(optarg); + break; + case '?': + default: + opterr++; + break; + } + } + + for (i = optind; i < argc; i++) { + fprintf(stderr, "Non-option argument: %s\n", argv[i]); + opterr++; + } + + if (opterr) { + displayUsage(argv[0]); + } +} + + +/* ============================================================================= + * processPackets + * ============================================================================= + */ +void +processPackets (void* argPtr) +{ + TM_THREAD_ENTER(); + + long threadId = thread_getId(); + + stream_t* streamPtr = ((arg_t*)argPtr)->streamPtr; + decoder_t* decoderPtr = ((arg_t*)argPtr)->decoderPtr; + vector_t** errorVectors = ((arg_t*)argPtr)->errorVectors; + + detector_t* detectorPtr = PDETECTOR_ALLOC(); + assert(detectorPtr); + PDETECTOR_ADDPREPROCESSOR(detectorPtr, &preprocessor_toLower); + + vector_t* errorVectorPtr = errorVectors[threadId]; + + while (1) { + + char* bytes; + TM_BEGIN(); + bytes = TMSTREAM_GETPACKET(streamPtr); + TM_END(); + if (!bytes) { + break; + } + + packet_t* packetPtr = (packet_t*)bytes; + long flowId = packetPtr->flowId; + + error_t error; + TM_BEGIN(); + error = TMDECODER_PROCESS(decoderPtr, + bytes, + (PACKET_HEADER_LENGTH + packetPtr->length)); + TM_END(); + if (error) { + /* + * Currently, stream_generate() does not create these errors. + */ + assert(0); + bool_t status = PVECTOR_PUSHBACK(errorVectorPtr, (void*)flowId); + assert(status); + } + + char* data; + long decodedFlowId; + TM_BEGIN(); + data = TMDECODER_GETCOMPLETE(decoderPtr, &decodedFlowId); + TM_END(); + if (data) { + error_t error = PDETECTOR_PROCESS(detectorPtr, data); + P_FREE(data); + if (error) { + bool_t status = PVECTOR_PUSHBACK(errorVectorPtr, + (void*)decodedFlowId); + assert(status); + } + } + + } + + PDETECTOR_FREE(detectorPtr); + + TM_THREAD_EXIT(); +} + + +/* ============================================================================= + * main + * ============================================================================= + */ +MAIN(argc, argv) +{ + GOTO_REAL(); + + /* + * Initialization + */ + + parseArgs(argc, (char** const)argv); + long numThread = global_params[PARAM_THREAD]; + SIM_GET_NUM_CPU(numThread); + TM_STARTUP(numThread); + P_MEMORY_STARTUP(numThread); + thread_startup(numThread); + + long percentAttack = global_params[PARAM_ATTACK]; + long maxDataLength = global_params[PARAM_LENGTH]; + long numFlow = global_params[PARAM_NUM]; + long randomSeed = global_params[PARAM_SEED]; + printf("Percent attack = %li\n", percentAttack); + printf("Max data length = %li\n", maxDataLength); + printf("Num flow = %li\n", numFlow); + printf("Random seed = %li\n", randomSeed); + + dictionary_t* dictionaryPtr = dictionary_alloc(); + assert(dictionaryPtr); + stream_t* streamPtr = stream_alloc(percentAttack); + assert(streamPtr); + long numAttack = stream_generate(streamPtr, + dictionaryPtr, + numFlow, + randomSeed, + maxDataLength); + printf("Num attack = %li\n", numAttack); + + decoder_t* decoderPtr = decoder_alloc(); + assert(decoderPtr); + + vector_t** errorVectors = (vector_t**)malloc(numThread * sizeof(vector_t*)); + assert(errorVectors); + long i; + for (i = 0; i < numThread; i++) { + vector_t* errorVectorPtr = vector_alloc(numFlow); + assert(errorVectorPtr); + errorVectors[i] = errorVectorPtr; + } + + arg_t arg; + arg.streamPtr = streamPtr; + arg.decoderPtr = decoderPtr; + arg.errorVectors = errorVectors; + + /* + * Run transactions + */ + + TIMER_T startTime; + TIMER_READ(startTime); + GOTO_SIM(); +#ifdef OTM +#pragma omp parallel + { + processPackets((void*)&arg); + } + +#else + thread_start(processPackets, (void*)&arg); +#endif + GOTO_REAL(); + TIMER_T stopTime; + TIMER_READ(stopTime); + printf("Elapsed time = %f seconds\n", TIMER_DIFF_SECONDS(startTime, stopTime)); + + /* + * Check solution + */ + + long numFound = 0; + for (i = 0; i < numThread; i++) { + vector_t* errorVectorPtr = errorVectors[i]; + long e; + long numError = vector_getSize(errorVectorPtr); + numFound += numError; + for (e = 0; e < numError; e++) { + long flowId = (long)vector_at(errorVectorPtr, e); + bool_t status = stream_isAttack(streamPtr, flowId); + assert(status); + } + } + printf("Num found = %li\n", numFound); + assert(numFound == numAttack); + + /* + * Clean up + */ + + for (i = 0; i < numThread; i++) { + vector_free(errorVectors[i]); + } + free(errorVectors); + decoder_free(decoderPtr); + stream_free(streamPtr); + dictionary_free(dictionaryPtr); + + TM_SHUTDOWN(); + P_MEMORY_SHUTDOWN(); + + GOTO_SIM(); + + thread_shutdown(); + + MAIN_RETURN(0); +} + + +/* ============================================================================= + * + * End of intruder.java + * + * ============================================================================= + */ diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Node.java b/Robust/src/Benchmarks/SingleTM/Intruder/Node.java new file mode 100644 index 00000000..13a87167 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Node.java @@ -0,0 +1,90 @@ + +#define LDA(a) a +#define STA(a,v) a = v +#define LDV(a) a +#define STV(a,v) a = v +#define LDF(o,f) o.f +#define STF(o,f,v) o.f = v +#define LDNODE(o,f) LDF(o,f) + +#define RED 0 +#define BLACK 1 + +#define SUCCESSOR(n) sucessor(n) +#define PARENT_OF(n) parentOf(n) +#define LEFT_OF(n) leftOf(n) +#define RIGHT_OF(n) rightOf(n) +#define COLOR_OF(n) colorOf(n) +#define SET_COLOR(n, c) setColor(n, c) + +public class Node { + int k; + Object v; + RBTree p; + RBTree l; + RBTree r; + int c; + + public Node() { } + + public Node parentOf(Node n) { + if (n != null) { + return LDNODE(n,p); + } + return null; + } + + public Node leftOf(Node n) { + if (n != null) { + return LDNODE(n, l); + } + return null; + } + + public Node rightOf(Node n) { + if (n != null) { + return LDNODE(n, r); + } + return null; + } + + public int colorOf(Node n) { + if (n != null) { + return LDNODE(n, c); + } + return BLACK; + } + + public void setColor(Node n, int c) { + if (n != null) { + STF(n, c, c); + } + } + + /* + * Return the given node's successor node---the node which has the + * next key in the the left to right ordering. If the node has + * no successor, a null pointer is returned rather than a pointer to + * the nil node + */ + public Node successor(Node t) { + if (t == null) { + return null; + } else if (LDNODE(t, r) != null) { + Node p = LDNODE(t, r); + while (LDNODE(p, l) != null) { + p = LDNODE(p, l); + } + return p; + } else { + Node p = LDNODE(t, p); + Node ch = t; + while (p != NULL && ch == LDNODE(p, r)) { + ch = p; + p = LDNODE(p, p); + } + return p; + } + } + +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Packet.java b/Robust/src/Benchmarks/SingleTM/Intruder/Packet.java new file mode 100644 index 00000000..da555179 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Packet.java @@ -0,0 +1,23 @@ +public class Packet { + int flowId; + int fragmentId; + int numFragment; + int length; + String data; + + public Packet(int numDataBytes) + { + char c[] = new char[numDataByte]; + data = new String(c); + } + + public long packet_compareFlowID(Packet aPtr, Packet bPtr) + { + return aPtr.flowId - bPtr.flowId; + } + + public long packet_compareFragmentID(Packet aPtr, Packet bPtr) + { + return aPtr.fragmentId - bPtr.fragmentId; + } +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Preprocessor.java b/Robust/src/Benchmarks/SingleTM/Intruder/Preprocessor.java new file mode 100644 index 00000000..89c989a6 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Preprocessor.java @@ -0,0 +1,15 @@ +public class Preprocessor { + public Preprocessor() {} + + public void preprocessor_convertURNHex(char str[]) + { + System.out.printString("Error: preprocessor_convertURNHex not implemented\n"); + System.exit(0); + return; + } + + public String preprocessor_toLower(String str) + { + return str.toLowerCase(); + } +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/RBTree.java b/Robust/src/Benchmarks/SingleTM/Intruder/RBTree.java new file mode 100644 index 00000000..94045033 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/RBTree.java @@ -0,0 +1,88 @@ +#define ROTATE_LEFT(set, node) rotateLeft(set, node) +#define ROTATE_RIGHT(set, node) rotateRight(set, node) + +public class RBTree { + Node root; + // FIXME Test if this is correct + public int compare(int a, int b) { + return (a-b); + } + + public RBTree(void) { + root = null; + } + + public Node lookup(RBTree s, Object k) { + Node p = s.root; + while (p != null) { + int cmp = s.compare(k, p.k); + if (cmp == 0) { + return p; + } + if (cmp < 0) { + p = p.l; + } else { + p = p.r; + } + } + return null; + } + + /* + * Balancing operations. + * + * Implementations of rebalancings during insertion and deletion are + * slightly different than the CLR version. Rather than using dummy + * nilnodes, we use a set of accessors that deal properly with null. They + * are used to avoid messiness surrounding nullness checks in the main + * algorithms. + * + * From CLR + */ + public void rotateLeft(Node x) { + Node r = LDNODE(x, r); + Node rl = LDNODE(r, l); + STF(x, r, rl); + if (rl != null) { + STF(rl, p, x); + } + /* TODO: compute p = xp = x->p. Use xp for R-Values in following */ + Node xp = LDNODE(x, p); + STF(r, p, xp); + if (xp == NULL) { + STF(this, root, r); + } else if (LDNODE(xp, l) == x) { + STF(xp, l, r); + } else { + STF(xp, r, r); + } + } + + public rotateRight(Node x) { + Node l = LDNODE(x, l); + Node lr = LDNODE(l, r); + STF(x, l, lr); + if (lr != NULL) { + STF(lr, p, x); + } + Node xp = LDNODE(x, p); + STF(l, p, xp); + if (xp == NULL) { + STF(this, root, l); + } else if (LDNODE(xp, r) == x) { + STF(xp, r, l); + } else { + STF(xp, l, l); + } + STF(l, r, x); + STF(x, p, l); + } + + public Node parentOf(Node n) { + if (n ! = null) { + return LDNODE(n,p); + } + return null; + } + +} diff --git a/Robust/src/Benchmarks/SingleTM/Intruder/Stream.java b/Robust/src/Benchmarks/SingleTM/Intruder/Stream.java new file mode 100644 index 00000000..9d4bcc89 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Intruder/Stream.java @@ -0,0 +1,171 @@ +#define MAP_T RBTree +#define MAP_ALLOC(hash, cmp) RBTree() +#define MAP_INSERT(map, key, data) map.rbtree_insert(key, data) +#define MAP_CONTAINS(map, key) map.rbtree_contains(key); + +public class Stream { + int percentAttack; + Random randomPtr; + Vector_t allocVectorPtr; + Queue packetQueuePtr; + MAP_T attackMapPtr; + + public Stream(int percentAttack) { + Stream streamPtr; + + if (percentAttack < 0 || percentAttack > 100) { + System.out.print("Error: Invalid percentAttack value\n"); + System.exit(0); + } + this.percentAttack = percentAttack; + randomPtr = new Random(); + allocVectorPtr = vector_alloc(1); + if (allocVectorPtr == null) { + System.out.print("Error: Vector allocation failed\n"); + System.exit(0); + } + packetQueuePtr = queue_alloc(1); + if (packetQueuePtr == null) { + System.out.print("Error: Queue allocation failed\n"); + System.exit(0); + } + attackMapPtr = MAP_ALLOC(null, null); + if (attackMapPtr == null) { + System.out.print("Error: MAP_ALLOC failed\n"); + System.exit(0); + } + } + + public void splitIntoPackets(String str, int flowId, Random randomPtr, + Vector_t allocVectorPtr, Queue packetQueuePtr) + { + int numByte = str.length(); + int numPacket = randomPtr.random_generate() % numByte + 1; + int numDataByte = numByte / numPacket; + + int p; + for (p = 0; p < (numPacket - 1); p++) { + Packet bytes = new Packet(numDataByte); + if (bytes == null) { + System.out.printString("Error: Packet class allocation failed\n"); + System.exit(-1); + } + boolean status; + status = allocVectorPtr.vector_pushBack(bytes); + if (status == false) { + System.out.printString("Error: Vector pushBack failed\n"); + System.exit(-1); + } + bytes.flowId = flowId; + bytes.fragmentId = p; + bytes.numFragment = numPacket; + bytes.length = numDataByte; + int beginIndex = p * numDataByte; + int endIndex = beginIndex + numDataByte; + String tmpstr = str.subString(beginIndex, endIndex); + bytes.data = new String(tmpstr); + status = packetQueuePtr.queue_push(bytes); + if (status == false) { + System.out.printString("Error: Queue push failed\n"); + System.exit(0); + } + } + boolean status; + int lastNumDataByte = numDataByte + numByte % numPacket; + Packet bytes = new Packet(lastNumDataByte); + if (bytes == null) { + System.out.printString("Error: Packet class allocation failed\n"); + System.exit(0); + } + bytes.flowId = flowId; + bytes.fragmentId = p; + bytes.numFragment = numPacket; + bytes.length = lastNumDataByte; + int beginIndex = p * numDataByte; + int endIndex = beginIndex + lastNumDataByte; + String tmpstr = str.subString(beginIndex, endIndex); + bytes.data = new String(tmpstr); + status = packetQueuePtr.queue_push(bytes); + if (status == false) { + System.out.printString("Error: Queue push failed\n"); + System.exit(0); + } + } + + int stream_generate(Stream streamPtr, Dictionary dictionaryPtr, + int numFlow, int seed, int maxLength) + { + int numAttack = 0; + + int percentAttack = streamPtr.percentAttack; + Random randomPtr = streamPtr.randomPtr; + Vector_t allocVectorPtr = streamPtr.allocVectorPtr; + Queue packetQueuePtr = streamPtr.packetQueuePtr; + MAP_T attackMapPtr = streamPtr.attackMapPtr; + + Detector detectorPtr = new Detector(); + //detectorPtr.detector_addPreprocessor( + randomPtr.random_seed(); + packetQueuePtr.queue_clear(); + + int range = '~' - ' ' + 1; + if (range <= 0) { + System.out.printString("Assert failed range <= 0\n"); + System.exit(0); + } + + int f; + for (f = 1; f <= numFlow; f++) { + String str; + if ((randomPtr.random_generate() % 100) < percentAttack) { + int s = randomPtr.random_generate() % global_numDefaultSignature; + str = dictionaryPtr.dictionary_get(s); + boolean status = MAP_INSERT(attackMapPtr, f, str); + if (status == false) { + System.out.printString("Assert failed: status is false\n"); + System.exit(0); + } + numAttack++; + } else { + /* Create random string */ + int length = (randomPtr.random_generate() % maxLength) + 1; + str = new String[length+1]; + boolean status = allocVectorPtr.vector_pushBack(str); + if (status == null) { + System.out.printString("Assert failed: status is null\n"); + System.exit(0); + } + char c[] = str.toCharArray(); + for (l = 0; l < length; l++) { + c[l] = ' ' + (char) randomPtr.random_generate() % range; + } + c[l] = 0; + String str2 = new String(c); + int err = detectorPtr.detector_process(str2); + if (err == ERROR_SIGNATURE) { + boolean status = MAP_INSERT(attackMapPtr, f, str); + if (status == null) { + System.out.printString("Assert failed status is null\n"); + System.exit(0); + } + numAttack++; + } + } + splitIntoPackets(str, f, randomPtr, allocVectorPtr, packetQueuePtr); + } + packetQueuePtr.queue_shuffle(randomPtr); + + return numAttack; + } + + String stream_getPacket(Stream streamPtr) + { + return streamPtr.queue_pop(); + } + + boolean stream_isAttack(Stream streamPtr, int flowId) + { + return MAP_CONTAINS(streamPtr.attackMapPtr, flowId); + } + +} -- 2.34.1