libnids中主要数据结构和函数“nids.h”

来源:互联网 发布:外汇储备最新数据 编辑:程序博客网 时间:2024/05/16 08:21


/*  Copyright (c) 1999 Rafal Wojtczuk <nergal@7bulls.com>. All rights reserved.  See the file COPYING for license details.*/#ifndef _NIDS_NIDS_H# define _NIDS_NIDS_H# include <sys/types.h>#include <netinet/in_systm.h>#include <netinet/in.h># include <netinet/ip.h># include <netinet/tcp.h># include <pcap.h>//关于链接指示extern "C"参考文章//http://blog.csdn.net/u013074465/article/details/42738553# ifdef __cplusplusextern "C" {# endif# define NIDS_MAJOR 1   //Libnids的主版本号# define NIDS_MINOR 24  //Libnids的次版本号enum{  NIDS_WARN_IP = 1,  //IP数据包异常  NIDS_WARN_TCP,     //TCP数据报异常  NIDS_WARN_UDP,     //UDP数据报异常  NIDS_WARN_SCAN     //出现了扫描攻击};enum{  NIDS_WARN_UNDEFINED = 0, //未定义  NIDS_WARN_IP_OVERSIZED,  //IP数据包超长  NIDS_WARN_IP_INVLIST,    //无效的碎片队列  NIDS_WARN_IP_OVERLAP,    //IP数据发生重叠  NIDS_WARN_IP_HDR,      //无效IP首部,IP数据异常  NIDS_WARN_IP_SRR,      //源路由IP数据包  //TCP数据过多,因为Libnids在同一时刻捕获的TCP个数  //最大值是TCP连接参数的哈希表长度  NIDS_WARN_TCP_TOOMUCH,    NIDS_WARN_TCP_HDR,       //无效TCP首部,TCP数据包异常  NIDS_WARN_TCP_BIGQUEUE,  //TCP接受的队列数据过多  NIDS_WARN_TCP_BADFLAGS   //TCP错误标记};//如下描述的是TCP连接的逻辑状态,真正的TCP连接11种状态,//参考文章http://blog.csdn.net/u013074465/article/details/44539391# define NIDS_JUST_EST 1    //TCP连接建立# define NIDS_DATA 2        //表示接受数据的状态# define NIDS_CLOSE 3       //TCP连接正常关闭# define NIDS_RESET 4       //TCP连接被重置关闭# define NIDS_TIMED_OUT 5   //由于超时TCP关闭# define NIDS_EXITING   6/* nids is exiting; last chance to get data */# define NIDS_DO_CHKSUM  0    //告诉libnids要计算校验和# define NIDS_DONT_CHKSUM 1   //告诉Libnids不计算校验和//该结构描述地址及端口struct tuple4       {  u_short source;   //源端口  u_short dest;     //目的端口  u_int saddr;      //源IP  u_int daddr;      //目的IP};//描述在TCP连接中一端的所有信息,可以是客户端,可以是服务端 struct half_stream{  char state;        //套接字的状态  char collect;      //可以表示有数据到达,此数据存放在data成员中;                     //也可以表示不存储数据到data中,此数据忽略 //如果大于0就存储,构造忽略  char collect_urg;  //表示有紧急数据达到,此数据就存放在urgdata中,                     //也可以表示不存储此数据到urgdata中,此数据忽略 //如果大于0就存储,否则忽略  char *data;         //用户存储正常接收的数据  int offset;         //存储在data中的数据的第一个字节的偏移量  int count;          //从TCP连接开始存储到data中的数据字节数  int count_new;      //有多少新数据存储到data中  int bufsize;        //缓冲区大小  int rmem_alloc;  int urg_count;        u_int acked;  u_int seq;  u_int ack_seq;  u_int first_data_seq;  u_char urgdata;  u_char count_new_urg;  u_char urg_seen;   //收到了紧急数据  u_int urg_ptr;  u_short window;    //窗口大小  u_char ts_on;      //tcp时间戳选项是否打开  u_char wscale_on;  //窗口扩大选项是否打开  u_int curr_ts;   u_int wscale;  //双向链表,存放数据包  struct skbuff *list;  /*当收到的TCP包中seq比期望的seq大时,证明数据乱序到达,现将数据  保存到此双向链表中;为了访问方便,此链表中seq从小到大排序*/  struct skbuff *listtail;};//描述一个完整的TCP链接,包括客户端到服务器的以及反向的struct tcp_stream{  struct tuple4 addr;  char nids_state;    //tcp的逻辑连接状态  struct lurker_node *listeners;  struct half_stream client;  struct half_stream server;  struct tcp_stream *next_node;  struct tcp_stream *prev_node;  int hash_index;  struct tcp_stream *next_time;  struct tcp_stream *prev_time;  int read;      //本次读进数据缓冲区的数据长度  struct tcp_stream *next_free;  //指向下一个空闲tcp结构  void *user;};//Libnids的一些全局参数信息struct nids_prm{  /* 表示哈西表大小,此哈西表用来存放tcp_stream 数据结构,默认值 1040.  在同一时刻 Libnids 捕获的 TCP 数据包的最大个数必须是此参数值的3/4*/  int n_tcp_streams;   /*表示哈西表的大小,此哈西表用来存储IP碎片信息的,默认值为 256*/  int n_hosts;   /* 表示网络接口 ,Libnids 将在此网络接口上捕获数据, 默认值为 NULL.   这样 Libnids将使用 pcap_lookupdev来查找可以用的网络接口 .   如果其值为 all, 表示捕获所有网络接口的数据*/  char *device;  /* 表示用来存储网络数据的捕获文件 , 此文件的类型必须与 Libpcap 类型一致,   如果设置了文件, 与此同时就应该设置 device 为 NULL,默认值为 NULL*/  char *filename;  /* 表示的是数据接口 sk_buff 的大小.sk_buff 是Linux 内核中一个重要的  数据结构, 是用来进行数据包排队操作的 , 默认值为 168*/  int sk_buff_size;   /* 表示在数据结构 sk_buff 中用于网络接口上信息的字节数. 如果是 -1( 默认值 ),   那么 Libnids 会根据不同的网络接口进行修正 */  int dev_addon;   /* 是一个函数指针 , 默认值为nids_syslog() 函数 . 在 syslog函数中可以检测   入侵攻击 , 如网络扫描攻击 , 也可以检测一些异常情况, 如无效 TCP 标记 */  void (*syslog) ();    int syslog_level;   /* 表示日志等级 , 默认值是LOG_ALERT*/   /* 表示一个哈西表的大小 ,(此哈西表用来存储端口扫描信息) 表示 Libnids 将要   检测的同时扫描的端口数据.如果其值为 0,Libnids将不提供端口扫描功能.默认值 256*/  int scan_num_hosts;   /* 表示在扫描检测中 , 俩端口扫描的间隔时间, 以毫秒来计算 , 缺省值为 3000*/  int scan_delay;   /* 表示相同源地址必须扫描的 TCP 端口数目 , 默认值为10*/  int scan_num_ports;  /* 是一个函数指针 , 当Libnids 发生内存溢出时被调用*/  void (*no_mem) (char *);  /* 是一个函数指针 , 此函数可以用来分析IP 数据包 , 当有 IP 数据包到达时 ,   此函数就被调用. 如果此函数返回非零值 , 此数据包就被处理 ;  如果返回零 , 此IP 数据包就被丢弃.默认值为 nids_ip_filter函数,总是返回 1.*/  int (*ip_filter) ();  /* 表示过滤规则 , 即Libpcap 的过滤规则 , 默认值为 NULL,  表示捕获所有数据包 . 可以在此设置过滤规则 , 只捕获感兴趣的开发包*/  char *pcap_filter;  /* 表示网卡模式 , 如果是非零, 就把此网卡设置为混杂模式 ;   否则 , 设为非混杂模式 . 默认值为1*/  int promisc;  int one_loop_less;  /* 表示捕获数据返回的时间 , 以毫秒计算. 实际上它表示的就是   Libpcap 函数中的 pcap_open_live函数的 timeout 参数 , 默认值 1024*/  int pcap_timeout;  int multiproc;  int queue_limit;  int tcp_workarounds;  pcap_t *pcap_desc;};struct tcp_timeout{  struct tcp_stream *a_tcp;  struct timeval timeout;  struct tcp_timeout *next;  struct tcp_timeout *prev;};/* * 返回值 : 调用成功返回 1,失败返回 0 * 参   数 : 无 * 功   能 : 对 Libnids 初始化, 这是所有设计基于 Libnids 的程序最开始调用的函数 .              它的主要内容包括打开网络接口 , 打开文件 , 编译过滤规则 ,判断网络链路             层类型, 进行必要的初始化工作 */ int nids_init (void);/* * 返回值 : 无 * 参   数 : 回调函数名字 * 功   能 : 注册一个能够检测所有 IP 数据包的回调函数, 包括 IP 碎片 .e.g         nids_register_ip_frag(ip_frag_function);         void ip_frag_function(struct ip * a_packet,int len)         a_packet 表示接收的IP 数据包 len 表示接收的数据包长度         此回调函数可以检测所有的IP 数据包 , 包括 IP 碎片 */ void nids_register_ip_frag (void (*));void nids_unregister_ip_frag (void (*));/* * 返回值 : 无 * 参   数 : 回调函数名字 * 功   能 : 注册一个回调函数 , 此回调函数可以接收正常的IP 数据包 .e.g         nids_register_ip_frag(ip_frag_function);         void ip_frag_function(struct ip * a_packet)         a_packet 表示接收的IP 数据包         此回调函数可以接收正常的IP 数据包 , 并在此函数中对捕获数到的         IP数据包进行分析 . */ void nids_register_ip (void (*));void nids_unregister_ip (void (*));/* * 返回值 : 无 * 参   数 : 回调函数 * 功   能 : 注册一个 TCP 连接的回调函数. 回调函数的类型定义如下 :          void tcp_callback(struct tcp_stream * ns,void ** param)          ns 表示一个TCP 连接的所有信息 , param 表示要传递的参数信息 , 可以指向  一个 TCP连接的私有数据. 此回调函数接收的TCP 数据存放在 half_stream 的缓存中,   应该马上取出来 ,一旦此回调函数返回 , 此数据缓存中存储的数据就不存在了.  half_stream 成员 offset描述了被丢弃的数据字节数 . 如果不想马上取出来 ,   而是等到存储一定数量的数据之后再取出来, 那么可以使用函数  nids_discard(struct tcp_stream * ns, int num_bytes)来处理 . 这样回调函数  返回时 ,Libnids 将丢弃缓存数据之前的 num_bytes 字节的数据 .如果不调用  nids_discard()函数 , 那么缓存数据的字节应该为 count_new 字节 . 一般情况下,  缓存中的数据应该是count-offset 字节 */ void nids_register_tcp (void (*));void nids_unregister_tcp (void (*x));/* * 返回值 : 无 * 参   数 : 回调函数 * 功   能 : 注册一个分析 UDP 协议的回调函数, 回调函数的类型定义如下 :          void udp_callback(struct tuple4 * addr,char *buf,int len,struct ip *iph)          addr 表示地址端口信息buf 表示 UDP 协议负载的数据内容 len表是 UDP 负载数据  的长度 iph 表示一个IP 数据包 , 包括 IP 首部 ,UDP 首部以及UDP 负载内容 */ void nids_register_udp (void (*));void nids_unregister_udp (void (*));/* * 返回值 : 无 * 参   数 : 表示一个 TCP 连接 * 功   能 : 终止 TCP 连接.它实际上是调用 Libnet的函数进行构造数据包 , 然后发送出去 */ void nids_killtcp (struct tcp_stream *); /* * 返回值 : 无 * 参   数 : 参数 1 一个 TCP 连接 参数 2 个数 * 功   能 : 丢弃参数 2 字节 TCP 数据 , 用于存储更多的数据 */ void nids_discard (struct tcp_stream *, int);/* * 返回值 : 无 * 参   数 : 无 * 功   能 : 运行 Libnids, 进入循环捕获数据包状态. 它实际上是调用              Libpcap 函数 pcap_loop()来循环捕获数据包 */ int nids_run (void);void nids_exit(void);/* * 返回值 : 调用成功返回文件描述符 ,失败返回 -1 * 参   数 : 无 * 功   能 : 获得文件描述符号*/ int nids_getfd (void);/* * 返回值 : 调用成功返回个数 ,失败返回负数 * 参   数 : 表示捕获数据包的个数 * 功   能 : 调用 Libpcap 中的捕获数据包函数pcap_dispatch() */ int nids_dispatch (int);int nids_next (void);void nids_pcap_handler(u_char *, struct pcap_pkthdr *, u_char *);struct tcp_stream *nids_find_tcp_stream(struct tuple4 *);void nids_free_tcp_stream(struct tcp_stream *);/*libnids.c定以了一个全部变量 , 其定义和初始值在 nids_params*/extern struct nids_prm nids_params;extern char *nids_warnings[];extern char nids_errbuf[];extern struct pcap_pkthdr *nids_last_pcap_header;extern u_char *nids_last_pcap_data;extern u_int nids_linkoffset;extern struct tcp_timeout *nids_tcp_timeouts;/* 描述的是计算校验和 , 用于决定是否计算校验和*/struct nids_chksum_ctl {u_int netaddr;     //地址u_int mask;         //掩码 /* 表示动作 , 如果是NIDS_DO_CHKSUM, 表示计算校验和;  如果是 NIDS_DONT_CHKSUM, 表示不计算校验和 */u_int action;       u_int reserved;  //未使用};/* * 返回值 : 无 * 参   数 : 参数 1 表示 nids_chksum_ctl 列表 参数 2 表示列表中的个数 * 功   能 : 决定是否计算校验和 . 它是根据数据结构nids_chksum_ctl             中的action 进行决定的 , 如果所要计算的对象 不在列表中 , 则必须都要计算校验和 */ extern void nids_register_chksum_ctl(struct nids_chksum_ctl *, int);# ifdef __cplusplus}# endif#endif /* _NIDS_NIDS_H */


0 0
原创粉丝点击