static void push_step_to_b(t_opt *opt, int nbr_elem_lst, int i) { int count; count = nbr_elem_lst - opt->step * (i - 1); while (count) { if ((*opt->a_heap_save)->nbr >= opt->split) { push_src_to_dst(opt->a_heap_save, opt->b_heap_save); print_ft("pb", opt); } else { rotate_list(opt->a_heap_save); print_ft("ra", opt); } --count; } }
static void sort_step(t_heap **a_heap, t_heap **b_heap, t_opt *opt) { int nbr_elem; int max; int n; while ((nbr_elem = get_nbr_elem(b_heap))) { opt->pos = choice_min_or_max(b_heap, nbr_elem, &n, &max); while ((*b_heap)->nbr != n) choose_rx_rrx(b_heap, nbr_elem, opt, 'b'); push_src_to_dst(b_heap, a_heap); print_ft("pa", opt); if (n != max) { rotate_list(a_heap); print_ft("ra", opt); } } }
static void put_max_in_last_pos(t_opt *opt) { int nbr_elem; int max; int pos; nbr_elem = get_nbr_elem(opt->a_heap_save); max = get_max(opt->a_heap_save, &pos); while ((*opt->a_heap_save)->nbr != max) choose_rx_rrx(opt->a_heap_save, nbr_elem, opt, 'a'); rotate_list(opt->a_heap_save); print_ft("ra", opt); }
void remove_old_ft_entries(struct sr_instance* sr) { struct ft* ft_walker = 0; struct ft* prev = 0; struct ft* del = 0; time_t cur = time(NULL); int maxTTL = MAX_ENTRY_TTL; ft_walker = sr->flow_table; print_ft(sr); while(ft_walker) { if((cur - ft_walker->creation_time > ft_walker->ttl) || (ft_walker->ttl > maxTTL)) { if(prev == 0) { del = ft_walker; sr->flow_table = sr->flow_table->next; ft_walker = sr->flow_table; sr->ft_size--; if(del) free(del); } else if(!ft_walker->next) { free(prev->next); sr->ft_size--; ft_walker = 0; } else { prev->next=ft_walker->next; sr->ft_size--; if(ft_walker) free(ft_walker); ft_walker = prev->next; } } else { prev=ft_walker; ft_walker=ft_walker->next; } } } /* end remove_old_ft_entries() */
void sort(t_heap **a_heap, t_heap **b_heap, t_opt *opt, int *sorted) { int nbr_elem; nbr_elem = get_nbr_elem(a_heap); if (is_sort(a_heap) != 1) { if (nbr_elem == 2) { swap_two_first_elem(a_heap); print_ft("sa", opt); } else if (nbr_elem == 3) if_three_elem(a_heap, opt); else if (nbr_elem < 25) select_sort(a_heap, b_heap, opt); else handle_sort(a_heap, b_heap, opt, sorted); } }
int add_ft_entry(struct sr_instance* sr, struct in_addr srcIP, struct in_addr dstIP, uint8_t IPprotocol, int srcPort, int dstPort) { printf("Entered add entry method.\n"); printf("-------FLOW TABLE --------\n"); print_ft(sr); struct ft* ft_walker = 0; int maxSize = MAX_FT_SIZE; /* check to see if table is full */ if(sr->ft_size >= maxSize) { printf("Flow table is max size.\n"); remove_old_ft_entries(sr); } /* if remove_old_ft_entries() didn't remove anything */ if(sr->ft_size >= maxSize) { /*************************************** SEND AN ICMP RESPONSE HOST UNREACHABLE ****************************************/ printf("Delete failed and Rule table is still max size.\n"); return 0; /* ICMP "connection refused" returned and log entry generated */ } /* ------------------------------------- * Check if flow table already contains the connection * If it does, we need to update ttl, but not re-add * -------------------------------------*/ if(ft_contains(sr, srcIP, dstIP, IPprotocol, srcPort, dstPort) == 1) { printf("Connection already contained in flow table.\n"); return 1; } printf("Going to add now.\n"); /* see if the table is empty */ if(sr->flow_table == 0) { sr->flow_table = (struct ft*)malloc(sizeof(struct ft)); assert(sr->flow_table); sr->flow_table->next = 0; sr->flow_table->srcIP = srcIP; sr->flow_table->dstIP = dstIP; sr->flow_table->IPprotocol = IPprotocol; sr->flow_table->srcPort = srcPort; sr->flow_table->dstPort = dstPort; time(&sr->flow_table->creation_time); sr->flow_table->ttl += TTL_INCREMENT; sr->ft_size++; return 1; } /* find the end of the linked list */ ft_walker = sr->flow_table; assert(ft_walker); while(ft_walker->next) { ft_walker = ft_walker->next; } ft_walker->next = (struct ft*)malloc(sizeof(struct ft)); assert(ft_walker->next); ft_walker = ft_walker->next; ft_walker->next = 0; ft_walker->srcIP = srcIP; ft_walker->dstIP = dstIP; ft_walker->IPprotocol = IPprotocol; ft_walker->srcPort = srcPort; ft_walker->dstPort = dstPort; ft_walker->creation_time = time(NULL); ft_walker->ttl += TTL_INCREMENT; sr->ft_size++; return 1; } /* end add_ft_entry() */