队列 实现 抓包

主要原理很简单,就是用一个队列(Queue)对高峰流量做一下缓冲,先存下来再慢慢分析。计算机专业的人应该就容易了,数据结构有讲。


整个程序代码比较长就不贴了,把队列代码贴在下面吧,放在网上的比硬盘的还是方便一点,随时可以查阅,以备不时之需。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. typedef struct _Node
  6. {
  7. int data_len; // 存储在节点中的数据长度
  8. char *data; // 存储在节点中的数据
  9. struct _Node *next; // 队列中的下一个节点地址
  10. }NODE;
  11. typedef struct _Queue
  12. {
  13. NODE *head; // 队列的头部
  14. NODE *end; // 队列的尾部
  15. int count; // 队列长度
  16. }QUEUE;
  17. bool InitQueue ( QUEUE *queue )
  18. {
  19. if ( NULL == queue )
  20. {
  21. return false;
  22. }
  23. queue->head = NULL;
  24. queue->end = NULL;
  25. queue->count = 0;
  26. return true;
  27. }
  28. // 在队列中插入节点
  29. bool Enqueue ( QUEUE *queue, char *queue_data, int data_len )
  30. {
  31. if ( NULL == queue || NULL == queue_data )
  32. {
  33. return false;
  34. }
  35. // 开辟新节点
  36. NODE *new_node = (NODE * ) malloc ( sizeof (NODE ) );
  37. if ( NULL == new_node )
  38. {
  39. return false;
  40. }
  41. // 开辟空间存储数据
  42. new_node->data = ( char * ) malloc ( data_len );
  43. if ( NULL == new_node->data )
  44. {
  45. return false;
  46. }
  47. memcpy ( new_node->data, queue_data, data_len );
  48. new_node->next = NULL;
  49. new_node->data_len = data_len;
  50. // 如果队列为空,则新节点即是头部,也是尾部
  51. if ( queue->head == NULL )
  52. {
  53. queue->head = new_node;
  54. queue->end = new_node;
  55. }
  56. else
  57. {
  58. // 如果队列不为空,将此节点连接到队列的尾部
  59. queue->end->next = new_node;
  60. // 队列新尾部指向此节点
  61. queue->end = new_node;
  62. }
  63. queue->count ++;
  64. return true;
  65. }
  66. // 从队列中读出一个节点
  67. NODE *Dequeue ( QUEUE *queue )
  68. {
  69. if ( NULL == queue )
  70. {
  71. return NULL;
  72. }
  73. // 如果队列为空,则无数据可从数列读出,直接返回
  74. if ( NULL == queue->head )
  75. {
  76. return NULL;
  77. }
  78. // 保存队列首节点
  79. NODE *node_tmp = queue->head;
  80. // 将首节点的下一个节点(第二个节点)设置为首节点,即删除了首节点
  81. queue->head = node_tmp->next;
  82. // 如果新首节点为空,则队列为空
  83. if ( NULL == queue->head )
  84. {
  85. queue->end = NULL;
  86. }
  87. queue->count --;
  88. return node_tmp;
  89. }
  90. // 释放队列所有内存
  91. void FreeQueue ( QUEUE *queue )
  92. {
  93. if ( !queue )
  94. {
  95. return;
  96. }
  97. NODE *tmp_node1 = queue->head;
  98. while ( tmp_node1 )
  99. {
  100. NODE *tmp_node2 = tmp_node1;
  101. free ( tmp_node1->data );
  102. free ( tmp_node1 );
  103. tmp_node1->data = NULL;
  104. tmp_node1 = NULL;
  105. tmp_node1 = tmp_node2->next;
  106. }
  107. }

这样没抓到一个包,就压入到队列里面。流量较大的尖峰时刻时候队列会比较长,但是流量较小的时候队列就比较短了。服务器流量不会一直保持在顶峰,所以可以把部分过高的流量缓冲到流量较小的时候做分析,测试的时候发现确实稳定了很多。首先需要注意的是设置一个队列最大长度,必要的时候丢掉一些数据免得内存耗尽。另外一个是分析的时候可以多线程读取队列进行分析,这样速度会更快,只是做好线程的同步就好了,CreateMutex是最简单易行的了。我用5线程来分析数据包,基本足够了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值