简体   繁体   中英

TCP Header and Checksum

I need clarification on correctly using the TCP header and pseudoheader when calculating the checksum. Would the pseudoheader need to come immediately after the IP header and before the real TCP header? Here is what I have:

IPHeader *iph = (IPHeader *)(packet + ETHER_SIZE); //ETHER_SIZE == 14
ipLen = ntohs(iph->totLen) * 4;
TCPPseudo *tcps = (TCPPseudo *)(packet + ETHER_SIZE + ipLen);
TCPHeader *tcp = (TCPHeader *)(tcps + sizeof(TCPPseudo));

Here are my headers:

typedef struct __attribute__((__packed__)) IPHeader {
  #if __BYTE_ORDER__ == __LITTLE_ENDIAN__
  uint8_t hdrLen:4;
  uint8_t version:4;
  #else
  uint8_t version:4;
  uint8_t hdrLen:4;
  #endif
  uint8_t TOS;
  uint16_t totLen;
  uint16_t id;
  uint16_t offset;
  #define DF 0x4            
  #define MF 0x2           
  #define OFF 0 
  uint8_t TTL;
  uint8_t protocol;
  uint16_t checksum;
  struct in_addr srcIP;
  struct in_addr destIP; 
}IPHeader;

typedef struct __attribute__((__packed__)) TCPHeader {
  uint16_t srcPort;
  uint16_t destPort;
  uint32_t seqNum;
  uint32_t ackNum;
  uint8_t offset:4;
  uint8_t res:4;
  uint8_t flags;
  uint16_t window;
  uint16_t checksum;
  uint16_t urg;
}TCPHeader;

typedef struct __attribute__((__packed__)) TCPPseudo {
  struct in_addr srcAddr;
  struct in_addr destAddr;
  uint8_t zeroes;
  uint8_t protocol;
  uint16_t len;
}TCPPseudo;

Does the checksum involve taking the length of both the pseudoheader and the "real" header as well as the address of both of them?

Pseudo header does not exist physically. It is not a part of the packet sent over the network. It is only a help for explaining which parts of the IP header are included into TCP header checksum calculation.

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