ping不使用路由时,内核不更新arp高速缓存

本文档展示了如何编写一个自定义的ping函数,用于获取局域网内IP的MAC地址。作者遇到的问题是,当设置套接字不路由时,内核在收到ARP响应后不会更新ARP高速缓存。代码中包含了发送ICMP消息、计算校验和等功能的实现。
摘要由CSDN通过智能技术生成

我自己写了一个ping函数,用来ping局域网内的ip,并获得其mac地址,为此必须从arp高速缓存中读取。

由于我将发送icmp包的套接口设置为不路由,导致内核在收到arp应答后,不更新arp高速缓存

 setsockopt(sockfd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
     sizeof(on))) {

我的代码如下:

 /******************************************************************************
Copyright (C), 2006-2008, GuangDong Newstart Technology Service Co., Ltd.
FileName: ping_float.c
Author:zhoujing      
Version :2.1         
Date:2007-05-28
Description: this module is used to ping the other node in cluster.
History:        
      <author>   <time>      <version >   <desc>
      zhoujing    07/05/28      2.1               
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <fcntl.h>

#include "clplumbing/ping_float.h"

#ifdef __sun__
#define INADDR_NONE  ((unsigned long int) 0xffffffff)
#endif

#define TRUE  1
#define FALSE  0
static void send_msg(int sockfd, struct sockaddr *dest_addr, pid_t pid,
       int *pnsent);
static int proc_msg(char *, ssize_t, struct timeval *, pid_t);
static unsigned int readloop(struct sockaddr *dest_addr, int times,
        char *src_addr);
static void tv_sub(struct timeval *, struct timeval *);
static unsigned short in_cksum(unsigned short *, int);

#define BUFSIZE   1500  /*buffer size */

/********************************************************************
Function:    send_msg
Description:
        This function is used to send icmp messages
Parameter Description:
sockfd 
   the socket descriptor for send data
dest_addr
         point to the destination address
Return Value:

********************************************************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你能通过 `ping` 命令成功地与其他设备进行通信,但使用 `arp -a` 命令无法查到其他设备的 IP 和 MAC 地址,可能是以下原因之一: 1. 本地网络中没有其他设备的 ARP 缓存 `arp -a` 命令显示的是本地设备的 ARP 缓存,如果其他设备之前没有与本地设备通信,那么本地设备就不会有其他设备的 ARP 缓存。 可以通过 `ping` 命令或者其他网络应用程序与其他设备进行通信,这样就可以在本地设备的 ARP 缓存中生成其他设备的 ARP 记录。 2. 其他设备的 ARP 缓存已过期 ARP 缓存中存储的是 IP 地址和 MAC 地址的映射关系,如果其他设备的 MAC 地址发生了变化,或者 ARP 缓存已经过期,那么本地设备就无法通过 `arp -a` 命令查到其他设备的 ARP 记录。 可以使用 `arp -d` 命令清除本地设备的 ARP 缓存,然后再次与其他设备进行通信,这样就可以生成新的 ARP 记录。 3. 其他设备的 ARP 请求被阻止 在网络中,ARP 请求是广播发送的,其他设备可以通过监听广播的方式获取 ARP 请求,并回复 ARP 响应。如果其他设备的 ARP 请求被阻止,那么本地设备就无法获取其他设备的 MAC 地址。 可以检查其他设备的网络配置和防火墙设置,确保其他设备能够发送和接收 ARP 请求。 总之,如果你能通过 `ping` 命令成功地与其他设备进行通信,但使用 `arp -a` 命令无法查到其他设备的 IP 和 MAC 地址,可以检查其他设备的 ARP 缓存是否已过期,或者其他设备的 ARP 请求是否被阻止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值