#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ether.h>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
#include <time.h>
#include <syslog.h>
#include <pcap.h>
// 定义常量
#define SMTP_PORT 25
#define IMAP_PORT 143
#define POP3_PORT 110
#define SPECIAL_DOMAIN "tp-link.com.cn"
#define MAX_PACKETS 10000
// 数据包结构体,用于存储捕获到的数据包信息
typedef struct PacketInfo {
struct timeval timestamp; // 时间戳
char src_ip[INET_ADDRSTRLEN]; // 源IP地址
char dest_ip[INET_ADDRSTRLEN]; // 目的IP地址
unsigned short src_port; // 源端口
unsigned short dest_port; // 目的端口
unsigned short protocol; // 协议类型
int length; // 报文长度
} PacketInfo;
// 全局变量
PacketInfo packets[MAX_PACKETS]; // 存储捕获的数据包信息
int packet_count = 0; // 当前捕获的数据包数量
// 打印时间戳
void print_timestamp(struct timeval ts) {
struct tm *local = localtime(&ts.tv_sec);
printf("%02d:%02d:%02d.%06ld ", local->tm_hour, local->tm_min, local->tm_sec, ts.tv_usec);
}
// 解析TCP数据包并处理邮件流量
void handle_tcp_packet(const u_char *packet, int size, struct timeval ts) {
struct iphdr *ip_header = (struct iphdr *)(packet + sizeof(struct ethhdr));
struct tcphdr *tcp_header = (struct tcphdr *)(packet + ip_header->ihl * 4 + sizeof(struct ethhdr));
int ip_header_size = ip_header->ihl * 4;
int tcp_header_size = tcp_header->doff * 4;
int data_size = size - (ip_header_size + tcp_header_size + sizeof(struct ethhdr));
printf("Protocol: TCP\n");