IP校验和(checksum)原理

目录

1.校验和原理

1.1 校验和计算

1.2 校验和验证

2.IP校验和原理

2.1 IP校验数据范围

2.2 发送方IP校验和计算

2.3 接收方IP校验和验证

3.IP校验和示例代码

3.1 发送方示例代码

3.2 接收方示例代码

4.判断IP校验和是否计算正确?


1.校验和原理

1.1 校验和计算

  • 校验数据以16bit为单位进行累加求和,校验数据需为偶数字节,奇数字节末尾填充0变为偶数字节。
  • 如果累加和超过16bit,产生了进位,需将高16bit和低16bit累加求和。
  • 循环步骤2,直至未产生进位为止。
  • 累加和取反得到校验和。


1.2 校验和验证

  • 校验数据16bit为单位进行累加求和,校验数据需为偶数字节,奇数字节末尾填充0变为偶数字节。
  • 如果累加和超过16bit,产生了进位,需将高16bit和低16bit累加求和。
  • 循环步骤2,直至未产生进位为止。
  • 累加和和校验和相加得到0xffff,校验成功,否则失败。

2.IP校验和原理

2.1 IP校验数据范围

范围:IP首部(20字节)


2.2 发送方IP校验和计算

  • IP首部校验和清零。
  • 校验数据(IP头部)按照校验和原理计算出校验和。
  • 填充校验和至IP首部校验和字段。


2.3 接收方IP校验和验证

  • 接收方接收IP数据报文。
  • 校验数据(IP头部)按照校验和原理校验,满足累加和为0xffff,校验成功。

图 1 

3.IP校验和示例代码

3.1 发送方示例代码

uint16_t checksum(uint16_t *buf, int size) {
    register uint32_t sum = 0;

    while(size > 1) {
        sum += *(buf++);
        size -= 2;
    }

    while(sum >> 16) {
        sum = (sum >> 16) + (sum & 0xffff);
    }

    return ~sum;
}

uint32_t create_pack(char *buf, const char *payload, uint32_t payload_len) {
    struct ethhdr *eh = (struct ethhdr *)buf;
    memcpy(eh->h_dest, nexthop_mac, ETH_ALEN);
    memcpy(eh->h_source, src_mac, ETH_ALEN);
    eh->h_proto = htons(ETH_P_IP);

    struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
    iph->ihl = IP_HDRLEN / sizeof(uint32_t);
    iph->version = 4;
    iph->tos = 0;
    iph->tot_len = htons(IP_HDRLEN + UDP_HDRLEN + payload_len);
    iph->id = htons(0);
    iph->frag_off = htons(0);
    iph->ttl = 255;
    iph->protocol = IPPROTO_UDP;
    iph->saddr = inet_addr(SRC_IP);
    iph->daddr = inet_addr(DST_IP);
    iph->check = 0;
    iph->check = checksum((uint16_t *)iph, IP_HDRLEN);

    struct udphdr *uh = (struct udphdr *)(buf + ETH_HLEN + sizeof(struct iphdr));
    uh->uh_sport = htons(SPORT);
    uh->uh_dport = htons(DPORT);
    uh->uh_ulen = htons(UDP_HDRLEN + payload_len);
    uh->uh_sum = 0;

    memcpy(buf + ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr), payload, payload_len);

    return ETH_HLEN + IP_HDRLEN + UDP_HDRLEN + payload_len;
}

3.2 接收方示例代码

uint16_t checksum_nofold(uint16_t *buf, int size) {
    register uint32_t sum = 0;

    while(size > 1) {
        sum += *(buf++);
        size -= 2;
    }

    while(sum >> 16) {
        sum = (sum >> 16) + (sum & 0xffff);
    }

    return sum;
}


int isvalid(const char *buf) {
    struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);

    uint16_t ip_csum = checksum_nofold((uint16_t*)iph, IP_HDRLEN);
    printf("ip csum:%04x\n", ip_csum);
    if (ip_csum != 0xffff) return -3;


    return 0;
}

4.判断IP校验和是否计算正确?

wireshark工具自带IP校验和验证功能,可以通过wireshark抓包协助判断IP校验和是否正确,wireshark IP校验和验证功能需手动开启。


开启步骤:

1.打开 [编辑]-[首选项]-[Protocols]-[IPv4]。

2.“Validate the IPv4 checksum if possible”打勾。

 图 2

wireshark检测到IP校验和出错,会有相应的提示,如下图:

 图 3

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网心球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值