栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

ARP 探测 C语言实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ARP 探测 C语言实现

#include 
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include  
#include 
#include 
#include 
#include   
#include  
#include "common_ifaddrs.h"
#include "prod_common_wan.h"


#define    FAILURE   -1
#define    SUCCESS    0
#define    IP_LENGTH  4
#define    MAC_LENGTH 6
#define    ARP_DELAY  20*10000

static unsigned char g_arp_src_ip[IP_LENGTH]   = {0};                						    //要检测的主机IP地址  
static unsigned char g_arp_src_mac[MAC_LENGTH] = {0};   										//要检测的主机的MAC地址  
static unsigned char g_arp_dst_ip[IP_LENGTH]   = {0};                    					    //目标IP地址  
static unsigned char g_arp_dst_mac[MAC_LENGTH] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };    	//ARP广播地址  
static char *g_Network_card_name = NULL;

static struct  tar_arp_packet
{    
    struct ether_header  eh;    
    struct ether_arp     arp;    
};

static int send_arp(int sockfd, struct sockaddr_ll *peer_addr); 
static int recv_arp(int sockfd, struct sockaddr_ll *peer_addr);


int get_wan_ip()
{
    int i = 0,j = 0;
    char  *str = NULL;
    unsigned char num[4]={0};
    char tmp[3];
    int n = 0,value=0;
    
    WAN_ID_NUM_ENUM  wan_id = 0;
    WAN_IP_INFO_STRU ip_info;
    
    memset(&ip_info,0x0,sizeof(ip_info));
    if(currently_lte_working_mode())
    {
        wan_id = LTE_WANID;             
    }
    else
    {
        wan_id = ETH_WANID;
    }
    
    file_wan_ip_info_get(wan_id, &ip_info);
    if(0 == strlen(ip_info.wan_ip))
    {
        printf("plaese check internet connectionn");
        return -1;
    }
    //将字符串形式的IP转化为 unsigned char 型
    str = ip_info.wan_ip;
    for(i=0;i<4;i++)
    {
        // 每次最多转化3位字符
        for( j = 0;j<3;j++)
        {
          if((*str == '.') || (*str == ''))
          break;
          tmp[j] = *str;
          value = value * 10 + tmp[j] - '0';
          str += 1;
        }

        num[n] = value;
        value = 0;

        if(*str == '')
        {
          break;
        }
        else
        {
          str += 1;
          n += 1;
        }
    };
      
    for( i = 0;i UDP套接字
	if ((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1)
	{
		perror("socket failed");
		return -1;
	}
	
	//3,配置参数
	memset(&peer_addr, 0, sizeof(peer_addr));  
    peer_addr.sll_family = PF_PACKET;  

    struct ifreq req;
	bzero(&req, sizeof(struct ifreq));
    strcpy(req.ifr_name, g_Network_card_name);  

    if(ioctl(sockfd, SIOCGIFINDEX, &req) != 0)
    {
    	perror("ioctl()");  
    }

    peer_addr.sll_ifindex = req.ifr_ifindex;  
    peer_addr.sll_protocol = htons(ETH_P_ARP);

	get_wan_mac();
    get_wan_ip();
	while(1)
    {
		if(0 == get_wan_ip())
		{
			break;
		}
		else
	    {
			sleep(2);
	    }
	}

    //初始化目的IP
    for( i = 0;i<4;i++)
    {
        g_arp_dst_ip[i] = g_arp_src_ip[i];
    }
    printf("arp test start**********************************************************n");
	
    for(i=1;i<255;i++)
    {
        g_arp_dst_ip[3] = i;
        //arp发送
        rtval = send_arp(sockfd, &peer_addr);  
		
        times = 3;
        while(times--)
        {
            //arp接收
            rtval = recv_arp(sockfd, &peer_addr);
            if(FAILURE == rtval)
            { 
                usleep(ARP_DELAY);
                rtval = send_arp(sockfd, &peer_addr);
                continue;
            }
            else //success
            {
                break;
            }
        }
        
        usleep(ARP_DELAY);
    }
	
	close(sockfd);
    printf("arp test end*************************************************************n");
   
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号