流量分析器k2

#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");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值