static bool addReservation_seq (MAP_T* tablePtr, long id, long num, long price) { reservation_t* reservationPtr; bool status; reservationPtr = (reservation_t*)MAP_FIND(tablePtr, id); if (reservationPtr == NULL) { /* Create new reservation */ if (num < 1 || price < 0) { return false; } reservationPtr = reservation_alloc_seq(id, num, price); assert(reservationPtr != NULL); status = MAP_INSERT(tablePtr, id, reservationPtr); assert(status); } else { /* Update existing reservation */ if (!reservation_addToTotal_seq(reservationPtr, num)) { return false; } if (reservationPtr->numTotal == 0) { status = MAP_REMOVE(tablePtr, id); assert(status); } else { reservation_updatePrice_seq(reservationPtr, price); } } return true; }
bool manager_addCustomer_seq (manager_t* managerPtr, long customerId) { customer_t* customerPtr; bool status; if (MAP_CONTAINS(managerPtr->customerTablePtr, customerId)) { return false; } customerPtr = customer_alloc_seq(customerId); assert(customerPtr != NULL); status = MAP_INSERT(managerPtr->customerTablePtr, customerId, customerPtr); assert(status); return true; }
/*** Initialize the counter */ void bench_init() { //SET = new HashTable(); //SET = (MAP_T*)hcmalloc(sizeof(MAP_T)); SET = MAP_ALLOC(NULL, NULL); //SET->init((CFG.elements/4)); std::cout << "startup " << std::endl; // warm up the datastructure TM_BEGIN_FAST_INITIALIZATION(); for (uint32_t w = 0; w < CFG.elements; w++) { uint32_t seed = 7; int val = rand_r(&seed) % CFG.elements; //SET->insert(val TM_PARAM); MAP_INSERT(SET, val, val); } TM_END_FAST_INITIALIZATION(); }
/* ============================================================================= * decoder_process * ============================================================================= */ int_error_t decoder_process (decoder_t* decoderPtr, char* bytes, long numByte) { bool_t status; /* * Basic error checking */ if (numByte < (long)PACKET_HEADER_LENGTH) { return ERROR_SHORT; } packet_t* packetPtr = (packet_t*)bytes; long flowId = packetPtr->flowId; long fragmentId = packetPtr->fragmentId; long numFragment = packetPtr->numFragment; long length = packetPtr->length; if (flowId < 0) { return ERROR_FLOWID; } if ((fragmentId < 0) || (fragmentId >= numFragment)) { return ERROR_FRAGMENTID; } if (length < 0) { return ERROR_LENGTH; } #if 0 /* * With the above checks, this one is redundant */ if (numFragment < 1) { return ERROR_NUMFRAGMENT; } #endif /* * Add to fragmented map for reassembling */ if (numFragment > 1) { MAP_T* fragmentedMapPtr = decoderPtr->fragmentedMapPtr; list_t* fragmentListPtr = (list_t*)MAP_FIND(fragmentedMapPtr, (void*)flowId); if (fragmentListPtr == NULL) { fragmentListPtr = list_alloc(&decoder_comparator); assert(fragmentListPtr); status = list_insert(fragmentListPtr, (void*)packetPtr); assert(status); status = MAP_INSERT(fragmentedMapPtr, (void*)flowId, (void*)fragmentListPtr); assert(status); } else { list_iter_t it; list_iter_reset(&it, fragmentListPtr); assert(list_iter_hasNext(&it, fragmentListPtr)); packet_t* firstFragmentPtr = (packet_t*)list_iter_next(&it, fragmentListPtr); long expectedNumFragment = firstFragmentPtr->numFragment; if (numFragment != expectedNumFragment) { status = MAP_REMOVE(fragmentedMapPtr, (void*)flowId); assert(status); return ERROR_NUMFRAGMENT; } status = list_insert(fragmentListPtr, (void*)packetPtr); assert(status); /* * If we have all the fragments we can reassemble them */ if (list_getSize(fragmentListPtr) == numFragment) { long numByte = 0; long i = 0; list_iter_reset(&it, fragmentListPtr); while (list_iter_hasNext(&it, fragmentListPtr)) { packet_t* fragmentPtr = (packet_t*)list_iter_next(&it, fragmentListPtr); assert(fragmentPtr->flowId == flowId); if (fragmentPtr->fragmentId != i) { status = MAP_REMOVE(fragmentedMapPtr, (void*)flowId); assert(status); return ERROR_INCOMPLETE; /* should be sequential */ } numByte += fragmentPtr->length; i++; } char* data = (char*)SEQ_MALLOC(numByte + 1); assert(data); data[numByte] = '\0'; char* dst = data; list_iter_reset(&it, fragmentListPtr); while (list_iter_hasNext(&it, fragmentListPtr)) { packet_t* fragmentPtr = (packet_t*)list_iter_next(&it, fragmentListPtr); memcpy(dst, (void*)fragmentPtr->data, fragmentPtr->length); dst += fragmentPtr->length; } assert(dst == data + numByte); decoded_t* decodedPtr = (decoded_t*)SEQ_MALLOC(sizeof(decoded_t)); assert(decodedPtr); decodedPtr->flowId = flowId; decodedPtr->data = data; queue_t* decodedQueuePtr = decoderPtr->decodedQueuePtr; status = queue_push(decodedQueuePtr, (void*)decodedPtr); assert(status); list_free(fragmentListPtr); status = MAP_REMOVE(fragmentedMapPtr, (void*)flowId); assert(status); } } } else { /* * This is the only fragment, so it is ready */ if (fragmentId != 0) { return ERROR_FRAGMENTID; } char* data = (char*)SEQ_MALLOC(length + 1); assert(data); data[length] = '\0'; memcpy(data, (void*)packetPtr->data, length); decoded_t* decodedPtr = (decoded_t*)SEQ_MALLOC(sizeof(decoded_t)); assert(decodedPtr); decodedPtr->flowId = flowId; decodedPtr->data = data; queue_t* decodedQueuePtr = decoderPtr->decodedQueuePtr; status = queue_push(decodedQueuePtr, (void*)decodedPtr); assert(status); } return ERROR_NONE; }
/* ============================================================================= * stream_generate * -- Returns number of attacks generated * ============================================================================= */ long stream_generate (stream_t* streamPtr, dictionary_t* dictionaryPtr, long numFlow, long seed, long maxLength) { long numAttack = 0; long percentAttack = streamPtr->percentAttack; random_t* randomPtr = streamPtr->randomPtr; vector_t* allocVectorPtr = streamPtr->allocVectorPtr; queue_t* packetQueuePtr = streamPtr->packetQueuePtr; MAP_T* attackMapPtr = streamPtr->attackMapPtr; detector_t* detectorPtr = detector_alloc(); detector_addPreprocessor(detectorPtr, &preprocessor_toLower); random_seed(randomPtr, seed); queue_clear(packetQueuePtr); long range = '~' - ' ' + 1; long f; for (f = 1; f <= numFlow; f++) { char* str; if ((random_generate(randomPtr) % 100) < percentAttack) { long s = random_generate(randomPtr) % global_numDefaultSignature; str = dictionary_get(dictionaryPtr, s); bool_t status = MAP_INSERT(attackMapPtr, (void*)f, (void*)str); numAttack++; } else { /* * Create random string */ long length = (random_generate(randomPtr) % maxLength) + 1; str = (char*)malloc((length + 1) * sizeof(char)); bool_t status = vector_pushBack(allocVectorPtr, (void*)str); long l; for (l = 0; l < length; l++) { str[l] = ' ' + (char)(random_generate(randomPtr) % range); } str[l] = '\0'; char* str2 = (char*)malloc((length + 1) * sizeof(char)); strcpy(str2, str); error_t error = detector_process(detectorPtr, str2); /* updates in-place */ if (error == ERROR_SIGNATURE) { bool_t status = MAP_INSERT(attackMapPtr, (void*)f, (void*)str); numAttack++; } free(str2); } splitIntoPackets(str, f, randomPtr, allocVectorPtr, packetQueuePtr); } queue_shuffle(packetQueuePtr, randomPtr); detector_free(detectorPtr); return numAttack; }