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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我自己写了一个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:

********************************************************************/
static void send_msg(int sockfd, struct sockaddr *dest_ad

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值