flow.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include "flow.h"
  7. #include "crypto.h"
  8. #include "slitheen.h"
  9. #include "relay.h"
  10. static flow_table *table;
  11. static session_cache *sessions;
  12. data_queue *downstream_queue;
  13. stream_table *streams;
  14. /* Initialize the table of tagged flows */
  15. int init_tables(void) {
  16. table = calloc(1, sizeof(flow_table));
  17. table->first_entry = NULL;
  18. table->len = 0;
  19. downstream_queue = calloc(1, sizeof(data_queue));
  20. downstream_queue->first_block = NULL;
  21. streams = calloc(1, sizeof(stream_table));
  22. streams->first = NULL;
  23. printf("initialized downstream queue\n");
  24. return 0;
  25. }
  26. /* Add a new flow to the tagged flow table */
  27. flow *add_flow(flow newFlow) {
  28. flow_entry *entry = calloc(1, sizeof(flow_entry));
  29. flow *ptr = calloc(1, sizeof(flow));
  30. entry->f = ptr;
  31. entry->next = NULL;
  32. printf("there are %d flows in the table\n", table->len);
  33. newFlow.state = TLS_CLNT_HELLO;
  34. newFlow.in_encrypted = 0;
  35. newFlow.out_encrypted = 0;
  36. newFlow.application = 0;
  37. newFlow.resume_session = 0;
  38. newFlow.current_session = NULL;
  39. newFlow.packet_chain = NULL;
  40. newFlow.censored_queue = NULL;
  41. newFlow.outbox = NULL;
  42. newFlow.outbox_len = 0;
  43. newFlow.outbox_offset = 0;
  44. newFlow.remaining_record_len = 0;
  45. newFlow.remaining_response_len = 0;
  46. newFlow.httpstate = PARSE_HEADER;
  47. newFlow.replace_response = 0;
  48. newFlow.finish_md_ctx = EVP_MD_CTX_create();
  49. const EVP_MD *md = EVP_sha384();
  50. EVP_DigestInit_ex(newFlow.finish_md_ctx, md, NULL);
  51. memset(newFlow.read_seq, 0, 8);
  52. memset(newFlow.write_seq, 0, 8);
  53. *ptr = newFlow;
  54. flow_entry *last = table->first_entry;
  55. if(last == NULL){
  56. table->first_entry = entry;
  57. } else {
  58. for(int i=0; i< table->len-1; i++){
  59. last = last->next;
  60. }
  61. last->next = entry;
  62. }
  63. table->len ++;
  64. return ptr;
  65. }
  66. /** Observes TLS handshake messages and updates the state of
  67. * the flow
  68. *
  69. * Inputs:
  70. * f: the tagged flow
  71. *
  72. * Output:
  73. * 0 on success, 1 on failure
  74. */
  75. int update_flow(flow *f) {
  76. uint8_t *record;
  77. const struct record_header *record_hdr;
  78. const struct handshake_header *handshake_hdr;
  79. uint8_t *p = f->packet_chain->data;
  80. record_hdr = (struct record_header*) p;
  81. int record_len;
  82. int data_len;
  83. record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  84. data_len = f->packet_chain->data_len;
  85. packet *current = f->packet_chain;
  86. int incoming = current->incoming;
  87. record = calloc(1, record_len);
  88. for(int i=0; (i<data_len) && (i<record_len); i++){
  89. record[i] = p[i];
  90. }
  91. while(record_len > data_len) {
  92. if(current->next == NULL){
  93. free(record);
  94. return 0;
  95. }
  96. if(current->next->seq_num != current->seq_num + current->len){
  97. printf("Missing packet: seq_num= %d, datalen= %d, nextseq= %d\n", current->seq_num, current->len, current->next->seq_num);
  98. free(record);
  99. return 0;
  100. }
  101. current = current->next;
  102. p = current->data;
  103. int i;
  104. for(i=0; (i<current->data_len) && (i+data_len < record_len); i++){
  105. record[data_len+i] = p[i];
  106. }
  107. data_len += current->data_len;
  108. }
  109. switch(record_hdr->type){
  110. case HS:
  111. p = record;
  112. p += RECORD_HEADER_LEN;
  113. if((incoming && f->in_encrypted) || (!incoming && f->out_encrypted)){
  114. printf("Decrypting finished (%d bytes) (%x:%d -> %x:%d)\n", record_len - RECORD_HEADER_LEN, f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  115. int32_t n = encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0);
  116. if(n<=0){
  117. printf("Error decrypting finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  118. }
  119. printf("Finished decrypted: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  120. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  121. printf("record:\n");
  122. for(int i=0; i< n; i++){
  123. printf("%02x ", p[i]);
  124. }
  125. printf("\n");
  126. update_context(f, p, n, incoming, 0x16, 0);
  127. if(incoming) f->in_encrypted = 2;
  128. else f->out_encrypted = 2;
  129. }
  130. handshake_hdr = (struct handshake_header*) p;
  131. f->state = handshake_hdr->type;
  132. switch(f->state){
  133. case TLS_CLNT_HELLO:
  134. printf("Received tagged client hello (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  135. update_finish_hash(f, p);
  136. check_session(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  137. break;
  138. case TLS_SERV_HELLO:
  139. printf("Received server hello (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  140. if(f->resume_session){
  141. verify_session_id(f,p);
  142. } else {
  143. save_session_id(f,p);
  144. }
  145. extract_server_random(f, p);
  146. update_finish_hash(f, p);
  147. break;
  148. case TLS_NEW_SESS:
  149. printf("Received new session\n");
  150. save_session_ticket(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  151. update_finish_hash(f, p);
  152. break;
  153. case TLS_CERT:
  154. printf("Received cert\n");
  155. update_finish_hash(f, p);
  156. break;
  157. case TLS_SRVR_KEYEX:
  158. printf("Received server keyex\n");
  159. update_finish_hash(f, p);
  160. if(extract_parameters(f, p)){
  161. printf("Error extracting params\n");
  162. }
  163. if(compute_master_secret(f)){
  164. printf("Error computing master secret\n");
  165. }
  166. break;
  167. case TLS_CERT_REQ:
  168. update_finish_hash(f, p);
  169. break;
  170. case TLS_SRVR_HELLO_DONE:
  171. printf("Received server hello done\n");
  172. update_finish_hash(f, p);
  173. break;
  174. case TLS_CERT_VERIFY:
  175. printf("received cert verify\n");
  176. update_finish_hash(f, p);
  177. break;
  178. case TLS_CLNT_KEYEX:
  179. printf("Received client key exchange\n");
  180. update_finish_hash(f, p);
  181. break;
  182. case TLS_FINISHED:
  183. printf("Received finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  184. verify_finish_hash(f,p, incoming);
  185. update_finish_hash(f, p);
  186. if((f->in_encrypted == 2) && (f->out_encrypted == 2)){
  187. printf("Handshake complete!\n");
  188. f->application = 1;
  189. if(current->incoming)
  190. f->seq_num = current->seq_num + current->len;
  191. while(current->next != NULL){
  192. current = current->next;
  193. if(current->incoming)
  194. f->seq_num = current->seq_num+ current->len;
  195. }
  196. }
  197. break;
  198. default:
  199. printf("Error? (%x:%d -> %x:%d)...\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  200. break;
  201. }
  202. break;
  203. case APP:
  204. printf("Application Data\n");
  205. break;
  206. case CCS:
  207. printf("CCS (%x:%d -> %x:%d) \n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  208. if(incoming){
  209. f->in_encrypted = 1;
  210. } else {
  211. f->out_encrypted = 1;
  212. }
  213. /*Initialize ciphers */
  214. init_ciphers(f);
  215. break;
  216. case ALERT:
  217. p = record;
  218. p += RECORD_HEADER_LEN;
  219. if(((incoming) && (f->in_encrypted > 0)) || ((!incoming) && (f->out_encrypted > 0))){
  220. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0);
  221. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  222. }
  223. printf("Alert (%x:%d -> %x:%d) %02x %02x \n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port, p[0], p[1]);
  224. fflush(stdout);
  225. break;
  226. case HB:
  227. printf("Heartbeat\n");
  228. break;
  229. default:
  230. printf("Error: Not a Record (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  231. fflush(stdout);
  232. //TODO: later figure this out, for now delete
  233. f->packet_chain = f->packet_chain->next;
  234. if( f->packet_chain != NULL){
  235. update_flow(f);
  236. }
  237. return 0;
  238. }
  239. if(!f->application){
  240. f->seq_num = current->seq_num;
  241. if(record_len == data_len){
  242. /* record ended on packet boundary */
  243. f->packet_chain = current->next;
  244. } else {
  245. /* need to update data */
  246. f->packet_chain = current;
  247. current->data = current->data + (current->data_len - (data_len - record_len));
  248. current->data_len = data_len - record_len;
  249. update_flow(f);
  250. }
  251. }
  252. free(record);
  253. return 0;
  254. }
  255. /** Removes the tagged flow from the flow table: happens when
  256. * the station receives a TCP RST or FIN packet
  257. *
  258. * Input:
  259. * index: the index into the flow table of the tagged flow
  260. *
  261. * Output:
  262. * 0 on success, 1 on failure
  263. */
  264. int remove_flow(flow *f) {
  265. flow_entry *entry = table->first_entry;
  266. if(entry->f == f){
  267. table->first_entry = entry->next;
  268. free(entry->f);
  269. free(entry);
  270. printf("flow removed!\n");
  271. fflush(stdout);
  272. table->len --;
  273. return 0;
  274. }
  275. flow_entry *next;
  276. for(int i=0; i< table->len; i++){
  277. if(entry->next != NULL){
  278. next = entry->next;
  279. } else {
  280. printf("Flow not in table\n");
  281. return 1;
  282. }
  283. if(next->f == f){
  284. entry->next = next->next;
  285. free(next->f);
  286. free(next);
  287. printf("flow removed!\n");
  288. table->len --;
  289. return 0;
  290. }
  291. entry = next;
  292. }
  293. printf("Should not get here!\n");
  294. return 1;
  295. }
  296. /** Expands the flow table when we run out of space
  297. * TODO: implement and test
  298. */
  299. int grow_table() {
  300. return 0;
  301. }
  302. /** Returns the index of a flow in the flow table if
  303. * it exists, returns 0 if it is not present.
  304. *
  305. * Inputs:
  306. * observed: details for the observed flow
  307. *
  308. * Output:
  309. * index of flow in table or -1 if it doesn't exist
  310. */
  311. flow *check_flow(flow observed){
  312. /* Loop through flows in table and see if it exists */
  313. int i;
  314. flow_entry *entry = table->first_entry;
  315. flow *candidate;
  316. if(entry == NULL)
  317. return NULL;
  318. /* Check first in this direction */
  319. for(i=0; i<table->len; i++){
  320. if(entry == NULL){
  321. printf("Error: entry is null\n");
  322. break;
  323. }
  324. candidate = entry->f;
  325. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  326. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  327. if(candidate->src_port == observed.src_port){
  328. if(candidate->dst_port == observed.dst_port){
  329. return candidate;
  330. }
  331. }
  332. }
  333. }
  334. entry = entry->next;
  335. }
  336. entry = table->first_entry;
  337. /* Then in the other direction */
  338. for(i=0; i<table->len; i++){
  339. if(entry == NULL){
  340. printf("Error: entry is null\n");
  341. break;
  342. }
  343. candidate = entry->f;
  344. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  345. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  346. if(candidate->src_port == observed.dst_port){
  347. if(candidate->dst_port == observed.src_port){
  348. return candidate;
  349. }
  350. }
  351. }
  352. }
  353. entry = entry->next;
  354. }
  355. return NULL;
  356. }
  357. /** Returns the flow in the flow table at a specified index
  358. *
  359. * Input:
  360. * index: the desired index of the flow table
  361. *
  362. * Output:
  363. * the flow at the specified index
  364. flow *get_flow(int index){
  365. if(index < table->len){
  366. return table->table+index;
  367. } else {
  368. return NULL;
  369. }
  370. }
  371. */
  372. int init_session_cache(void){
  373. sessions = malloc(sizeof(session_cache));
  374. sessions->length = 0;
  375. sessions->first_session = NULL;
  376. return 0;
  377. }
  378. /** Called from ServerHello, verifies that the session id returned matches
  379. * the session id requested from the client hello
  380. *
  381. * Input:
  382. * f: the tagged flow
  383. * hs: a pointer to the ServerHello message
  384. *
  385. * Output:
  386. * 0 if success, 1 if failed
  387. */
  388. int verify_session_id(flow *f, uint8_t *hs){
  389. //increment pointer to point to sessionid
  390. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  391. p += 2; //skip version
  392. p += SSL3_RANDOM_SIZE; //skip random
  393. uint8_t id_len = (uint8_t) p[0];
  394. p ++;
  395. //check to see if it matches flow's session id set by ClientHello
  396. if(f->current_session != NULL && f->current_session->session_id_len > 0 && !memcmp(f->current_session->session_id, p, id_len)){
  397. //if it matched, update flow with master secret :D
  398. session *last = sessions->first_session;
  399. int found = 0;
  400. for(int i=0; ((i<sessions->length) && (!found)); i++){
  401. if(!memcmp(last->session_id, f->current_session->session_id, id_len)){
  402. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  403. found = 1;
  404. }
  405. last = last->next;
  406. }
  407. if((!found) && (f->current_session->session_ticket_len > 0)){
  408. last = sessions->first_session;
  409. for(int i=0; ((i<sessions->length) && (!found)); i++){
  410. if(!memcmp(last->session_ticket, f->current_session->session_ticket, f->current_session->session_ticket_len)){
  411. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  412. found = 1;
  413. printf("Found new session ticket (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  414. for(int i=0; i< last->session_ticket_len; i++){
  415. printf("%02x ", last->session_ticket[i]);
  416. }
  417. printf("\n");
  418. }
  419. last = last->next;
  420. }
  421. }
  422. } else if (f->current_session != NULL && f->current_session->session_id_len > 0){
  423. //check to see if server's hello extension matches the ticket
  424. save_session_id(f, p);
  425. }
  426. //now check
  427. return 0;
  428. }
  429. /* Called from ClientHello. Checks to see if the session id len is > 0. If so,
  430. * saves sessionid for later verification. Also checks to see if a session
  431. * ticket is included as an extension.
  432. *
  433. * Input:
  434. * f: the tagged flow
  435. * hs: a pointer to the ServerHello message
  436. *
  437. * Output:
  438. * 0 if success, 1 if failed
  439. */
  440. int check_session(flow *f, uint8_t *hs, uint32_t len){
  441. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  442. p += 2; //skip version
  443. p += SSL3_RANDOM_SIZE; //skip random
  444. session *new_session = calloc(1, sizeof(session));
  445. new_session->session_id_len = (uint8_t) p[0];
  446. new_session->session_ticket_len = 0;
  447. p ++;
  448. if(new_session->session_id_len > 0){
  449. f->resume_session = 1;
  450. memcpy(new_session->session_id, p, new_session->session_id_len);
  451. new_session->next = NULL;
  452. printf("Requested new session (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  453. f->current_session = new_session;
  454. }
  455. p += new_session->session_id_len;
  456. //check to see if there is a session ticket included
  457. //skip to extensions
  458. uint16_t ciphersuite_len = (p[0] << 8) + p[1];
  459. p += 2 + ciphersuite_len;
  460. uint8_t compress_meth_len = p[0];
  461. p += 1 + compress_meth_len;
  462. //search for SessionTicket TLS extension
  463. if(2 + SSL3_RANDOM_SIZE + new_session->session_id_len + 1 + 2 + ciphersuite_len + 1 + compress_meth_len > len){
  464. //no extension
  465. if(f->current_session == NULL)
  466. free(new_session);
  467. return 0;
  468. }
  469. uint16_t extensions_len = (p[0] << 8) + p[1];
  470. p += 2;
  471. while(extensions_len > 0){
  472. uint16_t type = (p[0] << 8) + p[1];
  473. p += 2;
  474. uint16_t ext_len = (p[0] << 8) + p[1];
  475. p += 2;
  476. if(type == 0x23){
  477. if(ext_len > 0){
  478. f->resume_session = 1;
  479. new_session->session_ticket_len = ext_len;
  480. new_session->session_ticket = calloc(1, ext_len);
  481. memcpy(new_session->session_ticket, p, ext_len);
  482. f->current_session = new_session;
  483. }
  484. }
  485. p += ext_len;
  486. extensions_len -= (4 + ext_len);
  487. }
  488. if(!f->resume_session){
  489. //see if a ticket is incuded
  490. free(new_session);
  491. }
  492. return 0;
  493. }
  494. /* Called from ServerHello during full handshake. Adds the session id to the
  495. * cache for later resumptions
  496. *
  497. * Input:
  498. * f: the tagged flow
  499. * hs: a pointer to the ServerHello message
  500. *
  501. * Output:
  502. * 0 if success, 1 if failed
  503. */
  504. int save_session_id(flow *f, uint8_t *hs){
  505. printf("saving session id\n");
  506. //increment pointer to point to sessionid
  507. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  508. p += 2; //skip version
  509. p += SSL3_RANDOM_SIZE; //skip random
  510. session *new_session = calloc(1, sizeof(session));
  511. new_session->session_id_len = (uint8_t) p[0];
  512. if(new_session->session_id_len <= 0){
  513. //if this value is zero, the session is non-resumable or the
  514. //server will issue a NewSessionTicket handshake message
  515. free(new_session);
  516. return 0;
  517. }
  518. p++;
  519. memcpy(new_session->session_id, p, new_session->session_id_len);
  520. new_session->next = NULL;
  521. f->current_session = new_session;
  522. if(sessions->first_session == NULL){
  523. sessions->first_session = new_session;
  524. } else {
  525. session *last = sessions->first_session;
  526. for(int i=0; i< sessions->length; i++){
  527. if(last == NULL)
  528. printf("UH OH: last is null?\n");
  529. last = last->next;
  530. }
  531. last->next = new_session;
  532. }
  533. sessions->length ++;
  534. printf("Saved session id:");
  535. for(int i=0; i< new_session->session_id_len; i++){
  536. printf(" %02x", p[i]);
  537. }
  538. printf("\n");
  539. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  540. return 0;
  541. }
  542. /* Called from NewSessionTicket. Adds the session ticket to the
  543. * cache for later resumptions
  544. *
  545. * Input:
  546. * f: the tagged flow
  547. * hs: a pointer to the ServerHello message
  548. *
  549. * Output:
  550. * 0 if success, 1 if failed
  551. */
  552. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len){
  553. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  554. p += 4; //skip lifetime TODO: add to session struct
  555. session *new_session = calloc(1,sizeof(session));
  556. new_session->session_id_len = 0;
  557. new_session->session_ticket_len = (p[0] << 8) + p[1];
  558. printf("saving ticket of size %d\n", new_session->session_ticket_len);
  559. fflush(stdout);
  560. p += 2;
  561. uint8_t *ticket = calloc(1, new_session->session_ticket_len);
  562. memcpy(ticket, p, new_session->session_ticket_len);
  563. new_session->session_ticket = ticket;
  564. memcpy(new_session->master_secret, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  565. if(sessions->first_session == NULL){
  566. sessions->first_session = new_session;
  567. } else {
  568. session *last = sessions->first_session;
  569. for(int i=0; i< (sessions->length-1); i++){
  570. if(last == NULL){
  571. printf("UH OH: last is null?\n");
  572. fflush(stdout);
  573. }
  574. last = last->next;
  575. }
  576. last->next = new_session;
  577. }
  578. sessions->length ++;
  579. printf("Saved session ticket:");
  580. for(int i=0; i< new_session->session_ticket_len; i++){
  581. printf(" %02x", p[i]);
  582. }
  583. printf("\n");
  584. fflush(stdout);
  585. printf("Saved session master secret:");
  586. for(int i=0; i< SSL3_MASTER_SECRET_SIZE; i++){
  587. printf(" %02x", new_session->master_secret[i]);
  588. }
  589. printf("\n");
  590. fflush(stdout);
  591. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  592. fflush(stdout);
  593. return 0;
  594. }
  595. /* Adds a packet the flow's packet chain */
  596. int add_packet(flow *f, struct packet_info *info){
  597. packet *new_packet = malloc(sizeof(packet));
  598. if (info->tcp_hdr == NULL){
  599. return 0;
  600. }
  601. new_packet->seq_num = htonl(info->tcp_hdr->sequence_num);
  602. new_packet->len = info->app_data_len;
  603. new_packet->data = info->app_data;
  604. new_packet->data_len = new_packet->len;
  605. new_packet->next = NULL;
  606. new_packet->incoming =
  607. (info->ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  608. /* Find appropriate place in chain */
  609. if(new_packet->data_len > 0){
  610. packet *previous = NULL;
  611. packet *next = f->packet_chain;
  612. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  613. previous = next;
  614. next = next->next;
  615. }
  616. //place packet after current
  617. if(previous == NULL){
  618. //goes at the beginning of chain
  619. new_packet->next = f->packet_chain;
  620. f->packet_chain = new_packet;
  621. } else {
  622. new_packet->next = next;
  623. previous->next = new_packet;
  624. }
  625. }
  626. return 0;
  627. }