4 int make_ack_pkt(tcp_model_t * model, RawEthernetPacket * data_pkt, RawEthernetPacket * ack_pkt) {
5 unsigned long * seq_num_ptr ;
6 unsigned long seq_num = 0;
7 unsigned long rem_seq_num = 0;
8 unsigned long payload_len = 0;
9 unsigned short tcp_hdr_len = 0;
10 unsigned long ack = 0;
11 unsigned long local_ts = 0;
12 unsigned short tcp_cksum = 0;
13 unsigned char ip_hdr_len = IP_HDR_LEN(pkt->data);
14 unsigned short ip_pkt_len = *(unsigned short *)(pkt->data + ETH_HDR_LEN + 2);
15 unsigned short ack_ip_pkt_len = *(unsigned short *)(ack_pkt->data + ETH_HDR_LEN + 2);
16 unsigned char ack_ip_hdr_len = IP_HDR_LEN(ack_pkt->data);
18 ip_pkt_len = ntohs(ip_pkt_len);
19 ack_ip_pkt_len = ntohs(ack_ip_pkt_len);
21 seq_num_ptr = (unsigned long *)(pkt->data + ETH_HDR_LEN + ip_hdr_len + 4);
22 seq_num = ntohl(*seq_num_ptr);
23 JRLDBG("Sequence Number = %lu\n", seq_num);
25 tcp_hdr_len = (*(pkt->data + ETH_HDR_LEN + ip_hdr_len + 12) & 0xf0) >> 2;
27 if (is_syn_pkt(pkt) == 1) {
30 payload_len = ip_pkt_len - (ip_hdr_len + tcp_hdr_len);
32 JRLDBG("TCP Header Length = %hu\n", tcp_hdr_len);
33 JRLDBG("Payload Length = %lu\n", payload_len);
35 ack = seq_num + payload_len;
36 JRLDBG("Ack Num = %lu\n", ack);
40 g_vtp_cons[vcon_i].ip_id--;
41 *(unsigned short *)(ack_pkt->data + ETH_HDR_LEN + 4) = htons(g_vtp_cons[vcon_i].ip_id);
43 // Recompute IP checksum
44 *(unsigned short *)(ack_pkt->data + ETH_HDR_LEN + 10) = 0;
45 *(unsigned short *)(ack_pkt->data + ETH_HDR_LEN + 10) = get_ip_checksum(ack_pkt);
48 // Set Sequence Number
49 rem_seq_num = htonl(g_vtp_cons[vcon_i].rem_seq_num);
50 *(unsigned long *)(ack_pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 4) = rem_seq_num;
54 *(unsigned long *)(ack_pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 8) = ack;
56 // Set TCP Timestamp option
57 local_ts = get_tcp_timestamp(pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 20, tcp_hdr_len - 20);
59 /* We use this for debugging:
60 * If the TCPDump trace shows timestamps with the value of '5' then they are our packets
63 *(unsigned long *)(ack_pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 24) = g_vtp_cons[vcon_i].tcp_timestamp;
64 //*(unsigned long *)(ack_pkt->data + ETH_HDR_LEN + ip_hdr_len + 24) = htonl(5);
67 *(unsigned long *)(ack_pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 28) = local_ts;
70 *(unsigned short *)(ack_pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 16) = 0;
73 tcp_cksum = get_tcp_checksum(ack_pkt, ack_ip_pkt_len - ack_ip_hdr_len);
76 *(unsigned short *)(ack_pkt->data + ETH_HDR_LEN + ack_ip_hdr_len + 16) = tcp_cksum;