flow.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "flow.h"
  4. static flow_table *table;
  5. /* Initialize the table of tagged flows */
  6. int init_flow_table(void) {
  7. table = malloc(sizeof(flow_table));
  8. table->table = (flow *) malloc(sizeof(flow)*MAX_FLOWS);
  9. if( table->table == NULL){
  10. fprintf(stderr, "malloc failed.\n");
  11. return 1;
  12. }
  13. table->len = 0;
  14. table->max_len = MAX_FLOWS;
  15. return 0;
  16. }
  17. /* Add a new flow to the tagged flow table */
  18. int add_flow(flow newFlow) {
  19. flow *ptr;
  20. if(table->len == table->max_len){
  21. //grow_table();
  22. return(1);
  23. }
  24. printf("there are %d flows in the table\n", table->len);
  25. ptr = table->table + table->len;
  26. newFlow.tls_state = 1;
  27. newFlow.encrypted = 0;
  28. *ptr = newFlow;
  29. table->len ++;
  30. return(0);
  31. }
  32. int update_flow(int index, int code) {
  33. flow *f;
  34. f = get_flow(index-1);
  35. int state = 0;
  36. switch(code){
  37. case 1: state = state | TLS_CLNT_HELLO;
  38. break;
  39. case 2: state = state | TLS_SERV_HELLO;
  40. break;
  41. case 4: state = state | TLS_NEW_SESS;
  42. break;
  43. case 16: state = state | TLS_KEY_EXCHG;
  44. break;
  45. case 20: state = state | TLS_FINISHED;
  46. break;
  47. }
  48. f->tls_state = state;
  49. return 0;
  50. }
  51. int remove_flow(int index) {
  52. int i;
  53. flow *ptr;
  54. if(index){
  55. ptr = table->table + index -1;
  56. for(i=0; i< table->len - index; i++){
  57. ptr += i;
  58. *ptr = *(ptr + 1);
  59. }
  60. table->len --;
  61. } else {
  62. return 1;
  63. }
  64. printf("flow removed!\n");
  65. return 0;
  66. }
  67. int grow_table() {
  68. return 0;
  69. }
  70. int check_flow(flow observed){
  71. /* Loop through flows in table and see if it exists */
  72. int i;
  73. flow *candidate = table->table;
  74. /* Check first in this direction */
  75. for(i=0; i<table->len; i++){
  76. candidate += i;
  77. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  78. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  79. if(candidate->src_port == observed.src_port){
  80. if(candidate->dst_port == observed.dst_port){
  81. return i+1;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. candidate = table->table;
  88. /* Then in the other direction */
  89. for(i=0; i<table->len; i++){
  90. candidate += i;
  91. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  92. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  93. if(candidate->src_port == observed.dst_port){
  94. if(candidate->dst_port == observed.src_port){
  95. return i+1;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return 0;
  102. }
  103. flow *get_flow(int index){
  104. if(index < table->len){
  105. return table->table+index;
  106. } else {
  107. return NULL;
  108. }
  109. }