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