资源简介

调锁调了蛮多时间.单线程序抓包,三个缓冲区, 默认三个线程进行处理.由于为了尽量保证包的顺序,三个线程同时只从一个缓冲区取数据

资源截图

代码片段和文件信息

/* 
 * =====================================================================================
 *
 *       Filename:  FlowClassThread.c
 *
 *    Description:  Class Packet 
 *
 *        Version:  1.0
 *        Created:  04/30/2009 08:51:46 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  WengJian Lv 
 *        Company:  GreeNet
 *
 * =====================================================================================
 */
#include “test.h“ 
 
 void * FlowClassThread(PktMemC *pktmemc_buf)
{
    int temp;
int count = 0;
    u_char  packet[1550];
    u_char  *tmp_packet; /*just for readable*/
    /**/
    while(1)
    {
      pthread_mutex_lock(&(pktmemc_buf->m_pktbuf->pktbufglobal_m));/**/
      pthread_mutex_lock(&(pktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pktbufhead_m));
      temp=pktmemc_buf->m_pktbuf->ocurrentbuf;
      if(pktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pkt_num==0)
      { /*free the lock*/
        pktmemc_buf->m_pktbuf->ocurrentbuf = (pktmemc_buf->m_pktbuf->ocurrentbuf+1) % PKT_BUFlink_HEADNUM;
        //tmp_packet = pktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pkt_buf_c;
        //tmp_packet = pktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pkt_buf_h;
        pthread_mutex_unlock(&(pktmemc_buf->m_pktbuf->pktbufglobal_m));
        pthread_mutex_unlock(&(pktmemc_buf->m_pktbuf->pktbuf_array[temp].pktbufhead_m)); 
count++;
if(count>10)
{
printf(“the Class Thread Count is %d\n“count);
sleep(1);
count=0;
}

        continue;
      }
      
      
      /*
      release pktbufglobal_mso the capture thread could quickly capture the packet
      */
      pthread_mutex_unlock(&(pktmemc_buf->m_pktbuf->pktbufglobal_m));
      tmp_packet = pktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pkt_buf_c;
      memcpy(packettmp_packet2+*((u_short*)(tmp_packet)));
      tmp_packet = tmp_packet + 1550;
      pktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pkt_num--;
  printf(“the %d pool has only %d‘s packet\n“pktmemc_buf->m_pktbuf->ocurrentbufpktmemc_buf->m_pktbuf->pktbuf_array[pktmemc_buf->m_pktbuf->ocurrentbuf].pkt_num);
     //sleep();
  pthread_mutex_unlock(&(pktmemc_buf->m_pktbuf->pktbuf_array[temp].pktbufhead_m));
      
      /*class the packet in below*/
      switch(pktmemc_buf->methods)
      {
      case 1:
         ClassBySsDdP(pktmemc_buf(u_char*)packet);
         break;
      case 2:
        
         break;
      case 3:
        
         break;
        
        default:
         break;
      }
    }   
}




void ClassBySsDdP(PktMemC *pktmemc_bufu_char *packet)
{
ip_header  *ih;
udp_header *uh;
tcp_header *th;   
int ip_len;
int ip_hlen;
int i;
int tcpflag = 0;
int udpflag = 0;

ih      = (ip_header *) (packet +16);

评论

共有 条评论