TCP标头和校验和

时间:2014-01-14 02:52:52

标签: c tcp pcap

我需要澄清在计算校验和时正确使用TCP头和伪头。伪头是否需要在IP头之后和真正的TCP头之前立即出现?这就是我所拥有的:

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));

以下是我的标题:

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;

校验和是否涉及获取伪头和“真实”头的长度以及两者的地址?

1 个答案:

答案 0 :(得分:1)

伪标头在物理上不存在。它不是通过网络发送的数据包的一部分。它只是帮助解释IP头的哪些部分包含在TCP头校验和计算中。