大家好,最近在做vxworks5.4到6.5的移植,需要得到对端的MAC地址,在5.4中系统提供了API etherAddrResolve()来获得相应IP的mac地址,通过查资料说在后面的版本中用arpResolve()来代替了,到6.5,arpResolve()也没有了,在文档platform_general_vxworks_migration_guide_3.5里有这么一句话:“Use the ioctl( ) function SIOCPARP with the struct arpreq as an argument.”但我查了6.5的代码,没有SIOCPARP,在网上搜了下,有用SIOCGARP的,我就参考写了下面的代码,但没有成功,报EADDRNOTAVAIL(0x31)的错,大家帮忙看看,不甚感激!
int getpeermac_by_ip(char *ipaddr, char* buf)
{
STATUS sts;
int sockfd;
int i;
unsigned char *ptr;
struct arpreq myarpreq;
struct in_addr iaddr;
struct sockaddr_in *arptr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket error");
return -1;
}
memset(&myarpreq,0,sizeof(myarpreq));
arptr = (struct sockaddr_in *)&myarpreq.arp_pa;
arptr->sin_family = AF_INET;
myarpreq.arp_flags = ATF_PUBL;
myarpreq.arp_ha.sdl_family = AF_UNSPEC;
iaddr.s_addr = inet_addr(ipaddr);
printf("s_addr inet_addr: %x\n", iaddr.s_addr);
memcpy((char *)&arptr->sin_addr, (char *)&iaddr, sizeof(struct in_addr));
if (ioctl(sockfd, SIOCGARP, &myarpreq)<0)
perror("ioctl SIOCGARP");
ptr = (unsigned char *)&myarpreq.arp_ha;
for (i=0; i<20; i++)
printf("%x ", *ptr);
return 0;
}
void mactest(void)
{
char buff[6] = {0};
int i;
getpeermac_by_ip("172.19.232.73", buff);
printf("\nmac address: \n");
for (i=0; i<6; i++)
printf("%3x ", buff[i]);
}