[英]TCP checksum for two structures
我试图创建一个为TCP生成校验和的函数,但是我使用TCP的两种结构,因为在tcp.h标头中为Mac OS X制作的tcphdr结构不包含我制作数据包所需的所有内容。 我的数据包包含tcphdr结构和以下结构:
typedef struct tcp_option{
u_int16_t mss_opt:8,
mss_len:8;
u_int16_t mss;
u_int16_t sack_kind:8,
sack_len:8;
u_int16_t win_opt:8,
win_len:8;
u_int32_t win:8,
win_nop:8,
time_opt:8,
time_len:8;
u_int32_t time;
u_int32_t time_echo;
} tcp_option;
因此,没有人有想法进行校验和以将该结构和tcphdr结构传递给它。
我的校验和功能适用于IP校验和,但不适用于其中具有两种结构的TCP校验和。 这是校验和功能:
unsigned short checksum(const char *buf, unsigned size)
{
unsigned long long sum = 0;
const unsigned long long *b = (unsigned long long *) buf;
unsigned t1, t2;
unsigned short t3, t4;
/* Main loop - 8 bytes at a time */
while (size >= sizeof(unsigned long long))
{
unsigned long long s = *b++;
sum += s;
if (sum < s) sum++;
size -= 8;
}
/* Handle tail less than 8-bytes long */
buf = (const char *) b;
if (size & 4)
{
unsigned s = *(unsigned *)buf;
sum += s;
if (sum < s) sum++;
buf += 4;
}
if (size & 2)
{
unsigned short s = *(unsigned short *) buf;
sum += s;
if (sum < s) sum++;
buf += 2;
}
if (size)
{
unsigned char s = *(unsigned char *) buf;
sum += s;
if (sum < s) sum++;
}
/* Fold down to 16 bits */
t1 = sum;
t2 = sum >> 32;
t1 += t2;
if (t1 < t2) t1++;
t3 = t1;
t4 = t1 >> 16;
t3 += t4;
if (t3 < t4) t3++;
return ~t3;
}
我自己没做,我从互联网上偷了。
解决方案:我删除了两个结构并将其组合为一个结构,然后创建了一个伪头结构,该结构用于进行校验和!
您的结构在u_int_16_t
成员之前有u_int_32_t
成员的奇数,因此,为了确保32位成员正确对齐,它将在其中包含一个隐藏的16位块,您将永远无法通过成员引用进行设置,其内容将是不确定的。 因此,比较缓冲区中的每个16位字将包括比较该16位值。 如果您不使用某种方法来确保该空间的值始终相同,例如memset(0)
结构,然后再为其成员分配值,那么您将不得不使用不同的比较过程(显式比较成员) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.