[英]Is there a pre-existing function or code I can use to compute a TCP segment checksum in a POSIX program
I am writing a little POSIX program and I need to compute the checksum of a TCP segment, I would like use an existing function in order to avoid to writing one myself. 我正在写一个小POSIX程序,我需要计算一个TCP段的校验和,我想使用一个现有函数以避免自己写一个。
Something like (pseudocode) : 类似于(伪代码)的东西:
char *data = ....
u16_integer = computeChecksum(data);
I searched on the web but I did not find a right answer, any suggestion ? 我在网上搜索,但没有找到正确的答案,有什么建议吗?
Here, it's taken more or less directly from the RFC: 在这里,它或多或少直接来自RFC:
uint16_t ip_calc_csum(int len, uint16_t * ptr)
{
int sum = 0;
unsigned short answer = 0;
unsigned short *w = ptr;
int nleft = len;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.