Skip to content

Commit 6b6b5c7

Browse files
committed
Enhanced STUN stats
1 parent b7405c8 commit 6b6b5c7

File tree

6 files changed

+72
-20
lines changed

6 files changed

+72
-20
lines changed

example/calls.conf

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#Useful ndpiReader configuration to analyse audio/video calls traffic
2+
#
3+
# ndpiReader --conf calls.conf
4+
#
25

36
#Generic limits
47
--cfg=packets_limit_per_flow,255 -U 0 -T 0

example/reader_util.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,12 @@ static void process_ndpi_monitoring_info(struct ndpi_flow_info *flow) {
11981198
add_to_address_port_list(&flow->stun.peer_address, &flow->ndpi_flow->monit->protos.dtls_stun_rtp.peer_address);
11991199
add_to_address_port_list(&flow->stun.relayed_address, &flow->ndpi_flow->monit->protos.dtls_stun_rtp.relayed_address);
12001200
add_to_address_port_list(&flow->stun.response_origin, &flow->ndpi_flow->monit->protos.dtls_stun_rtp.response_origin);
1201-
1201+
flow->stun.num_mapped_address = flow->ndpi_flow->stun.num_mapped_address;
1202+
flow->stun.num_relayed_address = flow->ndpi_flow->stun.num_relayed_address;
1203+
flow->stun.num_non_stun_pkts = flow->ndpi_flow->stun.num_non_stun_pkts;
1204+
flow->stun.num_stun_transitions = flow->ndpi_flow->stun.num_stun_transitions;
12021205
flow->multimedia_flow_types |= flow->ndpi_flow->flow_multimedia_types;
12031206
}
1204-
12051207
}
12061208

12071209
/* ****************************************************** */
@@ -1607,6 +1609,10 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
16071609
add_to_address_port_list(&flow->stun.relayed_address, &flow->ndpi_flow->stun.relayed_address);
16081610
add_to_address_port_list(&flow->stun.response_origin, &flow->ndpi_flow->stun.response_origin);
16091611
add_to_address_port_list(&flow->stun.other_address, &flow->ndpi_flow->stun.other_address);
1612+
flow->stun.num_mapped_address = flow->ndpi_flow->stun.num_mapped_address;
1613+
flow->stun.num_relayed_address = flow->ndpi_flow->stun.num_relayed_address;
1614+
flow->stun.num_non_stun_pkts = flow->ndpi_flow->stun.num_non_stun_pkts;
1615+
flow->stun.num_stun_transitions = flow->ndpi_flow->stun.num_stun_transitions;
16101616
}
16111617

16121618
flow->multimedia_flow_types |= flow->ndpi_flow->flow_multimedia_types;

example/reader_util.h

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ typedef struct ndpi_flow_info {
323323
struct {
324324
ndpi_address_port_list mapped_address, peer_address,
325325
relayed_address, response_origin, other_address;
326+
u_int8_t num_mapped_address, num_relayed_address, num_non_stun_pkts, num_stun_transitions;
326327
} stun;
327328

328329
struct {

src/include/ndpi_typedefs.h

+1
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ struct ndpi_flow_struct {
13711371
struct {
13721372
u_int8_t maybe_dtls : 1, is_turn : 1, pad : 6;
13731373
ndpi_address_port mapped_address, peer_address, relayed_address, response_origin, other_address;
1374+
u_int8_t num_mapped_address, num_relayed_address, num_non_stun_pkts, last_first_byte, num_stun_transitions; /* SRTP */
13741375
} stun;
13751376

13761377
struct {

src/lib/ndpi_utils.c

+6
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,8 @@ int ndpi_dpi2json(struct ndpi_detection_module_struct *ndpi_struct,
16291629
if(flow->stun.other_address.port)
16301630
ndpi_serialize_string_string(serializer, "other_address", print_ndpi_address_port(&flow->stun.other_address, buf, sizeof(buf)));
16311631

1632+
ndpi_serialize_string_string(serializer, "multimedia_flow_types",
1633+
ndpi_multimedia_flowtype2str(content, sizeof(content), flow->flow_multimedia_types));
16321634
ndpi_serialize_end_of_block(serializer);
16331635
break;
16341636

@@ -1649,6 +1651,10 @@ int ndpi_dpi2json(struct ndpi_detection_module_struct *ndpi_struct,
16491651
case NDPI_PROTOCOL_DTLS:
16501652
ndpi_tls2json(serializer, flow);
16511653
break;
1654+
1655+
#ifdef CUSTOM_NDPI_PROTOCOLS
1656+
#include "../../../nDPI-custom/ndpi_utils_dpi2json.c"
1657+
#endif
16521658
} /* switch */
16531659

16541660
ndpi_serialize_end_of_block(serializer); // "ndpi"

src/lib/protocols/stun.c

+53-18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ static int is_subclassification_real_by_proto(u_int16_t proto)
8484
return 1;
8585
}
8686

87+
/* ***************************************************** */
88+
8789
static int is_subclassification_real(struct ndpi_flow_struct *flow)
8890
{
8991
/* No previous subclassification */
@@ -92,6 +94,8 @@ static int is_subclassification_real(struct ndpi_flow_struct *flow)
9294
return is_subclassification_real_by_proto(flow->detected_protocol_stack[0]);
9395
}
9496

97+
/* ***************************************************** */
98+
9599
static int is_new_subclassification_better(struct ndpi_detection_module_struct *ndpi_struct,
96100
struct ndpi_flow_struct *flow,
97101
u_int16_t new_app_proto)
@@ -121,6 +125,7 @@ static int is_new_subclassification_better(struct ndpi_detection_module_struct *
121125
return 0;
122126
}
123127

128+
/* ***************************************************** */
124129

125130
static u_int16_t search_into_cache(struct ndpi_detection_module_struct *ndpi_struct,
126131
struct ndpi_flow_struct *flow)
@@ -167,6 +172,8 @@ static u_int16_t search_into_cache(struct ndpi_detection_module_struct *ndpi_str
167172
return NDPI_PROTOCOL_UNKNOWN;
168173
}
169174

175+
/* ***************************************************** */
176+
170177
static void add_to_cache(struct ndpi_detection_module_struct *ndpi_struct,
171178
struct ndpi_flow_struct *flow,
172179
u_int16_t app_proto)
@@ -187,6 +194,8 @@ static void add_to_cache(struct ndpi_detection_module_struct *ndpi_struct,
187194
}
188195
}
189196

197+
/* ***************************************************** */
198+
190199
static void parse_ip_port_attribute(const u_int8_t *payload, u_int16_t payload_length,
191200
int off, u_int16_t real_len, ndpi_address_port *ap,
192201
ndpi_address_port *ap_monit)
@@ -238,6 +247,8 @@ static void parse_ip_port_attribute(const u_int8_t *payload, u_int16_t payload_l
238247
}
239248
}
240249

250+
/* ***************************************************** */
251+
241252
static void parse_xor_ip_port_attribute(struct ndpi_detection_module_struct *ndpi_struct,
242253
struct ndpi_flow_struct *flow,
243254
const u_int8_t *payload, u_int16_t payload_length,
@@ -354,7 +365,7 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
354365
u_int32_t transaction_id[3];
355366

356367
if(payload_length < STUN_HDR_LEN)
357-
return(-1);
368+
return(-1);
358369

359370
/* Some really old/legacy stuff */
360371
if(strncmp((const char *)payload, "RSP/", 4) == 0 &&
@@ -400,7 +411,7 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
400411
if(packet->tcp) {
401412
if(msg_len + STUN_HDR_LEN > payload_length)
402413
return 0;
403-
414+
404415
payload_length = msg_len + STUN_HDR_LEN;
405416
}
406417

@@ -531,7 +542,7 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
531542
if(flow->host_server_name[0] == '\0') {
532543
int i;
533544
bool valid = true;
534-
545+
535546
ndpi_hostname_sni_set(flow, payload + off + 4, ndpi_min(len, payload_length - off - 4), NDPI_HOSTNAME_NORM_ALL);
536547
NDPI_LOG_DBG(ndpi_struct, "Realm [%s]\n", flow->host_server_name);
537548

@@ -604,6 +615,7 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
604615
&flow->stun.mapped_address,
605616
flow->monit ? &flow->monit->protos.dtls_stun_rtp.mapped_address : NULL,
606617
transaction_id, magic_cookie, 0);
618+
flow->stun.num_mapped_address++;
607619
}
608620
break;
609621

@@ -614,6 +626,7 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct,
614626
&flow->stun.relayed_address,
615627
flow->monit ? &flow->monit->protos.dtls_stun_rtp.relayed_address : NULL,
616628
transaction_id, magic_cookie, 0);
629+
flow->stun.num_relayed_address++;
617630
}
618631
break;
619632

@@ -634,18 +647,35 @@ static int keep_extra_dissection(struct ndpi_detection_module_struct *ndpi_struc
634647
struct ndpi_flow_struct *flow)
635648
{
636649
/* We want extra dissection for:
637-
* sub-classification
638-
* metadata extraction (*-ADDRESS) or looking for RTP
639-
At the moment:
640-
* it seems ZOOM doens't have any meaningful attributes
641-
* we want (all) XOR-PEER-ADDRESS only for Telegram.
642-
* for the other protocols, we stop after we have all metadata (if enabled)
643-
* for some specific protocol, we might know that some attributes
644-
are never used
645-
646-
**After** extra dissection is ended, we might move to monitoring. Note that:
647-
* classification doesn't change while in monitoring!
648-
*/
650+
* sub-classification
651+
* metadata extraction (*-ADDRESS) or looking for RTP
652+
At the moment:
653+
* it seems ZOOM doens't have any meaningful attributes
654+
* we want (all) XOR-PEER-ADDRESS only for Telegram.
655+
* for the other protocols, we stop after we have all metadata (if enabled)
656+
* for some specific protocol, we might know that some attributes
657+
are never used
658+
659+
**After** extra dissection is ended, we might move to monitoring. Note that:
660+
* classification doesn't change while in monitoring!
661+
*/
662+
663+
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
664+
bool is_stun_pkt = true;
665+
666+
if((packet->payload[0] != 0x0) && (packet->payload[0] != 0x1))
667+
flow->stun.num_non_stun_pkts++, is_stun_pkt = false;
668+
669+
if(flow->packet_counter > 1) {
670+
if((flow->stun.last_first_byte != 0x0) && (flow->stun.last_first_byte != 0x1)) {
671+
if(is_stun_pkt)
672+
flow->stun.num_stun_transitions++;
673+
} else {
674+
if(!is_stun_pkt)
675+
flow->stun.num_stun_transitions++;
676+
}
677+
}
678+
flow->stun.last_first_byte = packet->payload[0];
649679

650680
if(flow->monitoring)
651681
return 1;
@@ -705,6 +735,8 @@ static int keep_extra_dissection(struct ndpi_detection_module_struct *ndpi_struc
705735
return 1;
706736
}
707737

738+
/* ***************************************************** */
739+
708740
static u_int32_t __get_master(struct ndpi_flow_struct *flow) {
709741

710742
if(flow->detected_protocol_stack[1] != NDPI_PROTOCOL_UNKNOWN)
@@ -714,6 +746,8 @@ static u_int32_t __get_master(struct ndpi_flow_struct *flow) {
714746
return NDPI_PROTOCOL_STUN;
715747
}
716748

749+
/* ***************************************************** */
750+
717751
static int stun_search_again(struct ndpi_detection_module_struct *ndpi_struct,
718752
struct ndpi_flow_struct *flow)
719753
{
@@ -1082,13 +1116,12 @@ void switch_extra_dissection_to_stun(struct ndpi_detection_module_struct *ndpi_s
10821116

10831117
/* ************************************************************ */
10841118

1085-
10861119
static void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
10871120
{
10881121
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
10891122
u_int16_t app_proto;
10901123
int rc;
1091-
1124+
10921125
NDPI_LOG_DBG(ndpi_struct, "search stun\n");
10931126

10941127
app_proto = NDPI_PROTOCOL_UNKNOWN;
@@ -1101,7 +1134,7 @@ static void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, s
11011134
}
11021135

11031136
rc = is_stun(ndpi_struct, flow, &app_proto);
1104-
1137+
11051138
if(rc == 1) {
11061139
ndpi_int_stun_add_connection(ndpi_struct, flow, app_proto, __get_master(flow));
11071140
return;
@@ -1112,6 +1145,8 @@ static void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, s
11121145
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
11131146
}
11141147

1148+
/* ************************************************************ */
1149+
11151150
void init_stun_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id) {
11161151
ndpi_set_bitmask_protocol_detection("STUN", ndpi_struct, *id,
11171152
NDPI_PROTOCOL_STUN,

0 commit comments

Comments
 (0)