简体   繁体   English

两种结构的TCP校验和

[英]TCP checksum for two structures

I am trying to make a function that generates checksums for TCP, but I work with two structures of TCP, because the tcphdr structure made for Mac OS X in tcp.h header does not contain everything that I need to make a packet. 我试图创建一个为TCP生成校验和的函数,但是我使用TCP的两种结构,因为在tcp.h标头中为Mac OS X制作的tcphdr结构不包含我制作数据包所需的所有内容。 My packet contains the tcphdr structure and this structure: 我的数据包包含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;

So does anyone have an idea to make a checksum to pass this structure and the tcphdr structure to. 因此,没有人有想法进行校验和以将该结构和tcphdr结构传递给它。

My checksum function works for the IP checksum, but not for the TCP checksum with two structures in it. 我的校验和功能适用于IP校验和,但不适用于其中具有两种结构的TCP校验和。 This is the checksum function: 这是校验和功能:

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;
}

Didn't made it myself, I stole it from the internet. 我自己没做,我从互联网上偷了。

SOLUTION: I removed the two structures and combined it to one structure, then I made an pseudo header structure which I used to make an checksum! 解决方案:我删除了两个结构并将其组合为一个结构,然后创建了一个伪头结构,该结构用于进行校验和!

Your structure has an odd number of u_int_16_t members before the u_int_32_t members, so in order to assure the 32-bit members are aligned properly it will have a hidden 16-bit chunk in it that you will never be able to set by member reference, and the contents of which will be undefined. 您的结构在u_int_16_t成员之前有u_int_32_t成员的奇数,因此,为了确保32位成员正确对齐,它将在其中包含一个隐藏的16位块,您将永远无法通过成员引用进行设置,其内容将是不确定的。 Thus comparing every 16-bit word in your buffer will include comparing that 16-bit value. 因此,比较缓冲区中的每个16位字将包括比较该16位值。 If you don't use some method to make sure the value of that space is always the same, like memset(0) the structure before assigning values to its members, then you will have to use a different comparison process (compare the members explicitly.) 如果您不使用某种方法来确保该空间的值始终相同,例如memset(0)结构,然后再为其成员分配值,那么您将不得不使用不同的比较过程(显式比较成员) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM