#include
#include
#include
#include
#include
#include
int Main(char *ip,char * mac)
{
int sockfd;
unsigned char *ptr;
struct arpreq arp_req;
struct sockaddr_in *sin;
struct sockaddr_storage ss;
char addr[50];
memset(addr,0,sizeof(addr));
memset(&ss,0,sizeof(ss));
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd < 0)
{
debugf("socket error\n");
return -1;
}
memset(&arp_req,0,sizeof(arp_req));
sin = (struct sockaddr_in *) &ss;
arp_req.arp_pa.sa_family = AF_INET;
sin->sin_family = AF_INET;
if(inet_pton(AF_INET,ip,&(sin->sin_addr)) <= 0)
{
debugf("inet_aton error ...\n");
close(sockfd);
return -1;
}
sin = (struct sockaddr_in *) &arp_req.arp_pa;
memcpy(sin,&ss,sizeof(struct sockaddr_in));
strcpy(arp_req.arp_dev,"eth0");
strcpy(arp_req.arp_dev,InterfaceName);
arp_req.arp_ha.sa_family = AF_UNSPEC;
if(ioctl(sockfd,SIOCGARP,&arp_req) < 0)
{
debugf("ioctl SIOCGARP: error\n");
close(sockfd);
return -1;
}
ptr = (unsigned char *) arp_req.arp_ha.sa_data;
// debugf("%x:%x:%x:%x:%x:%x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
sprintf(mac,"%x:%x:%x:%x:%x:%x",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
// debugf("Mac = %s ... \n",mac);
close(sockfd);
return 0;
}
阅读(1168) | 评论(2) | 转发(0) |