diff options
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r-- | example/ndpiReader.c | 86 |
1 files changed, 78 insertions, 8 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index a4afef821..6f054b75f 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -3071,6 +3071,77 @@ void automataUnitTest() { } /* *********************************************** */ + +void serializerUnitTest() { + ndpi_serializer serializer, deserializer; + int i; + u_int8_t trace = 0; + + assert(ndpi_init_serializer(&serializer, ndpi_serialization_format_tlv) != -1); + + for(i=0; i<16; i++) { + char kbuf[32], vbuf[32]; + assert(ndpi_serialize_uint32_uint32(&serializer, i, i*i) != -1); + + snprintf(kbuf, sizeof(kbuf), "Hello %u", i); + snprintf(vbuf, sizeof(vbuf), "World %u", i); + assert(ndpi_serialize_uint32_string(&serializer, i, "Hello") != -1); + assert(ndpi_serialize_string_string(&serializer, kbuf, vbuf) != -1); + } + + if(trace) + printf("Serialization size: %u/%u\n", serializer.size_used, serializer.buffer_size); + + assert(ndpi_init_deserializer(&deserializer, &serializer) != -1); + + while(1) { + ndpi_serialization_element_type et = ndpi_deserialize_get_nextitem_type(&deserializer); + + if(et == ndpi_serialization_unknown) + break; + else { + u_int32_t k32, v32; + ndpi_string ks, vs; + + switch(et) { + case ndpi_serialization_uint32_uint32: + assert(ndpi_deserialize_uint32_uint32(&deserializer, &k32, &v32) != -1); + if(trace) printf("%u=%u\n", k32, v32); + break; + + case ndpi_serialization_uint32_string: + assert(ndpi_deserialize_uint32_string(&deserializer, &k32, &vs) != -1); + if(trace) { + u_int8_t bkp = vs.str[vs.str_len]; + + vs.str[vs.str_len] = '\0'; + printf("%u=%s\n", k32, vs.str); + vs.str[vs.str_len] = bkp; + } + break; + + case ndpi_serialization_string_string: + assert(ndpi_deserialize_string_string(&deserializer, &ks, &vs) != -1); + if(trace) { + u_int8_t bkpk = ks.str[ks.str_len], bkp = vs.str[vs.str_len]; + + ks.str[ks.str_len] = vs.str[vs.str_len] = '\0'; + printf("%s=%s\n", ks.str, vs.str); + ks.str[ks.str_len] = bkpk, vs.str[vs.str_len] = bkp; + } + break; + + default: + break; + } + } + } + + ndpi_term_serializer(&serializer); +} + +/* *********************************************** */ + /** * @brief Produce bpf filter to filter ports and hosts * in order to remove a peak in terms of number of packets @@ -3112,7 +3183,7 @@ void bpf_filter_pkt_peak_filter(json_object **jObj_bpfFilter, int l; if(port_array[0] != INIT_VAL) - strncat(filter, " and not (src ", sizeof(" and not (src ")); + strncat(filter, " and not (src ", sizeof(filter)/sizeof(char)); else strcpy(filter, "not (src "); @@ -3139,7 +3210,7 @@ void bpf_filter_pkt_peak_filter(json_object **jObj_bpfFilter, int l; if(port_array[0] != INIT_VAL || src_host_array[0] != NULL) - strncat(filter, " and not (dst ", sizeof(" and not (dst ")); + strncat(filter, " and not (dst ", sizeof(filter)/sizeof(char)); else strcpy(filter, "not (dst "); @@ -3286,7 +3357,6 @@ void bpf_filter_port_array_add(int filter_array[], int size, int port) { float getAverage(struct json_object *jObj_stat, char *field){ json_object *field_stat; json_bool res; - float average; float sum = 0; int r; int j = 0; @@ -3334,11 +3404,11 @@ float getStdDeviation(struct json_object *jObj_stat, float average, char *field) json_object *field_stat; json_bool res; float sum = 0; - int j; + int j = 0; int r; if((r = strcmp(field, "top.scanner.stats")) == 0){ - for(j=0; j<json_object_array_length(jObj_stat); j++) { + for(; j<json_object_array_length(jObj_stat); j++) { field_stat = json_object_array_get_idx(jObj_stat, j); json_object *jObj_tot_flows_number; @@ -3523,7 +3593,6 @@ static void produceBpfFilter(char *filePath) { const char *filterPktDstHosts[48]; struct stat statbuf; FILE *fp = NULL; - char *fileName; char _filterFilePath[1024]; json_object *jObj_bpfFilter; void *fmap; @@ -3619,7 +3688,6 @@ static void produceBpfFilter(char *filePath) { } - fileName = basename(filePath); snprintf(_filterFilePath, sizeof(_filterFilePath), "%s.bpf", filePath); if((fp = fopen(_filterFilePath,"w")) == NULL) { @@ -3662,8 +3730,10 @@ int orginal_main(int argc, char **argv) { return(-1); } + /* Internal checks */ automataUnitTest(); - + serializerUnitTest(); + gettimeofday(&startup_time, NULL); ndpi_info_mod = ndpi_init_detection_module(); |