ioctl之获取arp表项

    ioctl是跟内核交互的一种方式,网络设计中广泛使用了ioctl函数来和内核协议栈进行交互。其原型为:
   int ioctl(int d,int request,...)

   

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc, char *argv[])
{
	int s;
	struct arpreq arpreq;
	struct sockaddr_in *addr = (struct sockaddr_in*)&arpreq.arp_pa;
	unsigned char *hw;
	int err = -1;
	if(argc < 2){
		printf("错误的使用方式,格式为:\nmyarp ip(myarp 127.0.0.1)\n");
		return -1;
	}
											/*建立一个数据报套接字*/
	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s < 0)	{
		printf("socket() 出错\n");
		return -1;
	}
	/*填充arpreq的成员arp_pa*/
	addr->sin_family = AF_INET;
	addr->sin_addr.s_addr = inet_addr(argv[1]);
	if(addr->sin_addr.s_addr == INADDR_NONE){
		printf("IP 地址格式错误\n");
	}
	/*网络接口为eth1*/
	strcpy(arpreq.arp_dev, "eth1");
	err = ioctl(s, SIOCGARP, &arpreq);  //<span style="font-family: Arial, Helvetica, sans-serif;">SIOCGARP:获取arp表中的一项,如果没有就会出错</span>
	if(err < 0){								/*失败*/
		printf("IOCTL 错误\n");
		return -1;
	}else{/*成功*/
		hw = (unsigned char*)&arpreq.arp_ha.sa_data;	/*硬件地址*/
		printf("%s:",argv[1]);/*打印IP*/
		printf("%02x:%02x:%02x:%02x:%02x:%02x\n",		/*打印硬件地址*/
				hw[0],hw[1],hw[2],hw[3],hw[4],hw[5]);
	};
	close(s);
	return 0;
}
运行结果:
[root@localhost 12.6.5]# ./ioctl_arp 10.10.10.2
10.10.10.2:00:50:56:fb:8f:da


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Linux C中,可以使用系统调用函数`ioctl()`来查询ARP。下面是一个简单的代码示例,能够获取并打印出ARP的内容: ```c #include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <netinet/if_ether.h> int main() { int sockfd; struct arpreq arpreq; struct sockaddr_in *sin; // 创建一个套接字 sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("socket"); exit(1); } // 设置查询的接口 strncpy(arpreq.arp_dev, "eth0", IFNAMSIZ); // 发送IOCTL命令SIOCGARP获取ARP项 if (ioctl(sockfd, SIOCGARP, &arpreq) < 0) { perror("ioctl"); exit(1); } // 解析得到的ARP项 sin = (struct sockaddr_in *)&(arpreq.arp_pa); char ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &(sin->sin_addr), ip, INET_ADDRSTRLEN); if (arpreq.arp_flags & ATF_COM) { printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\tIP: %s\n", (unsigned char)arpreq.arp_ha.sa_data[0], (unsigned char)arpreq.arp_ha.sa_data[1], (unsigned char)arpreq.arp_ha.sa_data[2], (unsigned char)arpreq.arp_ha.sa_data[3], (unsigned char)arpreq.arp_ha.sa_data[4], (unsigned char)arpreq.arp_ha.sa_data[5], ip); } // 关闭套接字 close(sockfd); return 0; } ``` 上述代码创建一个套接字,并使用`ioctl()`函数发送`SIOCGARP`命令来获取ARP项。接口名"eth0"需要根据需要修改为实际的网络接口名称。然后,通过解析返回的结果,可以得到MAC地址和IP地址的信息。 需要注意的是,为了编译该程序,需要添加以下头文件的引用: ```c #include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <netinet/if_ether.h> ``` 此外,编译链接时需要加上`-lsocket -lnsl`参数,因为这些函数位于`libnsl`和`libsocket`库中。 这段代码只是一个简单的示例,可以作为开始探索`ioctl()`函数和ARP的参考。在实际应用中,可能需要结合其他函数和标志来进一步处理ARP的查询和解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的幸运儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值