Partial fix for #1129
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
#define MYDBG(m, ...) \
|
||||
printf(" DBG[%s:%s:%u]: \t" m "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
|
||||
|
||||
|
||||
// #define NDPI_ENABLE_DEBUG_POINTER_MESSAGES
|
||||
// #define NDPI_ENABLE_DEBUG_INFO_MESSAGES
|
||||
// #define NDPI_ENABLE_DEBUG_TRACE_MESSAGES
|
||||
|
||||
+4
-3
@@ -4997,9 +4997,10 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
|
||||
|
||||
invalidate_ptr:
|
||||
#ifdef FRAG_MAN
|
||||
if (flow->must_free[flow->packet.packet_direction] &&
|
||||
flow->packet.payload_packet_len>0 && flow->packet.payload) {
|
||||
// if the payload is allocated for segments reassembling, it must be free
|
||||
if((flow->must_free[flow->packet.packet_direction] == 1)
|
||||
&& (flow->packet.payload_packet_len > 0)
|
||||
&& flow->packet.payload) {
|
||||
// if the payload is allocated for segments reassembling, it must be freed
|
||||
ndpi_free((void*)flow->packet.payload);
|
||||
// flow->packet.payload=NULL; done after
|
||||
flow->packet.payload_packet_len = 0;
|
||||
|
||||
+88
-35
@@ -57,6 +57,10 @@
|
||||
|
||||
// #define MATCH_DEBUG 1
|
||||
|
||||
// #define DEBUG_REASSEMBLY
|
||||
|
||||
/* ****************************************** */
|
||||
|
||||
/* implementation of the punycode check function */
|
||||
int ndpi_check_punycode_string(char * buffer , int len) {
|
||||
int i = 0;
|
||||
@@ -74,6 +78,8 @@ int ndpi_check_punycode_string(char * buffer , int len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ****************************************** */
|
||||
|
||||
/* ftp://ftp.cc.uoc.gr/mirrors/OpenBSD/src/lib/libc/stdlib/tsearch.c */
|
||||
/* find or insert datum into search tree */
|
||||
void * ndpi_tsearch(const void *vkey, void **vrootp,
|
||||
@@ -103,6 +109,8 @@ void * ndpi_tsearch(const void *vkey, void **vrootp,
|
||||
return ((void *)q->key);
|
||||
}
|
||||
|
||||
/* ****************************************** */
|
||||
|
||||
/* delete node with given key */
|
||||
void * ndpi_tdelete(const void *vkey, void **vrootp,
|
||||
int (*compar)(const void *, const void *))
|
||||
@@ -145,6 +153,8 @@ void * ndpi_tdelete(const void *vkey, void **vrootp,
|
||||
return(key);
|
||||
}
|
||||
|
||||
/* ****************************************** */
|
||||
|
||||
/* Walk the nodes of a tree */
|
||||
static void ndpi_trecurse(ndpi_node *root, void (*action)(const void *, ndpi_VISIT, int, void*), int level, void *user_data)
|
||||
{
|
||||
@@ -161,6 +171,8 @@ static void ndpi_trecurse(ndpi_node *root, void (*action)(const void *, ndpi_VIS
|
||||
}
|
||||
}
|
||||
|
||||
/* ****************************************** */
|
||||
|
||||
/* Walk the nodes of a tree */
|
||||
void ndpi_twalk(const void *vroot, void (*action)(const void *, ndpi_VISIT, int, void *), void *user_data)
|
||||
{
|
||||
@@ -170,6 +182,8 @@ void ndpi_twalk(const void *vroot, void (*action)(const void *, ndpi_VISIT, int,
|
||||
ndpi_trecurse(root, action, 0, user_data);
|
||||
}
|
||||
|
||||
/* ****************************************** */
|
||||
|
||||
/* find a node, or return 0 */
|
||||
void * ndpi_tfind(const void *vkey, void *vrootp,
|
||||
int (*compar)(const void *, const void *))
|
||||
@@ -1845,9 +1859,10 @@ void shell_sort_array(sorter_index_item_t arr[], int n) {
|
||||
int j;
|
||||
for(j = i; j >= interval && arr[j - interval].sort_value > temp.sort_value; j -= interval) {
|
||||
arr[j] = arr[j - interval];
|
||||
DBGTRACER("exchanged item no. %d (%d) with: %d (%d)", j, arr[j].sort_value, j-interval, temp.sort_value)
|
||||
DBGTRACER("exchanged item no. %d (%d) with: %d (%d)", j, arr[j].sort_value, j-interval, temp.sort_value);
|
||||
}
|
||||
DBGTRACER("item no. %d value: %d", j, temp.sort_value)
|
||||
|
||||
DBGTRACER("item no. %d value: %d", j, temp.sort_value);
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
@@ -1871,15 +1886,17 @@ void free_fragment(fragments_wrapper_t *frag) {
|
||||
*
|
||||
* */
|
||||
if(frag) {
|
||||
DBGTRACER("(frag:%p) freeing fragments list -> %p",frag, frag->fragments_list)
|
||||
DBGTRACER("(frag:%p) freeing fragments list -> %p",frag, frag->fragments_list);
|
||||
if(frag->fragments_list) {
|
||||
DBGTRACER("fragments are %u.",frag->ct_frag)
|
||||
DBGTRACER("fragments are %u.",frag->ct_frag);
|
||||
|
||||
for(int y=0;y<frag->ct_frag;y++) {
|
||||
if(frag->fragments_list[y]) {
|
||||
if(frag->fragments_list[y]->data) {
|
||||
DBGPOINTER("freeing fragment item %d -> %p",y, frag->fragments_list[y])
|
||||
DBGPOINTER("freeing fragment item %d -> %p",y, frag->fragments_list[y]);
|
||||
ndpi_free(frag->fragments_list[y]->data);
|
||||
}
|
||||
|
||||
ndpi_free(frag->fragments_list[y]);
|
||||
}
|
||||
}
|
||||
@@ -1901,7 +1918,7 @@ uint8_t add_segment_to_buffer(struct ndpi_flow_struct *flow, struct ndpi_tcphdr
|
||||
|
||||
if(flow->tcp_segments_management) {
|
||||
fragments_wrapper_t *fragW= &flow->tcp_segments_list[flow->packet.packet_direction];
|
||||
DBGTRACER("tcp segments management enabled (list container: %p)",fragW)
|
||||
DBGTRACER("tcp segments management enabled (list container: %p)", fragW);
|
||||
|
||||
if(fragW->ct_frag == 0) {
|
||||
if(fragW->fragments_list)
|
||||
@@ -1909,7 +1926,7 @@ uint8_t add_segment_to_buffer(struct ndpi_flow_struct *flow, struct ndpi_tcphdr
|
||||
|
||||
// initialize the offset with the first fragment seq number
|
||||
fragW->initial_offset = new_expected_seq;
|
||||
DBGTRACER("initialized initial_offset: %u)",fragW->initial_offset)
|
||||
DBGTRACER("initialized initial_offset: %u)",fragW->initial_offset);
|
||||
}
|
||||
|
||||
if(flow->packet.payload_packet_len > 0) {
|
||||
@@ -1917,28 +1934,28 @@ uint8_t add_segment_to_buffer(struct ndpi_flow_struct *flow, struct ndpi_tcphdr
|
||||
|
||||
// allocate memory for pointer
|
||||
size_t new_len= (1+fragW->ct_frag) * sizeof(fragment_t*);
|
||||
DBGTRACER("actual fragment list ct=%d, new size: %llu", fragW->ct_frag, (unsigned long long)new_len)
|
||||
DBGTRACER("actual fragment list ct=%d, new size: %llu", fragW->ct_frag, (unsigned long long)new_len);
|
||||
|
||||
fragW->fragments_list = ndpi_realloc(fragW->fragments_list,(fragW->ct_frag * sizeof(fragment_t*)),new_len);
|
||||
if(fragW->fragments_list == NULL) {
|
||||
flow->tcp_segments_management= 0;
|
||||
fprintf(stderr, "[%8u] Not enough memory for new fragment \n", flow->packet_counter);
|
||||
// fprintf(stderr, "[%8u] Not enough memory for new fragment \n", flow->packet_counter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DBGPOINTER("fragments_list initialized for item no. %u, list->%p i-esimo->%p",
|
||||
fragW->ct_frag, fragW->fragments_list, fragW->fragments_list[fragW->ct_frag])
|
||||
fragW->ct_frag, fragW->fragments_list, fragW->fragments_list[fragW->ct_frag]);
|
||||
|
||||
// allocate memory for item
|
||||
fragment_t *new_frag = (fragment_t*)ndpi_calloc(1, sizeof(fragment_t));
|
||||
if(new_frag == NULL) {
|
||||
flow->tcp_segments_management= 0;
|
||||
free_fragment(fragW);
|
||||
fprintf(stderr, "[%8u] Not enough memory for new fragment \n", flow->packet_counter);
|
||||
// fprintf(stderr, "[%8u] Not enough memory for new fragment \n", flow->packet_counter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DBGPOINTER("new_frag=> %p",new_frag)
|
||||
DBGPOINTER("new_frag=> %p",new_frag);
|
||||
|
||||
// fill item with offsetm len and data fragment/segment
|
||||
seq = (0xffffffff & ntohl(tcph->seq));
|
||||
@@ -1956,25 +1973,31 @@ uint8_t add_segment_to_buffer(struct ndpi_flow_struct *flow, struct ndpi_tcphdr
|
||||
}
|
||||
|
||||
DBGTRACER("offset calculation: seq %u, init: %u, offset result: %u", ntohl(tcph->seq),
|
||||
fragW->initial_offset, new_frag->offset)
|
||||
fragW->initial_offset, new_frag->offset);
|
||||
new_frag->len = flow->packet.payload_packet_len;
|
||||
|
||||
new_frag->data = (void*)ndpi_calloc(new_frag->len, sizeof(char));
|
||||
DBGPOINTER("new_frag->data=> %p",new_frag->data)
|
||||
if(new_frag->data) {
|
||||
memcpy(new_frag->data,flow->packet.payload,new_frag->len);
|
||||
fragW->fragments_list[fragW->ct_frag++]= new_frag;
|
||||
|
||||
#ifdef DEBUG_REASSEMBLY
|
||||
printf("[%s:%u] ==>> %s() [%p][offset: %u]\n",
|
||||
__FILE__, __LINE__, __FUNCTION__, new_frag, new_frag->offset);
|
||||
#endif
|
||||
|
||||
fragW->fragments_list[fragW->ct_frag++]= new_frag; /* Add it to the list */
|
||||
} else {
|
||||
flow->tcp_segments_management= 0;
|
||||
ndpi_free(new_frag);
|
||||
free_fragment(fragW);
|
||||
fprintf(stderr, "[%8u] Not enough memory for new fragment data \n", flow->packet_counter);
|
||||
// fprintf(stderr, "[%8u] Not enough memory for new fragment data \n", flow->packet_counter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DBGINFO("item no. %u: %p->%p [off:%u, len:%u, data:%p]",
|
||||
fragW->ct_frag, fragW->fragments_list, *fragW->fragments_list,
|
||||
(unsigned int) new_frag->offset, (unsigned int)new_frag->len, new_frag->data)
|
||||
(unsigned int) new_frag->offset, (unsigned int)new_frag->len, new_frag->data);
|
||||
|
||||
return fragW->ct_frag;
|
||||
}
|
||||
@@ -1987,7 +2010,9 @@ uint8_t add_segment_to_buffer(struct ndpi_flow_struct *flow, struct ndpi_tcphdr
|
||||
|
||||
/* ******************************************************************** */
|
||||
|
||||
uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tcphdr const * tcph, uint8_t **ret_buffer, size_t *len_buffer) {
|
||||
uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow,
|
||||
struct ndpi_tcphdr const * tcph,
|
||||
uint8_t **ret_buffer, size_t *len_buffer) {
|
||||
uint32_t ret_value = 0;
|
||||
uint16_t last_item = 0;
|
||||
size_t length = 0, tot_length = 0;
|
||||
@@ -1995,6 +2020,10 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
fragments_wrapper_t *fragW;
|
||||
uint8_t *buffer;
|
||||
|
||||
#ifdef DEBUG_REASSEMBLY
|
||||
printf("[%s:%u] ==>> %s()\n", __FILE__, __LINE__, __FUNCTION__);
|
||||
#endif
|
||||
|
||||
fragW = &flow->tcp_segments_list[flow->packet.packet_direction];
|
||||
|
||||
DBGTRACER("tcph:%p, ret_buffer:%p, len_buffer:%u", tcph, ret_buffer, len_buffer);
|
||||
@@ -2002,16 +2031,15 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
// phase 1: calculate the size and fill the indexes array
|
||||
DBGINFO("phase 1: init sorter, calculate the size of buffer to reassemble: %u items", fragW->ct_frag);
|
||||
|
||||
sorted_indexes = ndpi_calloc(fragW->ct_frag, sizeof(sorter_index_item_t));
|
||||
sorted_indexes = (sorter_index_item_t*)ndpi_calloc(fragW->ct_frag, sizeof(sorter_index_item_t));
|
||||
|
||||
if(sorted_indexes == NULL) {
|
||||
fprintf(stderr, "[%8u] Not enough memory to sort the %u segments \n",
|
||||
flow->packet_counter, fragW->ct_frag);
|
||||
//fprintf(stderr, "[%8u] Not enough memory to sort the %u segments \n", flow->packet_counter, fragW->ct_frag);
|
||||
free_fragment(fragW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DBGPOINTER("sorted_indexes=> %p", sorted_indexes)
|
||||
DBGPOINTER("sorted_indexes=> %p", sorted_indexes);
|
||||
|
||||
for(int i=0; i<fragW->ct_frag; i++) {
|
||||
fragment_t *item = (fragment_t*)fragW->fragments_list[i];
|
||||
@@ -2037,32 +2065,33 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
DBGTRACER("checking %d/%d element: offset=%lu vs t_length=%lu",
|
||||
i, sorted_indexes[i].item_index, (unsigned long)item->offset, (unsigned long)tot_length);
|
||||
|
||||
if (item->offset > (uint32_t)tot_length) {
|
||||
if((item->offset+item->len) > (uint32_t)tot_length) {
|
||||
// update the last index of elements to elaborate
|
||||
DBGINFO("stop processing at %d/%d element: len= %u; offset= %u",
|
||||
i, sorted_indexes[i].item_index, (unsigned)length, (unsigned)item->offset)
|
||||
|
||||
tot_length = length;
|
||||
// tot_length = length; /* CHECK THIS CASE */
|
||||
#ifdef DEBUG_REASSEMBLY
|
||||
printf("[%s:%u] ==>> Too long [last_item: %u][offset: %u/len: %u][%p]\n", __FILE__, __LINE__,
|
||||
last_item, item->offset, item->len, item);
|
||||
#endif
|
||||
|
||||
tot_length = item->offset+item->len; /* CHECK THIS CASE */
|
||||
|
||||
// set the first offset to wait for the next segment
|
||||
ret_value = fragW->initial_offset + item->offset;
|
||||
|
||||
break;
|
||||
|
||||
// 2: for every len(buffer) must exists a offset fragment
|
||||
} else if(item->offset != (uint32_t)length) {
|
||||
|
||||
// update the last index of elements to elaborate
|
||||
DBGINFO("checking %d/%d element: stop processing! len: %u; n_offset: %u",
|
||||
i, sorted_indexes[i].item_index, (unsigned)length, (unsigned)item->offset);
|
||||
|
||||
tot_length = length;
|
||||
// tot_length = length; /* CHECK THIS CASE */
|
||||
|
||||
// set the first offset to wait for the next segment
|
||||
ret_value = fragW->initial_offset + item->offset;
|
||||
|
||||
break;
|
||||
|
||||
} else {
|
||||
// continue to sum length data bytes
|
||||
length += item->len;
|
||||
@@ -2074,11 +2103,15 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
// phase 3: allocate memory and fill the buffer
|
||||
DBGINFO("phase 3: allocate memory for %u items and fill the buffer tot: %lu", last_item, (unsigned long int)tot_length);
|
||||
|
||||
#ifdef DEBUG_REASSEMBLY
|
||||
printf("[%s:%u] ==>> [tot_length: %u][length: %u]\n",
|
||||
__FILE__, __LINE__, (u_int32_t)tot_length, (u_int32_t)length);
|
||||
#endif
|
||||
|
||||
buffer = ndpi_calloc(tot_length, sizeof(uint8_t));
|
||||
|
||||
if(buffer == NULL) {
|
||||
fprintf(stderr, "[%8u] Not enough memory for buffer for %u segments \n",
|
||||
flow->packet_counter,last_item);
|
||||
// fprintf(stderr, "[%8u] Not enough memory for buffer for %u segments \n", flow->packet_counter,last_item);
|
||||
free_fragment(fragW);
|
||||
ndpi_free(sorted_indexes);
|
||||
return 0;
|
||||
@@ -2093,6 +2126,22 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
sorted_indexes[i].item_index, (unsigned long int)item->len, buffer,
|
||||
(unsigned long int)item->offset);
|
||||
|
||||
if((item->offset+item->len) > tot_length) {
|
||||
//#ifdef DEBUG_REASSEMBLY
|
||||
printf("[%s:%u] ==>> Out of boundary [%u vs %u][offset: %u][len: %u][item: %u/%u]\n", __FILE__, __LINE__,
|
||||
(u_int32_t)(item->offset+item->len), (u_int32_t)tot_length,
|
||||
(u_int32_t)item->offset, (u_int32_t)item->len, i, last_item);
|
||||
//#endif
|
||||
continue;
|
||||
} else {
|
||||
#ifdef DEBUG_REASSEMBLY
|
||||
printf("[%s:%u] ==>> memcpy OK [%u vs %u][offset: %u][item: %u/%u]\n",
|
||||
__FILE__, __LINE__,
|
||||
(u_int32_t)(item->offset+item->len), (u_int32_t)tot_length,
|
||||
item->offset, i, last_item);
|
||||
#endif
|
||||
}
|
||||
|
||||
memcpy((void*)(buffer + item->offset), item->data, item->len);
|
||||
|
||||
// free memory item
|
||||
@@ -2114,7 +2163,7 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
fragW_old_list, fragW->fragments_list);
|
||||
|
||||
if(!fragW->fragments_list) {
|
||||
fprintf(stderr, "[%8u] Not enough memory for new segments list \n", flow->packet_counter);
|
||||
// fprintf(stderr, "[%8u] Not enough memory for new segments list \n", flow->packet_counter);
|
||||
free_fragment(fragW);
|
||||
ndpi_free(buffer);
|
||||
ndpi_free(sorted_indexes);
|
||||
@@ -2171,7 +2220,7 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
|
||||
}
|
||||
|
||||
DBGINFO("returning: %d", ret_value);
|
||||
return ret_value;
|
||||
return(ret_value);
|
||||
}
|
||||
|
||||
/* ******************************************************************** */
|
||||
@@ -2190,7 +2239,8 @@ uint8_t check_for_sequence(struct ndpi_flow_struct *flow, struct ndpi_tcphdr con
|
||||
not_sorted = &flow->not_sorted[flow->packet.packet_direction];
|
||||
trigger = &flow->trigger[flow->packet.packet_direction];
|
||||
|
||||
DBGTRACER("dir:%d, trg:%u, next:%u", flow->packet.packet_direction,*trigger, flow->next_tcp_seq_nr[flow->packet.packet_direction]);
|
||||
DBGTRACER("dir:%d, trg:%u, next:%u", flow->packet.packet_direction,*trigger,
|
||||
flow->next_tcp_seq_nr[flow->packet.packet_direction]);
|
||||
|
||||
expected = (*not_sorted && *trigger) ? ndpi_min(*trigger, flow->next_tcp_seq_nr[flow->packet.packet_direction]) : flow->next_tcp_seq_nr[flow->packet.packet_direction];
|
||||
|
||||
@@ -2228,7 +2278,10 @@ uint8_t check_for_sequence(struct ndpi_flow_struct *flow, struct ndpi_tcphdr con
|
||||
}
|
||||
|
||||
} else {
|
||||
DBGTRACER("seq (%u) and expected (%u) matched! sorted flag: %d", (0xffffffff & ntohl(tcph->seq)), flow->next_tcp_seq_nr[flow->packet.packet_direction], *not_sorted);
|
||||
DBGTRACER("seq (%u) and expected (%u) matched! sorted flag: %d",
|
||||
(0xffffffff & ntohl(tcph->seq)),
|
||||
flow->next_tcp_seq_nr[flow->packet.packet_direction],
|
||||
*not_sorted);
|
||||
|
||||
if(*not_sorted) {
|
||||
if(add_segment_to_buffer(flow, tcph, 0)) {
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
Unknown 200 20067 1
|
||||
|
||||
|
||||
|
||||
Undetected flows:
|
||||
1 TCP 192.168.145.147:51218 <-> 10.209.8.148:21999 [proto: 0/Unknown][187 pkts/13776 bytes <-> 13 pkts/6291 bytes][Goodput ratio: 7/86][1475.07 sec][bytes ratio: 0.373 (Upload)][IAT c2s/s2c min/avg/max/stddev: 0/1 8292/21500 30100/30165 13444/13597][Pkt Len c2s/s2c min/avg/max/stddev: 60/122 74/484 113/793 8/332][PLAIN TEXT (details /)][Plen Bins: 80,8,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
|
||||
@@ -0,0 +1,3 @@
|
||||
HTTP 82 77940 1
|
||||
|
||||
1 TCP 172.17.36.21:57619 <-> 63.190.145.43:80 [proto: GTP:7/HTTP][cat: Web/5][28 pkts/3184 bytes <-> 54 pkts/74756 bytes][Goodput ratio: 0/93][15.67 sec][bytes ratio: -0.918 (Download)][IAT c2s/s2c min/avg/max/stddev: 0/0 448/205 1615/2133 476/518][Pkt Len c2s/s2c min/avg/max/stddev: 94/90 114/1384 130/1490 9/330][PLAIN TEXT (.iJoJJ)][Plen Bins: 0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,1,0,0,92,0,0,0,0]
|
||||
Reference in New Issue
Block a user