添加数据处理过程
static void add_from_skb(struct tcp_stream * a_tcp, struct half_stream * rcv, struct half_stream * snd, u_char *data, int datalen, u_int this_seq, char fin, char urg, u_int urg_ptr) { u_int lost = EXP_SEQ - this_seq;//比较序列号,如果新到的序列号比期望的大,有可能丢失了部分数据 int to_copy, to_copy2; if (urg && after(urg_ptr, EXP_SEQ - 1) && (!rcv->urg_seen || after(urg_ptr, rcv->urg_ptr))) { rcv->urg_ptr = urg_ptr; rcv->urg_seen = 1; } if (after(rcv->urg_ptr + 1, this_seq + lost) && before(rcv->urg_ptr, this_seq + datalen)) { to_copy = rcv->urg_ptr - (this_seq + lost); if (to_copy > 0) {//数据中有新的数据到达,将数据进行添加处理,并进行数据的回推处理 if (rcv->collect) { add2buf(rcv, data + lost, to_copy); notify(a_tcp, rcv); } else { rcv->count += to_copy; rcv->offset = rcv->count; /* clear the buffer */ } } rcv->urgdata = data[rcv->urg_ptr - this_seq]; rcv->count_new_urg = 1; notify(a_tcp, rcv); rcv->count_new_urg = 0; rcv->urg_count++; to_copy2 = this_seq + datalen - rcv->urg_ptr - 1; if (to_copy2 > 0) { if (rcv->collect) { add2buf(rcv, data + lost + to_copy + 1, to_copy2); notify(a_tcp, rcv); } else { rcv->count += to_copy2; rcv->offset = rcv->count; /* clear the buffer */ } } } else { if (datalen - lost > 0) { if (rcv->collect) { add2buf(rcv, data + lost, datalen - lost); notify(a_tcp, rcv); } else { rcv->count += datalen - lost; rcv->offset = rcv->count; /* clear the buffer */ } } } if (fin) snd->state = FIN_SENT;//出现FIN状态 }
static void notify(struct tcp_stream * a_tcp, struct half_stream * rcv)//完整的和半个方向的流 { struct lurker_node *i, **prev_addr; char mask; if (rcv->count_new_urg) { if (!rcv->collect_urg) return; if (rcv == &a_tcp->client) mask = COLLECT_ccu; else mask = COLLECT_scu; ride_lurkers(a_tcp, mask); goto prune_listeners; } if (rcv->collect) { if (rcv == &a_tcp->client) mask = COLLECT_cc; else mask = COLLECT_sc; do { int total; a_tcp->read = rcv->count - rcv->offset; total=a_tcp->read; ride_lurkers(a_tcp, mask); if (a_tcp->read>total-rcv->count_new) rcv->count_new=total-a_tcp->read; if (a_tcp->read > 0) { memmove(rcv->data, rcv->data + a_tcp->read, rcv->count - rcv->offset - a_tcp->read); rcv->offset += a_tcp->read; } }while (nids_params.one_loop_less && a_tcp->read>0 && rcv->count_new); // we know that if one_loop_less!=0, we have only one callback to notify rcv->count_new=0; } prune_listeners: prev_addr = &a_tcp->listeners; i = a_tcp->listeners; while (i) if (!i->whatto) { *prev_addr = i->next; free(i); i = *prev_addr; } else { prev_addr = &i->next; i = i->next; } }
static void ride_lurkers(struct tcp_stream * a_tcp, char mask) { struct lurker_node *i; char cc, sc, ccu, scu; for (i = a_tcp->listeners; i; i = i->next) if (i->whatto & mask) { cc = a_tcp->client.collect; sc = a_tcp->server.collect; ccu = a_tcp->client.collect_urg; scu = a_tcp->server.collect_urg; (i->item) (a_tcp, &i->data); if (cc < a_tcp->client.collect) i->whatto |= COLLECT_cc; if (ccu < a_tcp->client.collect_urg) i->whatto |= COLLECT_ccu; if (sc < a_tcp->server.collect) i->whatto |= COLLECT_sc; if (scu < a_tcp->server.collect_urg) i->whatto |= COLLECT_scu; if (cc > a_tcp->client.collect) i->whatto &= ~COLLECT_cc; if (ccu > a_tcp->client.collect_urg) i->whatto &= ~COLLECT_ccu; if (sc > a_tcp->server.collect) i->whatto &= ~COLLECT_sc; if (scu > a_tcp->server.collect_urg) i->whatto &= ~COLLECT_scu; } }