原来的代码耦合性太高了,我重构了一下,模块化了。
扫描部分还待扩充
头文件:
config.h
#ifndef lib_pcap_pcap_h
#include "pcap.h"
#endif
#ifndef HAVE_PARAM_H
#include "param.h"
#endif
#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
/*
IPSIZE IP地址的长度
MACSIZE MAC地址的长度
PORTNUM PORT的个数
*/
#define IPSIZE 16
#define MACSIZE 14
#define PORTNUM 65536
const int judge_ip_times = 4; // 设置判为可疑的次数
const int judge_port_times = 100;
const char port_scan_log[] = "port_scan_log.txt";
#endif // CONFIG_H
dnmc_packet_data.h
#ifndef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef HAVE_DNMC_PACKET_DATA_H
#define HAVE_DNMC_PACKET_DATA_H
typedef struct protocol_type {
int dport_hash[PORTNUM];
int dport_last_time[PORTNUM]; // ??预想画出特征曲线
}protocol_type;
typedef struct smac_info {
char *dmac;
struct protocol_type *pprot_type;
struct smac_info* next;
}smac_info;
typedef struct list_head {
char *smac;
struct smac_info *psmac_info;
struct list_head *next;
}list_head;
typedef struct rule_list_node {
char *name;
int mode;
int rval; // 0表示不检测,1表示检测
struct rule_list_node* next;
struct list_head* rule_list;
}rule_list_node;
#endif // DNMC_PACKET_DATA_H
main.h
#ifndef lib_pcap_pcap_h
#include "pcap.h"
#endif
#ifdef WINDOWS
#define unsigned (unsigned int)
#endif
#ifdef LINUX
#define unsigned (unsigned long)
#endif
char *packet_filter; // 需要从文件中读取过滤数据参数
struct pcap_pkthdr **pool_header; // 数据包池
u_char **pool_pkt_data;
u_int pool_in_index;
u_int pool_out_index;
packet.h
#ifndef HAVE_CONFIG_H
#include "config.h"
#define HAVE_CONFIG_H
#endif
#ifndef HAVE_PACKET_H
#define HAVE_PACKET_H
// 以太网协议格式的定义
typedef struct ether_header {
u_char dhost[6]; // 目标地址
u_char shost[6]; // 源地址
u_short type; // 以太网类型
}ether_header;
// 用户保存4字节的IP地址
typedef struct ip_address {
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
// 用于保存IPV4的首部
typedef struct ip_header {
#ifdef WORDS_BIGENDIAN
u_char ip_version : 4, header_length : 4;
#else
u_char header_length : 4, ip_version : 4;
#endif
u_char ver_ihl; // 版本以及首部长度,各4位
u_char tos; // 服务质量
u_short tlen; // 总长度
u_short identification; // 身份识别
u_short offset; // 分组偏移
u_char ttl; // 生命周期
u_char protocol; // 协议类型
u_short checksum; // 包头测验码
ip_address saddr; // 源IP地址
ip_address daddr; // 目的IP地址
u_int op_pad; //可选 填充