zoukankan      html  css  js  c++  java
  • 郁闷的libnet API设计


    看以下两个函数的源代码:

    (1)构建ipv4 header的函数
    libnet_ptag_t
    libnet_build_ipv4(u_int16_t len, u_int8_t tos, u_int16_t id, u_int16_t frag,
    u_int8_t ttl, u_int8_t prot, u_int16_t sum, u_int32_t src, u_int32_t dst,
    u_int8_t 
    *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
    {
        

        ip_hdr.ip_tos        
    = tos;                       /* IP tos */
        ip_hdr.ip_len        
    = htons(h);                  /* total length */
        ip_hdr.ip_id         
    = htons(id);                 /* IP ID */
        ip_hdr.ip_off        
    = htons(frag);               /* fragmentation flags */
        ip_hdr.ip_ttl        
    = ttl;                       /* time to live */
        ip_hdr.ip_p          
    = prot;                      /* transport protocol */
        ip_hdr.ip_sum        
    = (sum ? htons(sum) : 0);    /* checksum */
        ip_hdr.ip_src.s_addr 
    = src;                       /* source ip */
        ip_hdr.ip_dst.s_addr 
    = dst;                       /* destination ip */

        

    }

    (2)构建udp header的函数
    libnet_ptag_t
    libnet_build_udp(u_int16_t sp, u_int16_t dp, u_int16_t len, u_int16_t sum,
    u_int8_t 
    *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
    {
        

        udp_hdr.uh_sport   
    = htons(sp);             /* source port */
        udp_hdr.uh_dport   
    = htons(dp);             /* destination port */
        udp_hdr.uh_ulen    
    = htons(h);              /* total length of UDP packet*/
        udp_hdr.uh_sum     
    = (sum ? htons(sum) : 0);/* checksum */

        
    }

    其中,第一个函数中,ip参数需要以网络字节序传入,第二个函数中,端口参数需要以本机字节序传入。真是一个别扭啊。写程序,又是抓包,又是打印字节,折腾了很长时间才确定不是我的代码有问题,而是libnet1.1.*有缺陷。

    版权所有,欢迎转载
  • 相关阅读:
    NOIP 2017 游记?
    bzoj4596 [Shoi2016]黑暗前的幻想乡
    bzoj2467 [中山市选2010]生成树
    bzoj3534 [Sdoi2014]重建
    bzoj1016 [JSOI2008]最小生成树计数
    bzoj4044 [Cerc2014] Virus synthesis
    bzoj4530 [Bjoi2014]大融合
    bzoj2594 [Wc2006]水管局长数据加强版
    bzoj 2342 [Shoi2011]双倍回文
    bzoj [HNOI2008]Cards
  • 原文地址:https://www.cnblogs.com/xiaotie/p/325888.html
Copyright © 2011-2022 走看看