查看局域网内所有IP的mac地址

使用原始套接字,在创建接收和发送线程,获取当前网段内所有IP的mac地址

#include 
#include //socket
#include//htons
#include//ETH_P_ALL
#include //close
#include 
#include 
#include //sockaddr_ll
#include 
#include 
void *recvfroms(void *arg);
void *sendtos(void *arg);
int main(int argc, char const *argv[])
{    //创建一个原始套接字   
 int fd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
    //创建一个发送线程和接收线程 
       pthread_t tid,tid1;   
        pthread_create(&tid,NULL,recvfroms,(void *)fd);//接收线程  
          pthread_create(&tid1,NULL,sendtos,(void *)fd);//发送线程  
            //线程分离 
               pthread_detach(tid);  
              pthread_detach(tid1);  
                while (1)  
                {
    		} 
         close(fd); 
            return 0;
            }
            void *recvfroms(void *arg)
            {   
             int fd = (int)arg;  
               //组包  
                 int i = 0;  
                   for ( i = 0; i < 255; i++) 
                      {         
                         unsigned char msg[1500] = {0xff,0xff,0xff,0xff,0xff,0xff,                               		 	0x00,0x0c,0x29,0xca,0xc4,0xf5,                               0x08,0x06,                               0x00,0x01,                               0x08,0x00,                               6,                               4,                               0x00,0x01,                               0x00,0x0c,0x29,0xca,0xc4,0xf5,                               10,0,121,87,                               0x00,0x00,0x00,0x00,0x00,0x00,                                 10,0,121,i                           }; 
                            int len = 42; 
                               //从原始套接字发送数据   
                                //获取网络接口类型  
                                  struct ifreq ethreq;
                                      strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);  
                                        ioctl(fd,SIOCGIFINDEX,&ethreq);  
                                          //指明出去的网络接口   
                                           struct sockaddr_ll sll;  
                                             bzero(&sll,sizeof(sll)); 
                                                sll.sll_ifindex = ethreq.ifr_ifindex;  
                                                  sendto(fd,msg,len,0,(struct sockaddr *)&sll,sizeof(sll));  
                                                    }  
}
void *sendtos(void *arg)
{    
int fd = (int)arg; 
   //接收对方的应答  
     while (1)    {     
        unsigned char recv_buf[1500] = "";  
              recvfrom(fd,recv_buf,sizeof(recv_buf),0,NULL,NULL);   
                   if(ntohs(*(unsigned short *)(recv_buf+12)) == 0x0806)//判断是否为ARP报文
                           {         
                              if(ntohs(*(unsigned short *)(recv_buf+14+6))==2)     
                                     {       
                                              char mac[18] = "";          
                                                    sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x:",\                recv_buf[6],recv_buf[7],recv_buf[8],recv_buf[9],recv_buf[10],recv_buf[11]);
                                                                    char ip[16] = "";                sprintf(ip,"%d.%d.%d.%d",recv_buf[28],recv_buf[29],recv_buf[30],recv_buf[31]);     
                                                                               printf("%s----->%s\n",ip,mac);            }        }    }}

你可能感兴趣的:(网络编程)