0

I have been trying for a little while to get the offset value from a TCP packet header and keep getting a value of 0. Here is a link to the header file I am basing my code off of:

http://unix.superglobalmegacorp.com/BSD4.4/newsrc/netinet/tcp.h.html

I am using tcpHdr->th_off and am receiving a value of 0. I understand the value is 4 bits long, so the above line is probably not what I want. For the flags I used the th_flags value and then performed a bit-wise and with the defined flag I was looking for, but I do not see a way to do that with the payload offset value. How can I get the payload offset value?

Edit:

Here is my initialization of the tcpHdr pointer:

packetSt += ipHdr->ip_hl*4; // Moves IP header pointer to the TCP header location.
tcpHdr = (struct tcphdr*) packetSt; // My TCP header pointer.

Here is the line I use to get the TCP offset value:

tcpHeader->th_off;

I actually saw an error in my code I just fixed. It was the following:

was

tcpHdr = (struct tcphdr*) (packetSt + sizeof(ipHdr));

changed to

tcpHdr = (struct tcphdr*) packetSt;

Since I already moved the packetSt ahead by the ipHrd->th_hl. I will try this, but would still like someone to let me know if this looks right.

MrJman006
  • 752
  • 10
  • 26

1 Answers1

1

I would suggest using this version of the TCP header instead, and fetch the offset with TH_OFF(tcpHdr) (remove the UNALIGNED from the end of the structure - that's a tcpdumpism). That's less dependent on 1) the way your C compiler handles bitfields and 2) whether BYTE_ORDER is set correctly.