checksum-8位和16位校验和代码示例

// linux 系统编译通过
// gcc filename.c -o filename
// ./filename

#include 
#include 

unsigned char ip_hdr_8[] = 
{
    0x45,0x00,
    0x00,0x3c,
    0x00,0x00,
    0x00,0x00,
    0x40,0x01,
    0x00,0x00,  // checksum
    0xc0,0xa8,
    0x01,0x25,
    0xda,0x3c,
    0x06,0x82
};

unsigned short ip_hdr_16[] = 
{
    0x4500,
    0x003c,
    0x0000,
    0x0000,
    0x4001,
    0x0000,     // checksum
    0xc0a8,
    0x0125,
    0xda3c,
    0x0682
};

unsigned int checksum_8(unsigned int cksum, void *pBuffer, unsigned int size)
{
    char num = 0;
    unsigned char *p = (unsigned char *)pBuffer;

    if ((NULL == pBuffer) || (0 == size))
    {
        return cksum;
    }

    while (size > 1)
    {
        cksum += ((unsigned short)p[num] << 8 & 0xff00) | (unsigned short)p[num + 1] & 0x00FF;
        size  -= 2;
        num   += 2;
    }

    if (size > 0)
    {
        cksum += ((unsigned short)p[num] << 8) & 0xFFFF;
        num += 1;
    }

    while (cksum >> 16)
    {
        cksum = (cksum & 0xFFFF) + (cksum >> 16);
    }

    return ~cksum;
}


unsigned short checksum_16(unsigned short *addr,int len)
{
    unsigned short chksum;
    unsigned int sum = 0;

    while(len > 1)
    {
        sum += *addr++;
        len -= 2;
    }

    if(len == 1)
        sum += *(unsigned char*)addr;

    sum = (sum>>16) + (sum & 0xffff);
    sum += (sum>>16);
    chksum = ~sum;
    return (chksum);
}

void main()
{
    unsigned short chksum;

    printf("--------------------------------------------\n");

    chksum = checksum_8(0,ip_hdr_8,sizeof(ip_hdr_8));
    printf("checksum_8 = 0x%04x \n",chksum);
    ip_hdr_8[10] = chksum >> 8;
    ip_hdr_8[11] = chksum & 0xff;

    chksum = checksum_8(0,ip_hdr_8,20);
    if(chksum)
        printf("Checksum_8 is incorrect! \n");
    else
        printf("Checksum_8 is correct! \n");

    printf("--------------------------------------------\n");

    chksum = checksum_16(ip_hdr_16,sizeof(ip_hdr_16));
    printf("checksum_16 = 0x%04x \n",chksum);
    ip_hdr_16[5] = chksum;

    chksum = checksum_16(ip_hdr_16,20);
    if(chksum)
        printf("Checksum 16 is incorrect! \n");
    else
        printf("Checksum 16 is correct! \n");

    printf("--------------------------------------------\n");
}

你可能感兴趣的:(网络编程)