给指定ip回复arp reply报文

来源:互联网 发布:淘宝小当家水浒卡 编辑:程序博客网 时间:2024/06/09 23:36
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <sys/socket.h>  
#include <netinet/in.h>  


struct arp_packet  
{  
    unsigned char hard_type[2];  
    unsigned char pro_type[2];  
    unsigned char hard_len;  
    unsigned char pro_len;  
    unsigned char op[2];  
    unsigned char mac_sender[6];  
    unsigned char ip_sender[4];  
    unsigned char mac_target[6];  
    unsigned char ip_target[4];  
};


struct ethernet_packet  
{  
    unsigned char mac_des[6];  
    unsigned char mac_src[6];  
    unsigned char frame_type[2];  
    struct arp_packet arp;  
    unsigned char pad[18];  
};


int arp_reply(char *dev_name, 
              unsigned char *e_mac_src, 
              unsigned char *e_mac_des, 
              unsigned char *arp_mac_send, 
              unsigned char *arp_ip_send, 
              unsigned char *arp_mac_tgt, 
              unsigned char *arp_ip_tgt)  
{  
    int ret = 0;
    
    struct ethernet_packet ep;
    
    ep.frame_type[0] = 0x08;
    ep.frame_type[1] = 0x06;
    ep.arp.hard_type[0] = 0x00;
    ep.arp.hard_type[1] = 0x01;
    ep.arp.pro_type[0] = 0x08;
    ep.arp.pro_type[1] = 0x00;
    ep.arp.hard_len = 0x6;
    ep.arp.pro_len = 0x4;
    ep.arp.op[0] = 0x00;
    ep.arp.op[1] = 0x02;
    memset(ep.pad, 0, 18);
    
    struct in_addr inaddr;  
    if(0 == inet_aton(arp_ip_tgt, &inaddr))
    {  
        printf("inet_aton error");
        return -1;  
    }    
    memcpy((void*)ep.arp.ip_target, (void*)&inaddr, 4);  
    
    
    if(0 == inet_aton(arp_ip_send, &inaddr))
    {  
        printf("inet_aton error");
        return -2;  
    }
    memcpy((void*)ep.arp.ip_sender, (void*)&inaddr, 4);  


    memcpy(ep.mac_src, e_mac_src, 6);
    memcpy(ep.mac_des, e_mac_des, 6);
    memcpy(ep.arp.mac_sender, arp_mac_send, 6);
    memcpy(ep.arp.mac_target, arp_mac_tgt, 6);
    
    int fd = socket(AF_INET, SOCK_PACKET, htons(0x0806));  
    if(fd < 0) 
    {  
        printf("errno: %d\n",errno); 
        char * mesg = strerror(errno);
        printf("socket, Mesg:%s\n",mesg);
        return -1;
    }
    
    struct sockaddr sa;
    strcpy(sa.sa_data, dev_name);
    
    ret = sendto(fd, &ep, sizeof(ep), 0, &sa, sizeof(sa));
    if(ret == -1)
    {
        printf("errno=%d\n",errno); 
        char * mesg = strerror(errno);
        printf("sendto, Mesg:%s\n",mesg);
        
        close(fd);
        return -1;
    }
    
    close(fd);
    return 0;
}  


int main(int argc, char** argv)  
{  
    char *src_ip = "192.168.150.134";
    char *dst_ip = "192.168.150.188";
    char *dev_name = "eth5";
    
    unsigned char src_mac[6] = {0x00, 0x0c, 0x29, 0x0d, 0xb5, 0xba}; //"00:0c:29:0d:b5:ba"
    unsigned char dst_mac[6] = {0x00, 0x50, 0x56, 0xc0, 0x00, 0x08}; //"192.168.150.188"
    
    printf("start\n");
    
    if(0 != arp_reply(dev_name, src_mac, dst_mac, src_mac, src_ip, dst_mac, dst_ip)) 
    {  
        printf("Usage: marp mac_src mac_des mac_send ip_send mac_tgt ip_tgt\n");  
        return -1;  
    }
    
    printf("end\n");


    return 0;
}
原创粉丝点击