自己和自己分離
2023-08-17 03:11:07
#include <stdio.h>
#include <pcap.h>
void packet_handler(u_char *user_data, const struct pcap_pkthdr *pkthdr, const u_char *packet) {
// 在此處處理封包
printf("Packet captured\n");
}
int main() {
pcap_t *handle;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header;
const u_char *packet;
char *dev;
// 取得可用的網路介面
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
printf("Error finding device: %s\n", errbuf);
return 1;
}
// 開啟網路介面
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
printf("Could not open device %s: %s\n", dev, errbuf);
return 1;
}
// 監聽封包
pcap_loop(handle, 0, packet_handler, NULL);
// 關閉網路介面
pcap_close(handle);
return 0;
}