int stream_fill_gap(struct stream *stream, struct stream_pkt *p, uint32_t gap, int reverse_dir) { if (gap > stream->max_buff_size) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : gap of %u too big. not filling", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack, gap); return POM_OK; } if (!reverse_dir) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : filling gap of %u in forward direction", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack, gap); } else { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : filling gap of %u in reverse direction", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack, gap); } uint32_t gap_step = gap; if (gap_step > STREAM_GAP_STEP_MAX) gap_step = STREAM_GAP_STEP_MAX; void *zero = malloc(gap_step); if (!zero) { pom_oom(gap_step); return POM_ERR; } memset(zero, 0, gap_step); struct proto_process_stack *s = &p->stack[p->stack_index]; uint32_t plen_old = s->plen; void *pload_old = s->pload; int dir_old = s->direction; int dir_new = s->direction; if (reverse_dir) dir_new = POM_DIR_REVERSE(s->direction); uint32_t pos; for (pos = 0; pos < gap; pos += gap_step) { if (pos + gap_step < gap) s->plen = gap_step; else s->plen = gap - pos; s->pload = zero; s->direction = dir_new; int res = stream->handler(stream->ce, p->pkt, p->stack, p->stack_index); if (res == PROTO_ERR) break; } free(zero); s->pload = pload_old; s->plen = plen_old; s->direction = dir_old; return POM_OK; }
struct packet_stream* packet_stream_alloc(uint32_t start_seq, uint32_t start_ack, int direction, uint32_t max_buff_size, struct conntrack_entry *ce, unsigned int flags) { struct packet_stream *res = malloc(sizeof(struct packet_stream)); if (!res) { pom_oom(sizeof(struct packet_stream)); return NULL; } memset(res, 0, sizeof(struct packet_stream)); int rev_direction = POM_DIR_REVERSE(direction); res->cur_seq[direction] = start_seq; res->cur_ack[direction] = start_ack; res->cur_seq[rev_direction] = start_ack; res->cur_ack[rev_direction] = start_seq; res->max_buff_size = max_buff_size; res->ce = ce; if (pthread_mutex_init(&res->lock, NULL)) { pomlog(POMLOG_ERR "Error while initializing stream lock : %s", pom_strerror(errno)); free(res); return NULL; } if (pthread_mutex_init(&res->wait_lock, NULL)) { pomlog(POMLOG_ERR "Error while initializing stream wait lock : %s", pom_strerror(errno)); free(res); return NULL; } res->flags = flags; debug_stream("thread %p, entry %p, allocated, start_seq %u, start_ack %u, direction %u", pthread_self(), res, start_seq, start_ack, direction); return res; }
struct stream* stream_alloc(uint32_t max_buff_size, struct conntrack_entry *ce, unsigned int flags, int (*handler) (struct conntrack_entry *ce, struct packet *p, struct proto_process_stack *stack, unsigned int stack_index)) { struct stream *res = malloc(sizeof(struct stream)); if (!res) { pom_oom(sizeof(struct stream)); return NULL; } memset(res, 0, sizeof(struct stream)); res->max_buff_size = max_buff_size; res->ce = ce; if (pthread_mutex_init(&res->lock, NULL)) { pomlog(POMLOG_ERR "Error while initializing stream lock : %s", pom_strerror(errno)); free(res); return NULL; } if (pthread_mutex_init(&res->wait_lock, NULL)) { pomlog(POMLOG_ERR "Error while initializing stream wait lock : %s", pom_strerror(errno)); free(res); return NULL; } res->flags = flags; res->handler = handler; debug_stream("thread %p, entry %p, allocated", pthread_self(), res); return res; }
static void packet_stream_end_process_packet(struct packet_stream *stream) { pom_mutex_unlock(&stream->lock); pom_mutex_lock(&stream->wait_lock); if (stream->wait_list_head) { debug_stream("thread %p, entry %p : signaling thread %p", pthread_self(), stream, stream->wait_list_head->thread); pthread_cond_broadcast(&stream->wait_list_head->cond); } pom_mutex_unlock(&stream->wait_lock); }
static void stream_end_process_packet(struct stream *stream) { conntrack_delayed_cleanup(stream->ce, stream->timeout, stream->last_ts); pom_mutex_unlock(&stream->lock); pom_mutex_lock(&stream->wait_lock); if (stream->wait_list_head) { debug_stream("thread %p, entry %p : signaling thread %p", pthread_self(), stream, stream->wait_list_head->thread); pthread_cond_broadcast(&stream->wait_list_head->cond); } pom_mutex_unlock(&stream->wait_lock); }
static int packet_stream_is_packet_next(struct packet_stream *stream, struct packet_stream_pkt *pkt, int direction) { int rev_direction = POM_DIR_REVERSE(direction); uint32_t cur_seq = stream->cur_seq[direction]; uint32_t rev_seq = stream->cur_seq[rev_direction]; // Check that there is no gap with what we expect if ((cur_seq < pkt->seq && pkt->seq - cur_seq < PACKET_HALF_SEQ) || (cur_seq > pkt->seq && cur_seq - pkt->seq > PACKET_HALF_SEQ)) { // There is a gap debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : GAP : cur_seq %u, rev_seq %u", pthread_self(), stream, pkt->pkt->ts.tv_sec, pkt->pkt->ts.tv_usec, pkt->seq, pkt->ack, cur_seq, rev_seq); return 0; } if (stream->flags & PACKET_FLAG_STREAM_BIDIR) { // There is additional checking for bi dir stream if ((rev_seq < pkt->ack && pkt->ack - rev_seq < PACKET_HALF_SEQ) || (rev_seq > pkt->ack && rev_seq - pkt->ack > PACKET_HALF_SEQ)) { // The host processed data in the reverse direction which we haven't processed yet if (stream->t) conntrack_timer_queue(stream->t, stream->rev_dir_timeout); debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : reverse missing : cur_seq %u, rev_seq %u", pthread_self(), stream, pkt->pkt->ts.tv_sec, pkt->pkt->ts.tv_usec, pkt->seq, pkt->ack, cur_seq, rev_seq); return 0; } } // This packet can be processed debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : is next : cur_seq %u, rev_seq %u", pthread_self(), stream, pkt->pkt->ts.tv_sec, pkt->pkt->ts.tv_usec, pkt->seq, pkt->ack, cur_seq, rev_seq); return 1; }
int stream_increase_seq(struct stream *stream, unsigned int direction, uint32_t inc) { // This function must be called locked stream->cur_seq[direction] += inc; debug_stream("thread %p, entry %p, seq %u : increasing sequence by %u for direction %u", pthread_self(), stream, stream->cur_seq[direction], inc, direction); // Check if additional packets can be processed struct stream_pkt *p = NULL; while ((p = stream_get_next(stream, &direction))) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process additional", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack); // Flag the stream as running stream->flags |= STREAM_FLAG_RUNNING; if (stream->handler(stream->ce, p->pkt, p->stack, p->stack_index) == PROTO_ERR) return POM_ERR; stream->cur_seq[direction] += p->plen; stream_free_packet(p); } return POM_OK; }
int stream_set_start_seq(struct stream *stream, unsigned int direction, uint32_t seq) { pom_mutex_lock(&stream->lock); if (stream->flags & STREAM_FLAG_RUNNING) { debug_stream("thread %p, entry %p : not accepting additional sequence update as the stream stared", pthread_self(), stream); stream_end_process_packet(stream); return POM_OK; } int dir_flag = (direction == POM_DIR_FWD ? STREAM_FLAG_GOT_FWD_STARTSEQ : STREAM_FLAG_GOT_REV_STARTSEQ); stream->flags |= dir_flag; stream->cur_seq[direction] = seq; debug_stream("thread %p, entry %p : start_seq for direction %u set to %u", pthread_self(), stream, direction, seq); struct stream_pkt *p = NULL; while ((p = stream_get_next(stream, &direction))) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process additional", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack); // Flag the stream as running stream->flags |= STREAM_FLAG_RUNNING; if (stream->handler(stream->ce, p->pkt, p->stack, p->stack_index) == PROTO_ERR) { stream_end_process_packet(stream); return POM_ERR; } stream->cur_seq[direction] += p->plen; stream_free_packet(p); } stream_end_process_packet(stream); return POM_OK; }
int stream_cleanup(struct stream *stream) { if (stream->wait_list_head) { pomlog(POMLOG_ERR "Internal error, cleaning up stream while packets still present!"); return POM_ERR; } while (stream->head[0] || stream->head[1]) { if (stream_force_dequeue(stream) == POM_ERR) { pomlog(POMLOG_ERR "Error while processing remaining packets in the stream"); break; } } conntrack_delayed_cleanup(stream->ce, 0, stream->last_ts); int res = pthread_mutex_destroy(&stream->lock); if (res){ pomlog(POMLOG_ERR "Error while destroying stream lock : %s", pom_strerror(res)); } res = pthread_mutex_destroy(&stream->wait_lock); if (res){ pomlog(POMLOG_ERR "Error while destroying stream wait lock : %s", pom_strerror(res)); } while (stream->wait_list_unused) { struct stream_thread_wait *tmp = stream->wait_list_unused; stream->wait_list_unused = tmp->next; if (pthread_cond_destroy(&tmp->cond)) pomlog(POMLOG_WARN "Error while destroying list condition"); free(tmp); } free(stream); debug_stream("thread %p, entry %p, released", pthread_self(), stream); return POM_OK; }
int packet_stream_process_packet(struct packet_stream *stream, struct packet *pkt, struct proto_process_stack *stack, unsigned int stack_index, uint32_t seq, uint32_t ack) { if (!stream || !pkt || !stack) return PROTO_ERR; debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : start", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); struct proto_process_stack *cur_stack = &stack[stack_index]; int direction = cur_stack->direction; int must_wait = 0; pom_mutex_lock(&stream->wait_lock); int res = pthread_mutex_trylock(&stream->lock); if (res == EBUSY) { // Already locked, let's wait a bit must_wait = 1; } else if (res) { pomlog(POMLOG_ERR "Error while locking packet stream lock : %s", pom_strerror(errno)); abort(); return POM_ERR; } else { // We got the processing lock. But was it really this thread's turn ? struct packet_stream_thread_wait *tmp = stream->wait_list_head; // A thread with a packet preceding ours is waiting if (tmp && (tmp->ts.tv_sec < pkt->ts.tv_sec || (tmp->ts.tv_sec == pkt->ts.tv_sec && tmp->ts.tv_usec < pkt->ts.tv_usec))) { // No it wasn't, release it and signal the right thread must_wait = 2; pom_mutex_unlock(&stream->lock); debug_stream("thread %p, entry %p : signaling thread %p", pthread_self(), stream, stream->wait_list_head->thread); pthread_cond_broadcast(&stream->wait_list_head->cond); } else { // Yes it was. YAY ! pom_mutex_unlock(&stream->wait_lock); } } if (must_wait) { // Add ourself in the waiting list struct packet_stream_thread_wait *lst = NULL; if (stream->wait_list_unused) { lst = stream->wait_list_unused; stream->wait_list_unused = lst->next; lst->next = NULL; } else { lst = malloc(sizeof(struct packet_stream_thread_wait)); if (!lst) { pom_oom(sizeof(struct packet_stream_thread_wait)); pom_mutex_unlock(&stream->wait_lock); return POM_ERR; } memset(lst, 0, sizeof(struct packet_stream_thread_wait)); if (pthread_cond_init(&lst->cond, NULL)) { pomlog(POMLOG_ERR "Error while initializing wait list condition : %s", pom_strerror(errno)); free(lst); return POM_ERR; } } memcpy(&lst->ts, &pkt->ts, sizeof(struct timeval)); lst->thread = pthread_self(); struct packet_stream_thread_wait *tmp; for (tmp = stream->wait_list_head; tmp && (tmp->ts.tv_sec < lst->ts.tv_sec || (tmp->ts.tv_sec == lst->ts.tv_sec && tmp->ts.tv_usec < lst->ts.tv_usec)); tmp = tmp->next); if (tmp) { lst->prev = tmp->prev; if (lst->prev) lst->prev->next = lst; else stream->wait_list_head = lst; lst->next = tmp; lst->next->prev = lst; } else { lst->prev = stream->wait_list_tail; if (lst->prev) lst->prev->next = lst; else stream->wait_list_head = lst; stream->wait_list_tail = lst; } while (1) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : waiting", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); if (pthread_cond_wait(&lst->cond, &stream->wait_lock)) { pomlog(POMLOG_ERR "Error while waiting for the packet stream wait cond : %s", pom_strerror(errno)); abort(); return POM_ERR; } if (stream->wait_list_head != lst) { // There is a small chance that another stream lock stream->wait_lock while pthread_cond_wait acquires it // If we are not the right thread, then simply signal the right one and wait again for our turn debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : wrong thread woke up", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); pthread_cond_broadcast(&stream->wait_list_head->cond); continue; } break; } tmp = stream->wait_list_head; stream->wait_list_head = tmp->next; if (stream->wait_list_head) stream->wait_list_head->prev = NULL; else stream->wait_list_tail = NULL; tmp->next = stream->wait_list_unused; tmp->prev = NULL; stream->wait_list_unused = tmp; pom_mutex_unlock(&stream->wait_lock); pom_mutex_lock(&stream->lock); } debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : start locked", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); // Update the stream flags if (stream->flags & PACKET_FLAG_STREAM_BIDIR) { // Update flags if (direction == POM_DIR_FWD && !(stream->flags & PACKET_FLAG_STREAM_GOT_FWD_DIR)) { stream->flags |= PACKET_FLAG_STREAM_GOT_FWD_DIR; } else if (direction == POM_DIR_REV && !(stream->flags & PACKET_FLAG_STREAM_GOT_REV_DIR)) { stream->flags |= PACKET_FLAG_STREAM_GOT_REV_DIR; } } // Put this packet in our struct packet_stream_pkt struct packet_stream_pkt spkt = {0}; spkt.pkt = pkt; spkt.seq = seq; spkt.ack = ack; spkt.plen = cur_stack->plen; spkt.stack = stack; spkt.stack_index = stack_index; // Check if the packet is worth processing uint32_t cur_seq = stream->cur_seq[direction]; if (cur_seq != seq) { if (packet_stream_is_packet_old_dupe(stream, &spkt, direction)) { // cur_seq is after the end of the packet, discard it packet_stream_end_process_packet(stream); debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : discard", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); return PROTO_OK; } if (packet_stream_remove_dupe_bytes(stream, &spkt, direction) == POM_ERR) { packet_stream_end_process_packet(stream); return PROTO_ERR; } } // Ok let's process it then // Check if it is the packet we're waiting for if (packet_stream_is_packet_next(stream, &spkt, direction)) { // Process it stream->cur_seq[direction] += cur_stack->plen; stream->cur_ack[direction] = ack; debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); int res = stream->handler(stream->ce, pkt, stack, stack_index); if (res == PROTO_ERR) { packet_stream_end_process_packet(stream); return PROTO_ERR; } // Check if additional packets can be processed struct packet_stream_pkt *p = NULL; unsigned int cur_dir = direction, additional_processed = 0; while ((p = packet_stream_get_next(stream, &cur_dir))) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process additional", pthread_self(), stream, p->pkt->ts.tv_sec, p->pkt->ts.tv_usec, p->seq, p->ack); if (stream->handler(stream->ce, p->pkt, p->stack, p->stack_index) == POM_ERR) { packet_stream_end_process_packet(stream); return PROTO_ERR; } stream->cur_seq[cur_dir] += p->plen; stream->cur_ack[cur_dir] = p->ack; packet_stream_free_packet(p); additional_processed = 1; } if (additional_processed) { if (!stream->head[POM_DIR_FWD] && !stream->head[POM_DIR_REV]) conntrack_timer_dequeue(stream->t); else conntrack_timer_queue(stream->t, stream->same_dir_timeout); } packet_stream_end_process_packet(stream); debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : done processed", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); return res; } // Queue the packet then debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : queue", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); struct packet_stream_pkt *p = malloc(sizeof(struct packet_stream_pkt)); if (!p) { pom_oom(sizeof(struct packet_stream_pkt)); packet_stream_end_process_packet(stream); return PROTO_ERR; } memset(p, 0 , sizeof(struct packet_stream_pkt)); if (cur_stack->plen) { // No need to backup this if there is no payload p->pkt = packet_clone(pkt, stream->flags); if (!p->pkt) { packet_stream_end_process_packet(stream); free(p); return PROTO_ERR; } p->stack = core_stack_backup(stack, pkt, p->pkt); if (!p->stack) { packet_stream_end_process_packet(stream); packet_pool_release(p->pkt); free(p); return PROTO_ERR; } } p->plen = cur_stack->plen; p->seq = seq; p->ack = ack; p->stack_index = stack_index; if (!stream->tail[direction]) { stream->head[direction] = p; stream->tail[direction] = p; } else { struct packet_stream_pkt *tmp = stream->tail[direction]; while ( tmp && ((tmp->seq >= seq && tmp->seq - seq < PACKET_HALF_SEQ) || (tmp->seq <= seq && seq - tmp->seq > PACKET_HALF_SEQ))) { tmp = tmp->prev; } if (!tmp) { // Packet goes at the begining of the list p->next = stream->head[direction]; if (p->next) p->next->prev = p; else stream->tail[direction] = p; stream->head[direction] = p; } else { // Insert the packet after the current one p->next = tmp->next; p->prev = tmp; if (p->next) p->next->prev = p; else stream->tail[direction] = p; tmp->next = p; } } stream->cur_buff_size += cur_stack->plen; if (stream->cur_buff_size >= stream->max_buff_size) { // Buffer overflow debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : buffer overflow, forced dequeue", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); if (packet_stream_force_dequeue(stream) != POM_OK) { packet_stream_end_process_packet(stream); return POM_ERR; } if (stream->t) conntrack_timer_dequeue(stream->t); } // Add timeout if (stream->t && (stream->head[POM_DIR_FWD] || stream->head[POM_DIR_REV])) conntrack_timer_queue(stream->t, stream->same_dir_timeout); packet_stream_end_process_packet(stream); debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : done queued", pthread_self(), stream, pkt->ts.tv_sec, pkt->ts.tv_usec, seq, ack); return PROTO_OK; }
int packet_stream_force_dequeue(struct packet_stream *stream) { struct packet_stream_pkt *p = NULL; unsigned int next_dir = 0; while (1) { if (!stream->head[POM_DIR_FWD] && !stream->head[POM_DIR_REV]) return POM_OK; if (!stream->head[POM_DIR_FWD]) { next_dir = POM_DIR_REV; } else if (!stream->head[POM_DIR_REV]) { next_dir = POM_DIR_FWD; } else { // We have packets in both direction, lets see which one we'll process first int i; for (i = 0; i < POM_DIR_TOT; i++) { int r = POM_DIR_REVERSE(i); struct packet_stream_pkt *a = stream->head[i], *b = stream->head[r]; uint32_t end_seq = a->seq + a->plen; if ((end_seq <= b->ack && b->ack - end_seq < PACKET_HALF_SEQ) || (b->ack > end_seq && end_seq - b->ack > PACKET_HALF_SEQ)) break; } if (i == POM_DIR_TOT) { // There is a gap in both direction // Process the first packet received struct packet *a = stream->head[POM_DIR_FWD]->pkt, *b = stream->head[POM_DIR_REV]->pkt; if (a->ts.tv_sec < b->ts.tv_sec || (a->ts.tv_sec == b->ts.tv_sec && a->ts.tv_usec < b->ts.tv_usec)) { next_dir = POM_DIR_FWD; } else { next_dir = POM_DIR_REV; } debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : processing next by timestamp", pthread_self(), stream, stream->head[next_dir]->pkt->ts.tv_sec, stream->head[next_dir]->pkt->ts.tv_usec, stream->head[next_dir]->seq, stream->head[next_dir]->ack); } else { next_dir = i; } } p = stream->head[next_dir]; if (p->next) p->next->prev = NULL; else stream->tail[next_dir] = NULL; stream->head[next_dir] = p->next; stream->cur_buff_size -= p->plen; if (packet_stream_is_packet_old_dupe(stream, p, next_dir)) { packet_stream_free_packet(p); } else { break; } } if (packet_stream_remove_dupe_bytes(stream, p, next_dir) == POM_ERR) return POM_ERR; uint32_t gap = p->seq - stream->cur_seq[next_dir]; int res = PROTO_OK; if (gap) { if (gap < stream->max_buff_size) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : filling gap of %u", pthread_self(), stream, p->pkt->ts.tv_sec, p->pkt->ts.tv_usec, p->seq, p->ack, gap); uint32_t gap_step = gap; if (gap_step > 2048) gap_step = 2048; void *zero = malloc(gap_step); if (!zero) { pom_oom(gap_step); return POM_ERR; } memset(zero, 0, gap_step); struct proto_process_stack *s = &p->stack[p->stack_index]; uint32_t plen_old = s->plen; void *pload_old = s->pload; uint32_t pos; for (pos = 0; pos < gap; pos += gap_step) { if (pos + gap_step < gap) s->plen = gap_step; else s->plen = gap - pos; s->pload = zero; res = stream->handler(stream->ce, p->pkt, p->stack, p->stack_index); s->direction = next_dir; if (res == PROTO_ERR) break; } free(zero); s->pload = pload_old; s->plen = plen_old; } else { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : gap of %u too big. not filling", pthread_self(), stream, p->pkt->ts.tv_sec, p->pkt->ts.tv_usec, p->seq, p->ack, gap); } } if (res != PROTO_ERR) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process forced", pthread_self(), stream, p->pkt->ts.tv_sec, p->pkt->ts.tv_usec, p->seq, p->ack); res = stream->handler(stream->ce, p->pkt, p->stack, p->stack_index); } stream->cur_seq[next_dir] = p->seq + p->plen; stream->cur_ack[next_dir] = p->ack; packet_stream_free_packet(p); if (res == PROTO_ERR) return POM_ERR; // See if we can process additional packets // Check if additional packets can be processed while ((p = packet_stream_get_next(stream, &next_dir))) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process additional", pthread_self(), stream, p->pkt->ts.tv_sec, p->pkt->ts.tv_usec, p->seq, p->ack); if (stream->handler(stream->ce, p->pkt, p->stack, p->stack_index) == PROTO_ERR) return POM_ERR; stream->cur_seq[next_dir] += p->plen; stream->cur_ack[next_dir] = p->ack; packet_stream_free_packet(p); } return POM_OK; }
int stream_force_dequeue(struct stream *stream) { struct stream_pkt *p = NULL; unsigned int next_dir = 0; while (1) { if (!stream->head[POM_DIR_FWD] && !stream->head[POM_DIR_REV]) return POM_OK; if (!stream->head[POM_DIR_FWD]) { next_dir = POM_DIR_REV; } else if (!stream->head[POM_DIR_REV]) { next_dir = POM_DIR_FWD; } else { // We have packets in both direction, lets see which one we'll process first int i; for (i = 0; i < POM_DIR_TOT; i++) { int r = POM_DIR_REVERSE(i); struct stream_pkt *a = stream->head[i], *b = stream->head[r]; uint32_t end_seq = a->seq + a->plen; if ((end_seq <= b->ack && b->ack - end_seq < STREAM_HALF_SEQ) || (b->ack > end_seq && end_seq - b->ack > STREAM_HALF_SEQ)) break; } if (i == POM_DIR_TOT) { // There is a gap in both direction // Process the first packet received struct packet *a = stream->head[POM_DIR_FWD]->pkt, *b = stream->head[POM_DIR_REV]->pkt; if (a->ts < b->ts) { next_dir = POM_DIR_FWD; } else { next_dir = POM_DIR_REV; } debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : processing next by timestamp", pthread_self(), stream, pom_ptime_sec(stream->head[next_dir]->pkt->ts), pom_ptime_usec(stream->head[next_dir]->pkt->ts), stream->head[next_dir]->seq, stream->head[next_dir]->ack); } else { next_dir = i; } } p = stream->head[next_dir]; if (p->next) p->next->prev = NULL; else stream->tail[next_dir] = NULL; stream->head[next_dir] = p->next; stream->cur_buff_size -= p->plen; if (stream_is_packet_old_dupe(stream, p, next_dir)) { stream_free_packet(p); } else { break; } } if (stream_remove_dupe_bytes(stream, p, next_dir) == POM_ERR) return POM_ERR; // Flag the stream as running stream->flags |= STREAM_FLAG_RUNNING; // If we didn't we now know about the sequence int dir_flag = (next_dir == POM_DIR_FWD ? STREAM_FLAG_GOT_FWD_STARTSEQ : STREAM_FLAG_GOT_REV_STARTSEQ); if (!(stream->flags & dir_flag)) { stream->cur_seq[next_dir] = p->seq; // We know about the reverse direction as well now if (stream->flags & STREAM_FLAG_BIDIR) { stream->flags |= STREAM_FLAG_GOT_BOTH_STARTSEQ; stream->cur_seq[POM_DIR_REVERSE(next_dir)] = p->ack; } else { stream->flags |= dir_flag; } } int res = PROTO_OK; // Check if we were waiting on the reverse direction if (stream->flags & STREAM_FLAG_BIDIR) { unsigned int next_rev_dir = POM_DIR_REVERSE(next_dir); int rev_dir_flag = (next_rev_dir == POM_DIR_FWD ? STREAM_FLAG_GOT_FWD_DIR : STREAM_FLAG_GOT_REV_DIR); // Only fill a gap in the reverse direction if we've had packets in that direction if (stream->flags & rev_dir_flag) { uint32_t rev_seq = stream->cur_seq[next_rev_dir]; if ((rev_seq < p->ack && p->ack - rev_seq < STREAM_HALF_SEQ) || (rev_seq > p->ack && rev_seq - p->ack > STREAM_HALF_SEQ)) { // We were waiting for reverse uint32_t rev_gap = p->ack - stream->cur_seq[next_rev_dir]; res = stream_fill_gap(stream, p, rev_gap, 1); stream->cur_seq[next_rev_dir] = p->ack; } } } uint32_t gap = p->seq - stream->cur_seq[next_dir]; if (gap) { if (res != PROTO_ERR) res = stream_fill_gap(stream, p, gap, 0); } // Update the cur_seq in our direction stream->cur_seq[next_dir] = p->seq + p->plen; if (res != PROTO_ERR) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process forced", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack); res = stream->handler(stream->ce, p->pkt, p->stack, p->stack_index); } stream_free_packet(p); if (res == PROTO_ERR) return POM_ERR; // See if we can process additional packets // Check if additional packets can be processed while ((p = stream_get_next(stream, &next_dir))) { debug_stream("thread %p, entry %p, packet %u.%06u, seq %u, ack %u : process additional", pthread_self(), stream, pom_ptime_sec(p->pkt->ts), pom_ptime_usec(p->pkt->ts), p->seq, p->ack); if (stream->handler(stream->ce, p->pkt, p->stack, p->stack_index) == PROTO_ERR) return POM_ERR; stream->cur_seq[next_dir] += p->plen; stream_free_packet(p); } return POM_OK; }
inline void obj_parser::yy_symbol_value_print_ (int yytype, const semantic_type* yyvaluep, const location_type* yylocationp) { YYUSE (yylocationp); YYUSE (yyvaluep); std::ostream& yyo = debug_stream (); std::ostream& yyoutput = yyo; YYUSE (yyoutput); YYUSE (yytype); }