是否有一个pre-现有功能或code,我可以用它来计算TCP段的校验和在POSIX程序 [英] Is there a pre-existing function or code I can use to compute a TCP segment checksum in a POSIX program
本文介绍了是否有一个pre-现有功能或code,我可以用它来计算TCP段的校验和在POSIX程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写一个小程序POSIX,我需要计算一个TCP段的校验,我想使用现有的功能,以避免写一个自己。
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.
喜欢的东西(伪code):
Something like (pseudocode) :
char *data = ....
u16_integer = computeChecksum(data);
我在网上搜索上,但我没有找到一个正确的答案,任何建议?
I searched on the web but I did not find a right answer, any suggestion ?
推荐答案
下面,它采取或多或少直接从RFC:
Here, it's taken more or less directly from the 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);
}
这篇关于是否有一个pre-现有功能或code,我可以用它来计算TCP段的校验和在POSIX程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文