显示arp缓存的程序

从FreeBSD4.5的arp命令的源代码中获得,可以在macos10.6上编译,运行。

gcc -o arp -Wall arp.c

#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <nlist.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
void
ether_print(u_char * cp)
{
printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
}
void
print_entry(struct sockaddr_dl * sdl, struct sockaddr_inarp * sin, struct rt_msghdr * rtm)
{
const char *host;
struct hostent *hp;
char ifname[IF_NAMESIZE];
hp = 0;
host = "?";
printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
if (sdl->sdl_alen) {
ether_print((u_char *) LLADDR(sdl));
} else {
printf("(incomplete)");
}
if (if_indextoname(sdl->sdl_index, ifname) != NULL) {
printf(" on %s", ifname);
}
if (rtm->rtm_rmx.rmx_expire == 0) {
printf(" permanent");
}
if (sin->sin_other & SIN_PROXY) {
printf(" published (proxy only)");
}
if (rtm->rtm_addrs & RTA_NETMASK) {
sin = (struct sockaddr_inarp *) (ROUNDUP(sdl->sdl_len) + (char *) sdl);
if (sin->sin_addr.s_addr == 0xffffffff) {
printf(" published");
}
if (sin->sin_len != 8) {
printf("(weird)");
}
}
switch (sdl->sdl_type) {
case IFT_ETHER:
printf(" [ethernet]");
break;
case IFT_ISO88025:
printf(" [token-ring]");
break;
case IFT_L2VLAN:
printf(" [vlan]");
break;
}
printf("/n");
}
void
search(u_long addr, void (*action) (struct sockaddr_dl * sdl, struct sockaddr_inarp * sin, struct rt_msghdr * rtm))
{
int mib[6];
size_t needed;
char *lim, *buf, *next;
struct rt_msghdr *rtm;
struct sockaddr_inarp *sin;
struct sockaddr_dl *sdl;

mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = AF_INET;
mib[4] = NET_RT_FLAGS;
mib[5] = RTF_LLINFO;

if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
errx(1, "route-sysctl-estimate");
}
if ((buf = malloc(needed)) == NULL) {
errx(1, "malloc");
}
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
errx(1, "actual retrieval of routing table");
}
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *) next;
sin = (struct sockaddr_inarp *) (rtm + 1);
(char *) sdl = (char *) sin + ROUNDUP(sin->sin_len);
if (addr) {
if (addr != sin->sin_addr.s_addr) {
continue;
}
}
(*action) (sdl, sin, rtm);
}
free(buf);
}
int
main()
{
search(0, print_entry);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mounter625

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

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

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

打赏作者

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

抵扣说明:

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

余额充值