I need to be able to generate SIP packets over TCP layer, I am performing the following initialization for the TCP header values:
//TCP layer
//src dest port
*(uint16_t*)(pkt + OFF_SRC_PORT) = addrs->a->port;
*(uint16_t*)(pkt + OFF_DST_PORT) = addrs->b->port;
//seq number
*(uint64_t*)(pkt + OFF_DST_PORT+2) = htonl(sequence_no);
//ack number
*(uint64_t*)(pkt + OFF_DST_PORT+2+4) = htonl(ack_no);
//header len +reserved bits
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4) = htons(0x50);
//flags ACK PSH set
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4+1) = htons(0x18);
//Window size
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4+1+1) = htons(0x402);
//Checksum nothing, I have read this gets allocated on kernel level later
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4+1+1+2) = htons(0x0);
//Urg pointer
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4+1+1+2+2) = htons(0x0);
//Options missing
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4+1+1+2+2+2) = htons(0x00);
//Padding
*(uint16_t*)(pkt + OFF_DST_PORT+2+4+4+1+1+2+2+2+2) = htons(0x00);
Upon performing the above initialization I am seeing this in the tcp dump: tcp dump Where am I going wrong in the initialization of the header? PS: I calculated seq number, ack number according to my receiving setup.
Maybe try this:
//TCP layer //src dest port *(uint16_t*)(pkt + OFF_SRC_PORT) = addrs->a->port; *(uint16_t*)(pkt + OFF_SRC_PORT + 2) = addrs->b->port; //seq number *(uint32_t*)(pkt + OFF_SRC_PORT + 4) = htonl(sequence_no); //ack number *(uint32_t*)(pkt + OFF_SRC_PORT + 8) = htonl(ack_no); //header len +reserved bits *(uint8_t*)(pkt + OFF_SRC_PORT + 12) = 0x50; //flags ACK PSH set *(uint8_t*)(pkt + OFF_SRC_PORT + 13) = 0x18; //Window size *(uint16_t*)(pkt + OFF_SRC_PORT + 14) = htons(0x0402); //Checksum nothing, I have read this gets allocated on kernel level later *(uint16_t*)(pkt + OFF_SRC_PORT + 16) = htons(0x0000); //Urg pointer *(uint16_t*)(pkt + OFF_SRC_PORT + 18) = htons(0x0000); //No options (data offset is 20, i.e., 5 * 4), so don't add anything else to the TCP header
Ref: RFC 793, section 3.1. Header Format:
TCP Header Format 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Port | Destination Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgment Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | |U|A|P|R|S|F| | | Offset| Reserved |R|C|S|S|Y|I| Window | | | |G|K|H|T|N|N| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum | Urgent Pointer | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ TCP Header Format