-
-/* structure in Deocder
- */
-
public class Decoded {
int flowId;
byte[] data;
* =============================================================================
*/
-
-#define MAP_T RBTree
-#define MAP_ALLOC(hash, cmp) RBTree.alloc(cmp)
-#define MAP_INSERT(map, key, data) map.insert(key, data)
-#define MAP_CONTAINS(map, key) map.contains(key)
-#define MAP_FIND(map,key) map.get(key)
-#define MAP_REMOVE(map,key) map.deleteNode(key)
-
public class Decoder {
-
- MAP_T fragmentedMapPtr; /* contains list of packet_t* */
- Queue_t decodedQueuePtr; /* contains decoded_t* */
+ RBTree fragmentedMapPtr; /* contains list of packet_t* */
+ Queue_t decodedQueuePtr; /* contains decoded_t* */
int cnt;
- public Decoder() {}
-
/* =============================================================================
* decoder_alloc
* =============================================================================
decoder_t* decoder_alloc ();
*/
- public static Decoder alloc() {
- Decoder decoderPtr;
-
- decoderPtr = new Decoder();
- if(decoderPtr != null) {
- decoderPtr.fragmentedMapPtr = MAP_ALLOC(0,0);
-
- if(decoderPtr.fragmentedMapPtr == null)
- {
- System.out.println("Assert in Decoder.alloc");
- System.exit(1);
- }
-
- decoderPtr.decodedQueuePtr = Queue_t.queue_alloc(1024);
- if(decoderPtr.decodedQueuePtr == null)
- {
- System.out.println("Assert in Decoder.alloc");
- System.exit(1);
- }
-
- decoderPtr.cnt = 0;
-
- }
-
- return decoderPtr;
- }
-
+ public Decoder() {
+ fragmentedMapPtr = new RBTree(0);
+ decodedQueuePtr = new Queue_t(1024);
+ }
/* =============================================================================
* decoder_free
* =============================================================================
void decoder_free (decoder_t* decoderPtr);
*/
-
-
/* =============================================================================
* decoder_process
* =============================================================================
er_t decoder_process (decoder_t* decoderPtr, char* bytes, long numByte);
*/
- public int process(Packet packetPtr,int numByte)
- {
- boolean status;
- ERROR er = new ERROR();
-
- /*
- * Basic error checking
- */
-
- if (numByte < 0) {
- return er.SHORT;
- }
-
-
- int flowId = packetPtr.flowId;
- int fragmentId = packetPtr.fragmentId;
- int numFragment = packetPtr.numFragment;
- int length = packetPtr.length;
-
-
- if (flowId < 0) {
- return er.FLOWID;
- }
-
- if ((fragmentId < 0) || (fragmentId >= numFragment)) {
- return er.FRAGMENTID;
- }
-
- if (length < 0) {
- return er.LENGTH;
- }
-
-
- /*
- * Add to fragmented map for reassembling
- */
-
- if (numFragment > 1) {
-
- List_t fragmentListPtr = (List_t)MAP_FIND(fragmentedMapPtr,flowId);
-
- if (fragmentListPtr == null) {
-
- fragmentListPtr = List_t.alloc(1); // packet_compareFragmentId
- if(fragmentListPtr == null) {
- System.out.println("Assertion in Decoder1.process");
- System.exit(1);
- }
- status = fragmentListPtr.insert(packetPtr);
-
- if(!status) {
- System.out.println("Assertion Decoer.process");
- System.exit(1);
- }
-
- status = MAP_INSERT(fragmentedMapPtr,flowId,fragmentListPtr);
- if(!status) {
- System.out.println("Assertion Decoder!!!!.process");
- System.exit(1);
- }
-
- } else {
-
- List_Iter it = new List_Iter();
- it.reset(fragmentListPtr);
-
- if(!it.hasNext(fragmentListPtr)) {
- System.out.println("Assertion in Decoder2.process");
- System.exit(1);
- }
-
- System.out.print("");
- Packet firstFragmentPtr = (Packet)it.next(fragmentListPtr);
-
-
- int expectedNumFragment = firstFragmentPtr.numFragment;
-
-
- if (numFragment != expectedNumFragment) {
- status = MAP_REMOVE(fragmentedMapPtr,flowId);
- if(!status) {
- System.out.println("Assertion in process1");
- System.exit(1);
- }
- return er.NUMFRAGMENT;
- }
-
- status = fragmentListPtr.insert(packetPtr);
-
- if(!status) {
- System.out.println("Assertion in process2");
- System.exit(1);
- }
-
-
-
- /*
- * If we have all thefragments we can reassemble them
- */
-
- if(fragmentListPtr.getSize() == numFragment) {
-
- int numBytes = 0;
- int i = 0;
- it.reset(fragmentListPtr);
-
- while (it.hasNext(fragmentListPtr)) {
- Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
-
- if(fragmentPtr.fragmentId != i) {
- status = MAP_REMOVE(fragmentedMapPtr,flowId);
-
- if(!status) {
- System.out.println("Assertion in process3");
- System.exit(1);
- }
- return er.INCOMPLETE; /* should be sequential */
- }
- numBytes = numBytes + fragmentPtr.length;
- i++;
-
- }
-
- byte[] data = new byte[numBytes];
-
- it.reset(fragmentListPtr);
- int index=0;
-
- while(it.hasNext(fragmentListPtr)) {
- Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
-
- for(i=0;i<fragmentPtr.length;i++) {
- data[index++] = fragmentPtr.data[i];
- }
- }
-
-
- Decoded decodedPtr = new Decoded();
- if(decodedPtr == null) {
- System.out.println("Assertion in process6");
- System.exit(1);
- }
-
- decodedPtr.flowId = flowId;
- decodedPtr.data = data;
-
- status = decodedQueuePtr.queue_push(decodedPtr);
-
- if(!status) {
- System.out.println("Assertion in process7");
- System.exit(1);
- }
-
- status = MAP_REMOVE(fragmentedMapPtr,flowId);
-
- if(!status) {
- System.out.println("Assertion in process8");
- System.exit(1);
- }
- }
- }
- }else {
-
-
- /*
- * This is the only fragment, so it is ready
- */
-
- if (fragmentId != 0) {
- return er.FRAGMENTID;
- }
-
- byte[] data = packetPtr.data;
-
- if(data == null) {
- System.out.println("Assertion in proces9");
- System.exit(1);
- }
-
- Decoded decodedPtr = new Decoded();
-
- if(decodedPtr == null) {
- System.out.println("Assertion in process0");
- System.exit(1);
- }
-
- decodedPtr.flowId = flowId;
- decodedPtr.data = data;
-
- status = decodedQueuePtr.queue_push(decodedPtr);
-
- if(!status) {
- System.out.println("Assertion in process10");
- System.exit(1);
- }
- }
-
-// cnt++;
-// System.out.println("method call Count = " + cnt);
-
- return er.NONE;
-
-
-
+ public int process(Packet packetPtr,int numByte) {
+ boolean status;
+ ERROR er = new ERROR();
+ /*
+ * Basic error checking
+ */
+ if (numByte < 0) {
+ return er.SHORT;
}
-
-
+ int flowId = packetPtr.flowId;
+ int fragmentId = packetPtr.fragmentId;
+ int numFragment = packetPtr.numFragment;
+ int length = packetPtr.length;
+ if (flowId < 0) {
+ return er.FLOWID;
+ }
+ if ((fragmentId < 0) || (fragmentId >= numFragment)) {
+ return er.FRAGMENTID;
+ }
+ if (length < 0) {
+ return er.LENGTH;
+ }
+ /*
+ * Add to fragmented map for reassembling
+ */
+ if (numFragment > 1) {
+ List_t fragmentListPtr = (List_t)fragmentedMapPtr.get(flowId);
+ if (fragmentListPtr == null) {
+ fragmentListPtr = new List_t(1); // packet_compareFragmentId
+ status = fragmentListPtr.insert(packetPtr);
+ if(!status) {
+ System.out.println("Assertion Decoer.process");
+ }
+ status = fragmentedMapPtr.insert(flowId, fragmentListPtr);
+ if(!status) {
+ System.out.println("Assertion Decoder!!!!.process");
+ }
+ } else {
+ List_Iter it = new List_Iter();
+ it.reset(fragmentListPtr);
+ if(!it.hasNext(fragmentListPtr)) {
+ System.out.println("Assertion in Decoder2.process");
+ }
+ System.out.print("");
+ Packet firstFragmentPtr = (Packet)it.next(fragmentListPtr);
+ int expectedNumFragment = firstFragmentPtr.numFragment;
+ if (numFragment != expectedNumFragment) {
+ status = fragmentedMapPtr.deleteNode(flowId);
+ if(!status) {
+ System.out.println("Assertion in process1");
+ }
+ return er.NUMFRAGMENT;
+ }
+ status = fragmentListPtr.insert(packetPtr);
+ if(!status) {
+ System.out.println("Assertion in process2");
+ }
+ /*
+ * If we have all thefragments we can reassemble them
+ */
+ if(fragmentListPtr.getSize() == numFragment) {
+ int numBytes = 0;
+ int i = 0;
+ it.reset(fragmentListPtr);
+ while (it.hasNext(fragmentListPtr)) {
+ Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
+ if(fragmentPtr.fragmentId != i) {
+ status = fragmentedMapPtr.deleteNode(flowId);
+ if(!status) {
+ System.out.println("Assertion in process3");
+ }
+ return er.INCOMPLETE; /* should be sequential */
+ }
+ numBytes = numBytes + fragmentPtr.length;
+ i++;
+ }
+ byte[] data = new byte[numBytes];
+ it.reset(fragmentListPtr);
+ int index=0;
+ while(it.hasNext(fragmentListPtr)) {
+ Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
+ for(i=0;i<fragmentPtr.length;i++) {
+ data[index++] = fragmentPtr.data[i];
+ }
+ }
+ Decoded decodedPtr = new Decoded();
+
+ decodedPtr.flowId = flowId;
+ decodedPtr.data = data;
+ status = decodedQueuePtr.queue_push(decodedPtr);
+ if(!status) {
+ System.out.println("Assertion in process7");
+ }
+ status = fragmentedMapPtr.deleteNode(flowId);
+ if(!status) {
+ System.out.println("Assertion in process8");
+ }
+ }
+ }
+ } else {
+ /*
+ * This is the only fragment, so it is ready
+ */
+ if (fragmentId != 0) {
+ return er.FRAGMENTID;
+ }
+ byte[] data = packetPtr.data;
+ if(data == null) {
+ System.out.println("Assertion in proces9");
+ }
+ Decoded decodedPtr = new Decoded();
+
+ decodedPtr.flowId = flowId;
+ decodedPtr.data = data;
+ status = decodedQueuePtr.queue_push(decodedPtr);
+ if(!status) {
+ System.out.println("Assertion in process10");
+ }
+ }
+ return er.NONE;
+ }
/* =============================================================================
* TMdecoder_process
* =============================================================================
er_t TMdecoder_process (TM_ARGDECL decoder_t* decoderPtr, char* bytes, long numByte);
*/
-
-
/* =============================================================================
* decoder_getComplete
* -- If none, returns NULL
public byte[] getComplete(int[] decodedFlowId) {
byte[] data;
Decoded decodedPtr = (Decoded)decodedQueuePtr.queue_pop();
-
if(decodedPtr != null) {
decodedFlowId[0] = decodedPtr.flowId;
data = decodedPtr.data;
-
} else {
decodedFlowId[0] = -1;
data= null;
}
-
-
return data;
}
-
}
-
/* =============================================================================
* TMdecoder_getComplete
* -- If none, returns NULL
* =============================================================================
char* TMdecoder_getComplete (TM_ARGDECL decoder_t* decoderPtr, long* decodedFlowIdPtr);
*/
-
-
/* =============================================================================
*
* End of decoder.java
Dictionary dictionaryPtr;
Vector_t preprocessorVectorPtr;
- public Detector() {}
-
/* =============================================================================
* detector_alloc
* =============================================================================
detector_t* detector_alloc ();
*/
- public static Detector alloc() {
- Detector detectorPtr = new Detector();
-
- if(detectorPtr != null) {
- detectorPtr.dictionaryPtr = new Dictionary();
- if(detectorPtr.dictionaryPtr == null) {
- System.out.println("Assertion in Detector.alloc");
- System.exit(1);
- }
-
- detectorPtr.preprocessorVectorPtr = Vector_t.vector_alloc(1);
- if(detectorPtr.preprocessorVectorPtr == null) {
- System.out.println("Assertion in Detector.alloc");
- System.exit(1);
- }
- }
-
- return detectorPtr;
- }
-
-/* =============================================================================
- * Pdetector_alloc
- * =============================================================================
- detector_t* Pdetector_alloc ();
- */
-
-
-/* =============================================================================
- * detector_free
- * =============================================================================
- void detector_free (detector_t* detectorPtr);
- */
-
-
-/* =============================================================================
- * Pdetector_free
- * =============================================================================
- void Pdetector_free (detector_t* detectorPtr);
- */
-
+ public Detector() {
+ dictionaryPtr = new Dictionary();
+ preprocessorVectorPtr = new Vector_t(1);
+ }
/* =============================================================================
* detector_addPreprocessor
* =============================================================================
* error_t detector_process (detector_t* detectorPtr, char* str);
*/
- public int process(byte[] str)
- {
- /*
- * Apply preprocessors
- */
-
- int p;
- int numPreprocessor = preprocessorVectorPtr.vector_getSize();
- int i;
- for(p = 0; p < numPreprocessor; p++) {
- Integer preprocessor = (Integer)preprocessorVectorPtr.vector_at(p);
- if(preprocessor.intValue() == 1) {
- System.out.println("NOOOOOOOOOOOOO");
- }
- else if(preprocessor.intValue() == 2) {
- for(i=0;i<str.length;i++)
- {
- if(str[i] >'A' && str[i] < 'Z')
- {
- str[i] = (byte)(str[i] + (byte)32);
- }
- }
-
-
- }
- else {
- System.out.println("NOOOOOOOOOOOOO");
- }
- }
-
- /*
- * Check against signatures of known attacks
- */
-
- ERROR err = new ERROR();
-// System.out.print("str = \"" + str+ "\"");
- String signature = dictionaryPtr.match(new String(str));
-// System.out.println("\tSign = \"" + signature+ "\"");
- if(signature != null) {
- return err.SIGNATURE;
- }
-
- return err.NONE;
+ public int process(byte[] str) {
+ /*
+ * Apply preprocessors
+ */
+ int numPreprocessor = preprocessorVectorPtr.vector_getSize();
+ for(int p = 0; p < numPreprocessor; p++) {
+ Integer preprocessor = (Integer)preprocessorVectorPtr.vector_at(p);
+ if(preprocessor.intValue() == 1) {
+ System.out.println("NOOOOOOOOOOOOO");
+ } else if(preprocessor.intValue() == 2) {
+ for(int i=0;i<str.length;i++) {
+ if(str[i] >'A' && str[i] < 'Z') {
+ str[i] +=(byte)32;
+ }
+ }
+ } else {
+ System.out.println("NOOOOOOOOOOOOO");
+ }
+ }
+
+ /*
+ * Check against signatures of known attacks
+ */
+
+ ERROR err = new ERROR();
+ String signature = dictionaryPtr.match(new String(str));
+ if(signature != null) {
+ return err.SIGNATURE;
}
+
+ return err.NONE;
+ }
}
/* =============================================================================
threadID = myID;
}
- private void setDefaultParams()
- {
- percentAttack = PARAM_DEFAULT_ATTACK;
- maxDataLength = PARAM_DEFAULT_LENGTH;
- numFlow = PARAM_DEFAULT_NUM;
- randomSeed = PARAM_DEFAULT_SEED;
- numThread = PARAM_DEFAULT_THREAD;
- }
+ private void setDefaultParams() {
+ percentAttack = PARAM_DEFAULT_ATTACK;
+ maxDataLength = PARAM_DEFAULT_LENGTH;
+ numFlow = PARAM_DEFAULT_NUM;
+ randomSeed = PARAM_DEFAULT_SEED;
+ numThread = PARAM_DEFAULT_THREAD;
+ }
/* =============================================================================
Vector_t[] errorVectors = argPtr.errorVectors;
- Detector detectorPtr = Detector.alloc();
- if(detectorPtr == null)
- {
- System.out.println("Assertion in processPackets");
- System.exit(1);
- }
-
+ Detector detectorPtr = new Detector();
detectorPtr.addPreprocessor(2);
Vector_t errorVectorPtr = errorVectors[threadID];
while(true) {
Packet packetPtr;
-
- System.out.println("Before atomic Brace");
- // TM_BEGIN();
+
atomic {
- System.out.println("In the atomic");
packetPtr = streamPtr.getPacket();
}
- System.out.println("After atomic");
- // TM_END();
- //
-
if(packetPtr == null) {
break;
}
int flowId = packetPtr.flowId;
int error;
- // TM_BEGIN();
atomic {
error = decoderPtr.process(packetPtr,(packetPtr.length));
}
- // TM_END();
- //
- //
-
+
if (error != 0) {
/*
* Currently, stream_generate() does not create these errors.
*/
- System.out.println("Here?");
- System.exit(1);
+ System.out.println("Here?"+error);
}
byte[] data;
int[] decodedFlowId = new int[1];
- // TM_BEGIN();
atomic {
data = decoderPtr.getComplete(decodedFlowId);
}
- // TM_END();
if(data != null) {
- int err = detectorPtr.process(data);
-
- if(err != 0) {
- boolean status = errorVectorPtr.vector_pushBack(new Integer(decodedFlowId[0]));
- if(!status) {
- System.out.println("Assertion in Intruder.processPacket");
- System.exit(1);
- }
- }
+ int err = detectorPtr.process(data);
+
+ if(err != 0) {
+ boolean status = errorVectorPtr.vector_pushBack(new Integer(decodedFlowId[0]));
+ if(!status) {
+ System.out.println("Assertion in Intruder.processPacket");
+ }
+ }
}
}
System.out.println("Random seed = " + in.randomSeed);
Dictionary dictionaryPtr = new Dictionary();
-
- if(dictionaryPtr == null) {
- System.out.println("Assertion in main");
- System.exit(1);
- }
-
- Stream streamPtr = Stream.alloc(in.percentAttack);
-
- if(streamPtr == null) {
- System.out.println("Assertion in main");
- System.exit(1);
- }
-
+ Stream streamPtr = new Stream(in.percentAttack);
int numAttack = streamPtr.generate(dictionaryPtr,in.numFlow,in.randomSeed,in.maxDataLength);
System.out.println("Num Attack = " + numAttack);
- Decoder decoderPtr = Decoder.alloc();
- if(decoderPtr == null) {
- System.out.println("Assertion in main");
- System.exit(1);
- }
-
+ Decoder decoderPtr = new Decoder();
Vector_t[] errorVectors = new Vector_t[in.numThread];
- if(errorVectors == null) {
- System.out.println("Assertion in main");
- System.exit(1);
- }
-
int i;
for(i =0;i< in.numThread;i++) {
- errorVectors[i] = Vector_t.vector_alloc(in.numFlow);
- if(errorVectors[i] == null) {
- System.out.println("Assertion in main");
- System.exit(1);
- }
+ errorVectors[i] = new Vector_t(in.numFlow);
}
Arg arg = new Arg();
Vector_t errorVectorPtr = errorVectors[i];
int e;
int numError = errorVectorPtr.vector_getSize();
- System.out.println("numError = " + numError);
+ //System.out.println("numError = " + numError);
numFound += numError;
for (e = 0; e< numError; e++) {
int flowId = ((Integer)errorVectorPtr.vector_at(e)).intValue();
void list_iter_reset (list_iter_t* itPtr, list_t* listPtr);
*/
public List_Iter() {
- itPtr = null;
}
public void reset(List_t listPtr)
* bool_t list_iter_hasNext (list_iter_t* itPtr, list_t* listPtr);
*/
public boolean hasNext(List_t listPtr) {
- return (itPtr.nextPtr != null)? true : false;
+ return itPtr.nextPtr != null;
}
/* =============================================================================
List_Node nextPtr;
public List_Node() {
- dataPtr = null;
- nextPtr = null;
}
}
private List_Node allocNode(Object dataPtr)
{
List_Node nodePtr = new List_Node();
-
- if(nodePtr == null) {
- return null;
- }
-
nodePtr.dataPtr = dataPtr;
- nodePtr.nextPtr = null;
-
return nodePtr;
}
*
*/
- public static List_t alloc(int chk)
- {
- List_t listPtr = new List_t();
-
- if(listPtr == null) {
- return null;
- }
-
- listPtr.head.dataPtr = null;
- listPtr.head.nextPtr = null;
- listPtr.size = 0;
-
- listPtr.chk = chk;
-
- return listPtr;
- }
+ public List_t(int chk) {
+ this.head = new List_Node();
+ this.chk=chk;
+ }
/* =============================================================================
* list_free
* =============================================================================
* void list_free (list_t* listPtr);
*/
- public static void free(List_t listPtr)
- {
- listPtr = null;
- }
-
-// privae freeList
/* =============================================================================
* list_isEmpty
int capacity;
Object[] elements;
- public Queue_t() {
- }
-
/* =============================================================================
* queue_alloc
* =============================================================================
*/
- public static Queue_t queue_alloc (int initCapacity)
- {
- Queue_t queuePtr = new Queue_t();
-
+ public Queue_t(int initCapacity) {
int capacity = ((initCapacity < 2) ? 2 : initCapacity);
- queuePtr.elements = new Object[capacity];
- if (queuePtr.elements == null) {
- queuePtr = null;
- return null;
- }
- queuePtr.pop = capacity - 1;
- queuePtr.push = 0;
- queuePtr.capacity = capacity;
-
- return queuePtr;
+ elements = new Object[capacity];
+ pop = capacity - 1;
+ push = 0;
+ this.capacity = capacity;
}
-
- /* =============================================================================
- * Pqueue_alloc
- * =============================================================================
- */
- public Queue_t
- Pqueue_alloc (int initCapacity)
- {
- Queue_t queuePtr = new Queue_t();
-
- int capacity = ((initCapacity < 2) ? 2 : initCapacity);
- queuePtr.elements = new Object[capacity];
- if (queuePtr.elements == null) {
- queuePtr = null;
- return null;
- }
- queuePtr.pop = capacity - 1;
- queuePtr.push = 0;
- queuePtr.capacity = capacity;
-
- return queuePtr;
- }
-
/* =============================================================================
* queue_free
* =============================================================================
queue_free (Queue_t queuePtr)
{
queuePtr.elements = null;
- queuePtr = null;
}
- /* =============================================================================
- * Pqueue_free
- * =============================================================================
- */
- public void
- Pqueue_free (Queue_t queuePtr)
- {
- queuePtr.elements = null;
- queuePtr = null;
- }
-
/* =============================================================================
* TMqueue_free
* queue_push
* =============================================================================
*/
- public boolean
- queue_push (Object dataPtr)
- {
-
- if(pop == push) {
+ public boolean queue_push (Object dataPtr) {
+ if(pop == push) {
+ System.out.println("push == pop in Queue.java");
+ return false;
+ }
- System.out.println("push == pop in Queue.java");
- return false;
+ /* Need to resize */
+ int newPush = (push + 1) % capacity;
+ if (newPush == pop) {
+ int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+ Object[] newElements = new Object[newCapacity];
+ if (newElements == null) {
+ return false;
}
-
-
- /* Need to resize */
- int newPush = (push + 1) % capacity;
- if (newPush == pop) {
-
- int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
- Object[] newElements = new Object[newCapacity];
- if (newElements == null) {
- return false;
- }
-
- int dst = 0;
- Object[] tmpelements = elements;
- if (pop < push) {
- int src;
- for (src = (pop + 1); src < push; src++, dst++) {
- newElements[dst] = elements[src];
- }
- } else {
- int src;
- for (src = (pop + 1); src < capacity; src++, dst++) {
- newElements[dst] = elements[src];
- }
- for (src = 0; src < push; src++, dst++) {
- newElements[dst] = elements[src];
- }
- }
-
- //elements = null;
- elements = newElements;
- pop = newCapacity - 1;
- capacity = newCapacity;
- push = dst;
- newPush = push + 1; /* no need modulo */
+
+ int dst = 0;
+ Object[] tmpelements = elements;
+ if (pop < push) {
+ int src;
+ for (src = (pop + 1); src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ } else {
+ int src;
+ for (src = (pop + 1); src < capacity; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ for (src = 0; src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
}
-
- elements[push] = dataPtr;
- push = newPush;
-
- return true;
+
+ //elements = null;
+ elements = newElements;
+ pop = newCapacity - 1;
+ capacity = newCapacity;
+ push = dst;
+ newPush = push + 1; /* no need modulo */
}
+
+ elements[push] = dataPtr;
+ push = newPush;
+
+ return true;
+ }
/* =============================================================================
* queue_pop
* =============================================================================
*/
- public Object
- //queue_pop (Queue queuePtr)
- queue_pop ()
- {
- //int pop = queuePtr.pop;
- //int push = queuePtr.push;
- //int capacity = queuePtr.capacity;
-
- int newPop = (pop + 1) % capacity;
- if (newPop == push) {
- return null;
- }
-
- //Object dataPtr = queuePtr.elements[newPop];
- //queuePtr.pop = newPop;
- Object dataPtr = elements[newPop];
- pop = newPop;
-
- return dataPtr;
+ public Object queue_pop () {
+ int newPop = (pop + 1) % capacity;
+ if (newPop == push) {
+ return null;
}
+
+ Object dataPtr = elements[newPop];
+ pop = newPop;
+
+ return dataPtr;
+ }
public void queue_shuffle (Random randomPtr)
{
#define RED 0
#define BLACK 1
-
+#define parentOf(n) (n!=null? n.p : null)
+#define leftOf(n) (n!=null? n.l : null)
+#define rightOf(n) (n!=null? n.r : null)
+#define colorOf(n) (n!=null? n.c : BLACK)
+#define setColor(n, col) if (n!=null) n.c=col;
public class RBTree {
Node root;
x.p = l;
}
- /* parentOf */
- private Node parentOf (Node n)
- {
- return ((n!=null) ? n.p : null);
- }
-
- /* leftOf */
- private Node leftOf (Node n)
- {
- return ((n != null)? n.l : null);
- }
-
- /* rightOf */
- private Node rightOf(Node n)
- {
- return ((n!= null) ? n.r : null);
- }
-
- /* colorOf */
- private int colorOf(Node n)
- {
- return ((n!=null) ? n.c : BLACK);
- }
- /* setColor */
- private void setColor(Node n, int c)
- {
- if ( n != null) {
- n.c = c;
- }
- }
/* fixAfterInsertion */
private void fixAfterInsertion(Node x)
* =============================================================================
* rbtree_t* rbtree_alloc (long (*compare)(const void*, const void*));
*/
- public static RBTree alloc(int compID)
- {
- RBTree n = new RBTree();
- if (n != null) {
- n.compID = compID;
- n.root = null;
- }
-
- return n;
- }
+ public RBTree(int compID) {
+ this.compID = compID;
+ this.root = null;
+ }
int MATRIX_A;
int UPPER_MASK;
int LOWER_MASK;
+ int[] mag01;
public Random() {
RANDOM_DEFAULT_SEED = 0;
MATRIX_A = 0x9908b0df; /* constant vector a */
UPPER_MASK = 0x80000000; /* most significant w-r bits */
LOWER_MASK = 0x7fffffff; /* least significant r bits */
+ mag01 = new int[2];
+ mag01[0] = 0x0;
+ mag01[1] = MATRIX_A;
+
}
public void random_alloc() {
/* initializes mt[N] with a seed */
public void init_genrand(int s) {
- int mti;
mt[0]= s & 0xFFFFFFFF;
- for (mti=1; mti<N; mti++) {
+ for (int mti=1; mti<N; mti++) {
mt[mti] = (1812433253 * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
}
public int random_generate() {
- return genrand_int32();
+ return 0x7fffffff&genrand_int32();
+ }
+
+ public int posrandom_generate() {
+ return 0x7fffffff&genrand_int32();
}
- //public static int genrand_int32(int[] mt, int mtiPtr) {
public int genrand_int32() {
int y;
- int[] mag01= new int[2];
- mag01[0] = 0x0;
- mag01[1] = MATRIX_A;
int mti = this.mti;
/* mag01[x] = x * MATRIX_A for x=0,1 */
- if (mti >= N) { /* generate N words at one time */
+ if (mti >= 624) { /* generate N words at one time */
int kk;
+ int[] mt = this.mt;
- if (mti == N+1) /* if init_genrand() has not been called, */
+ if (mti == 624+1) /* if init_genrand() has not been called, */
init_genrand(5489); /* a default initial seed is used */
- for (kk=0;kk<N-M;kk++) {
- y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
- mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[(int)(y & 0x1)];
+ for (kk=0;kk<(624-397);kk++) {
+ y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff);
+ mt[kk] = mt[kk+397] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0:0x9908b0df);
}
- for (;kk<N-1;kk++) {
- y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
- mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[(int)(y & 0x1)];
+ for (;kk<(624-1);kk++) {
+ y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff);
+ mt[kk] = mt[kk+(397-624)] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0:0x9908b0df);
}
- y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
- mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1)];
+ y = (mt[624-1]&0x80000000)|(mt[0]&0x7fffffff);
+ mt[624-1] = mt[397-1] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0:0x9908b0df);
mti = 0;
}
-#define MAP_T RBTree
-#define MAP_ALLOC(hash, cmp) RBTree.alloc(cmp)
-#define MAP_INSERT(map, key, data) map.insert(key, data)
-#define MAP_CONTAINS(map, key) map.contains(key)
-#define MAP_FIND(map,key) map.get(key)
-#define MAP_REMOVE(map,key) map.deleteNode(key)
-
public class Stream {
int percentAttack;
Random randomPtr;
Vector_t allocVectorPtr;
Queue_t packetQueuePtr;
- MAP_T attackMapPtr;
-
- public Stream() {}
-
-
- /* alloc */
- public static Stream alloc(int percentAttack) {
- Stream streamPtr = new Stream();
+ RBTree attackMapPtr;
+ public Stream(int percentAttack) {
if (percentAttack < 0 || percentAttack > 100) {
System.out.print("Error: Invalid percentAttack value\n");
System.exit(0);
}
- streamPtr.percentAttack = percentAttack;
-
- streamPtr.randomPtr = new Random();
- streamPtr.allocVectorPtr = Vector_t.vector_alloc(1);
- if (streamPtr.allocVectorPtr == null) {
- System.out.print("Error: Vector allocation failed\n");
- System.exit(0);
- }
- streamPtr.packetQueuePtr = Queue_t.queue_alloc(1);
- if (streamPtr.packetQueuePtr == null) {
- System.out.print("Error: Queue allocation failed\n");
- System.exit(0);
- }
- streamPtr.attackMapPtr = MAP_ALLOC(0,0);
- if (streamPtr.attackMapPtr == null) {
- System.out.print("Error: MAP_ALLOC failed\n");
- System.exit(0);
- }
- return streamPtr;
+ this.percentAttack = percentAttack;
+ randomPtr = new Random();
+ allocVectorPtr = new Vector_t(1);
+ packetQueuePtr = new Queue_t(1);
+ attackMapPtr = new RBTree(0);
}
-
/* splintIntoPackets
* -- Packets will be equal-size chunks except for last one, which will have
* all extra bytes
*/
private void splitIntoPackets(byte[] str,int flowId, Random randomPtr,
- Vector_t allocVectorPtr, Queue_t packetQueuePtr)
- {
+ Vector_t allocVectorPtr, Queue_t packetQueuePtr) {
int numByte = str.length;
int numPacket = randomPtr.random_generate() % numByte + 1;
int numDataByte = numByte / numPacket;
-
int i;
int p;
boolean status;
int z;
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);
- }
status = allocVectorPtr.vector_pushBack(bytes);
if (status == false) {
System.out.printString("Error: Vector pushBack failed\n");
bytes.numFragment = numPacket;
bytes.length = numDataByte;
endIndex = beginIndex + numDataByte;
-
-
for(i=beginIndex,z=0;i <endIndex;z++,i++) {
bytes.data[z] = str[i];
}
-
status = packetQueuePtr.queue_push(bytes);
if (status == false) {
System.out.printString("Error: Queue push failed\n");
System.exit(0);
}
beginIndex = endIndex;
-
}
-
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;
endIndex = numByte;
-
for(i=beginIndex,z=0;i<endIndex;z++,i++) {
bytes.data[z] = str[i];
}
status = packetQueuePtr.queue_push(bytes);
-
if (status == false) {
System.out.printString("Error: Queue push failed\n");
System.exit(0);
}
}
-
/*==================================================
/* stream_generate
* -- Returns number of attacks generated
/*==================================================*/
-
- public int generate(Dictionary dictionaryPtr,int numFlow, int seed, int maxLength)
- {
+ public int generate(Dictionary dictionaryPtr,int numFlow, int seed, int maxLength) {
int numAttack = 0;
ERROR error = new ERROR();
-
- Detector detectorPtr = Detector.alloc();
-
- if(detectorPtr == null)
- {
- System.out.println("Assertion in Stream.generate");
- System.exit(1);
- }
+ Detector detectorPtr = new Detector();
detectorPtr.addPreprocessor(2); // preprocessor_toLower
-
randomPtr.random_seed(seed);
packetQueuePtr.queue_clear();
-
int range = '~' - ' ' + 1;
if (range <= 0) {
System.out.printString("Assert failed range <= 0\n");
System.exit(0);
}
-
int f;
boolean status;
for (f = 1; f <= numFlow; f++) {
int s = randomPtr.random_generate() % dictionaryPtr.global_numDefaultSignature;
String str = dictionaryPtr.get(s);
c = str.getBytes();
-
- status = MAP_INSERT(attackMapPtr, f, c);
+ status = attackMapPtr.insert(f, c);
if (status == false) {
System.out.printString("Assert failed: status is false\n");
System.exit(0);
} else {
/* Create random string */
int length = (randomPtr.random_generate() % maxLength) + 1;
-
int l;
c = new byte[length+1];
for (l = 0; l < length; l++) {
- c[l] =(byte) (' ' + (char) (randomPtr.random_generate() % range));
+ c[l] =(byte) (' ' + (byte) (randomPtr.random_generate() % range));
}
status = allocVectorPtr.vector_pushBack(c);
-
if(!status) {
System.out.println("Assert faiiled status is null.");
System.exit(0);
}
-
-
int err = detectorPtr.process(c);
if (err == error.SIGNATURE) {
- status = MAP_INSERT(attackMapPtr, f, c);
-
+ status = attackMapPtr.insert(f, c);
System.out.println("Never here");
if (!status) {
System.out.printString("Assert failed status is null\n");
numAttack++;
}
}
-
splitIntoPackets(c, f, randomPtr, allocVectorPtr, packetQueuePtr);
-
}
packetQueuePtr.queue_shuffle(randomPtr);
-
return numAttack;
}
-
/*========================================================
* stream_getPacket
* -- If none, returns null
* ======================================================
*/
- Packet getPacket()
+ Packet getPacket()
{
return (Packet)packetQueuePtr.queue_pop();
}
-
/* =======================================================
* stream_isAttack
* =======================================================
*/
boolean isAttack(int flowId)
{
- return MAP_CONTAINS(attackMapPtr, flowId);
+ return attackMapPtr.contains(flowId);
}
-
}
int size;
int capacity;
Object[] elements;
-// QuickSort qsort;
-
- public Vector_t() {
-// qsort = new QuickSort();
- }
/* =============================================================================
* Vector_alloc
* -- Returns null if failed
* =============================================================================
*/
- public static Vector_t vector_alloc (int initCapacity) {
+ public Vector_t(int initCapacity) {
int capacity = Math.imax(initCapacity, 1);
- Vector_t vectorPtr = new Vector_t();
- if(vectorPtr != null) {
- vectorPtr.size = 0;
- vectorPtr.capacity = capacity;
- vectorPtr.elements = new Object[capacity];
- if(vectorPtr.elements == null)
- return null;
- }
- return vectorPtr;
+ this.size = 0;
+ this.capacity = capacity;
+ this.elements = new Object[capacity];
}
/* =============================================================================