typedef void (* t_pf_int_handler)()关于这个

本文通过具体实例详细解析了函数指针的概念及应用,包括如何定义函数指针类型、使用函数指针调用不同函数的过程,并解释了其在实际编程中的作用。

 在《自己动手》P212  代码5-58 有如下代码:

PRIVATE  void  init_idt_desc(unsigned  char  vector, t_8 desc_type, t_pf_int_handler handler, unsigned  char  privilege);

来关注下第三个参数 t_pf_int_handler  handler  ,我们来看看 t_pf_int_handler 它的定义 (在 type.h 中)

typedef  void  ( *  t_pf_int_handler)();

传说中的函数指针么。我不喜欢来啰哩啰嗦地大段砸概念,还是再来用一个实例看看究竟函数指针有什么用?

 

#include   < stdio.h >  

typedef 
void  ( * MyFuncPtr)( const   char * );  

void  hello( const   char *  s) 

  printf(
" hello, %s " ,s);  
}
 

void  shakehand( const   char *  s) 

  printf(
" let's shake hands, %s " , s);  
}
 

void  printmessage(MyFuncPtr ptr,  const   char *  s)  

   (
* ptr)(s); 
}
 

int  main() 

  
char   *  s  =   " peng_peng "

  MyFuncPtr pf
=  hello
  printmessage(pf, s); 
   
  pf
=  shakehand;  
  printmessage(pf, s); 
   
  
return   0
}
 

 

 主要关注下这三个方面:

1、我们在代码开头就定义了一个函数指针类型typedef void (*MyFuncPtr)(const char*)    而且这种类型似乎代表void,如果你觉得这个定义很碍眼,我们可以这样看看 typedef void Myfun  ——用Myfun这个符号替代void 这样好理解了吧?  那上面那段代码不就是用 我们定义的函数指针类型代表void么

2、接下来我们定义了两个打印字符串的函数,hello()shakehand() ,然后又是一个函数 void  printmessage(MyFuncPtr ptr,  const   char *  s)    我们先妥协下,暂时不管这个函数,看看主函数里面,我们定义了一个变量  MyFuncPtr pf  ,请注意这个不是一个普通变量,而是一个指针,而且还是一个指向void这种函数的指针,即是我们指向的是一个void函数的入口地址 ,接下来的东西应该比较好懂了,我们先用pf这个函数的指针指向了hello这个函数,并且把这个指针作为参数传递给了printmessage 这个函数,此时我们再来看看printmessage 这个函数体吧 仅仅一句话 ——(* ptr)(s);    取得的函数指针加上了一个*号,这个表示它指向的函数实体了,也就是我们可以用hello这个函数替换掉(*ptr), 上述内容变为hello(s) , 这种形式就很好理解了吧~


 我们再来回到原函数,按照上面的理解,t_pf_int_handler handler handler应该是一个指针,而且还是一个指向void类型函数的指针。我们来看看函数的调用,以验证我们的想法:

init_idt_desc(INT_VECTOR_DIVIDE,    DA_386IGate,   divide_error,        PRIVILEGE_KRNL);

第三个参数是一个函数名(代码如下),函数名就代表函数的入口地址,也可以理解为指向这个函数的指针了

divide_error:
    push    0xFFFFFFFF    
;  no err code

    push     0          ;  vector_no    = 0
    jmp    exception

 divide_error 这个函数是用汇编写的,不过原理仍一样,表示这个函数的入口地址,其实汇编代码更清楚表示出了divide_error 这个函数名的实质。看看上面代码,不就是一个子程序开始的标号么?

最后想说下的是 typedef void (*MyFuncPtr)(const char*)    并没有定义函数 只是声明了一个类型而已!

帮我解读这六段代码,我是初学者,尽量详细并补充相关知识 #ifndef _ARP_RECV_H #define _ARP_RECV_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <net/ethernet.h> /* 以太网帧首部长度 / #define ETHER_HEADER_LEN sizeof(struct ether_header) / 整个arp结构长度 / #define ETHER_ARP_LEN sizeof(struct ether_arp) / 以太网 + 整个arp结构长度 / #define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN / IP地址长度 */ #define IP_ADDR_LEN 4 void err_exit(const char *err_msg); #ifndef LOCAL #define LOCAL static #endif #endif。。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <net/ethernet.h> #include"arp_recv.h" #include “nvmp_utils.h” #include “nsd_common.h” #include “libds.h” #include “libdms.h” #include “flashio.h” #include “dms_tool.h” LOCAL void err_exit(const char *err_msg) { perror(err_msg); exit(1); } LOCAL void arp_recv_main() { struct ether_arp *arp_packet; char buf[ETHER_ARP_PACKET_LEN]; int sock_raw_fd, ret_len, i; if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) { err_exit(“socket()”); } while (1) { bzero(buf, ETHER_ARP_PACKET_LEN); ret_len = recv(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0); if (ret_len > 0) { /* 剥去以太头部 */ arp_packet = (struct ether_arp )(buf + ETHER_HEADER_LEN); / arp操作码为代表arp应答 */ if (ntohs(arp_packet->arp_op) == 2) { printf(“====ARP replay\n”); printf(“Sender IP address: “); for (i = 0; i < IP_ADDR_LEN; i++) { printf(”%u”, arp_packet->arp_spa[i]); if(i != (IP_ADDR_LEN-1)) { printf(“.”); } } printf(“\nSender MAC address: “); for (i = 0; i < ETH_ALEN; i++) { printf(”%02x”, arp_packet->arp_sha[i]); if(i != (ETH_ALEN-1)) { printf(“😊; } } printf(”\nTarget IP address: “); for (i = 0; i < IP_ADDR_LEN; i++) { printf(”%u", arp_packet->arp_tpa[i]); if(i != (IP_ADDR_LEN-1)) { printf(“.”); } } printf(“\nTarget MAC address: “); for (i = 0; i < ETH_ALEN; i++) { printf(”%02x”, arp_packet->arp_tha[i]); if(i != (ETH_ALEN-1)) { printf(“😊; } } printf(”\n"); } } } close(sock_raw_fd); } NSD_INIT(arp_recv_main);。。#ifndef _ARP_REQUEST_H #define _ARP_REQUEST_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/ethernet.h> #include <net/if_arp.h> #include <net/if.h> #include <netpacket/packet.h> /* 以太网帧首部长度 / #define ETHER_HEADER_LEN sizeof(struct ether_header) / 整个arp结构长度 / #define ETHER_ARP_LEN sizeof(struct ether_arp) / 以太网 + 整个arp结构长度 / #define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN / IP地址长度 / #define IP_ADDR_LEN 4 / 广播地址 */ #define BROADCAST_ADDR void err_exit(const char *err_msg); struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip); void arp_request(const char *if_name, const char *dst_ip); #endif。。/* Copyright© * file arp_request.c brief This is a work of sending arp request. author Zhou Shijun version 1.0.1 date 24Aug28 history */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/ethernet.h> #include <net/if_arp.h> #include <net/if.h> #include <netpacket/packet.h> #include"arp_request.h" void err_exit(const char *err_msg) { perror(err_msg); exit(1); } /* 填充arp包 */ struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip) { struct ether_arp *arp_packet; struct in_addr src_in_addr, dst_in_addr; unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; /* IP地址转换 / inet_pton(AF_INET, src_ip, &src_in_addr); inet_pton(AF_INET, dst_ip, &dst_in_addr); / 整个arp包 */ arp_packet = (struct ether_arp *)malloc(ETHER_ARP_LEN); arp_packet->arp_hrd = htons(ARPHRD_ETHER); arp_packet->arp_pro = htons(ETHERTYPE_IP); arp_packet->arp_hln = ETH_ALEN; arp_packet->arp_pln = IP_ADDR_LEN; arp_packet->arp_op = htons(ARPOP_REQUEST); memcpy(arp_packet->arp_sha, src_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_tha, dst_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_spa, &src_in_addr, IP_ADDR_LEN); memcpy(arp_packet->arp_tpa, &dst_in_addr, IP_ADDR_LEN); return arp_packet; } /* arp请求 */ void arp_request(const char *if_name, const char *dst_ip) { struct sockaddr_ll saddr_ll; struct ether_header *eth_header; struct ether_arp *arp_packet; struct ifreq ifr; char buf[ETHER_ARP_PACKET_LEN]; unsigned char src_mac_addr[ETH_ALEN]; unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; char *src_ip; int sock_raw_fd, ret_len, i; if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) { err_exit(“socket()”); } bzero(&saddr_ll, sizeof(struct sockaddr_ll)); bzero(&ifr, sizeof(struct ifreq)); /* 网卡接口名 / memcpy(ifr.ifr_name, if_name, strlen(if_name)); / 获取网卡接口索引 / if (ioctl(sock_raw_fd, SIOCGIFINDEX, &ifr) == -1) { err_exit(“ioctl() get ifindex”); } saddr_ll.sll_ifindex = ifr.ifr_ifindex; saddr_ll.sll_family = PF_PACKET; / 获取网卡接口IP */ if (ioctl(sock_raw_fd, SIOCGIFADDR, &ifr) == -1) { err_exit(“ioctl() get ip”); } src_ip = inet_ntoa(((struct sockaddr_in )&(ifr.ifr_addr))->sin_addr); printf(“local ip:%s\n”, src_ip); / 获取网卡接口MAC地址 / if (ioctl(sock_raw_fd, SIOCGIFHWADDR, &ifr)) { err_exit(“ioctl() get mac”); } memcpy(src_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); printf(“local mac”); for (i = 0; i < ETH_ALEN; i++) { printf(“:%0x”, src_mac_addr[i]); } printf(“\n”); bzero(buf, ETHER_ARP_PACKET_LEN); / 填充以太首部 */ eth_header = (struct ether_header )buf; memcpy(eth_header->ether_shost, src_mac_addr, ETH_ALEN); memcpy(eth_header->ether_dhost, dst_mac_addr, ETH_ALEN); eth_header->ether_type = htons(ETHERTYPE_ARP); / arp包 / arp_packet = fill_arp_packet(src_mac_addr, src_ip, dst_ip); memcpy(buf + ETHER_HEADER_LEN, arp_packet, ETHER_ARP_LEN); / 发送请求 */ ret_len = sendto(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0, (struct sockaddr *)&saddr_ll, sizeof(struct sockaddr_ll)); if (ret_len > 0) { printf(“Send successfully!\n”); } close(sock_raw_fd); } LOCAL void arp_request_main(int argc, const char *argv[]) { if (argc != 3) { printf(“usage:%s device_name dst_ip\n”, argv[0]); exit(1); } arp_request(argv[1], argv[2]); return 0; } NSD_INIT(arp_request_main);。。#ifndef _ARP_SCAN_H #define _ARP_SCAN_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <netpacket/packet.h> #include <sys/ioctl.h> #include <net/if.h> #include <time.h> #include <pthread.h> /* — 宏定义: 参数默认值 — / #define ETHER_HEADER_LEN sizeof(struct ether_header) #define ETHER_ARP_LEN sizeof(struct ether_arp) #define ETHER_ARP_PACKET_LEN (ETHER_HEADER_LEN + ETHER_ARP_LEN) #define BROADCAST_ADDR {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} #define IP_ADDR_LEN 4 #define MAX_ARP_ENTRIES 100 / — 数据结构定义 — */ typedef struct { unsigned char mac[ETH_ALEN]; char ip[INET_ADDRSTRLEN]; time_t last_seen; } arp_entry_t; typedef struct { int enabled; /* 功能开关*/ int scan_interval; /* 扫描周期(秒)/ int entry_lifetime; / 有效期(秒)/ int packet_interval; / 发包间隔(毫秒)/ char start_ip[INET_ADDRSTRLEN]; char end_ip[INET_ADDRSTRLEN]; arp_entry_t arp_entries[MAX_ARP_ENTRIES]; int entry_count; / 当前ARP条目数量*/ pthread_mutex_t lock; /* 互斥锁*/ } arp_config_t; arp_config_t arp_config = { .enabled = 1, /* 功能开关开启*/ .scan_interval = 60, /* 扫描周期为60秒*/ .entry_lifetime = 300, /* 有效期为300秒*/ .packet_interval = 100, /* 发包间隔为100毫秒*/ .start_ip = “192.168.1.100”, /* 起始IP*/ .end_ip = “192.168.1.200”, /* 结束IP*/ .entry_count = 0 /初始化ARP条目数量/ }; /* — 函数定义 — */ void err_exit(const char *err_msg); struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip); void add_arp_entry(const char *ip, const unsigned char *mac); void cleanup_arp_entries(); void arp_scan(const char *if_name); LOCAL int arp_scan_init(); LOCAL int arp_scan_check(); LOCAL int arp_scan_start(); LOCAL int arp_scan_reload(DS_MSG *msg); LOCAL void arp_scan_main(); #ifndef LOCAL #define LOCAL static #endif #endif。。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <netpacket/packet.h> #include <sys/ioctl.h> #include <net/if.h> #include <time.h> #include <pthread.h> #include “arp_scan.h” void err_exit(const char *err_msg) { perror(err_msg); exit(1); } struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip) { struct ether_arp *arp_packet = (struct ether_arp *)malloc(ETHER_ARP_LEN); unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; struct in_addr src_in_addr, dst_in_addr; inet_pton(AF_INET, src_ip, &src_in_addr); inet_pton(AF_INET, dst_ip, &dst_in_addr); arp_packet->arp_hrd = htons(ARPHRD_ETHER); arp_packet->arp_pro = htons(ETHERTYPE_IP); arp_packet->arp_hln = ETH_ALEN; arp_packet->arp_pln = IP_ADDR_LEN; arp_packet->arp_op = htons(ARPOP_REQUEST); memcpy(arp_packet->arp_sha, src_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_tha, dst_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_spa, &src_in_addr, IP_ADDR_LEN); memcpy(arp_packet->arp_tpa, &dst_in_addr, IP_ADDR_LEN); return arp_packet; } void add_arp_entry(const char *ip, const unsigned char *mac) { pthread_mutex_lock(&arp_config.lock); if (arp_config.entry_count < MAX_ARP_ENTRIES) { strcpy(arp_config.arp_entries[arp_config.entry_count].ip, ip); memcpy(arp_config.arp_entries[arp_config.entry_count].mac, mac, ETH_ALEN); arp_config.arp_entries[arp_config.entry_count].last_seen = time(NULL); arp_config.entry_count++; } pthread_mutex_unlock(&arp_config.lock); } void cleanup_arp_entries() { pthread_mutex_lock(&arp_config.lock); time_t now = time(NULL); for (int i = 0; i < arp_config.entry_count; i++) { if (now - arp_config.arp_entries[i].last_seen > arp_config.entry_lifetime) { // 删除过期条目 for (int j = i; j < arp_config.entry_count - 1; j++) { arp_config.arp_entries[j] = arp_config.arp_entries[j + 1]; } arp_config.entry_count–; i–; // 调整索引 } } pthread_mutex_unlock(&arp_config.lock); } void arp_scan(const char *if_name) { struct sockaddr_ll saddr_ll; struct ether_header *eth_header; struct ether_arp *arp_packet; struct ifreq ifr; char buf[ETHER_ARP_PACKET_LEN]; unsigned char src_mac_addr[ETH_ALEN]; char src_ip[INET_ADDRSTRLEN]; int sock_raw_fd; if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) err_exit(“socket()”); memset(&saddr_ll, 0, sizeof(struct sockaddr_ll)); memset(&ifr, 0, sizeof(struct ifreq)); memcpy(ifr.ifr_name, “eth0”, strlen(“eth0”)); // 使用默认接口名 if (ioctl(sock_raw_fd, SIOCGIFINDEX, &ifr) == -1) err_exit(“ioctl() get ifindex”); saddr_ll.sll_ifindex = ifr.ifr_ifindex; saddr_ll.sll_family = PF_PACKET; if (ioctl(sock_raw_fd, SIOCGIFADDR, &ifr) == -1) err_exit(“ioctl() get ip”); strcpy(src_ip, inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr)); if (ioctl(sock_raw_fd, SIOCGIFHWADDR, &ifr) == -1) err_exit(“ioctl() get mac”); memcpy(src_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); while (arp_config.enabled) { char target_ip[INET_ADDRSTRLEN]; for (int i = inet_addr(arp_config.start_ip); i <= inet_addr(arp_config.end_ip); i++) { struct in_addr addr; addr.s_addr = i; strcpy(target_ip, inet_ntoa(addr)); memset(buf, 0, ETHER_ARP_PACKET_LEN); eth_header = (struct ether_header *)buf; memcpy(eth_header->ether_shost, src_mac_addr, ETH_ALEN); memcpy(eth_header->ether_dhost, BROADCAST_ADDR, ETH_ALEN); eth_header->ether_type = htons(ETHERTYPE_ARP); arp_packet = fill_arp_packet(src_mac_addr, src_ip, target_ip); memcpy(buf + ETHER_HEADER_LEN, arp_packet, ETHER_ARP_LEN); sendto(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0, (struct sockaddr *)&saddr_ll, sizeof(struct sockaddr_ll)); free(arp_packet); printf(“ARP request sent to %s\n”, target_ip); usleep(arp_config.packet_interval * 1000); // 发包间隔 } cleanup_arp_entries(); // 清理过期的ARP条目 sleep(arp_config.scan_interval); // 等待下一次扫描 } close(sock_raw_fd); } LOCAL int arp_scan_init() { if (0 == ds_read(ARP_DATA_PATH, &arp_config_t, sizeof(arp_config_t))) { return SLP_ESYSTEM; } /* Initialize socket / if ((sockfd = socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(ETH_P_ARP))) < 0) { perror(“socket”); return ERROR; } / Initialize ip_mac_table */ U8 table_len = arp_config_t.end_ip[3] - arp_config_t.start_ip[3] + 1; ip_mac_table = (ARP_IPMAC_TABLE *)malloc(sizeof(ARP_IPMAC_TABLE) * table_len); struct timeval cur_time; gettimeofday(&cur_time, NULL); for (int i = 0; i < table_len; i++) { ip_mac_table[i].renew_time = cur_time; ip_mac_table[i].device_is_exist = 0; memset(ip_mac_table[i].mac, 0, ETH_ALEN); } msg_attach_handler(MSGID_DMS_CMD, arp_call_handle); return OK; } LOCAL int arp_scan_check() { if (arp_config_t.end_ip[2] != sender_ip[2] || arp_config_t.start_ip[2] != sender_ip[2] || arp_config_t.end_ip[3] <= arp_config_t.start_ip[3]) { ARP_DEBUG(“Invalid IP address range, please check.\n”); return ERROR; } return OK; } LOCAL int arp_scan_start() { /* Start address expiration check thread */ pthread_create(&time_tid, NULL, check_table_renew_time, NULL); pthread_detach(time_tid); /* Scan loop */ while (scanning_flag) { scan_once(); usleep(arpco.scan_interval); } return OK; } LOCAL int arp_scan_reload(DS_MSG msg) { / Stop scanning */ scanning_flag = 0; if (ds_path_id_exist(msg->id, msg->num, ARP_DATA_PATH)) { arp_config_t arp_data; memset(&arp_data, 0, sizeof(arp_config_t)); if (0 == ds_read(ARP_DATA_PATH, (U8 )&arp_data, sizeof(arp_config_t))) { ARP_ERROR(“Read arp data ERROR”); return ERROR; } / Reload params / memcpy(arp_config_t.start_ip, arp_data.start_ip, ARP_IPV4_LEN); memcpy(arp_config_t.end_ip, arp_data.end_ip, ARP_IPV4_LEN); arp_config_t.scan_interval = arp_data.scan_interval; arp_config_t.packet_interval = arp_data.packet_interval; arp_config_t.entry_lifetime = arp_data.entry_lifetime; / Cancel old checking thread / pthread_cancel(time_tid); / Realloc ip_mac_table */ U8 table_len = arp_config_t.end_ip[3] - arp_config_t.start_ip[3] + 1; ARP_IPMAC_TABLE *new_table; new_table = (ARP_IPMAC_TABLE )realloc(ip_mac_table, sizeof(ARP_IPMAC_TABLE) * table_len); if (NULL == new_table) { ARP_ERROR(“Realloc ipmac_table ERROR”); free(ip_mac_table); ip_mac_table = NULL; return ERROR; } ip_mac_table = new_table; struct timeval cur_time; gettimeofday(&cur_time, NULL); for (int i = 0; i < table_len; i++) { ip_mac_table[i].renew_time = cur_time; ip_mac_table[i].device_is_exist = 0; memset(ip_mac_table[i].mac, 0, ETH_ALEN); } / Restart checking thread / / Start scanning */ scanning_flag = 1; arp_scan_start(); return OK; } } LOCAL void arp_scan_main() { DS_OPT_DESC main_options[] = { DS_SWITCH_OPT(arp_config_t, enabled, OPT_FLAG_NORM), DS_STR_OPT(arp_config_t, start_ip, OPT_FLAG_NORM), DS_STR_OPT(arp_config_t, end_ip, OPT_FLAG_NORM), DS_S32_OPT(arp_config_t, scan_interval, OPT_FLAG_NORM), DS_S32_OPT(arp_config_t, packet_interval, OPT_FLAG_NORM), DS_S32_OPT(arp_config_t, entry_lifetime, OPT_FLAG_NORM), }; DS_SEG_DESC main_segments[] = { DS_STRUCT_SEG(“config”, SEG_LIM_RW, SEG_GROUP_ROOT, arp_config_t, main_options), }; DS_SECT_DESC arp_scan_sections[] = { DS_STRUCT_SECT(“config”, main_segments), }; DS_TBL_DESC arp_scan_tables[] = { DS_STRUCT_TBL(“arp_scan”, TBL_ATTR_STC, arp_scan_sections), }; DS_DAT_MON_DESC arp_data_monitor[] = { DS_DAT_MON(ARP_DATA_PATH, DATA_ATTRI_NOTIFY), }; DS_MOD_DESC arp_scan_module = DS_STRUCT_MOD(“arp_scan”, arp_scan_init, arp_scan_check, arp_scan_reload, arp_scan_start, arp_scan_tables, arp_data_monitor); MODULE *module_node = ds_register_module(“arp_scan”, &arp_module); NSD_ASSERT(NULL != module_node); } NSD_INIT(arp_scan_main);
08-27
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <arpa/inet.h> #include <sys/socket.h> #include <pthread.h> #define TRUE 1 #define BUF_SIZE 1024 #define SMALL_BUF 100 typedef struct sockaddr SA; void* request_handler(void* arg); void send_data(FILE* fp, char* ct, char* file_name); char* content_type(char* file); void send_error(FILE* fp); void error_handling(char* message); int main(int argc, char* argv[]) { int serv_sock, clnt_sock; struct sockaddr_in serv_adr, clnt_adr; int clnt_adr_sz; char buf[BUF_SIZE] = { 0 }; pthread_t tid; if (argc != 2) { printf("Usage: %s <port>\n", argv[0]); exit(1); } serv_sock = socket(PF_INET, SOCK_STREAM, 0); //为serv_sock套接字文件描述符设置SO_REUSEADDR可选项 int option = TRUE; int optlen = sizeof(option); setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, (void*)& option, optlen); memset(&serv_adr, 0, sizeof(serv_adr)); serv_adr.sin_family = AF_INET; serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); serv_adr.sin_port = htons(atoi(argv[1])); if (bind(serv_sock, (SA*)& serv_adr, sizeof(serv_adr)) == -1) error_handling("bind() error!"); if (listen(serv_sock, 20) == -1) error_handling("listen() error!"); while (1) { clnt_adr_sz = sizeof(clnt_adr); clnt_sock = accept(serv_sock, (SA*)& clnt_adr, &clnt_adr_sz); if (clnt_sock == -1) { error_handling("accept() error!"); continue; } printf("Connection Request: %s:%d\n", inet_ntoa(clnt_adr.sin_addr), ntohs(clnt_adr.sin_port)); pthread_create(&tid, NULL, request_handler, &clnt_sock); //创建线程 pthread_detach(tid); //线程分离 } close(serv_sock); return 0; } void* request_handler(void* arg) { int clnt_sock = *(int*)arg; char req_line[SMALL_BUF] = { 0 }; FILE* clnt_read, * clnt_write; char method[10] = { 0 }; char ct[15] = { 0 }; char file_name[30] = { 0 }; clnt_read = fdopen(clnt_sock, "r"); clnt_write = fdopen(dup(clnt_sock), "w"); fgets(req_line, SMALL_BUF, clnt_read); if (strstr(req_line, "HTTP/") == NULL) //查看是否为HTTP提出的请求 { send_error(clnt_write); fclose(clnt_read); fclose(clnt_write); return NULL; } strcpy(method, strtok(req_line, " /")); //获取请求行中的方法 strcpy(file_name, strtok(NULL, " /")); //获取请求文件名 //printf("file_name: %s\n", file_name); strcpy(ct, content_type(file_name)); //获取Content-type if (strcmp(method, "GET") != 0) //查看是否为GET方式请求 { send_error(clnt_write); fclose(clnt_read); fclose(clnt_write); return NULL; } fclose(clnt_read); send_data(clnt_write, ct, file_name); //响应处理 return NULL; } //发送响应消息给客户端 void send_data(FILE* fp, char* ct, char* file_name) { char protocol[] = "HTTP/1.1 200 OK\r\n"; char server[] = "Server:Linux Web Server \r\n"; char cnt_len[] = "Content-length:2048\r\n"; char cnt_type[SMALL_BUF] = { 0 }; char buf[BUF_SIZE] = { 0 }; FILE* send_file; sprintf(cnt_type, "Content-type:%s\r\n\r\n", ct); send_file = fopen(file_name, "r"); if (send_file == NULL) { printf("open file [%s] error!\n", file_name); send_error(fp); return; } //传输HTTP的状态行+消息头信息 fputs(protocol, fp); fputs(server, fp); fputs(cnt_len, fp); fputs(cnt_type, fp); //传输HTTP的消息体信息,即HTML文件内容 while (fgets(buf, BUF_SIZE, send_file) != NULL) { fputs(buf, fp); fflush(fp); } fflush(fp); fclose(fp); } //区分Content-type char* content_type(char* file) { char extension[SMALL_BUF] = { 0 }; //存放文件扩展名 char file_name[SMALL_BUF] = { 0 }; //存放文件名 strcpy(file_name, file); strtok(file_name, "."); strcpy(extension, strtok(NULL, ".")); if (!strcmp(extension, "html") || !strcmp(extension, "htm")) return "text/html"; else return "text/plain"; } void send_error(FILE * fp) { char protocol[] = "HTTP/1.1 400 Bad Request\r\n"; char server[] = "Server:Linux Web Server \r\n"; char cnt_len[] = "Content-length:2048\r\n"; char cnt_type[] = "Content-type:text/html\r\n\r\n"; char content[] = "<html>\ <head><title>NETWORK</title></head>\ <body><font size=+5><br>\ 发生错误!请检查请求文件名和请求方式!\ </font></body>\ </html>"; //传输HTTP的状态行+消息头信息 fputs(protocol, fp); fputs(server, fp); fputs(cnt_len, fp); fputs(cnt_type, fp); //传输HTTP的消息体信息,即HTML文件内容 fputs(content, fp); fflush(fp); fclose(fp); } void error_handling(char* message) { fputs(message, stderr); fputc('\n', stderr); exit(1); }
08-08
我给你我的代码,看是否正确。共六份。#ifndef _ARP_RECV_H #define _ARP_RECV_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <net/ethernet.h> /* 以太网帧首部长度 */ #define ETHER_HEADER_LEN sizeof(struct ether_header) /* 整个arp结构长度 */ #define ETHER_ARP_LEN sizeof(struct ether_arp) /* 以太网 + 整个arp结构长度 */ #define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN /* IP地址长度 */ #define IP_ADDR_LEN 4 void err_exit(const char *err_msg); #ifndef LOCAL #define LOCAL static #endif #endif。。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <net/ethernet.h> #include"arp_recv.h" #include "nvmp_utils.h" #include "nsd_common.h" #include "libds.h" #include "libdms.h" #include "flashio.h" #include "dms_tool.h" LOCAL void err_exit(const char *err_msg) { perror(err_msg); exit(1); } LOCAL void arp_recv_main() { struct ether_arp *arp_packet; char buf[ETHER_ARP_PACKET_LEN]; int sock_raw_fd, ret_len, i; if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) { err_exit("socket()"); } while (1) { bzero(buf, ETHER_ARP_PACKET_LEN); ret_len = recv(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0); if (ret_len > 0) { /* 剥去以太头部 */ arp_packet = (struct ether_arp *)(buf + ETHER_HEADER_LEN); /* arp操作码为代表arp应答 */ if (ntohs(arp_packet->arp_op) == 2) { printf("==========================ARP replay======================\n"); printf("Sender IP address: "); for (i = 0; i < IP_ADDR_LEN; i++) { printf("%u", arp_packet->arp_spa[i]); if(i != (IP_ADDR_LEN-1)) { printf("."); } } printf("\nSender MAC address: "); for (i = 0; i < ETH_ALEN; i++) { printf("%02x", arp_packet->arp_sha[i]); if(i != (ETH_ALEN-1)) { printf(":"); } } printf("\nTarget IP address: "); for (i = 0; i < IP_ADDR_LEN; i++) { printf("%u", arp_packet->arp_tpa[i]); if(i != (IP_ADDR_LEN-1)) { printf("."); } } printf("\nTarget MAC address: "); for (i = 0; i < ETH_ALEN; i++) { printf("%02x", arp_packet->arp_tha[i]); if(i != (ETH_ALEN-1)) { printf(":"); } } printf("\n"); } } } close(sock_raw_fd); } NSD_INIT(arp_recv_main);。。#ifndef _ARP_REQUEST_H #define _ARP_REQUEST_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/ethernet.h> #include <net/if_arp.h> #include <net/if.h> #include <netpacket/packet.h> /* 以太网帧首部长度 */ #define ETHER_HEADER_LEN sizeof(struct ether_header) /* 整个arp结构长度 */ #define ETHER_ARP_LEN sizeof(struct ether_arp) /* 以太网 + 整个arp结构长度 */ #define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN /* IP地址长度 */ #define IP_ADDR_LEN 4 /* 广播地址 */ #define BROADCAST_ADDR {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} void err_exit(const char *err_msg); struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip); void arp_request(const char *if_name, const char *dst_ip); #endif。。/* Copyright(c) * * file arp_request.c * brief This is a work of sending arp request. * * author Zhou Shijun * version 1.0.1 * date 24Aug28 * * history * * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netinet/if_ether.h> #include <net/ethernet.h> #include <net/if_arp.h> #include <net/if.h> #include <netpacket/packet.h> #include"arp_request.h" void err_exit(const char *err_msg) { perror(err_msg); exit(1); } /* 填充arp包 */ struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip) { struct ether_arp *arp_packet; struct in_addr src_in_addr, dst_in_addr; unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; /* IP地址转换 */ inet_pton(AF_INET, src_ip, &src_in_addr); inet_pton(AF_INET, dst_ip, &dst_in_addr); /* 整个arp包 */ arp_packet = (struct ether_arp *)malloc(ETHER_ARP_LEN); arp_packet->arp_hrd = htons(ARPHRD_ETHER); arp_packet->arp_pro = htons(ETHERTYPE_IP); arp_packet->arp_hln = ETH_ALEN; arp_packet->arp_pln = IP_ADDR_LEN; arp_packet->arp_op = htons(ARPOP_REQUEST); memcpy(arp_packet->arp_sha, src_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_tha, dst_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_spa, &src_in_addr, IP_ADDR_LEN); memcpy(arp_packet->arp_tpa, &dst_in_addr, IP_ADDR_LEN); return arp_packet; } /* arp请求 */ void arp_request(const char *if_name, const char *dst_ip) { struct sockaddr_ll saddr_ll; struct ether_header *eth_header; struct ether_arp *arp_packet; struct ifreq ifr; char buf[ETHER_ARP_PACKET_LEN]; unsigned char src_mac_addr[ETH_ALEN]; unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; char *src_ip; int sock_raw_fd, ret_len, i; if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) { err_exit("socket()"); } bzero(&saddr_ll, sizeof(struct sockaddr_ll)); bzero(&ifr, sizeof(struct ifreq)); /* 网卡接口名 */ memcpy(ifr.ifr_name, if_name, strlen(if_name)); /* 获取网卡接口索引 */ if (ioctl(sock_raw_fd, SIOCGIFINDEX, &ifr) == -1) { err_exit("ioctl() get ifindex"); } saddr_ll.sll_ifindex = ifr.ifr_ifindex; saddr_ll.sll_family = PF_PACKET; /* 获取网卡接口IP */ if (ioctl(sock_raw_fd, SIOCGIFADDR, &ifr) == -1) { err_exit("ioctl() get ip"); } src_ip = inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr); printf("local ip:%s\n", src_ip); /* 获取网卡接口MAC地址 */ if (ioctl(sock_raw_fd, SIOCGIFHWADDR, &ifr)) { err_exit("ioctl() get mac"); } memcpy(src_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); printf("local mac"); for (i = 0; i < ETH_ALEN; i++) { printf(":%0x", src_mac_addr[i]); } printf("\n"); bzero(buf, ETHER_ARP_PACKET_LEN); /* 填充以太首部 */ eth_header = (struct ether_header *)buf; memcpy(eth_header->ether_shost, src_mac_addr, ETH_ALEN); memcpy(eth_header->ether_dhost, dst_mac_addr, ETH_ALEN); eth_header->ether_type = htons(ETHERTYPE_ARP); /* arp包 */ arp_packet = fill_arp_packet(src_mac_addr, src_ip, dst_ip); memcpy(buf + ETHER_HEADER_LEN, arp_packet, ETHER_ARP_LEN); /* 发送请求 */ ret_len = sendto(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0, (struct sockaddr *)&saddr_ll, sizeof(struct sockaddr_ll)); if (ret_len > 0) { printf("Send successfully!\n"); } close(sock_raw_fd); } LOCAL void arp_request_main(int argc, const char *argv[]) { if (argc != 3) { printf("usage:%s device_name dst_ip\n", argv[0]); exit(1); } arp_request(argv[1], argv[2]); return 0; } NSD_INIT(arp_request_main);。。#ifndef _ARP_SCAN_H #define _ARP_SCAN_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <netpacket/packet.h> #include <sys/ioctl.h> #include <net/if.h> #include <time.h> #include <pthread.h> /* --- 宏定义: 参数默认值 --- */ #define ETHER_HEADER_LEN sizeof(struct ether_header) #define ETHER_ARP_LEN sizeof(struct ether_arp) #define ETHER_ARP_PACKET_LEN (ETHER_HEADER_LEN + ETHER_ARP_LEN) #define BROADCAST_ADDR {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} #define IP_ADDR_LEN 4 #define MAX_ARP_ENTRIES 100 /* --- 数据结构定义 --- */ typedef struct { unsigned char mac[ETH_ALEN]; char ip[INET_ADDRSTRLEN]; time_t last_seen; } arp_entry_t; typedef struct { int enabled; /* 功能开关*/ int scan_interval; /* 扫描周期(秒)*/ int entry_lifetime; /* 有效期(秒)*/ int packet_interval; /* 发包间隔(毫秒)*/ char start_ip[INET_ADDRSTRLEN]; char end_ip[INET_ADDRSTRLEN]; arp_entry_t arp_entries[MAX_ARP_ENTRIES]; int entry_count; /* 当前ARP条目数量*/ pthread_mutex_t lock; /* 互斥锁*/ } arp_config_t; arp_config_t arp_config = { .enabled = 1, /* 功能开关开启*/ .scan_interval = 60, /* 扫描周期为60秒*/ .entry_lifetime = 300, /* 有效期为300秒*/ .packet_interval = 100, /* 发包间隔为100毫秒*/ .start_ip = "192.168.1.100", /* 起始IP*/ .end_ip = "192.168.1.200", /* 结束IP*/ .entry_count = 0 /*初始化ARP条目数量*/ }; /* --- 函数定义 --- */ void err_exit(const char *err_msg); struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip); void add_arp_entry(const char *ip, const unsigned char *mac); void cleanup_arp_entries(); void arp_scan(const char *if_name); LOCAL int arp_scan_init(); LOCAL int arp_scan_check(); LOCAL int arp_scan_start(); LOCAL int arp_scan_reload(DS_MSG *msg); LOCAL void arp_scan_main(); #ifndef LOCAL #define LOCAL static #endif #endif。。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/if_ether.h> #include <net/if_arp.h> #include <netpacket/packet.h> #include <sys/ioctl.h> #include <net/if.h> #include <time.h> #include <pthread.h> #include "arp_scan.h" void err_exit(const char *err_msg) { perror(err_msg); exit(1); } struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip) { struct ether_arp *arp_packet = (struct ether_arp *)malloc(ETHER_ARP_LEN); unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; struct in_addr src_in_addr, dst_in_addr; inet_pton(AF_INET, src_ip, &src_in_addr); inet_pton(AF_INET, dst_ip, &dst_in_addr); arp_packet->arp_hrd = htons(ARPHRD_ETHER); arp_packet->arp_pro = htons(ETHERTYPE_IP); arp_packet->arp_hln = ETH_ALEN; arp_packet->arp_pln = IP_ADDR_LEN; arp_packet->arp_op = htons(ARPOP_REQUEST); memcpy(arp_packet->arp_sha, src_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_tha, dst_mac_addr, ETH_ALEN); memcpy(arp_packet->arp_spa, &src_in_addr, IP_ADDR_LEN); memcpy(arp_packet->arp_tpa, &dst_in_addr, IP_ADDR_LEN); return arp_packet; } void add_arp_entry(const char *ip, const unsigned char *mac) { pthread_mutex_lock(&arp_config.lock); if (arp_config.entry_count < MAX_ARP_ENTRIES) { strcpy(arp_config.arp_entries[arp_config.entry_count].ip, ip); memcpy(arp_config.arp_entries[arp_config.entry_count].mac, mac, ETH_ALEN); arp_config.arp_entries[arp_config.entry_count].last_seen = time(NULL); arp_config.entry_count++; } pthread_mutex_unlock(&arp_config.lock); } void cleanup_arp_entries() { pthread_mutex_lock(&arp_config.lock); time_t now = time(NULL); for (int i = 0; i < arp_config.entry_count; i++) { if (now - arp_config.arp_entries[i].last_seen > arp_config.entry_lifetime) { // 删除过期条目 for (int j = i; j < arp_config.entry_count - 1; j++) { arp_config.arp_entries[j] = arp_config.arp_entries[j + 1]; } arp_config.entry_count--; i--; // 调整索引 } } pthread_mutex_unlock(&arp_config.lock); } void arp_scan(const char *if_name) { struct sockaddr_ll saddr_ll; struct ether_header *eth_header; struct ether_arp *arp_packet; struct ifreq ifr; char buf[ETHER_ARP_PACKET_LEN]; unsigned char src_mac_addr[ETH_ALEN]; char src_ip[INET_ADDRSTRLEN]; int sock_raw_fd; if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) err_exit("socket()"); memset(&saddr_ll, 0, sizeof(struct sockaddr_ll)); memset(&ifr, 0, sizeof(struct ifreq)); memcpy(ifr.ifr_name, "eth0", strlen("eth0")); // 使用默认接口名 if (ioctl(sock_raw_fd, SIOCGIFINDEX, &ifr) == -1) err_exit("ioctl() get ifindex"); saddr_ll.sll_ifindex = ifr.ifr_ifindex; saddr_ll.sll_family = PF_PACKET; if (ioctl(sock_raw_fd, SIOCGIFADDR, &ifr) == -1) err_exit("ioctl() get ip"); strcpy(src_ip, inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr)); if (ioctl(sock_raw_fd, SIOCGIFHWADDR, &ifr) == -1) err_exit("ioctl() get mac"); memcpy(src_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); while (arp_config.enabled) { char target_ip[INET_ADDRSTRLEN]; for (int i = inet_addr(arp_config.start_ip); i <= inet_addr(arp_config.end_ip); i++) { struct in_addr addr; addr.s_addr = i; strcpy(target_ip, inet_ntoa(addr)); memset(buf, 0, ETHER_ARP_PACKET_LEN); eth_header = (struct ether_header *)buf; memcpy(eth_header->ether_shost, src_mac_addr, ETH_ALEN); memcpy(eth_header->ether_dhost, BROADCAST_ADDR, ETH_ALEN); eth_header->ether_type = htons(ETHERTYPE_ARP); arp_packet = fill_arp_packet(src_mac_addr, src_ip, target_ip); memcpy(buf + ETHER_HEADER_LEN, arp_packet, ETHER_ARP_LEN); sendto(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0, (struct sockaddr *)&saddr_ll, sizeof(struct sockaddr_ll)); free(arp_packet); printf("ARP request sent to %s\n", target_ip); usleep(arp_config.packet_interval * 1000); // 发包间隔 } cleanup_arp_entries(); // 清理过期的ARP条目 sleep(arp_config.scan_interval); // 等待下一次扫描 } close(sock_raw_fd); } LOCAL int arp_scan_init() { if (0 == ds_read(ARP_DATA_PATH, &arp_config_t, sizeof(arp_config_t))) { return SLP_ESYSTEM; } /* Initialize socket */ if ((sockfd = socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(ETH_P_ARP))) < 0) { perror("socket"); return ERROR; } /* Initialize ip_mac_table */ U8 table_len = arp_config_t.end_ip[3] - arp_config_t.start_ip[3] + 1; ip_mac_table = (ARP_IPMAC_TABLE *)malloc(sizeof(ARP_IPMAC_TABLE) * table_len); struct timeval cur_time; gettimeofday(&cur_time, NULL); for (int i = 0; i < table_len; i++) { ip_mac_table[i].renew_time = cur_time; ip_mac_table[i].device_is_exist = 0; memset(ip_mac_table[i].mac, 0, ETH_ALEN); } msg_attach_handler(MSGID_DMS_CMD, arp_call_handle); return OK; } LOCAL int arp_scan_check() { if (arp_config_t.end_ip[2] != sender_ip[2] || arp_config_t.start_ip[2] != sender_ip[2] || arp_config_t.end_ip[3] <= arp_config_t.start_ip[3]) { ARP_DEBUG("Invalid IP address range, please check.\n"); return ERROR; } return OK; } LOCAL int arp_scan_start() { /* Start address expiration check thread */ pthread_create(&time_tid, NULL, check_table_renew_time, NULL); pthread_detach(time_tid); /* Scan loop */ while (scanning_flag) { scan_once(); usleep(arpco.scan_interval); } return OK; } LOCAL int arp_scan_reload(DS_MSG *msg) { /* Stop scanning */ scanning_flag = 0; if (ds_path_id_exist(msg->id, msg->num, ARP_DATA_PATH)) { arp_config_t arp_data; memset(&arp_data, 0, sizeof(arp_config_t)); if (0 == ds_read(ARP_DATA_PATH, (U8 *)&arp_data, sizeof(arp_config_t))) { ARP_ERROR("Read arp data ERROR"); return ERROR; } /* Reload params */ memcpy(arp_config_t.start_ip, arp_data.start_ip, ARP_IPV4_LEN); memcpy(arp_config_t.end_ip, arp_data.end_ip, ARP_IPV4_LEN); arp_config_t.scan_interval = arp_data.scan_interval; arp_config_t.packet_interval = arp_data.packet_interval; arp_config_t.entry_lifetime = arp_data.entry_lifetime; /* Cancel old checking thread */ pthread_cancel(time_tid); /* Realloc ip_mac_table */ U8 table_len = arp_config_t.end_ip[3] - arp_config_t.start_ip[3] + 1; ARP_IPMAC_TABLE *new_table; new_table = (ARP_IPMAC_TABLE *)realloc(ip_mac_table, sizeof(ARP_IPMAC_TABLE) * table_len); if (NULL == new_table) { ARP_ERROR("Realloc ipmac_table ERROR"); free(ip_mac_table); ip_mac_table = NULL; return ERROR; } ip_mac_table = new_table; struct timeval cur_time; gettimeofday(&cur_time, NULL); for (int i = 0; i < table_len; i++) { ip_mac_table[i].renew_time = cur_time; ip_mac_table[i].device_is_exist = 0; memset(ip_mac_table[i].mac, 0, ETH_ALEN); } /* Restart checking thread */ /* Start scanning */ scanning_flag = 1; arp_scan_start(); return OK; } } LOCAL void arp_scan_main() { DS_OPT_DESC main_options[] = { DS_SWITCH_OPT(arp_config_t, enabled, OPT_FLAG_NORM), DS_STR_OPT(arp_config_t, start_ip, OPT_FLAG_NORM), DS_STR_OPT(arp_config_t, end_ip, OPT_FLAG_NORM), DS_S32_OPT(arp_config_t, scan_interval, OPT_FLAG_NORM), DS_S32_OPT(arp_config_t, packet_interval, OPT_FLAG_NORM), DS_S32_OPT(arp_config_t, entry_lifetime, OPT_FLAG_NORM), }; DS_SEG_DESC main_segments[] = { DS_STRUCT_SEG("config", SEG_LIM_RW, SEG_GROUP_ROOT, arp_config_t, main_options), }; DS_SECT_DESC arp_scan_sections[] = { DS_STRUCT_SECT("config", main_segments), }; DS_TBL_DESC arp_scan_tables[] = { DS_STRUCT_TBL("arp_scan", TBL_ATTR_STC, arp_scan_sections), }; DS_DAT_MON_DESC arp_data_monitor[] = { DS_DAT_MON(ARP_DATA_PATH, DATA_ATTRI_NOTIFY), }; DS_MOD_DESC arp_scan_module = DS_STRUCT_MOD("arp_scan", arp_scan_init, arp_scan_check, arp_scan_reload, arp_scan_start, arp_scan_tables, arp_data_monitor); MODULE *module_node = ds_register_module("arp_scan", &arp_module); NSD_ASSERT(NULL != module_node); } NSD_INIT(arp_scan_main);
08-27
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值