调用libnet 广播arp包

来源:互联网 发布:赛鸽记录软件 编辑:程序博客网 时间:2024/05/29 14:10
//编译方式gcc arp.c -lnet//arp.c#include <stdio.h>#include <libnet.h>#define MAC_ADDR_LEN 6#define IP_ADDR_LEN 4//向同一网络内所有机器发送ARP REPLY包,告诉他们,23.23.23.2Z在00:df:17:17:17:f2那里int main(int argc, char *argv[]){    printf("into arp\n");    libnet_t *l = NULL; // libnet context    int i = 0;    char *device = "eth0";    char err_buf[LIBNET_ERRBUF_SIZE];    libnet_ptag_t p_tag;    unsigned char src_mac[MAC_ADDR_LEN]//自己机器的MAC地址f4:6d:04:65:48:18        = {0xf4, 0x6d, 0x04, 0x65, 0x48, 0x18};    unsigned char dest_mac[MAC_ADDR_LEN]        = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};    char *src_ip_str = "192.168.100.91";    unsigned long src_ip, dest_ip = 0;    src_ip = libnet_name2addr4(l, src_ip_str, LIBNET_RESOLVE);        // create libnet environment    l = libnet_init(LIBNET_LINK_ADV, device, err_buf);    if(!l)        printf("libnet_init error\n"), exit(1);    //构造ARP数据包    p_tag = libnet_build_arp( // construct arp packet        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表 IP-MAC        (u_int8_t*)&src_ip, // source ip addr        (u_int8_t*)dest_mac, // dest mac addr        (u_int8_t*)&dest_ip, // dest ip addr        NULL, // payload        0, // payload length        l, // libnet context        0 //0 stands to build a new one    );    if(-1 == p_tag)        printf("libnet_build_arp error\n"), exit(1);    //以太网头部    p_tag = libnet_build_ethernet( // create ethernet header        (u_int8_t*)dest_mac, // dest mac addr        (u_int8_t*)src_mac, // source mac addr这里说明你链路层的源MAC地址,如果改了可以伪装自己        ETHERTYPE_ARP, // protocol type     NULL, // payload        0, // payload length        l, // libnet context        0 // 0 to build a new one    );    if(-1 == p_tag)        printf("libnet_build_ethernet error!\n"), exit(1);    int res;        for(;;){      if(-1 == (res = libnet_write(l))){        printf("libnet_write error!\n");exit(1);      }      printf("arp packet has been sent\n");      sleep(1);    }    libnet_destroy(l);    printf("out of arp\n");    return 0;}


0 0