#include #include #include "flow.h" static flow_table *table; /* Initialize the table of tagged flows */ int init_flow_table(void) { table = malloc(sizeof(flow_table)); table->table = (flow *) malloc(sizeof(flow)*MAX_FLOWS); if( table->table == NULL){ fprintf(stderr, "malloc failed.\n"); return 1; } table->len = 0; table->max_len = MAX_FLOWS; return 0; } /* Add a new flow to the tagged flow table */ int add_flow(flow newFlow) { flow *ptr; if(table->len == table->max_len){ //grow_table(); return(1); } printf("there are %d flows in the table\n", table->len); ptr = table->table + table->len; newFlow.tls_state = 1; newFlow.encrypted = 0; *ptr = newFlow; table->len ++; return(0); } int update_flow(int index, int code) { flow *f; f = get_flow(index-1); int state = 0; switch(code){ case 1: state = state | TLS_CLNT_HELLO; break; case 2: state = state | TLS_SERV_HELLO; break; case 4: state = state | TLS_NEW_SESS; break; case 16: state = state | TLS_KEY_EXCHG; break; case 20: state = state | TLS_FINISHED; break; } f->tls_state = state; return 0; } int remove_flow(int index) { int i; flow *ptr; if(index){ ptr = table->table + index -1; for(i=0; i< table->len - index; i++){ ptr += i; *ptr = *(ptr + 1); } table->len --; } else { return 1; } printf("flow removed!\n"); return 0; } int grow_table() { return 0; } int check_flow(flow observed){ /* Loop through flows in table and see if it exists */ int i; flow *candidate = table->table; /* Check first in this direction */ for(i=0; ilen; i++){ candidate += i; if(candidate->src_ip.s_addr == observed.src_ip.s_addr){ if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){ if(candidate->src_port == observed.src_port){ if(candidate->dst_port == observed.dst_port){ return i+1; } } } } } candidate = table->table; /* Then in the other direction */ for(i=0; ilen; i++){ candidate += i; if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){ if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){ if(candidate->src_port == observed.dst_port){ if(candidate->dst_port == observed.src_port){ return i+1; } } } } } return 0; } flow *get_flow(int index){ if(index < table->len){ return table->table+index; } else { return NULL; } }