我正在使用以下函数计算UDP校验和(在某处找到):
uint16_t udp_checksum(const void *buff, size_t len, in_addr_t src_addr, in_addr_t dest_addr)
{
const uint16_t *buf=(const uint16_t *)buff;
uint16_t *ip_src=(uint16_t *)&src_addr,
*ip_dst=(uint16_t *)&dest_addr;
uint32_t sum;
size_t length=len;
// Calculate the sum
sum = 0;
while (len > 1)
{
sum += *buf++;
if (sum & 0x80000000)
sum = (sum & 0xFFFF) + (sum >> 16);
len -= 2;
}
if ( len & 1 )
// Add the padding if the packet length is odd
sum += *((uint8_t *)buf);
// Add the pseudo-header
sum += *(ip_src++);
sum += *ip_src;
sum += *(ip_dst++);
sum += *ip_dst;
sum += htons(IPROTO_UDP);
sum += htons(length);
// Add the carries
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
// Return the one's complement of sum
return ( (uint16_t)(~sum) );
}
int form_checksums(char * buff)
{
// Get IP and UDP headers
IP_Header* ipHdr = (IP_Header*)(buff);
struct UDP_Header* udpHdr = (struct UDP_Header*) (buff + 4*ipHdr->ihl);
//---- Form and fill IP checksum now--------------------------------------
ipHdr->check = 0;
ipHdr->check = in_cksum((unsigned short *)ipHdr, sizeof(*ipHdr));
//---- calculate and fill udp checksum now ---
udpHdr->checksum = 0;
udpHdr->checksum = udp_checksum(buff + 4*ipHdr->ihl, udpHdr->length, ipHdr->saddr, ipHdr->daddr);
return 0;
}
Wireshark显示计算错误的UDP校验和。我没有看到该功能有任何问题。可能出现什么问题?
答案 0 :(得分:3)
UDP校验和计算需要UDP 伪标头。
以下是我的库中可能有用的一些代码示例:
// SmartBuffer is a stream-like buffer class
uint16_t SmartBuffer::checksum(const void* buf, size_t buflen)
{
assert(buf);
uint32_t r = 0;
size_t len = buflen;
const uint16_t* d = reinterpret_cast<const uint16_t*>(buf);
while (len > 1)
{
r += *d++;
len -= sizeof(uint16_t);
}
if (len)
{
r += *reinterpret_cast<const uint8_t*>(d);
}
while (r >> 16)
{
r = (r & 0xffff) + (r >> 16);
}
return static_cast<uint16_t>(~r);
}
UDPFrame
校验和计算:
uint16_t UDPFrame::computeChecksum(const ipv4_header& ih) const
{
udp_pseudo_header uph;
memset(&uph, 0x00, sizeof(uph));
uph.source = ih.source;
uph.destination = ih.destination;
uph.mbz = 0x00;
uph.type = ih.protocol;
uph.length = getData()->length;
systools::SmartBuffer tmp(sizeof(uph) + d_data.size());
tmp.appendValue(uph);
tmp.append(d_data); // d_data is the UDP frame payload
return tmp.checksum();
}
无论如何,请记住,通常wireshark警告您,由于UDP checksum offload,可以计算校验和的错误值。
也许你的校验和功能确实是错误的,但可靠的方法是尝试接收你的UDP帧。
答案 1 :(得分:1)
UDP校验和通常使用UDP pseudo header计算。这包括已经按网络顺序的协议ID(17)。我认为您必须将sum = htons(17)
替换为sum += 17
。
答案 2 :(得分:0)
在我的情况下,它运行不正常,因为我没有填写正确的源和目标地址参数(endianess问题),但该函数在此问题中发布的效果很好。 如果函数返回零,则必须将校验和设置为0xFFFF。