- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下函数(在某处找到)计算 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 校验和。我没有看到函数有任何问题。可能出了什么问题?
最佳答案
UDP 校验和计算需要 UDP 伪 header 。
以下是我的库中的一些代码示例,可能会有所帮助:
// 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 帧。
关于c++ - UDP校验和错误c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4012750/
如何在 PHP 中生成 CRC-8 校验和? 最佳答案 function crcnifull ($dato, $byte) { static $PolyFull=0x8c; for ($i=0
我正在编写代码来使用 32 位无符号整数计算 CRC16。当尝试打印执行 CRC 操作的 XOR 函数的返回值时,它总是打印 0。我尝试了各种调试方法,例如打印语句,但是,我似乎无法弄清楚! 这是我的
ThinkPHP3.2.3验证码显示、刷新、校验 ,具体如下: 显示验证码 首先在Home/Controller下创建一个公共控制器PublicController
我想将自定义验证绑定(bind)到 TimePicker 自定义控件,但下面的代码显示“无法将内容添加到 TimePicker 的对象类型。”。
目录 Spring 校验(validator,JSR-303)实现 什么是JSR-303规范 与Spring MVC结合 实体类添加
导包和配置 导入 JSR 303 的包、hibernate valid 的包 ?
我是一名优秀的程序员,十分优秀!