我正在生成GTP隧道数据包,格式如下:
“Outter IP” - > “Outter UDP” - > “GTP” - > “内部IP” - > “内部UDP”
我正在使用的UDP校验和算法实际上来自DHCPd
**********************code snippet start **********************
udp_header->check = wrapsum(in_cksum((unsigned char *)udp_header, sizeof(struct udp_header),
in_cksum((unsigned char *)&buffer[i], send_len-i,
in_cksum((unsigned char *)&ip_header->saddr,
2*sizeof(ip_header->saddr),
IPPROTO_UDP + ntohs(udp_header->len)))));
/* ******************************************* */
/*
* Checksum routine for Internet Protocol family headers (C Version)
*
* Borrowed from DHCPd
*/
static u_int32_t in_cksum(unsigned char *buf,
unsigned nbytes, u_int32_t sum) {
uint i;
/* Checksum all the pairs of bytes first... */
for (i = 0; i < (nbytes & ~1U); i += 2) {
sum += (u_int16_t) ntohs(*((u_int16_t *)(buf + i)));
/* Add carry. */
if(sum > 0xFFFF)
sum -= 0xFFFF;
}
/* If there's a single byte left over, checksum it, too. Network
byte order is big-endian, so the remaining byte is the high byte. */
if(i < nbytes) {
#ifdef DEBUG_CHECKSUM_VERBOSE
debug ("sum = %x", sum);
#endif
sum += buf [i] << 8;
/* Add carry. */
if(sum > 0xFFFF)
sum -= 0xFFFF;
}
return sum;
}
/* ******************************************* */
static u_int32_t wrapsum (u_int32_t sum) {
sum = ~sum & 0xFFFF;
return htons(sum);
}
**********************code snippet end**********************
outter UDP和内部UDP调用都使用相同的校验和函数。但是,我的内部UDP很好但是我的外部UDP没有通过wireshark的校验和检查。有没有人对此问题有任何建议?