简体   繁体   中英

Calculating payload in a tcp packet structure

When i want to split a tcp packet and print every part (eth/ip/tcp/payload) separately, how do i calculate the end of the payload?

PrintData(Buffer, iphdrlen);
PrintData(Buffer + iphdrlen, tcpheader->data_offset*4);
PrintData(Buffer + iphdrlen + tcpheader->data_offset*4, ??? );

I tried

PrintData( Buffer + iphdrlen + tcpheader->data_offset*4, ( Size - tcpheader->data_offset*4 - iphdr->ip_header_len*4 ) );

but all the packets was truncated. Which value have i to pass here?

Greetings

IP Header contains the length with the size of the IP header and payload. Also IP header contains IP header size field which equal the number of 32-bit words :

PrintData(Buffer + (iphdrlen * 4) + tcpheader->data_offset * 4, iplen - (iphdrlen * 4 + tcpheader->data_offset * 4));

As I undestand, you forgot to multiply iphdrlen by 4 in offset

TCP format gives the meaning of each field in the TCP header, including details like where the packet data starts and the packet data length.

Note that items in the TCP packet header use 'network order' for each of the fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM