20250515
This commit is contained in:
BIN
IP_region_query/ipquery
Normal file
BIN
IP_region_query/ipquery
Normal file
Binary file not shown.
18
cap.c
18
cap.c
@@ -121,6 +121,7 @@ void Processing_IP_addresses(char *src_ip)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 实时检测内存泄漏
|
||||||
dump_mem_leak();
|
dump_mem_leak();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -128,15 +129,28 @@ void Processing_IP_addresses(char *src_ip)
|
|||||||
// 回调函数,在捕获到每个数据包时调用
|
// 回调函数,在捕获到每个数据包时调用
|
||||||
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
|
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
|
||||||
{
|
{
|
||||||
|
// 定义以太网帧头部的长度 (目标 MAC 6字节 + 源 MAC 6字节 + 类型 2字节 = 14字节)
|
||||||
int ethernet_header_len = 14;
|
int ethernet_header_len = 14;
|
||||||
//struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
|
||||||
|
// 假设数据包是 Ethernet II 帧封装的 IP 包
|
||||||
|
// 计算 IP 头部的起始位置:跳过以太网头部
|
||||||
|
// 将该位置的指针强制转换为 struct ip 指针
|
||||||
struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
||||||
|
// 检查 IP 协议版本号
|
||||||
|
// ip_v 是 struct ip 结构中的版本号字段 (通常占 4 bits)
|
||||||
if (ip_header->ip_v != 4)
|
if (ip_header->ip_v != 4)
|
||||||
return; // 只处理 IPv4
|
return; // 只处理 IPv4
|
||||||
|
// 定义一个字符数组用于存储点分十进制格式的源 IP 地址字符串
|
||||||
|
// INET_ADDRSTRLEN 是 <arpa/inet.h> 中定义的足够存储 IPv4 地址字符串的最大长度 (包括结尾的 '\0')
|
||||||
char src_ip[INET_ADDRSTRLEN] = { 0 };
|
char src_ip[INET_ADDRSTRLEN] = { 0 };
|
||||||
|
|
||||||
|
// 将网络字节序 (二进制) 的 IPv4 源地址转换为点分十进制表示的字符串
|
||||||
|
// AF_INET: 指定地址族为 IPv4
|
||||||
|
// &(ip_header->ip_src): 指向 struct ip 结构中源地址字段 (struct in_addr 类型) 的指针
|
||||||
|
// src_ip: 存储转换后字符串的缓冲区
|
||||||
|
// INET_ADDRSTRLEN: 缓冲区的长度
|
||||||
inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN);
|
inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN);
|
||||||
|
|
||||||
Processing_IP_addresses(src_ip);
|
Processing_IP_addresses(src_ip);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user