目录
1.校验和原理
1.1 校验和计算
1.2 校验和验证
2.UDP校验和原理
2.1 UDP校验数据范围
2.2 发送方UDP校验和计算
2.3 接收方UDP校验和验证
3.UDP伪首部
4.UDP校验和示例代码
4.1 发送方示例代码
4.2 接收方示例代码
5.判断UDP校验和是否计算正确?
范围:UDP伪首部(12字节) + UDP首部(8字节) + UDP负载数据
接收方和发送方都遵守该约定。
思考:为什么发送方校验数据(UDP首部校验和)为0,而接收方校验数据(UDP首部校验和)已填充校验和,双方校验数据不一样,不会出错吗?
其实接收方是把计算累加和和校验和验证两个步骤合成了一个步骤,可以理解为接收方先计算校验数据(UDP首部校验和为0)的累加和,再把累加和UDP校验和相加。
图 1
作用:增加对IP地址等信息的校验,确保信息不出错。
图 2
字段分析:
源地址:源IP地址(4字节),网络字节序
目的地址:目的IP地址(4字节),网络字节序
保留:填0
协议名:UDP协议(17)
UDP报文长度:UDP数据包长度(包含UDP头部),网络字节序
伪头部自定义:
struct pseudo_udphdr {
uint32_t saddr;
uint32_t daddr;
uint8_t fill;
uint8_t protocol;
uint16_t len;
};
struct pseudo_udphdr {
uint32_t saddr;
uint32_t daddr;
uint8_t fill;
uint8_t protocol;
uint16_t len;
};
uint16_t checksum(uint16_t *buf, int size) {
register uint32_t sum = 0;
while(size > 1) {
sum += *(buf++);
size -= 2;
}
while(sum >> 16) {
sum = (sum >> 16) + (sum & 0xffff);
}
return ~sum;
}
uint32_t create_pack(char *buf, const char *payload, uint32_t payload_len) {
struct ethhdr *eh = (struct ethhdr *)buf;
memcpy(eh->h_dest, nexthop_mac, ETH_ALEN);
memcpy(eh->h_source, src_mac, ETH_ALEN);
eh->h_proto = htons(ETH_P_IP);
struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
iph->ihl = IP_HDRLEN / sizeof(uint32_t);
iph->version = 4;
iph->tos = 0;
iph->tot_len = htons(IP_HDRLEN + UDP_HDRLEN + payload_len);
iph->id = htons(0);
iph->frag_off = htons(0);
iph->ttl = 255;
iph->protocol = IPPROTO_UDP;
iph->saddr = inet_addr(SRC_IP);
iph->daddr = inet_addr(DST_IP);
iph->check = 0;
iph->check = checksum((uint16_t *)iph, IP_HDRLEN);
struct udphdr *uh = (struct udphdr *)(buf + ETH_HLEN + sizeof(struct iphdr));
uh->uh_sport = htons(SPORT);
uh->uh_dport = htons(DPORT);
uh->uh_ulen = htons(UDP_HDRLEN + payload_len);
uint32_t udp_len = UDP_HDRLEN + payload_len;
char checksum_buf[MAX_BUF_SIZE] = {0};
uint32_t checksum_len = sizeof(struct pseudo_udphdr) + udp_len;
if (checksum_len % 2 != 0) checksum_len += 1;
struct pseudo_udphdr puh = {
.saddr = iph->saddr,
.daddr = iph->daddr,
.fill = 0,
.protocol = IPPROTO_UDP,
.len = uh->uh_ulen,
};
uh->uh_sum = 0;
memcpy(checksum_buf, &puh, sizeof(struct pseudo_udphdr));
memcpy(checksum_buf + sizeof(struct pseudo_udphdr), uh, UDP_HDRLEN);
memcpy(checksum_buf + sizeof(struct pseudo_udphdr) + UDP_HDRLEN, payload, payload_len);
uh->uh_sum = checksum((uint16_t *)checksum_buf, checksum_len);
printf("udp checksum:%04x\n", uh->uh_sum);
memcpy(buf + ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr), payload, payload_len);
return ETH_HLEN + IP_HDRLEN + UDP_HDRLEN + payload_len;
}
struct pseudo_udphdr {
uint32_t saddr;
uint32_t daddr;
uint8_t fill;
uint8_t protocol;
uint16_t len;
};
uint16_t checksum_nofold(uint16_t *buf, int size) {
register uint32_t sum = 0;
while(size > 1) {
sum += *(buf++);
size -= 2;
}
while(sum >> 16) {
sum = (sum >> 16) + (sum & 0xffff);
}
return sum;
}
int isvalid(const char *buf) {
struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
struct udphdr *uh = (struct udphdr *)(buf + ETH_HLEN + sizeof(struct iphdr));
char checksum_buf[MAX_BUF_SIZE] = {0};
uint32_t checksum_len = sizeof(struct pseudo_udphdr) + ntohs(uh->uh_ulen);
if (checksum_len % 2 != 0) checksum_len += 1;
struct pseudo_udphdr puh = {
.saddr = iph->saddr,
.daddr = iph->daddr,
.fill = 0,
.protocol = IPPROTO_UDP,
.len = uh->uh_ulen,
};
memcpy(checksum_buf, &puh, sizeof(struct pseudo_udphdr));
memcpy(checksum_buf + sizeof(struct pseudo_udphdr), (char *)uh, ntohs(uh->uh_ulen));
uint16_t csum = checksum_nofold((uint16_t *)checksum_buf, checksum_len);
printf("udp csum:%04x\n", csum);
if (csum != 0xffff) return -1;
return 0;
}
wireshark工具自带UDP校验和验证功能,可以通过wireshark抓包协助判断UDP校验和是否正确,wireshark UDP校验和验证功能需手动开启。
开启步骤:
1.打开 [编辑]-[首选项]-[Protocols]-[UDP]。
2.“Validate the udp checksum if possible”打勾。
图 3
wireshark检测到UDP校验和出错,会有相应的提示,如下图:
图 4