static void server_close_bad_streams(server_t server) { int i; for (i = server->streamsnum - 1; i >= 0; i--) { stream_t stream = server->streams + i; if (!stream->good) { server_stream_destroy(server, stream); if (i != server->streamsnum - 1) { // move the last one here *stream = server->streams[server->streamsnum - 1]; stream_move(stream, server->streams + server->streamsnum - 1); } server->streamsnum--; } } }
/* * search a pattern into the stream * returns - NULL if not found * - the packet containing the string if found */ struct so_list * stream_search(struct stream_object *so, u_char *buf, size_t buflen, int mode) { u_char *tmpbuf = NULL, *find; size_t offset = 0, len = 0; struct so_list *so_curr = NULL, *first; size_t po_off = 0; /* get the values into temp variable */ switch (mode) { case STREAM_SIDE1: so_curr = so->side1.so_curr; po_off = so->side1.po_off; break; case STREAM_SIDE2: so_curr = so->side2.so_curr; po_off = so->side2.po_off; break; } first = so_curr; /* create the buffer from the current position to the end */ for ( ; so_curr != TAILQ_END(so->so_head); so_curr = TAILQ_NEXT(so_curr, next)) { /* skip packet in the wrong side */ if (so_curr->side != mode) { po_off = 0; continue; } if (so_curr == first) len += so_curr->po.DATA.len - po_off; else len += so_curr->po.DATA.len; SAFE_REALLOC(tmpbuf, len); /* * add the packet to the end of the buffer * containing the whole conversation */ if (so_curr == first) memcpy(tmpbuf, so_curr->po.DATA.data + po_off, so_curr->po.DATA.len - po_off); else memcpy(tmpbuf + len - so_curr->po.DATA.len, so_curr->po.DATA.data, so_curr->po.DATA.len); } /* the buffer is found in the conversation */ if ((find = memmem(tmpbuf, len, buf, buflen)) != NULL) { offset = find - tmpbuf; SAFE_FREE(tmpbuf); /* move the stream pointers to the buffer found */ stream_move(so, offset, SEEK_CUR, mode); switch (mode) { case STREAM_SIDE1: return so->side1.so_curr; case STREAM_SIDE2: return so->side2.so_curr; } } SAFE_FREE(tmpbuf); return NULL; }