![](/img/trans.png)
[英]How can I get the g_dbus_connection_signal_subscribe function to tell me about pre-existing objects/interfaces?
[英]Is there a pre-existing function or code I can use to compute a TCP segment checksum in a POSIX program
我正在寫一個小POSIX程序,我需要計算一個TCP段的校驗和,我想使用一個現有函數以避免自己寫一個。
類似於(偽代碼)的東西:
char *data = ....
u16_integer = computeChecksum(data);
我在網上搜索,但沒有找到正確的答案,有什么建議嗎?
在這里,它或多或少直接來自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.