简体   繁体   English

TCP标头和校验和

[英]TCP Header and Checksum

I need clarification on correctly using the TCP header and pseudoheader when calculating the checksum. 我需要澄清在计算校验和时正确使用TCP标头和伪标头。 Would the pseudoheader need to come immediately after the IP header and before the real TCP header? 伪标头是否需要紧跟在IP标头之后和真实TCP标头之前? 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. 它仅用于说明IP头的哪些部分包括在TCP头校验和计算中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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