static inline void ProcessUDPFlow(FlowSource_t *fs, struct FlowNode *NewNode ) { struct FlowNode *Node; assert(NewNode->memflag == NODE_IN_USE); // Flush DNS queries directly if ( NewNode->src_port == 53 || NewNode->dst_port == 53 ) { StorePcapFlow(fs, NewNode); Free_Node(NewNode); return; } // insert other UDP traffic Node = Insert_Node(NewNode); // if insert fails, the existing node is returned -> flow exists already if ( Node == NULL ) { dbg_printf("New UDP flow: Packets: %u, Bytes: %u\n", NewNode->packets, NewNode->bytes); return; } assert(Node->memflag == NODE_IN_USE); // update existing flow Node->packets++; Node->bytes += NewNode->bytes; Node->t_last = NewNode->t_last; dbg_printf("Existing UDP flow: Packets: %u, Bytes: %u\n", Node->packets, Node->bytes); Free_Node(NewNode); } // End of ProcessUDPFlow
static inline void ProcessTCPFlow(FlowSource_t *fs, struct FlowNode *NewNode ) { struct FlowNode *Node; assert(NewNode->memflag == NODE_IN_USE); Node = Insert_Node(NewNode); // Return existing Node if flow exists already, otherwise insert es new if ( Node == NULL ) { // Insert as new dbg_printf("New TCP flow: Packets: %u, Bytes: %u\n", NewNode->packets, NewNode->bytes); // in case it's a FIN/RST only packet - immediately flush it if ( NewNode->fin == FIN_NODE ) { // flush node if ( StorePcapFlow(fs, NewNode) ) { Remove_Node(NewNode); } } if ( !CacheCheck() ) { uint32_t NumFlows; LogError("Node cache exhausted! - Immediate flush - increase flow cache!!"); NumFlows = Flush_FlowTree(fs); LogError("Flushed flows: %u", NumFlows); } if ( Link_RevNode(NewNode)) { // if we could link this new node, it is the server answer // -> calculate server latency SetServer_latency(NewNode); } return; } assert(Node->memflag == NODE_IN_USE); // check for first client ACK for client latency if ( Node->latency.flag == 1 ) { SetClient_latency(Node, &(NewNode->t_first)); } else if ( Node->latency.flag == 2 ) { SetApplication_latency(Node, &(NewNode->t_first)); } // update existing flow Node->flags |= NewNode->flags; Node->packets++; Node->bytes += NewNode->bytes; Node->t_last = NewNode->t_last; dbg_printf("Existing TCP flow: Packets: %u, Bytes: %u\n", Node->packets, Node->bytes); if ( NewNode->fin == FIN_NODE) { // flush node Node->fin = FIN_NODE; if ( StorePcapFlow(fs, Node) ) { Remove_Node(Node); } } else { Free_Node(NewNode); } } // End of ProcessTCPFlow
static inline void ProcessTCPFlow (FlowSource_t *fs, struct FlowNode *NewNode) { struct FlowNode *Node; assert (NewNode->memflag == NODE_IN_USE); Node = Insert_Node (NewNode); // if insert fails, the existing node is returned -> flow exists already if (Node == NULL) { dbg_printf ("New TCP flow: Packets: %u, Bytes: %u\n", NewNode->packets, NewNode->bytes); // in case it's a FIN/RST only packet - immediately flush it if (NewNode->fin == FIN_NODE) { // flush node if (StorePcapFlow (fs, NewNode)) { Remove_Node (NewNode); } } if (!CacheCheck()) { uint32_t NumFlows; LogError ("Node cache exhausted! - Immediate flush - increase flow cache!!"); NumFlows = Flush_FlowTree (fs); LogError ("Flushed flows: %u", NumFlows); } return; } assert (Node->memflag == NODE_IN_USE); // update existing flow Node->flags |= NewNode->flags; Node->packets++; Node->bytes += NewNode->bytes; Node->t_last = NewNode->t_last; dbg_printf ("Existing TCP flow: Packets: %u, Bytes: %u\n", Node->packets, Node->bytes); if (NewNode->fin == FIN_NODE) { // flush node Node->fin = FIN_NODE; if (StorePcapFlow (fs, Node)) { Remove_Node (Node); } } else { Free_Node (NewNode); } } // End of ProcessTCPFlow
int Mem_Free(void *ptr){ //check edge cases if (ptr == NULL) return -1; ptr -= sizeof(node); node *tmpPtr = (node*) ptr; if (tmpPtr->wizard != -666) { m_error = E_BAD_POINTER; return -1; } //forward check - coalescion void *check_address = ptr + tmpPtr->data; node *forward_node; int check_forward = 1; while(check_forward != 0) { forward_node = Search_For_Node(check_address, &header); if (forward_node == NULL) { check_forward = 0; break; } else { if (forward_node->next != NULL) { tmpPtr->next = forward_node->next; } else { tmpPtr->next = NULL; } Delete_Node(forward_node, &header); check_address = check_address + forward_node->data; tmpPtr->data += forward_node->data; } } //back check - coalescion check_address = ptr; node *back_node; int size = tmpPtr->data; int check_back = 1; while(check_back != 0) { back_node = Search_For_Previous_Node(check_address, &header, size); if (back_node == NULL) { check_back = 0; break; } else { Delete_Node(back_node, &header); if (tmpPtr->next != NULL) { back_node->next = tmpPtr->next; } else { back_node->next = NULL; } check_address = check_address - back_node->data; back_node->data += tmpPtr->data; size = back_node->data; tmpPtr = back_node; } } //insert freed segment back into free list tmpPtr->wizard = -666; Insert_Node(&tmpPtr, &header); return 0; }
void *Mem_Alloc(int size, int style){ node* to_remove; node* new_free; void* return_ptr; //8-byte align for performance if (size % 8 != 0) { if (size < 8) { size = 8; } else { size = (size / 8 + 1) * 8; } } int total_size_free_space; size = (int)(size + sizeof(node)); //actual size needed is desired size + node size char* temp_ptr; //used to move forward during memory splitting //allocation handling, based on desired style switch (style){ case BESTFIT: to_remove = Get_Best_Fit(size, &header); if (to_remove == NULL) { m_error = E_NO_SPACE; return to_remove; break; } Delete_Node(to_remove, &header); total_size_free_space = to_remove->data; to_remove->data = size; if (total_size_free_space - size > 0) { temp_ptr = (char*) to_remove; temp_ptr += size; new_free = (node*) temp_ptr; new_free->data = total_size_free_space - size; new_free->next = to_remove->next; new_free->wizard = -666; Insert_Node(&new_free, &header); } return_ptr = (void*) to_remove; return_ptr += sizeof(node); return return_ptr; break; case WORSTFIT: to_remove = Get_Worst_Fit(size, &header); if (to_remove == NULL) { m_error = E_NO_SPACE; return to_remove; break; } Delete_Node(to_remove, &header); total_size_free_space = to_remove->data; to_remove->data = size; if (total_size_free_space - size > 0) { temp_ptr = (char*) to_remove; temp_ptr += size; new_free = (node*) temp_ptr; new_free->data = total_size_free_space - size; new_free->next = to_remove->next; new_free->wizard = -666; Insert_Node(&new_free, &header); } return_ptr = (void*) to_remove; return_ptr += sizeof(node); return return_ptr; break; case FIRSTFIT: to_remove = Get_First_Fit(size, &header); if (to_remove == NULL) { m_error = E_NO_SPACE; return to_remove; break; } Delete_Node(to_remove, &header); total_size_free_space = to_remove->data; to_remove->data = size; if (total_size_free_space - size > 0) { // move the pointer to the beginning of the new free space temp_ptr = (char*) to_remove; temp_ptr += size; new_free = (node*) temp_ptr; new_free->data = total_size_free_space - size; new_free->next = to_remove->next; new_free->wizard = -666; Insert_Node(&new_free, &header); } return_ptr = (void*) to_remove; return_ptr += sizeof(node); return return_ptr; break; } return NULL; }