zoukankan      html  css  js  c++  java
  • libnet应用之arp包发送

    /************************************
    author hjj
    date 2011-1-20
    function: send an arp packet to all machine on local net

    modify: wenhao
    gcc arp.c -o arp -Wall -lnet
    *************************************
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <libnet.h>

    #define MAC_ADDR_LEN 6
    #define IP_ADDR_LEN 4

    int main(int argc,char **argv)
    {
    libnet_t *net_t = NULL; //定义libnet_t指针变量
    char *dev = "eth0"; //定义设备名称
    char err_buf[LIBNET_ERRBUF_SIZE];
    libnet_ptag_t p_tag; //定义libnet_ptag_t变量

    unsigned char src_mac[MAC_ADDR_LEN] = {0x00,0x00,0xf1,0xe8,0x0e,0xc8};//发送者网卡地址
    unsigned char dst_mac[MAC_ADDR_LEN] = {0xff,0xff,0xff,0xff,0xff,0xff};//接收者网卡地址
    char *src_ip_str = "172.20.223.74"; //源主机IP地址

    if(argc == 2)
    {
    if(strcmp(argv[1],"-h") == 0||strcmp(argv[1],"--help") == 0)
    {
    printf("%s","help message");
    }else
    {
    src_ip_str = argv[1]; //赋值IP地址
    }
    }

    unsigned long src_ip,dst_ip = 0;

    src_ip = libnet_name2addr4(net_t,src_ip_str,LIBNET_RESOLVE);//将字符串类型的ip转换为顺序网络字节流

    net_t = libnet_init(LIBNET_LINK_ADV, dev, err_buf);//初始化发送包结构
    if(net_t == NULL)
    {
    printf("libnet_init error\n");
    exit(-1);
    }

    p_tag = libnet_build_arp(
    ARPHRD_ETHER,//hardware type ethernet
    ETHERTYPE_IP,//protocol type
    MAC_ADDR_LEN,//mac length
    IP_ADDR_LEN,//protocol length
    ARPOP_REPLY,//op type
    (u_int8_t *)src_mac,//source mac addr这里的作用是更新目的地的arp表
    (u_int8_t *)&src_ip,//source ip addr
    (u_int8_t *)dst_mac,//dest mac addr
    (u_int8_t *)&dst_ip,//dest ip addr
    NULL,//payload
    0,//payload length
    net_t,//libnet context
    0//0 stands to build a new one
    );

    if(-1 == p_tag)
    {
    printf("libnet_build_arp error");
    exit(-1);
    }

    //以太网头部
    p_tag = libnet_build_ethernet(//create ethernet header
    (u_int8_t *)dst_mac,//dest mac addr
    (u_int8_t *)src_mac,//source mac addr
    ETHERTYPE_ARP,//protocol type
    NULL,//payload
    0,//payload length
    net_t,//libnet context
    0//0 to build a new one
    );

    if(-1 == p_tag)
    {
    printf("libnet_build_ethernet error!\n");
    exit(-1);
    }

    int res;
    if(-1 == (res = libnet_write(net_t)))
    {
    printf("libnet_write error!\n");
    exit(-1);
    }

    libnet_destroy(net_t);
    return 0;
    }
  • 相关阅读:
    使用POI读取excel文件内容
    有序链表
    jQuery Validate验证框架详解
    怎样在VS2010中打开VS2012的项目
    在Win8.1系统下如何安装运行SQL Server 2005
    SQL2005 2008配置错误,无法识别的配置节 system.serviceModel machine.config配置文件有问题
    深入浅出学Spring Data JPA
    Java 学习摘要
    JFinal
    spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2184858.html
Copyright © 2011-2022 走看看