libnids tcp 重组代码注释

[c-sharp]  view plain copy
  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.         
  50.   if (!nids_params.n_tcp_streams) return;  
  51.    
  52.   /*tcp 头的长度,iplen -4*this_iphdr->ip_hl, 进行包头的校验*/  
  53.   if (my_tcp_check(this_tcphdr, iplen - 4 * this_iphdr->ip_hl,  
  54.            this_iphdr->ip_src.s_addr, this_iphdr->ip_dst.s_addr)) {  
  55.     nids_params.syslog(NIDS_WARN_TCP, NIDS_WARN_TCP_HDR, this_iphdr,  
  56.                this_tcphdr);  
  57.     return;  
  58.       
  59.   }  
  60.    
  61. #if 0  
  62.   check_flags(this_iphdr, this_tcphdr);  
  63. //ECN  
  64. #endif  
  65.   
  66. /*查找添加流*/  
  67.   
  68. /* 
  69.  
  70.     *************       三次握手的第一次握手***************************************** 
  71.  
  72. */  
  73.   
  74.   if (!(a_tcp = find_stream(this_tcphdr, this_iphdr, &from_client))) {  
  75.   
  76.    /*是三次握手的第一个包*/  
  77.   /*tcp里流不存在时:且tcp数据包里的(syn=1 && ack==0 && rst==0)时,添加一条tcp流*/  
  78.  /*tcp第一次握手*/  
  79.     if ((this_tcphdr->th_flags &  TH_SYN) &&  
  80.     !(this_tcphdr->th_flags & TH_ACK) &&  
  81.     !(this_tcphdr->th_flags & TH_RST))  
  82.     /*并且没有收到th_rest 包*/  
  83.       add_new_tcp(this_tcphdr, this_iphdr);/*节点加入链表中*/  
  84.   
  85.    /*第一次握手完毕返回*/      
  86.     return;  
  87.   }  
  88.   
  89.    
  90.   if (from_client) { /*从client --> server的包*/  
  91.     snd = &a_tcp->client;  
  92.     rcv = &a_tcp->server;  
  93.   }  
  94.   else {/* server --> client的包 */  
  95.     rcv = &a_tcp->client;  
  96.     snd = &a_tcp->server;  
  97.   }  
  98.   
  99.   
  100. /********************************************************************** 
  101.  
  102.                 三次握手的第二次握手 
  103.   
  104. ************************************************************************/  
  105.   
  106.    /*tcp 三次握手, SYN ==1,ACK==1,tcp第二次握手(server -> client的同步响应)*/  
  107.   if ((this_tcphdr->th_flags & TH_SYN)) {  
  108.     if (from_client || a_tcp->client.state != TCP_SYN_SENT ||  
  109.       a_tcp->server.state != TCP_CLOSE || !(this_tcphdr->th_flags & TH_ACK))  
  110.       return;  
  111.   
  112.   
  113. /*第二次回应包的ACK 值为第一个包的序列号+1,在初始化的时候已经加一*/           
  114.     if (a_tcp->client.seq != ntohl(this_tcphdr->th_ack))  
  115.       return;  
  116.       
  117.       
  118.     /*第二个包服务端赋值*/  
  119.     /*a_tcp 中服务端赋值,*/  
  120.   
  121.     a_tcp->server.state = TCP_SYN_RECV;  
  122.     a_tcp->server.seq = ntohl(this_tcphdr->th_seq) + 1;  
  123.     a_tcp->server.first_data_seq = a_tcp->server.seq;  
  124.     a_tcp->server.ack_seq = ntohl(this_tcphdr->th_ack);  
  125.     a_tcp->server.window = ntohs(this_tcphdr->th_win);  
  126.   
  127.   
  128.   /*对于tcp 选项的赋值*/     
  129.   //初始化客户端和服务器的时间截  
  130.     if (a_tcp->client.ts_on) {  
  131.         a_tcp->server.ts_on = get_ts(this_tcphdr, &a_tcp->server.curr_ts);  
  132.     if (!a_tcp->server.ts_on)  
  133.         a_tcp->client.ts_on = 0;  
  134.     } else a_tcp->server.ts_on = 0;    
  135.   
  136.   
  137. //初始化窗口大小     
  138.     if (a_tcp->client.wscale_on) {  
  139.         a_tcp->server.wscale_on = get_wscale(this_tcphdr, &a_tcp->server.wscale);  
  140.     if (!a_tcp->server.wscale_on) {  
  141.         a_tcp->client.wscale_on = 0;  
  142.         a_tcp->client.wscale  = 1;  
  143.         a_tcp->server.wscale = 1;  
  144.     }     
  145.     } else {  
  146.         a_tcp->server.wscale_on = 0;   
  147.         a_tcp->server.wscale = 1;  
  148.     }     
  149.  /*第二次握手完毕,返回*/  
  150.       
  151.     return;  
  152.   }  
  153.     
  154.   
  155.   
  156.   
  157.   
  158.    /* 
  159.                 (如果有数据存在或者修列号不等于确认号的)并且 
  160.  
  161.         序列号在窗口之外 
  162.         已经确认过的序号 
  163.  
  164.    */  
  165.    
  166.   if (  
  167.     ! (  !datalen && ntohl(this_tcphdr->th_seq) == rcv->ack_seq  )  
  168.     &&  
  169.   
  170.     /*th_seq - (ack_seq+ wscale) >  0 或者th_seq+datalen - ack_sql < 0*/   
  171.     ( !before(ntohl(this_tcphdr->th_seq), rcv->ack_seq + rcv->window*rcv->wscale) ||  
  172.           before(ntohl(this_tcphdr->th_seq) + datalen, rcv->ack_seq)    
  173.         )  
  174.      )       
  175.      return;   
  176.   
  177.     
  178.   
  179.   /*发送th_rst 重新开启一个连接*/  
  180.   if ((this_tcphdr->th_flags & TH_RST)) {  
  181.     /*是tcp 数据*/  
  182.     if (a_tcp->nids_state == NIDS_DATA) {  
  183.       struct lurker_node *i;  
  184.       a_tcp->nids_state = NIDS_RESET;  
  185.       for (i = a_tcp->listeners; i; i = i->next)  
  186.     (i->item) (a_tcp, &i->data);  
  187.     }  
  188.     nids_free_tcp_stream(a_tcp);  
  189.     return;  
  190.   }  
  191.   
  192.   
  193.   
  194.   
  195.   /* PAWS check */  
  196.   if (rcv->ts_on && get_ts(this_tcphdr, &tmp_ts) &&   
  197.     before(tmp_ts, snd->curr_ts))  
  198.   return;     
  199.   
  200.   
  201.   
  202.   
  203.   
  204.   
  205. /* 
  206.     ********************************************************************** 
  207.                         第三次握手包 
  208.  
  209.     ********************************************************************** 
  210. */  
  211.   
  212.   /* 
  213.    
  214.  
  215.   从client --> server的包 
  216.  
  217.    是从三次握手的第三个包分析开始的,进行一部分数据分析,和初始化 
  218.    连接状态 
  219.  
  220.   */  
  221.     
  222.   if ((this_tcphdr->th_flags & TH_ACK)) {  
  223.     if (from_client && a_tcp->client.state == TCP_SYN_SENT &&  
  224.     a_tcp->server.state == TCP_SYN_RECV) {  
  225.       if (ntohl(this_tcphdr->th_ack) == a_tcp->server.seq) {  
  226.     a_tcp->client.state = TCP_ESTABLISHED;  
  227.     a_tcp->client.ack_seq = ntohl(this_tcphdr->th_ack);  
  228.     {  
  229.       struct proc_node *i;  
  230.       struct lurker_node *j;  
  231.       void *data;  
  232.         
  233.       a_tcp->server.state = TCP_ESTABLISHED;  
  234.       a_tcp->nids_state = NIDS_JUST_EST;  
  235.       /*开始全双工传输,client server 连接已经建立起来了*/  
  236.   
  237.       /*三次握手tcp ip 连接建立*/  
  238.       for (i = tcp_procs; i; i = i->next) {  
  239.         char whatto = 0;  
  240.           
  241.         char cc = a_tcp->client.collect;  
  242.         char sc = a_tcp->server.collect;  
  243.         char ccu = a_tcp->client.collect_urg;  
  244.         char scu = a_tcp->server.collect_urg;  
  245.   
  246.         /*进入回调函数处理*/  
  247.   
  248.         /* 
  249.  
  250.             如果在相应端口出现 
  251.  
  252.         client.collect ++ ; 
  253.  
  254.         测审计次数据 
  255.         对应用来说tcp 连接已经建立 
  256.  
  257.        */     
  258.          
  259.          
  260.         (i->item) (a_tcp, &data);  
  261.   
  262.        /**/   
  263.         if (cc < a_tcp->client.collect)  
  264.           whatto |= COLLECT_cc;       
  265.         if (ccu < a_tcp->client.collect_urg)  
  266.           whatto |= COLLECT_ccu;  
  267.         if (sc < a_tcp->server.collect)  
  268.           whatto |= COLLECT_sc;  
  269.         if (scu < a_tcp->server.collect_urg)  
  270.           whatto |= COLLECT_scu;  
  271.         if (nids_params.one_loop_less) {  
  272.                 if (a_tcp->client.collect >=2) {  
  273.                     a_tcp->client.collect=cc;  
  274.                     whatto&=~COLLECT_cc;  
  275.                 }  
  276.                 if (a_tcp->server.collect >=2 ) {  
  277.                     a_tcp->server.collect=sc;  
  278.                     whatto&=~COLLECT_sc;  
  279.                 }  
  280.         }    
  281.              
  282.        /*加入监听队列,开始数据接收*/      
  283.         if (whatto) {  
  284.           j = mknew(struct lurker_node);  
  285.           j->item = i->item;/*放入监听队列*/  
  286.           j->data = data;  
  287.           j->whatto = whatto;  
  288.             
  289.           j->next = a_tcp->listeners;  
  290.           a_tcp->listeners = j;  
  291.         }  
  292.           
  293.       }  
  294.         
  295.         
  296.       /*不存在监听着*/{  
  297.         nids_free_tcp_stream(a_tcp);  
  298.         return;  
  299.       }  
  300.       if (!a_tcp->listeners)   
  301.         
  302.       a_tcp->nids_state = NIDS_DATA;  
  303.     }  
  304.       }  
  305.       // return;  
  306.     }  
  307.   }  
  308.   
  309.   
  310.   
  311. /* 
  312. ************************************************************ 
  313.  
  314.                 挥手过程 
  315.  
  316. ************************************************************* 
  317.  
  318. */  
  319.   
  320. /*数据结束的包的判断*/  
  321.   
  322.   if ((this_tcphdr->th_flags & TH_ACK)) {  
  323.       
  324.   
  325.    /* 从数据传输过程不断更新服务器客户端的ack_seq 
  326.     一直到接收到fin 包,数据传输结束 
  327.  
  328.    */  
  329.     handle_ack(snd, ntohl(this_tcphdr->th_ack));  
  330.       
  331.     if (rcv->state == FIN_SENT)  
  332.       rcv->state = FIN_CONFIRMED;  
  333.     if (rcv->state == FIN_CONFIRMED && snd->state == FIN_CONFIRMED) {  
  334.       struct lurker_node *i;  
  335.   
  336.       a_tcp->nids_state = NIDS_CLOSE;  
  337.       for (i = a_tcp->listeners; i; i = i->next)  
  338.     (i->item) (a_tcp, &i->data);  
  339.       nids_free_tcp_stream(a_tcp);  
  340.       return;  
  341.     }  
  342.   }  
  343.   
  344.   
  345.  /* 
  346.  
  347. *************************************************************    
  348.                         数据处理过程 
  349. ************************************************************* 
  350.  
  351.  */  
  352.   
  353.   
  354.    
  355.     
  356.   if (datalen + (this_tcphdr->th_flags & TH_FIN) > 0)  
  357.   
  358.   /* 
  359.          
  360.     a_tcp -----a_tcp 客户端连接包 
  361.     this_tcphdr  tcp 包头 
  362.     snd-----发送包 
  363.     rcv -----接收包 
  364.  
  365.     (char *) (this_tcphdr) + 4 * this_tcphdr->th_off -----数据包内容 
  366.     datalen---------数据包长度 
  367.      
  368.  
  369.   */  
  370.     
  371.     tcp_queue(a_tcp, this_tcphdr, snd, rcv,  
  372.           (char *) (this_tcphdr) + 4 * this_tcphdr->th_off,  
  373.           datalen, skblen);  
  374.     
  375.   snd->window = ntohs(this_tcphdr->th_win);  
  376.     
  377.   if (rcv->rmem_alloc > 65535)  
  378.       
  379.     prune_queue(rcv, this_tcphdr);  
  380.     
  381.   if (!a_tcp->listeners)  
  382.     nids_free_tcp_stream(a_tcp);  
  383.   
  384.   
  385. }  

[c-sharp]  view plain copy
  1. //判断  该TCP 数据包是添加到数据区还是添加到list双向链表中  
  2. static void  
  3. tcp_queue(struct tcp_stream * a_tcp, struct tcphdr * this_tcphdr,  
  4.       struct half_stream * snd, struct  half_stream* rcv,  
  5.       char *data, int datalen, int skblen  
  6.       )  
  7. {  
  8.   
  9.   u_int this_seq = ntohl(this_tcphdr->th_seq);  
  10.     
  11.   struct skbuff *pakiet, *tmp;  
  12.   
  13.   
  14.  /* 
  15.         #define EXP_SEQ (snd->first_data_seq + rcv->count + rcv->urg_count) 
  16.     EXP_SEQ > this_seq 
  17.   */  
  18.   
  19.   /*   如果EXP_SEQ > = this_seq  */  
  20.   /* 
  21.  
  22.   EXP_SEQ 期望到达的数据 
  23.   this_seq 实际到达的数据 
  24.  
  25.   */  
  26.     
  27.   if ( !after(this_seq, EXP_SEQ) ) {  
  28.   
  29.     /*this_seq + datalen + (this_tcphdr->th_flags & TH_FIN)>  EXP_SEQ*/  
  30.   
  31.       
  32.     /*有重叠数据存在(重叠数据怎么处理呢) 
  33.  
  34.         也可能没有重叠数据 
  35.      */  
  36.       
  37.     if ( after(this_seq + datalen + (this_tcphdr->th_flags & TH_FIN), EXP_SEQ) ) {  
  38.           
  39.       /* the packet straddles our window end */  
  40.       get_ts(this_tcphdr, &snd->curr_ts);  
  41.         
  42.       add_from_skb(a_tcp, rcv, snd, data, datalen, this_seq,  
  43.            (this_tcphdr->th_flags & TH_FIN),  
  44.            (this_tcphdr->th_flags & TH_URG),  
  45.                    ntohs(this_tcphdr->th_urp) + this_seq - 1);       
  46.     /* 
  47.     * Do we have any old packets to ack that the above 
  48.     * made visible? (Go forward from skb) 
  49.     */  
  50.   
  51.   
  52.   
  53.      /*从头节点释放节点*/  
  54.       pakiet = rcv->list;  
  55.       while (pakiet) {  
  56.      /* 
  57.  
  58.         期望的序列号小于该包的序列号则直接返回  
  59.  
  60.      */       
  61.     if (after(pakiet->seq, EXP_SEQ))  
  62.       break;  
  63.       
  64.     /* 
  65.  
  66.     对失序队列数据包的处理 
  67.  
  68.        如果包序列号加上长度还小于期望序列号 
  69.        则把该包添加到数据区,并在失序队列中删除该包 
  70.  
  71.     */  
  72.     if (after(pakiet->seq + pakiet->len + pakiet->fin, EXP_SEQ)) {  
  73.       add_from_skb(a_tcp, rcv, snd, pakiet->data,  
  74.                pakiet->len, pakiet->seq, pakiet->fin, pakiet->urg,  
  75.                pakiet->urg_ptr + pakiet->seq - 1);  
  76.         }  
  77.     rcv->rmem_alloc -= pakiet->truesize;  
  78.       
  79.         /*从头节点释放链表*/  
  80.           
  81.     if (pakiet->prev)  
  82.       pakiet->prev->next = pakiet->next;  
  83.     else  
  84.       rcv->list = pakiet->next;  
  85.     if (pakiet->next)  
  86.       pakiet->next->prev = pakiet->prev;  
  87.     else  
  88.       rcv->listtail = pakiet->prev;  
  89.     tmp = pakiet->next;  
  90.     free(pakiet->data);  
  91.     free(pakiet);  
  92.     pakiet = tmp;  
  93.       }     
  94.     }  
  95.     else  
  96.       return;  
  97.   }  
  98.   
  99.     
  100. /*提前到达数据的加入失去顺序队列中链表*/  
  101.   
  102. //序列号大于我们期望的序列号,则把该包添加到list双向链表中  
  103.   
  104.   
  105.   /*链表加入节点  */  
  106.   else {  
  107.   
  108.     /*初始化*/   
  109.     struct skbuff *p = rcv->listtail;  
  110.        /* 
  111.     pakiet 加入rcv->listtal 链表中     
  112.        */  
  113.        /* 
  114.     pakiet 节点的初始化 
  115.      
  116.     */  
  117.       
  118.     pakiet = mknew(struct skbuff);  
  119.     pakiet->truesize = skblen;  
  120.     rcv->rmem_alloc += pakiet->truesize;  
  121.   
  122.     /*数据包填充pakiet->data 中*/    
  123.     pakiet->len = datalen;  
  124.     pakiet->data = malloc(datalen);  
  125.     if (!pakiet->data)  
  126.     nids_params.no_mem("tcp_queue");  
  127.     memcpy(pakiet->data, data, datalen);  
  128.   
  129.     /*是否是结束包*/    
  130.     pakiet->fin = (this_tcphdr->th_flags & TH_FIN);  
  131.      
  132.     /* Some Cisco - at least - hardware accept to close a TCP connection 
  133.      * even though packets were lost before the first TCP FIN packet and 
  134.      * never retransmitted; this violates RFC 793, but since it really 
  135.      * happens, it has to be dealt with... The idea is to introduce a 10s 
  136.      * timeout after TCP FIN packets were sent by both sides so that 
  137.      * corresponding libnids resources can be released instead of waiting 
  138.      * for retransmissions which will never happen.  -- Sebastien Raveau 
  139.      */  
  140.       /* 
  141.  
  142.          硬件接收一个tcp 终止的链接, 
  143.          即使包在一个结束包的时候丢失,并且不再重传, 
  144.          处理这种方法就是引入时间机制处理 
  145.  
  146.       */  
  147.         
  148.     if (pakiet->fin) {  
  149.       snd->state = TCP_CLOSING;  
  150.       if (rcv->state == FIN_SENT || rcv->state == FIN_CONFIRMED)  
  151.     add_tcp_closing_timeout(a_tcp);  
  152.     }  
  153.       
  154.      /* 
  155.      
  156.      seq,urg ,urg_ptr 赋值 
  157.  
  158.     */  
  159.       
  160.     pakiet->seq = this_seq;  
  161.     pakiet->urg = (this_tcphdr->th_flags & TH_URG);  
  162.     pakiet->urg_ptr = ntohs(this_tcphdr->th_urp);  
  163.       
  164.     for (;;) {  
  165.           
  166.       if (!p || !after(p->seq, this_seq))  
  167.     break;  
  168.       p = p->prev;  
  169. }  
  170.       
  171.     if (!p) {  
  172. /*建立首节点*/     
  173.       pakiet->prev = 0;  
  174.       pakiet->next = rcv->list;  
  175.       if (rcv->list)  
  176.          rcv->list->prev = pakiet;  
  177.         
  178.       rcv->list = pakiet;  
  179.       if (!rcv->listtail)  
  180.     rcv->listtail = pakiet;  
  181.     }  
  182.   
  183. /*链表后插入*/     
  184.     else {  
  185.       pakiet->next = p->next;  
  186.       p->next = pakiet;  
  187.       pakiet->prev = p;  
  188.       if (pakiet->next)  
  189.     pakiet->next->prev = pakiet;  
  190.       else  
  191.     rcv->listtail = pakiet;  
  192.     }  
  193.       
  194.   }  
  195.     
  196. }  

 

[c-sharp]  view plain copy
  1. /* 
  2.  
  3. 分配空间,数据保存rcv->data 中 
  4. 长度为:rcv->count 
  5. 新数据为date 
  6. 长度为datalen 
  7.  
  8.  
  9. 接收到新数据,重新分配空间,要根据收到的数据的大小 
  10. buffersize重分配的空间 
  11. */  
  12. static void  
  13. add2buf(struct half_stream * rcv, char *data, int datalen)  
  14. {  
  15.   
  16.   int toalloc;  
  17.   
  18.   /*datalen + rcv->count - rcv->offset 如果大于buffersize 需要重新分配空间*/  
  19.     
  20.   if (datalen + rcv->count - rcv->offset > rcv->bufsize) {  
  21.     /*如果rcv->data 不存在*/  
  22.     if (!rcv->data) {  
  23.       if (datalen < 2048)  
  24.     toalloc = 4096;  
  25.       else  
  26.     toalloc = datalen * 2;  
  27.   
  28.       /*数据分配空间,bufsize 为分配空间的大小*/           
  29.       rcv->data = malloc(toalloc);  
  30.       rcv->bufsize = toalloc;  
  31.     }  
  32.    /*第一次分配空间,如果空间不够重新分配*/     
  33.     else {  
  34.           
  35.       if (datalen < rcv->bufsize)  
  36.         toalloc = 2 * rcv->bufsize;  
  37.       else    
  38.         toalloc = rcv->bufsize + 2*datalen;  
  39.         
  40.       rcv->data = realloc(rcv->data, toalloc);  
  41.       rcv->bufsize = toalloc;  
  42.     }  
  43.     if (!rcv->data)  
  44.       nids_params.no_mem("add2buf");  
  45.   }  
  46.   
  47.   
  48.   memcpy(rcv->data + rcv->count - rcv->offset, data, datalen);  
  49.     
  50.   rcv->count_new = datalen;   
  51.     
  52.   rcv->count += datalen; /*count 累加为收到的数据之和,从流开始建立*/  
  53.   
  54. }  

 

[c-sharp]  view plain copy
  1. /* 
  2.  
  3. 通知服务器或者客户端接受数据 
  4.  
  5. */  
  6.   
  7. static void  
  8. notify(struct tcp_stream * a_tcp, struct half_stream * rcv)  
  9. {  
  10.   
  11. /* 
  12.  
  13. 监听节点 
  14.  
  15. */  
  16.   struct lurker_node *i, **prev_addr;  
  17.   char mask;  
  18.   
  19. /* 
  20.  
  21. 紧急数据 
  22.  
  23.  
  24. */  
  25.   if (rcv->count_new_urg) {  
  26.     if (!rcv->collect_urg)  
  27.       return;  
  28.     if (rcv == &a_tcp->client)  
  29.       mask = COLLECT_ccu;  
  30.     else  
  31.       mask = COLLECT_scu;  
  32.       
  33.     ride_lurkers(a_tcp, mask);  
  34.     goto prune_listeners;  
  35.   }  
  36.   
  37.   
  38. /* 
  39.  
  40. 常规数据 
  41.  
  42. */  
  43.   
  44.   if (rcv->collect) {  
  45.     if (rcv == &a_tcp->client)  
  46.       mask = COLLECT_cc;  
  47.     else  
  48.       mask = COLLECT_sc;  
  49.   
  50.   /* 
  51.  
  52.   不断读取数据,一直到 
  53.  
  54.  
  55.   读取到数据结束 
  56.  
  57.   */  
  58.       
  59.    do {  
  60.             int total;  
  61.   
  62.         /* 
  63.  
  64.             a_tcp 为为收到的datalen 
  65.  
  66.             total = dalalen; 
  67.  
  68.         */    
  69.         a_tcp->read = rcv->count - rcv->offset;  
  70.         total  =  a_tcp->read;  
  71.           
  72.                /*处理监听节点上报信息*/  
  73.   
  74.         ride_lurkers(a_tcp, mask);  
  75.   
  76.   
  77.                  
  78.           
  79.         if (a_tcp->read >  total-rcv->count_new)  
  80.             rcv->count_new =  total-a_tcp->read;  
  81.           
  82.         if (a_tcp->read > 0) {  
  83.               
  84.         /* 
  85.             已经读过的数据部再读,把没有读的数据放在rcv->data 中, 
  86.             继续循环,hlf->data 指向新数据 
  87.  
  88.             rcv->count_new 为新数据的长度 
  89.              
  90.  
  91.         */    
  92.         memmove(rcv->data, rcv->data + a_tcp->read, rcv->count - rcv->offset - a_tcp->read);  
  93.   
  94.           /*rcv->ofset 最终== rcv->count */  
  95.           rcv->offset += a_tcp->read;  
  96.         }  
  97.     }while (nids_params.one_loop_less && a_tcp->read>0 && rcv->count_new);      
  98. // we know that if one_loop_less!=0, we have only one callback to notify  
  99.   
  100. /* 
  101.  
  102. rcv->count_new 初始化为0 
  103.  
  104. */  
  105.    rcv->count_new=0;       
  106.   }  
  107.   
  108.     
  109.  prune_listeners:  
  110.   prev_addr = &a_tcp->listeners;  
  111.   i = a_tcp->listeners;  
  112.   while (i)  
  113.     if (!i->whatto) {  
  114.       *prev_addr = i->next;  
  115.       free(i);  
  116.       i = *prev_addr;  
  117.     }  
  118.     else {  
  119.       prev_addr = &i->next;  
  120.       i = i->next;  
  121.     }  
  122.       
  123. }  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值