读书人

哪位高手成功使用过SIOCGARP获取过ARP

发布时间: 2013-09-28 10:01:20 作者: rapoo

谁成功使用过SIOCGARP获取过ARP的一个表项?
如题,在 http://www3.amherst.edu/~jwmanly/resnet97/getarp.c 找了个程序:

/*  getarp.c -- This simple program uses an IOCTL socket call to read an entry  */
/* from the ARP cache of the local machine. Original source unknown. */
/* Usage: getarp <ip-number> */
/* Example: getarp 148.85.2.1 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>

/* arp_flags and at_flags field values */
#defineATF_INUSE0x01/* entry in use */
#define ATF_COM0x02/* completed entry (enaddr valid) */
#defineATF_PERM0x04/* permanent entry */
#defineATF_PUBL0x08/* publish entry (respond for other host) */
#defineATF_USETRAILERS0x10/* has requested trailers */
#defineATF_PROXY0x20/* Do PROXY arp */

main (int argc, char *argv[]) {

int s;
struct arpreq arpreq;
struct sockaddr_in *sin;
unsigned char *eap;

if (argc < 2) exit(0);

memset(&arpreq, 0, sizeof(arpreq));

sin = (struct sockaddr_in *) &arpreq.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(argv[1]);

s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
perror("socket");
exit(0);
}
if (ioctl(s, SIOCGARP, &arpreq) < 0) {
perror("ioctl");
exit(0);
}
printf("IP address: %s\n", inet_ntoa(sin->sin_addr));

if (arpreq.arp_flags & ATF_COM) {
eap = (unsigned char *) &arpreq.arp_ha.sa_data[0];
printf("Ethernet address: %02X:%02X:%02X:%02X:%02X:%02X",
eap[0], eap[1], eap[2], eap[3], eap[4], eap[5]);
if (arpreq.arp_flags & ATF_PERM) printf(" PERM");
if (arpreq.arp_flags & ATF_PUBL) printf(" PUBLISHED");
if (arpreq.arp_flags & ATF_USETRAILERS) printf(" TRAILERS");
if (arpreq.arp_flags & ATF_PROXY) printf(" PROXY");
printf("\n");
} else {
printf("*** INCOMPLETE ***\n");
}
close(s);
exit(0);
}

用arp -n命令查看我机子的arp表,只有192.168.1.1这个地址,于是我输入 “getarp 192.168.1.1”,可是却返回错误信息 ioctl: No such device。难道是这个程序有问题吗?我想问下谁试过用SIOCGARP成功获取过arp的一个表项?
[解决办法]
你没有写设备选项
在44行上加上strcpy(arpreq.arp->dev, "eth0");试试

读书人网 >UNIXLINUX

热点推荐