Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3883194
  • 博文数量: 93
  • 博客积分: 3189
  • 博客等级: 中校
  • 技术积分: 4229
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-02 13:29
个人简介

出没于杭州和青岛的程序猿一枚,对内核略懂一二

文章分类

全部博文(93)

文章存档

2016年(2)

2015年(3)

2014年(11)

2013年(29)

2012年(16)

2011年(5)

2010年(5)

2009年(22)

分类: LINUX

2011-03-18 18:20:11

 

  1. void
  2. process_tcp(u_char * data, int skblen)
  3. {
  4.   
  5.   struct ip *this_iphdr = (struct ip *)data;
  6.   /*tcphdr 的头*/
  7.   struct tcphdr *this_tcphdr = (struct tcphdr *)(data + 4 * this_iphdr->ip_hl);
  8.   int datalen, iplen;
  9.   int from_client = 1; /*客户发送数据*/
  10.   unsigned int tmp_ts;
  11.      
  12.   /*流分为客户端服务端*/
  13.   struct tcp_stream *a_tcp;
  14.   struct half_stream *snd, *rcv;
  15.   
  16.   ugly_iphdr = this_iphdr;
  17.   iplen = ntohs(this_iphdr->ip_len);
  18.      
  19.   if ((unsigned)iplen < 4 * this_iphdr->ip_hl + sizeof(struct tcphdr)) {
  20.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  21.                this_tcphdr);
  22.     return;
  23.   } // ktos sie bawi

  24.   
  25.   /*tcp 数据长度*/
  26.   datalen = iplen - 4 * this_iphdr->ip_hl - 4 * this_tcphdr->th_off;
  27.      
  28.   if (datalen < 0) {
  29.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  30.                this_tcphdr);
  31.     return;
  32.   } // ktos sie bawi

  33.   
  34.   
  35.   /*源和目的地址都存在*/
  36.   
  37.   if ((this_iphdr->ip_src.s_addr | this_iphdr->ip_dst.s_addr) == 0) {
  38.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  39.                this_tcphdr);
  40.     return;
  41.   }
  42.   
  43.   /*如果没有th_ack 包,则进行扫描是否有攻击包*/
  44.   if (!(this_tcphdr->th_flags & TH_ACK))
  45.     detect_scan(this_iphdr);
  46.     /*

  47.    /* 表示有扫描攻击发生 */
  48.          
  49.   if (!nids_params.n_tcp_streams) return;
  50.     
  51.   /*tcp 头的长度,iplen -4*this_iphdr->ip_hl, 进行包头的校验*/
  52.   if (my_tcp_check(this_tcphdr, iplen - 4 * this_iphdr->ip_hl,
  53.            this_iphdr->ip_src.s_addr, this_iphdr->ip_dst.s_addr)) {
  54.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  55.                this_tcphdr);
  56.     return;
  57.        
  58.   }
  59.     
  60. #if 0
  61.   check_flags(this_iphdr, this_tcphdr);
  62. //ECN

  63. #endif
  64.   
  65. /*查找添加流*/
  66.   
  67. /*

  68.     ************* 三次握手的第一次握手*****************************************

  69. */
  70.   
  71.   if (!(a_tcp = find_stream(this_tcphdr, this_iphdr, &from_client))) {
  72.   
  73.    /*是三次握手的第一个包*/
  74.   /*tcp里流不存在时:且tcp数据包里的(syn=1 && ack==0 && rst==0)时,添加一条tcp流*/
  75.  /*tcp第一次握手*/
  76.     if ((this_tcphdr->th_flags & TH_SYN) &&
  77.     !(this_tcphdr->th_flags & TH_ACK) &&
  78.     !(this_tcphdr->th_flags & TH_RST))
  79.     /*并且没有收到th_rest 包*/
  80.       add_new_tcp(this_tcphdr, this_iphdr);/*节点加入链表中*/
  81.   
  82.    /*第一次握手完毕返回*/
  83.     return;
  84.   }
  85.   
  86.     
  87.   if (from_client) { /*从client --> server的包*/
  88.     snd = &a_tcp->client;
  89.     rcv = &a_tcp->server;
  90.   }
  91.   else {/* server --> client的包 */
  92.     rcv = &a_tcp->client;
  93.     snd = &a_tcp->server;
  94.   }
  95.   
  96.   
  97. /**********************************************************************

  98.                 三次握手的第二次握手
  99.    
  100. ************************************************************************/
  101.   
  102.    /*tcp 三次握手, SYN ==1,ACK==1,tcp第二次握手(server -> client的同步响应)*/
  103.   if ((this_tcphdr->th_flags & TH_SYN)) {
  104.     if (from_client || a_tcp->client.state != TCP_SYN_SENT ||
  105.       a_tcp->server.state != TCP_CLOSE || !(this_tcphdr->th_flags & TH_ACK))
  106.       return;
  107.   
  108.   
  109. /*第二次回应包的ACK 值为第一个包的序列号+1,在初始化的时候已经加一*/
  110.     if (a_tcp->client.seq != ntohl(this_tcphdr->th_ack))
  111.       return;
  112.        
  113.        
  114.     /*第二个包服务端赋值*/
  115.     /*a_tcp 中服务端赋值,*/
  116.   
  117.     a_tcp->server.state = TCP_SYN_RECV;
  118.     a_tcp->server.seq = ntohl(this_tcphdr->th_seq) + 1;
  119.     a_tcp->server.first_data_seq = a_tcp->server.seq;
  120.     a_tcp->server.ack_seq = ntohl(this_tcphdr->th_ack);
  121.     a_tcp->server.window = ntohs(this_tcphdr->th_win);
  122.   
  123.   
  124.   /*对于tcp 选项的赋值*/
  125.   //初始化客户端和服务器的时间截

  126.     if (a_tcp->client.ts_on) {
  127.         a_tcp->server.ts_on = get_ts(this_tcphdr, &a_tcp->server.curr_ts);
  128.     if (!a_tcp->server.ts_on)
  129.         a_tcp->client.ts_on = 0;
  130.     } else a_tcp->server.ts_on = 0;
  131.   
  132.   
  133. //初始化窗口大小

  134.     if (a_tcp->client.wscale_on) {
  135.         a_tcp->server.wscale_on = get_wscale(this_tcphdr, &a_tcp->server.wscale);
  136.     if (!a_tcp->server.wscale_on) {
  137.         a_tcp->client.wscale_on = 0;
  138.         a_tcp->client.wscale = 1;
  139.         a_tcp->server.wscale = 1;
  140.     }
  141.     } else {
  142.         a_tcp->server.wscale_on = 0;
  143.         a_tcp->server.wscale = 1;
  144.     }
  145.  /*第二次握手完毕,返回*/
  146.        
  147.     return;
  148.   }
  149.      
  150.   
  151.   
  152.   
  153.   
  154.    /*
  155.                 (如果有数据存在或者修列号不等于确认号的)并且

  156.         序列号在窗口之外
  157.         已经确认过的序号

  158.    */
  159.     
  160.   if (
  161.     ! ( !datalen && ntohl(this_tcphdr->th_seq) == rcv->ack_seq )
  162.     &&
  163.   
  164.     /*th_seq - (ack_seq+ wscale) > 0 或者th_seq+datalen - ack_sql < 0*/
  165.     ( !before(ntohl(this_tcphdr->th_seq), rcv->ack_seq + rcv->window*rcv->wscale) ||
  166.           before(ntohl(this_tcphdr->th_seq) + datalen, rcv->ack_seq)
  167.         )
  168.      )
  169.      return;
  170.   
  171.      
  172.   
  173.   /*发送th_rst 重新开启一个连接*/
  174.   if ((this_tcphdr->th_flags & TH_RST)) {
  175.     /*是tcp 数据*/
  176.     if (a_tcp->nids_state == NIDS_DATA) {
  177.       struct lurker_node *i;
  178.       a_tcp->nids_state = NIDS_RESET;
  179.       for (i = a_tcp->listeners; i; i = i->next)
  180.     (i->item) (a_tcp, &i->data);
  181.     }
  182.     nids_free_tcp_stream(a_tcp);
  183.     return;
  184.   }
  185.   
  186.   
  187.   
  188.   
  189.   /* PAWS check */
  190.   if (rcv->ts_on && get_ts(this_tcphdr, &tmp_ts) &&
  191.     before(tmp_ts, snd->curr_ts))
  192.   return;
  193.   
  194.   
  195.   
  196.   
  197.   
  198.   
  199. /*
  200.     **********************************************************************
  201.                         第三次握手包

  202.     **********************************************************************
  203. */
  204.   
  205.   /*
  206.     

  207.   从client --> server的包

  208.    是从三次握手的第三个包分析开始的,进行一部分数据分析,和初始化
  209.    连接状态

  210.   */
  211.      
  212.   if ((this_tcphdr->th_flags & TH_ACK)) {
  213.     if (from_client && a_tcp->client.state == TCP_SYN_SENT &&
  214.     a_tcp->server.state == TCP_SYN_RECV) {
  215.       if (ntohl(this_tcphdr->th_ack) == a_tcp->server.seq) {
  216.     a_tcp->client.state = TCP_ESTABLISHED;
  217.     a_tcp->client.ack_seq = ntohl(this_tcphdr->th_ack);
  218.     {
  219.       struct proc_node *i;
  220.       struct lurker_node *j;
  221.       void *data;
  222.          
  223.       a_tcp->server.state = TCP_ESTABLISHED;
  224.       a_tcp->nids_state = NIDS_JUST_EST;
  225.       /*开始全双工传输,client server 连接已经建立起来了*/
  226.   
  227.       /*三次握手tcp ip 连接建立*/
  228.       for (i = tcp_procs; i; i = i->next) {
  229.         char whatto = 0;
  230.            
  231.         char cc = a_tcp->client.collect;
  232.         char sc = a_tcp->server.collect;
  233.         char ccu = a_tcp->client.collect_urg;
  234.         char scu = a_tcp->server.collect_urg;
  235.   
  236.         /*进入回调函数处理*/
  237.   
  238.         /*

  239.             如果在相应端口出现

  240.         client.collect ++ ;

  241.         测审计次数据
  242.         对应用来说tcp 连接已经建立

  243.        */
  244.           
  245.           
  246.         (i->item) (a_tcp, &data);
  247.   
  248.        /**/
  249.         if (cc < a_tcp->client.collect)
  250.           whatto |= COLLECT_cc;
  251.         if (ccu < a_tcp->client.collect_urg)
  252.           whatto |= COLLECT_ccu;
  253.         if (sc < a_tcp->server.collect)
  254.           whatto |= COLLECT_sc;
  255.         if (scu < a_tcp->server.collect_urg)
  256.           whatto |= COLLECT_scu;
  257.         if (nids_params.one_loop_less) {
  258.                 if (a_tcp->client.collect >=2) {
  259.                     a_tcp->client.collect=cc;
  260.                     whatto&=~COLLECT_cc;
  261.                 }
  262.                 if (a_tcp->server.collect >=2 ) {
  263.                     a_tcp->server.collect=sc;
  264.                     whatto&=~COLLECT_sc;
  265.                 }
  266.         }
  267.               
  268.        /*加入监听队列,开始数据接收*/
  269.         if (whatto) {
  270.           j = mknew(struct lurker_node);
  271.           j->item = i->item;/*放入监听队列*/
  272.           j->data = data;
  273.           j->whatto = whatto;
  274.              
  275.           j->next = a_tcp->listeners;
  276.           a_tcp->listeners = j;
  277.         }
  278.            
  279.       }
  280.          
  281.          
  282.       /*不存在监听着*/{
  283.         nids_free_tcp_stream(a_tcp);
  284.         return;
  285.       }
  286.       if (!a_tcp->listeners)
  287.          
  288.       a_tcp->nids_state = NIDS_DATA;
  289.     }
  290.       }
  291.       // return;

  292.     }
  293.   }
  294.   
  295.   
  296.   
  297. /*
  298. ************************************************************

  299.                 挥手过程

  300. *************************************************************

  301. */
  302.   
  303. /*数据结束的包的判断*/
  304.   
  305.   if ((this_tcphdr->th_flags & TH_ACK)) {
  306.        
  307.   
  308.    /* 从数据传输过程不断更新服务器客户端的ack_seq
  309.     一直到接收到fin 包,数据传输结束

  310.    */
  311.     handle_ack(snd, ntohl(this_tcphdr->th_ack));
  312.        
  313.     if (rcv->state == FIN_SENT)
  314.       rcv->state = FIN_CONFIRMED;
  315.     if (rcv->state == FIN_CONFIRMED && snd->state == FIN_CONFIRMED) {
  316.       struct lurker_node *i;
  317.   
  318.       a_tcp->nids_state = NIDS_CLOSE;
  319.       for (i = a_tcp->listeners; i; i = i->next)
  320.     (i->item) (a_tcp, &i->data);
  321.       nids_free_tcp_stream(a_tcp);
  322.       return;
  323.     }
  324.   }
  325.   
  326.   
  327.  /*

  328. *************************************************************
  329.                         数据处理过程
  330. *************************************************************

  331.  */
  332.   
  333.   
  334.     
  335.      
  336.   if (datalen + (this_tcphdr->th_flags & TH_FIN) > 0)
  337.   
  338.   /*
  339.           
  340.     a_tcp -----a_tcp 客户端连接包
  341.     this_tcphdr tcp 包头
  342.     snd-----发送包
  343.     rcv -----接收包

  344.     (char *) (this_tcphdr) + 4 * this_tcphdr->th_off -----数据包内容
  345.     datalen---------数据包长度
  346.       

  347.   */
  348.      
  349.     tcp_queue(a_tcp, this_tcphdr, snd, rcv,
  350.           (char *) (this_tcphdr) + 4 * this_tcphdr->th_off,
  351.           datalen, skblen);
  352.      
  353.   snd->window = ntohs(this_tcphdr->th_win);
  354.      
  355.   if (rcv->rmem_alloc > 65535)
  356.        
  357.     prune_queue(rcv, this_tcphdr);
  358.      
  359.   if (!a_tcp->listeners)
  360.     nids_free_tcp_stream(a_tcp);
  361.   
  362.   
  363. }
  364. void
  365. process_tcp(u_char * data, int skblen)
  366. {
  367.   struct ip *this_iphdr = (struct ip *)data;
  368.   /*tcphdr 的头*/
  369.   struct tcphdr *this_tcphdr = (struct tcphdr *)(data + 4 * this_iphdr->ip_hl);
  370.   int datalen, iplen;
  371.   int from_client = 1; /*客户发送数据*/
  372.   unsigned int tmp_ts;
  373.   
  374.   /*流分为客户端服务端*/
  375.   struct tcp_stream *a_tcp;
  376.   struct half_stream *snd, *rcv;
  377.   ugly_iphdr = this_iphdr;
  378.   iplen = ntohs(this_iphdr->ip_len);
  379.   
  380.   if ((unsigned)iplen < 4 * this_iphdr->ip_hl + sizeof(struct tcphdr)) {
  381.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  382.          this_tcphdr);
  383.     return;
  384.   } // ktos sie bawi

  385.   /*tcp 数据长度*/
  386.   datalen = iplen - 4 * this_iphdr->ip_hl - 4 * this_tcphdr->th_off;
  387.   
  388.   if (datalen < 0) {
  389.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  390.          this_tcphdr);
  391.     return;
  392.   } // ktos sie bawi


  393.   /*源和目的地址都存在*/
  394.   if ((this_iphdr->ip_src.s_addr | this_iphdr->ip_dst.s_addr) == 0) {
  395.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  396.          this_tcphdr);
  397.     return;
  398.   }
  399.   /*如果没有th_ack 包,则进行扫描是否有攻击包*/
  400.   if (!(this_tcphdr->th_flags & TH_ACK))
  401.     detect_scan(this_iphdr);
  402.     /*
  403.    /* 表示有扫描攻击发生 */
  404.    
  405.   if (!nids_params.n_tcp_streams) return;

  406.   /*tcp 头的长度,iplen -4*this_iphdr->ip_hl, 进行包头的校验*/
  407.   if (my_tcp_check(this_tcphdr, iplen - 4 * this_iphdr->ip_hl,
  408.      this_iphdr->ip_src.s_addr, this_iphdr->ip_dst.s_addr)) {
  409.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,
  410.          this_tcphdr);
  411.     return;

  412.   }
  413.   
  414. #if 0
  415.   check_flags(this_iphdr, this_tcphdr);
  416. //ECN

  417. #endif
  418. /*查找添加流*/
  419. /*
  420.  ************* 三次握手的第一次握手*****************************************
  421. */
  422.   if (!(a_tcp = find_stream(this_tcphdr, this_iphdr, &from_client))) {
  423.    /*是三次握手的第一个包*/
  424.   /*tcp里流不存在时:且tcp数据包里的(syn=1 && ack==0 && rst==0)时,添加一条tcp流*/
  425.  /*tcp第一次握手*/
  426.     if ((this_tcphdr->th_flags & TH_SYN) &&
  427.  !(this_tcphdr->th_flags & TH_ACK) &&
  428.  !(this_tcphdr->th_flags & TH_RST))
  429.  /*并且没有收到th_rest 包*/
  430.       add_new_tcp(this_tcphdr, this_iphdr);/*节点加入链表中*/
  431.    /*第一次握手完毕返回*/
  432.     return;
  433.   }

  434.   if (from_client) { /*从client --> server的包*/
  435.     snd = &a_tcp->client;
  436.     rcv = &a_tcp->server;
  437.   }
  438.   else {/* server --> client的包 */
  439.     rcv = &a_tcp->client;
  440.     snd = &a_tcp->server;
  441.   }

  442. /**********************************************************************
  443.     三次握手的第二次握手

  444. ************************************************************************/
  445.    /*tcp 三次握手, SYN ==1,ACK==1,tcp第二次握手(server -> client的同步响应)*/
  446.   if ((this_tcphdr->th_flags & TH_SYN)) {
  447.     if (from_client || a_tcp->client.state != TCP_SYN_SENT ||
  448.       a_tcp->server.state != TCP_CLOSE || !(this_tcphdr->th_flags & TH_ACK))
  449.       return;

  450. /*第二次回应包的ACK 值为第一个包的序列号+1,在初始化的时候已经加一*/
  451.     if (a_tcp->client.seq != ntohl(this_tcphdr->th_ack))
  452.       return;


  453.  /*第二个包服务端赋值*/
  454.  /*a_tcp 中服务端赋值,*/
  455.     a_tcp->server.state = TCP_SYN_RECV;
  456.     a_tcp->server.seq = ntohl(this_tcphdr->th_seq) + 1;
  457.     a_tcp->server.first_data_seq = a_tcp->server.seq;
  458.     a_tcp->server.ack_seq = ntohl(this_tcphdr->th_ack);
  459.     a_tcp->server.window = ntohs(this_tcphdr->th_win);

  460.   /*对于tcp 选项的赋值*/
  461.   //初始化客户端和服务器的时间截

  462.     if (a_tcp->client.ts_on) {
  463.      a_tcp->server.ts_on = get_ts(this_tcphdr, &a_tcp->server.curr_ts);
  464.  if (!a_tcp->server.ts_on)
  465.   a_tcp->client.ts_on = 0;
  466.     } else a_tcp->server.ts_on = 0;

  467. //初始化窗口大小

  468.     if (a_tcp->client.wscale_on) {
  469.      a_tcp->server.wscale_on = get_wscale(this_tcphdr, &a_tcp->server.wscale);
  470.  if (!a_tcp->server.wscale_on) {
  471.   a_tcp->client.wscale_on = 0;
  472.   a_tcp->client.wscale = 1;
  473.   a_tcp->server.wscale = 1;
  474.  }
  475.     } else {
  476.      a_tcp->server.wscale_on = 0;
  477.      a_tcp->server.wscale = 1;
  478.     }
  479.  /*第二次握手完毕,返回*/

  480.     return;
  481.   }
  482.   


  483.    /*
  484.                 (如果有数据存在或者修列号不等于确认号的)并且
  485.   序列号在窗口之外
  486.    已经确认过的序号
  487.    */

  488.   if (
  489.    ! ( !datalen && ntohl(this_tcphdr->th_seq) == rcv->ack_seq )
  490.    &&
  491.   /*th_seq - (ack_seq+ wscale) > 0 或者th_seq+datalen - ack_sql < 0*/
  492.    ( !before(ntohl(this_tcphdr->th_seq), rcv->ack_seq + rcv->window*rcv->wscale) ||
  493.           before(ntohl(this_tcphdr->th_seq) + datalen, rcv->ack_seq)
  494.         )
  495.      )
  496.      return;
  497.   
  498.   /*发送th_rst 重新开启一个连接*/
  499.   if ((this_tcphdr->th_flags & TH_RST)) {
  500.    /*是tcp 数据*/
  501.     if (a_tcp->nids_state == NIDS_DATA) {
  502.       struct lurker_node *i;
  503.       a_tcp->nids_state = NIDS_RESET;
  504.       for (i = a_tcp->listeners; i; i = i->next)
  505.  (i->item) (a_tcp, &i->data);
  506.     }
  507.     nids_free_tcp_stream(a_tcp);
  508.     return;
  509.   }


  510.   /* PAWS check */
  511.   if (rcv->ts_on && get_ts(this_tcphdr, &tmp_ts) &&
  512.    before(tmp_ts, snd->curr_ts))
  513.   return;



  514. /*
  515.  **********************************************************************
  516.       第三次握手包
  517.  **********************************************************************
  518. */
  519.   /*
  520.   
  521.   从client --> server的包
  522.    是从三次握手的第三个包分析开始的,进行一部分数据分析,和初始化
  523.    连接状态
  524.   */
  525.   
  526.   if ((this_tcphdr->th_flags & TH_ACK)) {
  527.     if (from_client && a_tcp->client.state == TCP_SYN_SENT &&
  528.  a_tcp->server.state == TCP_SYN_RECV) {
  529.       if (ntohl(this_tcphdr->th_ack) == a_tcp->server.seq) {
  530.  a_tcp->client.state = TCP_ESTABLISHED;
  531.  a_tcp->client.ack_seq = ntohl(this_tcphdr->th_ack);
  532.  {
  533.    struct proc_node *i;
  534.    struct lurker_node *j;
  535.    void *data;
  536.    
  537.    a_tcp->server.state = TCP_ESTABLISHED;
  538.    a_tcp->nids_state = NIDS_JUST_EST;
  539.    /*开始全双工传输,client server 连接已经建立起来了*/
  540.    /*三次握手tcp ip 连接建立*/
  541.    for (i = tcp_procs; i; i = i->next) {
  542.      char whatto = 0;
  543.   
  544.      char cc = a_tcp->client.collect;
  545.      char sc = a_tcp->server.collect;
  546.      char ccu = a_tcp->client.collect_urg;
  547.      char scu = a_tcp->server.collect_urg;
  548.      /*进入回调函数处理*/
  549.      /*
  550.          如果在相应端口出现
  551.   client.collect ++ ;
  552.   测审计次数据
  553.   对应用来说tcp 连接已经建立
  554.     */
  555.     
  556.     
  557.      (i->item) (a_tcp, &data);
  558.     /**/
  559.      if (cc < a_tcp->client.collect)
  560.        whatto |= COLLECT_cc;
  561.      if (ccu < a_tcp->client.collect_urg)
  562.        whatto |= COLLECT_ccu;
  563.      if (sc < a_tcp->server.collect)
  564.        whatto |= COLLECT_sc;
  565.      if (scu < a_tcp->server.collect_urg)
  566.        whatto |= COLLECT_scu;
  567.      if (nids_params.one_loop_less) {
  568.        if (a_tcp->client.collect >=2) {
  569.         a_tcp->client.collect=cc;
  570.         whatto&=~COLLECT_cc;
  571.        }
  572.        if (a_tcp->server.collect >=2 ) {
  573.         a_tcp->server.collect=sc;
  574.         whatto&=~COLLECT_sc;
  575.        }
  576.      }
  577.            
  578.     /*加入监听队列,开始数据接收*/
  579.      if (whatto) {
  580.        j = mknew(struct lurker_node);
  581.        j->item = i->item;/*放入监听队列*/
  582.        j->data = data;
  583.        j->whatto = whatto;
  584.     
  585.        j->next = a_tcp->listeners;
  586.        a_tcp->listeners = j;
  587.      }
  588.   
  589.    }
  590.    
  591.    
  592.    /*不存在监听着*/{
  593.      nids_free_tcp_stream(a_tcp);
  594.      return;
  595.    }
  596.    if (!a_tcp->listeners)
  597.    
  598.    a_tcp->nids_state = NIDS_DATA;
  599.  }
  600.       }
  601.       // return;

  602.     }
  603.   }

  604. /*
  605. ************************************************************
  606.     挥手过程
  607. *************************************************************
  608. */
  609. /*数据结束的包的判断*/
  610.   if ((this_tcphdr->th_flags & TH_ACK)) {
  611.    
  612.    /* 从数据传输过程不断更新服务器客户端的ack_seq
  613.  一直到接收到fin 包,数据传输结束
  614.    */
  615.     handle_ack(snd, ntohl(this_tcphdr->th_ack));

  616.     if (rcv->state == FIN_SENT)
  617.       rcv->state = FIN_CONFIRMED;
  618.     if (rcv->state == FIN_CONFIRMED && snd->state == FIN_CONFIRMED) {
  619.       struct lurker_node *i;
  620.       a_tcp->nids_state = NIDS_CLOSE;
  621.       for (i = a_tcp->listeners; i; i = i->next)
  622.  (i->item) (a_tcp, &i->data);
  623.       nids_free_tcp_stream(a_tcp);
  624.       return;
  625.     }
  626.   }

  627.  /*
  628. *************************************************************
  629.       数据处理过程
  630. *************************************************************
  631.  */


  632.   
  633.   if (datalen + (this_tcphdr->th_flags & TH_FIN) > 0)
  634.   /*
  635.   
  636.  a_tcp -----a_tcp 客户端连接包
  637.  this_tcphdr tcp 包头
  638.  snd-----发送包
  639.  rcv -----接收包
  640.  (char *) (this_tcphdr) + 4 * this_tcphdr->th_off -----数据包内容
  641.  datalen---------数据包长度

  642.   */
  643.   
  644.     tcp_queue(a_tcp, this_tcphdr, snd, rcv,
  645.        (char *) (this_tcphdr) + 4 * this_tcphdr->th_off,
  646.        datalen, skblen);
  647.   
  648.   snd->window = ntohs(this_tcphdr->th_win);
  649.   
  650.   if (rcv->rmem_alloc > 65535)
  651.    
  652.     prune_queue(rcv, this_tcphdr);
  653.   
  654.   if (!a_tcp->listeners)
  655.     nids_free_tcp_stream(a_tcp);

  656. }

  657. view plaincopy to clipboardprint?
  658. //判断 该TCP 数据包是添加到数据区还是添加到list双向链表中

  659. static void
  660. tcp_queue(struct tcp_stream * a_tcp, struct tcphdr * this_tcphdr,
  661.       struct half_stream * snd, struct half_stream* rcv,
  662.       char *data, int datalen, int skblen
  663.       )
  664. {
  665.   
  666.   u_int this_seq = ntohl(this_tcphdr->th_seq);
  667.      
  668.   struct skbuff *pakiet, *tmp;
  669.   
  670.   
  671.  /*
  672.         #define EXP_SEQ (snd->first_data_seq + rcv->count + rcv->urg_count)
  673.     EXP_SEQ > this_seq
  674.   */
  675.   
  676.   /* 如果EXP_SEQ > = this_seq */
  677.   /*

  678.   EXP_SEQ 期望到达的数据
  679.   this_seq 实际到达的数据

  680.   */
  681.      
  682.   if ( !after(this_seq, EXP_SEQ) ) {
  683.   
  684.     /*this_seq + datalen + (this_tcphdr->th_flags & TH_FIN)> EXP_SEQ*/
  685.   
  686.        
  687.     /*有重叠数据存在(重叠数据怎么处理呢)

  688.         也可能没有重叠数据
  689.      */
  690.        
  691.     if ( after(this_seq + datalen + (this_tcphdr->th_flags & TH_FIN), EXP_SEQ) ) {
  692.            
  693.       /* the packet straddles our window end */
  694.       get_ts(this_tcphdr, &snd->curr_ts);
  695.          
  696.       add_from_skb(a_tcp, rcv, snd, data, datalen, this_seq,
  697.            (this_tcphdr->th_flags & TH_FIN),
  698.            (this_tcphdr->th_flags & TH_URG),
  699.                    ntohs(this_tcphdr->th_urp) + this_seq - 1);
  700.     /*
  701.     * Do we have any old packets to ack that the above
  702.     * made visible? (Go forward from skb)
  703.     */
  704.   
  705.   
  706.   
  707.      /*从头节点释放节点*/
  708.       pakiet = rcv->list;
  709.       while (pakiet) {
  710.      /*

  711.         期望的序列号小于该包的序列号则直接返回

  712.      */
  713.     if (after(pakiet->seq, EXP_SEQ))
  714.       break;
  715.        
  716.     /*

  717.     对失序队列数据包的处理

  718.        如果包序列号加上长度还小于期望序列号
  719.        则把该包添加到数据区,并在失序队列中删除该包

  720.     */
  721.     if (after(pakiet->seq + pakiet->len + pakiet->fin, EXP_SEQ)) {
  722.       add_from_skb(a_tcp, rcv, snd, pakiet->data,
  723.                pakiet->len, pakiet->seq, pakiet->fin, pakiet->urg,
  724.                pakiet->urg_ptr + pakiet->seq - 1);
  725.         }
  726.     rcv->rmem_alloc -= pakiet->truesize;
  727.        
  728.         /*从头节点释放链表*/
  729.            
  730.     if (pakiet->prev)
  731.       pakiet->prev->next = pakiet->next;
  732.     else
  733.       rcv->list = pakiet->next;
  734.     if (pakiet->next)
  735.       pakiet->next->prev = pakiet->prev;
  736.     else
  737.       rcv->listtail = pakiet->prev;
  738.     tmp = pakiet->next;
  739.     free(pakiet->data);
  740.     free(pakiet);
  741.     pakiet = tmp;
  742.       }
  743.     }
  744.     else
  745.       return;
  746.   }
  747.   
  748.      
  749. /*提前到达数据的加入失去顺序队列中链表*/
  750.   
  751. //序列号大于我们期望的序列号,则把该包添加到list双向链表中

  752.   
  753.   
  754.   /*链表加入节点 */
  755.   else {
  756.   
  757.     /*初始化*/
  758.     struct skbuff *p = rcv->listtail;
  759.        /*
  760.     pakiet 加入rcv->listtal 链表中
  761.        */
  762.        /*
  763.     pakiet 节点的初始化
  764.       
  765.     */
  766.        
  767.     pakiet = mknew(struct skbuff);
  768.     pakiet->truesize = skblen;
  769.     rcv->rmem_alloc += pakiet->truesize;
  770.   
  771.     /*数据包填充pakiet->data 中*/
  772.     pakiet->len = datalen;
  773.     pakiet->data = malloc(datalen);
  774.     if (!pakiet->data)
  775.     nids_params.no_mem("tcp_queue");
  776.     memcpy(pakiet->data, data, datalen);
  777.   
  778.     /*是否是结束包*/
  779.     pakiet->fin = (this_tcphdr->th_flags & TH_FIN);
  780.       
  781.     /* Some Cisco - at least - hardware accept to close a TCP connection
  782.      * even though packets were lost before the first TCP FIN packet and
  783.      * never retransmitted; this violates RFC 793, but since it really
  784.      * happens, it has to be dealt with... The idea is to introduce a 10s
  785.      * timeout after TCP FIN packets were sent by both sides so that
  786.      * corresponding libnids resources can be released instead of waiting
  787.      * for retransmissions which will never happen. -- Sebastien Raveau
  788.      */
  789.       /*

  790.          硬件接收一个tcp 终止的链接,
  791.          即使包在一个结束包的时候丢失,并且不再重传,
  792.          处理这种方法就是引入时间机制处理

  793.       */
  794.          
  795.     if (pakiet->fin) {
  796.       snd->state = TCP_CLOSING;
  797.       if (rcv->state == FIN_SENT || rcv->state == FIN_CONFIRMED)
  798.     add_tcp_closing_timeout(a_tcp);
  799.     }
  800.        
  801.      /*
  802.       
  803.      seq,urg ,urg_ptr 赋值

  804.     */
  805.        
  806.     pakiet->seq = this_seq;
  807.     pakiet->urg = (this_tcphdr->th_flags & TH_URG);
  808.     pakiet->urg_ptr = ntohs(this_tcphdr->th_urp);
  809.        
  810.     for (;;) {
  811.            
  812.       if (!p || !after(p->seq, this_seq))
  813.     break;
  814.       p = p->prev;
  815. }
  816.        
  817.     if (!p) {
  818. /*建立首节点*/
  819.       pakiet->prev = 0;
  820.       pakiet->next = rcv->list;
  821.       if (rcv->list)
  822.          rcv->list->prev = pakiet;
  823.          
  824.       rcv->list = pakiet;
  825.       if (!rcv->listtail)
  826.     rcv->listtail = pakiet;
  827.     }
  828.   
  829. /*链表后插入*/
  830.     else {
  831.       pakiet->next = p->next;
  832.       p->next = pakiet;
  833.       pakiet->prev = p;
  834.       if (pakiet->next)
  835.     pakiet->next->prev = pakiet;
  836.       else
  837.     rcv->listtail = pakiet;
  838.     }
  839.        
  840.   }
  841.      
  842. }
  843. //判断 该TCP 数据包是添加到数据区还是添加到list双向链表中

  844. static void
  845. tcp_queue(struct tcp_stream * a_tcp, struct tcphdr * this_tcphdr,
  846.    struct half_stream * snd, struct half_stream* rcv,
  847.    char *data, int datalen, int skblen
  848.    )
  849. {
  850.   u_int this_seq = ntohl(this_tcphdr->th_seq);
  851.   
  852.   struct skbuff *pakiet, *tmp;

  853.  /*
  854.         #define EXP_SEQ (snd->first_data_seq + rcv->count + rcv->urg_count)
  855.  EXP_SEQ > this_seq
  856.   */
  857.   /* 如果EXP_SEQ > = this_seq */
  858.   /*
  859.   EXP_SEQ 期望到达的数据
  860.   this_seq 实际到达的数据
  861.   */
  862.   
  863.   if ( !after(this_seq, EXP_SEQ) ) {
  864.     /*this_seq + datalen + (this_tcphdr->th_flags & TH_FIN)> EXP_SEQ*/

  865.     /*有重叠数据存在(重叠数据怎么处理呢)
  866.         也可能没有重叠数据
  867.      */

  868.     if ( after(this_seq + datalen + (this_tcphdr->th_flags & TH_FIN), EXP_SEQ) ) {
  869.   
  870.       /* the packet straddles our window end */
  871.       get_ts(this_tcphdr, &snd->curr_ts);
  872.    
  873.       add_from_skb(a_tcp, rcv, snd, data, datalen, this_seq,
  874.      (this_tcphdr->th_flags & TH_FIN),
  875.      (this_tcphdr->th_flags & TH_URG),
  876.        ntohs(this_tcphdr->th_urp) + this_seq - 1);
  877.  /*
  878.  * Do we have any old packets to ack that the above
  879.  * made visible? (Go forward from skb)
  880.  */

  881.      /*从头节点释放节点*/
  882.       pakiet = rcv->list;
  883.       while (pakiet) {
  884.   /*
  885.   期望的序列号小于该包的序列号则直接返回
  886.   */
  887.  if (after(pakiet->seq, EXP_SEQ))
  888.    break;

  889.  /*
  890.  对失序队列数据包的处理
  891.     如果包序列号加上长度还小于期望序列号
  892.     则把该包添加到数据区,并在失序队列中删除该包
  893.  */
  894.  if (after(pakiet->seq + pakiet->len + pakiet->fin, EXP_SEQ)) {
  895.    add_from_skb(a_tcp, rcv, snd, pakiet->data,
  896.          pakiet->len, pakiet->seq, pakiet->fin, pakiet->urg,
  897.          pakiet->urg_ptr + pakiet->seq - 1);
  898.         }
  899.  rcv->rmem_alloc -= pakiet->truesize;

  900.         /*从头节点释放链表*/
  901.   
  902.  if (pakiet->prev)
  903.    pakiet->prev->next = pakiet->next;
  904.  else
  905.    rcv->list = pakiet->next;
  906.  if (pakiet->next)
  907.    pakiet->next->prev = pakiet->prev;
  908.  else
  909.    rcv->listtail = pakiet->prev;
  910.  tmp = pakiet->next;
  911.  free(pakiet->data);
  912.  free(pakiet);
  913.  pakiet = tmp;
  914.       }
  915.     }
  916.     else
  917.       return;
  918.   }
  919.   
  920. /*提前到达数据的加入失去顺序队列中链表*/
  921. //序列号大于我们期望的序列号,则把该包添加到list双向链表中


  922.   /*链表加入节点 */
  923.   else {
  924.     /*初始化*/
  925.     struct skbuff *p = rcv->listtail;
  926.        /*
  927.  pakiet 加入rcv->listtal 链表中
  928.        */
  929.        /*
  930.  pakiet 节点的初始化

  931.  */

  932.     pakiet = mknew(struct skbuff);
  933.     pakiet->truesize = skblen;
  934.     rcv->rmem_alloc += pakiet->truesize;
  935.     /*数据包填充pakiet->data 中*/
  936.     pakiet->len = datalen;
  937.     pakiet->data = malloc(datalen);
  938.     if (!pakiet->data)
  939.     nids_params.no_mem("tcp_queue");
  940.     memcpy(pakiet->data, data, datalen);
  941.     /*是否是结束包*/
  942.     pakiet->fin = (this_tcphdr->th_flags & TH_FIN);
  943.    
  944.     /* Some Cisco - at least - hardware accept to close a TCP connection
  945.      * even though packets were lost before the first TCP FIN packet and
  946.      * never retransmitted; this violates RFC 793, but since it really
  947.      * happens, it has to be dealt with... The idea is to introduce a 10s
  948.      * timeout after TCP FIN packets were sent by both sides so that
  949.      * corresponding libnids resources can be released instead of waiting
  950.      * for retransmissions which will never happen. -- Sebastien Raveau
  951.      */
  952.       /*
  953.          硬件接收一个tcp 终止的链接,
  954.          即使包在一个结束包的时候丢失,并且不再重传,
  955.          处理这种方法就是引入时间机制处理
  956.       */
  957.       
  958.     if (pakiet->fin) {
  959.       snd->state = TCP_CLOSING;
  960.       if (rcv->state == FIN_SENT || rcv->state == FIN_CONFIRMED)
  961.  add_tcp_closing_timeout(a_tcp);
  962.     }

  963.      /*

  964.      seq,urg ,urg_ptr 赋值
  965.     */
  966.     
  967.     pakiet->seq = this_seq;
  968.     pakiet->urg = (this_tcphdr->th_flags & TH_URG);
  969.     pakiet->urg_ptr = ntohs(this_tcphdr->th_urp);

  970.     for (;;) {
  971.   
  972.       if (!p || !after(p->seq, this_seq))
  973.  break;
  974.       p = p->prev;
  975. }

  976.     if (!p) {
  977. /*建立首节点*/
  978.       pakiet->prev = 0;
  979.       pakiet->next = rcv->list;
  980.       if (rcv->list)
  981.          rcv->list->prev = pakiet;
  982.    
  983.       rcv->list = pakiet;
  984.       if (!rcv->listtail)
  985.  rcv->listtail = pakiet;
  986.     }
  987. /*链表后插入*/
  988.     else {
  989.       pakiet->next = p->next;
  990.       p->next = pakiet;
  991.       pakiet->prev = p;
  992.       if (pakiet->next)
  993.  pakiet->next->prev = pakiet;
  994.       else
  995.  rcv->listtail = pakiet;
  996.     }

  997.   }
  998.   
  999. }

  1000. view plaincopy to clipboardprint?
  1001. /*

  1002. 分配空间,数据保存rcv->data 中
  1003. 长度为:rcv->count
  1004. 新数据为date
  1005. 长度为datalen


  1006. 接收到新数据,重新分配空间,要根据收到的数据的大小
  1007. buffersize重分配的空间
  1008. */
  1009. static void
  1010. add2buf(struct half_stream * rcv, char *data, int datalen)
  1011. {
  1012.   
  1013.   int toalloc;
  1014.   
  1015.   /*datalen + rcv->count - rcv->offset 如果大于buffersize 需要重新分配空间*/
  1016.      
  1017.   if (datalen + rcv->count - rcv->offset > rcv->bufsize) {
  1018.     /*如果rcv->data 不存在*/
  1019.     if (!rcv->data) {
  1020.       if (datalen < 2048)
  1021.     toalloc = 4096;
  1022.       else
  1023.     toalloc = datalen * 2;
  1024.   
  1025.       /*数据分配空间,bufsize 为分配空间的大小*/
  1026.       rcv->data = malloc(toalloc);
  1027.       rcv->bufsize = toalloc;
  1028.     }
  1029.    /*第一次分配空间,如果空间不够重新分配*/
  1030.     else {
  1031.            
  1032.       if (datalen < rcv->bufsize)
  1033.         toalloc = 2 * rcv->bufsize;
  1034.       else
  1035.         toalloc = rcv->bufsize + 2*datalen;
  1036.          
  1037.       rcv->data = realloc(rcv->data, toalloc);
  1038.       rcv->bufsize = toalloc;
  1039.     }
  1040.     if (!rcv->data)
  1041.       nids_params.no_mem("add2buf");
  1042.   }
  1043.   
  1044.   
  1045.   memcpy(rcv->data + rcv->count - rcv->offset, data, datalen);
  1046.      
  1047.   rcv->count_new = datalen;
  1048.      
  1049.   rcv->count += datalen; /*count 累加为收到的数据之和,从流开始建立*/
  1050.   
  1051. }
  1052. /*
  1053. 分配空间,数据保存rcv->data 中
  1054. 长度为:rcv->count
  1055. 新数据为date
  1056. 长度为datalen

  1057. 接收到新数据,重新分配空间,要根据收到的数据的大小
  1058. buffersize重分配的空间
  1059. */
  1060. static void
  1061. add2buf(struct half_stream * rcv, char *data, int datalen)
  1062. {
  1063.   int toalloc;
  1064.   /*datalen + rcv->count - rcv->offset 如果大于buffersize 需要重新分配空间*/
  1065.   
  1066.   if (datalen + rcv->count - rcv->offset > rcv->bufsize) {
  1067.    /*如果rcv->data 不存在*/
  1068.     if (!rcv->data) {
  1069.       if (datalen < 2048)
  1070.  toalloc = 4096;
  1071.       else
  1072.  toalloc = datalen * 2;
  1073.       /*数据分配空间,bufsize 为分配空间的大小*/
  1074.       rcv->data = malloc(toalloc);
  1075.       rcv->bufsize = toalloc;
  1076.     }
  1077.    /*第一次分配空间,如果空间不够重新分配*/
  1078.     else {
  1079.   
  1080.       if (datalen < rcv->bufsize)
  1081.        toalloc = 2 * rcv->bufsize;
  1082.       else
  1083.        toalloc = rcv->bufsize + 2*datalen;
  1084.    
  1085.       rcv->data = realloc(rcv->data, toalloc);
  1086.       rcv->bufsize = toalloc;
  1087.     }
  1088.     if (!rcv->data)
  1089.       nids_params.no_mem("add2buf");
  1090.   }

  1091.   memcpy(rcv->data + rcv->count - rcv->offset, data, datalen);
  1092.   
  1093.   rcv->count_new = datalen;
  1094.   
  1095.   rcv->count += datalen; /*count 累加为收到的数据之和,从流开始建立*/
  1096. }
  1097. view plaincopy to clipboardprint?
  1098. /*

  1099. 通知服务器或者客户端接受数据

  1100. */
  1101.   
  1102. static void
  1103. notify(struct tcp_stream * a_tcp, struct half_stream * rcv)
  1104. {
  1105.   
  1106. /*

  1107. 监听节点

  1108. */
  1109.   struct lurker_node *i, **prev_addr;
  1110.   char mask;
  1111.   
  1112. /*

  1113. 紧急数据


  1114. */
  1115.   if (rcv->count_new_urg) {
  1116.     if (!rcv->collect_urg)
  1117.       return;
  1118.     if (rcv == &a_tcp->client)
  1119.       mask = COLLECT_ccu;
  1120.     else
  1121.       mask = COLLECT_scu;
  1122.        
  1123.     ride_lurkers(a_tcp, mask);
  1124.     goto prune_listeners;
  1125.   }
  1126.   
  1127.   
  1128. /*

  1129. 常规数据

  1130. */
  1131.   
  1132.   if (rcv->collect) {
  1133.     if (rcv == &a_tcp->client)
  1134.       mask = COLLECT_cc;
  1135.     else
  1136.       mask = COLLECT_sc;
  1137.   
  1138.   /*

  1139.   不断读取数据,一直到


  1140.   读取到数据结束

  1141.   */
  1142.        
  1143.    do {
  1144.             int total;
  1145.   
  1146.         /*

  1147.             a_tcp 为为收到的datalen

  1148.             total = dalalen;

  1149.         */
  1150.         a_tcp->read = rcv->count - rcv->offset;
  1151.         total = a_tcp->read;
  1152.            
  1153.                /*处理监听节点上报信息*/
  1154.   
  1155.         ride_lurkers(a_tcp, mask);
  1156.   
  1157.   
  1158.                   
  1159.            
  1160.         if (a_tcp->read > total-rcv->count_new)
  1161.             rcv->count_new = total-a_tcp->read;
  1162.            
  1163.         if (a_tcp->read > 0) {
  1164.                
  1165.         /*
  1166.             已经读过的数据部再读,把没有读的数据放在rcv->data 中,
  1167.             继续循环,hlf->data 指向新数据

  1168.             rcv->count_new 为新数据的长度
  1169.               

  1170.         */
  1171.         memmove(rcv->data, rcv->data + a_tcp->read, rcv->count - rcv->offset - a_tcp->read);
  1172.   
  1173.           /*rcv->ofset 最终== rcv->count */
  1174.           rcv->offset += a_tcp->read;
  1175.         }
  1176.     }while (nids_params.one_loop_less && a_tcp->read>0 && rcv->count_new);
  1177. // we know that if one_loop_less!=0, we have only one callback to notify

  1178.   
  1179. /*

  1180. rcv->count_new 初始化为0

  1181. */
  1182.    rcv->count_new=0;
  1183.   }
  1184.   
  1185.      
  1186.  prune_listeners:
  1187.   prev_addr = &a_tcp->listeners;
  1188.   i = a_tcp->listeners;
  1189.   while (i)
  1190.     if (!i->whatto) {
  1191.       *prev_addr = i->next;
  1192.       free(i);
  1193.       i = *prev_addr;
  1194.     }
  1195.     else {
  1196.       prev_addr = &i->next;
  1197.       i = i->next;
  1198.     }
  1199.        
  1200. }
  1201. /*
  1202. 通知服务器或者客户端接受数据
  1203. */
  1204. static void
  1205. notify(struct tcp_stream * a_tcp, struct half_stream * rcv)
  1206. {
  1207. /*
  1208. 监听节点
  1209. */
  1210.   struct lurker_node *i, **prev_addr;
  1211.   char mask;
  1212. /*
  1213. 紧急数据

  1214. */
  1215.   if (rcv->count_new_urg) {
  1216.     if (!rcv->collect_urg)
  1217.       return;
  1218.     if (rcv == &a_tcp->client)
  1219.       mask = COLLECT_ccu;
  1220.     else
  1221.       mask = COLLECT_scu;

  1222.     ride_lurkers(a_tcp, mask);
  1223.     goto prune_listeners;
  1224.   }

  1225. /*
  1226. 常规数据
  1227. */
  1228.   if (rcv->collect) {
  1229.     if (rcv == &a_tcp->client)
  1230.       mask = COLLECT_cc;
  1231.     else
  1232.       mask = COLLECT_sc;
  1233.   /*
  1234.   不断读取数据,一直到

  1235.   读取到数据结束
  1236.   */

  1237.    do {
  1238.          int total;
  1239.   /*
  1240.    a_tcp 为为收到的datalen
  1241.    total = dalalen;
  1242.   */
  1243.   a_tcp->read = rcv->count - rcv->offset;
  1244.   total = a_tcp->read;
  1245.   
  1246.                /*处理监听节点上报信息*/
  1247.   ride_lurkers(a_tcp, mask);

  1248.       
  1249.   
  1250.      if (a_tcp->read > total-rcv->count_new)
  1251.       rcv->count_new = total-a_tcp->read;
  1252.      
  1253.      if (a_tcp->read > 0) {
  1254.    
  1255.   /*
  1256.    已经读过的数据部再读,把没有读的数据放在rcv->data 中,
  1257.    继续循环,hlf->data 指向新数据
  1258.    rcv->count_new 为新数据的长度
  1259.    
  1260.   */
  1261.   memmove(rcv->data, rcv->data + a_tcp->read, rcv->count - rcv->offset - a_tcp->read);
  1262.        /*rcv->ofset 最终== rcv->count */
  1263.        rcv->offset += a_tcp->read;
  1264.      }
  1265.  }while (nids_params.one_loop_less && a_tcp->read>0 && rcv->count_new);
  1266. // we know that if one_loop_less!=0, we have only one callback to notify

  1267. /*
  1268. rcv->count_new 初始化为0
  1269. */
  1270.    rcv->count_new=0;
  1271.   }
  1272.   
  1273.  prune_listeners:
  1274.   prev_addr = &a_tcp->listeners;
  1275.   i = a_tcp->listeners;
  1276.   while (i)
  1277.     if (!i->whatto) {
  1278.       *prev_addr = i->next;
  1279.       free(i);
  1280.       i = *prev_addr;
  1281.     }
  1282.     else {
  1283.       prev_addr = &i->next;
  1284.       i = i->next;
  1285.     }

  1286. }
阅读(10793) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~