diff --git a/a.out b/a.out deleted file mode 100644 index f6d4755..0000000 Binary files a/a.out and /dev/null differ diff --git a/cache.c b/cache.c index 77ea9dd..06263b9 100644 --- a/cache.c +++ b/cache.c @@ -48,6 +48,8 @@ int is_ip_in_cache(const char *ip) struct ip_cache_node *current = ip_cache_head; struct ip_cache_node *prev = NULL; + if (ip_cache_head == NULL) return 0; // 如果 ip_cache_head == NULL,current->next 可能导致段错误 (Segmentation Fault)。 + while (current != NULL) { // 如果 IP 匹配并且未过期 if (strcmp(current->ip, ip) == 0) { diff --git a/cap.c b/cap.c index 35c5988..638421e 100644 --- a/cap.c +++ b/cap.c @@ -23,6 +23,7 @@ char *command_result = NULL; // 执行命令的结果 void Processing_IP_addresses(char *src_ip) { + // 地域白名单 char _region_list[WHITELIST_IP_NUM][WHITELIST_IP_NUM] = { { 0 }, { 0 } }; char _REGION_LIST[BUFFER] = { 0 }; @@ -108,11 +109,11 @@ void Processing_IP_addresses(char *src_ip) fprintf(stderr, "Failed to parse JSON.\n"); } - if (p != NULL) + if (p != NULL) { free(p); + } } - if (ip2region_area != NULL) { free(ip2region_area); ip2region_area = NULL; @@ -125,9 +126,14 @@ void Processing_IP_addresses(char *src_ip) void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { int ethernet_header_len = 14; + //struct ip *ip_header = (struct ip *)(packet + ethernet_header_len); struct ip *ip_header = (struct ip *)(packet + ethernet_header_len); + if (ip_header->ip_v != 4) return; // 只处理 IPv4 + char src_ip[INET_ADDRSTRLEN] = { 0 }; + + inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN); Processing_IP_addresses(src_ip);