zoukankan      html  css  js  c++  java
  • tcp/ip通信中udp头部结构udphdrp->check校验计算

    通过raw socket修改通信数据后,可通过函数

    set_udp_checksum1

    重新校验计算iph->check值

    在http://www.cnblogs.com/dpf-10/p/7899237.html查看实际应用

    static u_int16_t checksum(u_int32_t init, u_int8_t *addr, size_t count){ 
        /* Compute Internet Checksum for "count" bytes * beginning at location "addr". */ 
        u_int32_t sum = init; 
    
        while( count > 1 ) { 
            /* This is the inner loop */
            sum += ntohs(* (u_int16_t*) addr);
            addr += 2;
            count -= 2;
        } /* Add left-over byte, if any */
        if( count > 0 )
            sum += ntohs(* (u_int8_t*) addr); /* Fold 32-bit sum to 16 bits */ 
        while (sum>>16)
        sum = (sum & 0xffff) + (sum >> 16); 
        return (u_int16_t)~sum;
    } 
    static u_int16_t udp_checksum2(struct iphdr* iphdrp, struct udphdr* udphdrp){ size_t udplen = ntohs(iphdrp->tot_len) - (iphdrp->ihl<<2); u_int32_t cksum = 0; cksum += ntohs((iphdrp->saddr >> 16) & 0x0000ffff); cksum += ntohs(iphdrp->saddr & 0x0000ffff); cksum += ntohs((iphdrp->daddr >> 16) & 0x0000ffff); cksum += ntohs(iphdrp->daddr & 0x0000ffff); cksum += iphdrp->protocol & 0x00ff; cksum += udplen; return checksum(cksum, (u_int8_t*)udphdrp, udplen); } static u_int16_t udp_checksum1(struct iphdr* iphdrp){ struct udphdr *udphdrp = (struct udphdr*)((u_int8_t*)iphdrp + (iphdrp->ihl<<2)); return udp_checksum2(iphdrp, udphdrp); } static void set_udp_checksum2(struct iphdr* iphdrp, struct udphdr* udphdrp){ udphdrp->check = 0; udphdrp->check = htons(udp_checksum2(iphdrp, udphdrp)); } static void set_udp_checksum1(struct iphdr* iphdrp){ struct udphdr *udphdrp = (struct udphdr*)((u_int8_t*)iphdrp + (iphdrp->ihl<<2)); set_udp_checksum2(iphdrp, udphdrp); }
  • 相关阅读:
    我的Android平台“人人相册”客户端
    ADT 17及更高版本的混淆变更
    由glBitmap想到OpenGL编程涉及的坐标系
    版本控制之SVN
    工作了,要努力
    50 Most Frequently Used UNIX / Linux Commands (With Examples)
    linux命令分析SED (二)
    SeekBar也玩分段
    TSQL RAND()
    使用WebClient进行上传文件 [ZT]
  • 原文地址:https://www.cnblogs.com/dpf-10/p/8810203.html
Copyright © 2011-2022 走看看