zoukankan      html  css  js  c++  java
  • IP checksum

    To do an experiment, first capture an IP packet, as we can see the checksum is 0xCAD7. Then we set the checksum field to 0, and calculate the checksum, then we will get 0xCAD7. Set 0xCAD7 to the checksum field and calculate the checksum again, we will get 0 then.

    #include <bits/stdc++.h>
    #define DBG(x) cerr << #x << " = " << x << endl
    
    using namespace std;
    typedef long long LL;
    
    unsigned short ip_checksum(const vector<unsigned short> &v) {
        unsigned int sum = 0;
        for (int i = 0; i < v.size(); i++) {
            sum += v[i];
        }
        while (sum > 0xFFFF) {
            sum = (sum & 0xFFFF) + (sum >> 16);
        }
        return ~sum;
    }
    
    int main(int argc, char **argv) {
    
        vector<unsigned short> vec{0x4500, 0x0235, 0x0000, 0x4000, 0x4006, 0x0000, 0x0a01, 0xbf44, 0x8ba2, 0x1904};
        unsigned short sum = ip_checksum(vec);
        assert(sum == 0xcad7);
        printf("checksum = 0x%X
    ", sum);
    
        vec[5] = sum;
    
        sum = ip_checksum(vec);
        assert(sum == 0);
        printf("%X
    ", sum);
        return 0;
    }
    
  • 相关阅读:
    并发编程
    进程的介绍
    操作系统详解
    进程的粗略理解
    打印进度条
    FTP上传下载文件(面向对象版)
    socket套接字
    FTP上传下载文件(函数简易版)
    osi七层协议 Open System Interconnection
    __str__和__repr__的区别
  • 原文地址:https://www.cnblogs.com/ToRapture/p/12760541.html
Copyright © 2011-2022 走看看