[英]TCP checksum is 0
我有tcp標頭的以下代碼:
struct tcp_header {
uint16_t tcp_sport;
uint16_t tcp_dport;
uint32_t tcp_th_seq;
uint32_t tcp_ack;
uint8_t tcp_off:4;
uint8_t tcp_res:6;
uint8_t tcp_uf:1, tcp_af:1, tcp_pf:1, tcp_rf:1, tcp_sf:1, tcp_ff:1;
uint16_t tcp_win;
uint16_t tcp_sum;
uint16_t tcp_urp;
};
...
void decode_tcp(const unsigned char *header_start) {
const struct tcp_header *tcp_hdr;
tcp_hdr = (const struct tcp_header *)(header_start + ETHERNET_HEADER_SIZE + IP_HEADER_SIZE);
printf("\n TCP Header\n");
printf("\tSource Port : %u\n", ntohs(tcp_hdr->tcp_sport));
printf("\tDestination Port : %u\n", ntohs(tcp_hdr->tcp_dport));
printf("\tSequence number : %u\n", ntohl(tcp_hdr->tcp_th_seq));
printf("\tAcknowledge number: %u\n", ntohl(tcp_hdr->tcp_ack));
printf("\tOffset : %d\n", tcp_hdr->tcp_off);
printf("\tReserved : %d\n", (unsigned int)tcp_hdr->tcp_res);
printf("\tUrgent Flag : %d\n", (unsigned int)tcp_hdr->tcp_uf);
printf("\tAcknoledge Flag : %d\n", (unsigned int)tcp_hdr->tcp_af);
printf("\tPush Flag : %d\n", (unsigned int)tcp_hdr->tcp_pf);
printf("\tReset Flag : %d\n", (unsigned int)tcp_hdr->tcp_rf);
printf("\tSynchronise Flag : %d\n", (unsigned int)tcp_hdr->tcp_sf);
printf("\tFinish Flag : %d\n", (unsigned int)tcp_hdr->tcp_ff);
printf("\tWindow : %d\n", ntohs(tcp_hdr->tcp_win));
printf("\tChecksum : %d\n", ntohs(tcp_hdr->tcp_sum));
printf("\tUrgent Pointer : %d", ntohs(tcp_hdr->tcp_urp));
}
我得到的輸出是
CHecksum是0,所以我認為有些事情是錯誤的。 你們能發現錯誤嗎? 或者canchecksum應該是0?
sourceport也可以22?
這里使用的大多數轉換說明符都是錯誤的:
printf("\tSource Port : %u\n", ntohs(tcp_hdr->tcp_sport));
printf("\tDestination Port : %u\n", ntohs(tcp_hdr->tcp_dport));
printf("\tSequence number : %u\n", ntohl(tcp_hdr->tcp_th_seq));
printf("\tAcknowledge number: %u\n", ntohl(tcp_hdr->tcp_ack));
printf("\tOffset : %d\n", tcp_hdr->tcp_off);
printf("\tReserved : %d\n", (unsigned int)tcp_hdr->tcp_res);
printf("\tUrgent Flag : %d\n", (unsigned int)tcp_hdr->tcp_uf);
printf("\tAcknoledge Flag : %d\n", (unsigned int)tcp_hdr->tcp_af);
printf("\tPush Flag : %d\n", (unsigned int)tcp_hdr->tcp_pf);
printf("\tReset Flag : %d\n", (unsigned int)tcp_hdr->tcp_rf);
printf("\tSynchronise Flag : %d\n", (unsigned int)tcp_hdr->tcp_sf);
printf("\tFinish Flag : %d\n", (unsigned int)tcp_hdr->tcp_ff);
printf("\tWindow : %d\n", ntohs(tcp_hdr->tcp_win));
printf("\tChecksum : %d\n", ntohs(tcp_hdr->tcp_sum));
printf("\tUrgent Pointer : %d", ntohs(tcp_hdr->tcp_urp));
它應該是:
printf("\tSource Port : %hu\n", ntohs(tcp_hdr->tcp_sport));
printf("\tDestination Port : %hu\n", ntohs(tcp_hdr->tcp_dport));
printf("\tSequence number : %u\n", ntohl(tcp_hdr->tcp_th_seq));
printf("\tAcknowledge number: %u\n", ntohl(tcp_hdr->tcp_ack));
printf("\tOffset : %hhu\n", tcp_hdr->tcp_off);
printf("\tReserved : %u\n", (unsigned int)tcp_hdr->tcp_res);
printf("\tUrgent Flag : %u\n", (unsigned int)tcp_hdr->tcp_uf);
printf("\tAcknoledge Flag : %u\n", (unsigned int)tcp_hdr->tcp_af);
printf("\tPush Flag : %u\n", (unsigned int)tcp_hdr->tcp_pf);
printf("\tReset Flag : %u\n", (unsigned int)tcp_hdr->tcp_rf);
printf("\tSynchronise Flag : %u\n", (unsigned int)tcp_hdr->tcp_sf);
printf("\tFinish Flag : %u\n", (unsigned int)tcp_hdr->tcp_ff);
printf("\tWindow : %hu\n", ntohs(tcp_hdr->tcp_win));
printf("\tChecksum : %hu\n", ntohs(tcp_hdr->tcp_sum));
printf("\tUrgent Pointer : %hu", ntohs(tcp_hdr->tcp_urp));
如果你將所有這些轉換刪除為unsigned int
你可以使用hhu
作為printf()
轉換說明符,因為所有這些變量都被定義為unsigned
8位值。
您的代碼有一些問題:
__attribute__((__packed__))
注釋)。 ABI可以在其他屬性之間添加填充。 tcp_hdr = (const struct tcp_header *)(header_start
在讀取/寫入tcp_hdr
字段時可能導致錯位訪問。上面的__packed__
注釋可以防止這種情況;或者,您可以復制(正確對齊的)局部變量中的內容。 我會添加一些健全性檢查
BUILD_BUG_ON(offsetof(struct tcp_header, tcp_sum) != 0x10));
(請檢查tcp rfc tcp_sum是否確實位於0x10位置!)
我認為你的TCP頭定義有問題(這是24字節,但標准頭是20)。 在系統上使用標准聲明,也考慮對齊:
#include <netinet/tcp.h>
tcp頭定義:
struct tcphdr {
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
tcp_seq th_seq; /* sequence number */
tcp_seq th_ack; /* acknowledgement number */
#if __BYTE_ORDER == __LITTLE_ENDIAN
u_int th_x2:4, /* (unused) */
th_off:4; /* data offset */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
u_int th_off:4, /* data offset */
th_x2:4; /* (unused) */
#endif
u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG)
u_short th_win; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
};
入境或出境? 如果啟用了校驗和卸載到NIC,則出站校驗和可以為0 ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.