Skip to content

Commit 7dc5890

Browse files
authored
DNS: rework adding entries to the FPC-DNS cache (#2730)
Try to populate the FPC-DNS cache using directly the info from the current packet, and not from the metadata saved in `struct ndpi_flow_struct`. This will be important when adding monitoring support
1 parent c458c42 commit 7dc5890

File tree

4 files changed

+61
-62
lines changed

4 files changed

+61
-62
lines changed

src/include/ndpi_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ int load_config_file_fd(struct ndpi_detection_module_struct *ndpi_str, FILE *fd)
654654
int load_category_file_fd(struct ndpi_detection_module_struct *ndpi_str,
655655
FILE *fd, ndpi_protocol_category_t category_id);
656656

657-
u_int64_t fpc_dns_cache_key_from_dns_info(struct ndpi_flow_struct *flow);
657+
u_int64_t fpc_dns_cache_key_from_flow(struct ndpi_flow_struct *flow);
658658

659659
bool ndpi_cache_address(struct ndpi_detection_module_struct *ndpi_struct,
660660
ndpi_ip_addr_t ip_addr, char *hostname,

src/lib/ndpi_main.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7671,32 +7671,6 @@ u_int16_t ndpi_guess_host_protocol_id(struct ndpi_detection_module_struct *ndpi_
76717671

76727672
/* ********************************************************************************* */
76737673

7674-
static u_int64_t make_fpc_dns_cache_key(struct ndpi_flow_struct *flow) {
7675-
u_int64_t key;
7676-
7677-
if(flow->is_ipv6)
7678-
key = ndpi_quick_hash64((const char *)flow->s_address.v6, 16);
7679-
else
7680-
key = (u_int64_t)(flow->s_address.v4);
7681-
7682-
return key;
7683-
}
7684-
7685-
/* ********************************************************************************* */
7686-
7687-
u_int64_t fpc_dns_cache_key_from_dns_info(struct ndpi_flow_struct *flow) {
7688-
u_int64_t key;
7689-
7690-
if(flow->protos.dns.is_rsp_addr_ipv6[0])
7691-
key = ndpi_quick_hash64((const char *)&flow->protos.dns.rsp_addr[0].ipv6, 16);
7692-
else
7693-
key = (u_int64_t)(flow->protos.dns.rsp_addr[0].ipv4);
7694-
7695-
return key;
7696-
}
7697-
7698-
/* ********************************************************************************* */
7699-
77007674
static u_int64_t make_msteams_key(struct ndpi_flow_struct *flow, u_int8_t use_client) {
77017675
u_int64_t key;
77027676

@@ -8724,7 +8698,7 @@ static void fpc_check_eval(struct ndpi_detection_module_struct *ndpi_str,
87248698

87258699
/* Check via fpc DNS cache */
87268700
if(ndpi_str->fpc_dns_cache &&
8727-
ndpi_lru_find_cache(ndpi_str->fpc_dns_cache, make_fpc_dns_cache_key(flow),
8701+
ndpi_lru_find_cache(ndpi_str->fpc_dns_cache, fpc_dns_cache_key_from_flow(flow),
87288702
&fpc_dns_cached_proto, 0 /* Don't remove it as it can be used for other connections */,
87298703
ndpi_get_current_time(flow))) {
87308704
fpc_update(ndpi_str, flow, NDPI_PROTOCOL_UNKNOWN,

src/lib/protocols/dns.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,32 @@ static char* dns_error_code2string(u_int16_t error_code, char *buf, u_int buf_le
215215

216216
/* *********************************************** */
217217

218+
u_int64_t fpc_dns_cache_key_from_flow(struct ndpi_flow_struct *flow) {
219+
u_int64_t key;
220+
221+
if(flow->is_ipv6)
222+
key = ndpi_quick_hash64((const char *)flow->s_address.v6, 16);
223+
else
224+
key = (u_int64_t)(flow->s_address.v4);
225+
226+
return key;
227+
}
228+
229+
/* *********************************************** */
230+
231+
static u_int64_t fpc_dns_cache_key_from_packet(const unsigned char *ip, int ip_len) {
232+
u_int64_t key;
233+
234+
if(ip_len == 16)
235+
key = ndpi_quick_hash64((const char *)ip, 16);
236+
else
237+
key = (u_int64_t)(*(u_int32_t *)ip);
238+
239+
return key;
240+
}
241+
242+
/* *********************************************** */
243+
218244
static u_int8_t ndpi_grab_dns_name(struct ndpi_packet_struct *packet,
219245
u_int *off /* payload offset */,
220246
char *_hostname, u_int max_len,
@@ -324,13 +350,17 @@ static int process_queries(struct ndpi_detection_module_struct *ndpi_struct,
324350
static int process_answers(struct ndpi_detection_module_struct *ndpi_struct,
325351
struct ndpi_flow_struct *flow,
326352
struct ndpi_dns_packet_header *dns_header,
327-
u_int payload_offset, u_int8_t ignore_checks) {
353+
u_int payload_offset,
354+
ndpi_master_app_protocol *proto) {
328355
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
329356
u_int x = payload_offset;
330357
u_int16_t rsp_type;
331358
u_int32_t rsp_ttl;
332359
u_int16_t num;
333360
u_int8_t found = 0;
361+
int ignore_checks;
362+
363+
ignore_checks = (proto->master_protocol == NDPI_PROTOCOL_MDNS);
334364

335365
for(num = 0; num < dns_header->num_answers; num++) {
336366
u_int16_t data_len;
@@ -419,6 +449,18 @@ static int process_answers(struct ndpi_detection_module_struct *ndpi_struct,
419449
if(flow->protos.dns.num_rsp_addr >= MAX_NUM_DNS_RSP_ADDRESSES)
420450
found = 1;
421451
}
452+
453+
/* Add to FPC DNS cache */
454+
if(flow->protos.dns.num_rsp_addr == 1 && /* Only the first one */
455+
ndpi_struct->cfg.fpc_enabled &&
456+
proto->app_protocol != NDPI_PROTOCOL_UNKNOWN &&
457+
proto->app_protocol != proto->master_protocol &&
458+
ndpi_struct->fpc_dns_cache) {
459+
ndpi_lru_add_to_cache(ndpi_struct->fpc_dns_cache,
460+
fpc_dns_cache_key_from_packet(packet->payload + x, data_len),
461+
proto->app_protocol,
462+
ndpi_get_current_time(flow));
463+
}
422464
}
423465

424466
x += data_len;
@@ -727,16 +769,6 @@ static int process_hostname(struct ndpi_detection_module_struct *ndpi_struct,
727769
&ret_match,
728770
proto->master_protocol,
729771
ndpi_struct->cfg.dns_subclassification_enabled ? 1 : 0);
730-
/* Add to FPC DNS cache */
731-
if(ndpi_struct->cfg.fpc_enabled &&
732-
proto->app_protocol != NDPI_PROTOCOL_UNKNOWN &&
733-
proto->app_protocol != proto->master_protocol &&
734-
(flow->protos.dns.rsp_type == 0x1 || flow->protos.dns.rsp_type == 0x1c) && /* A, AAAA */
735-
ndpi_struct->fpc_dns_cache) {
736-
ndpi_lru_add_to_cache(ndpi_struct->fpc_dns_cache,
737-
fpc_dns_cache_key_from_dns_info(flow), proto->app_protocol,
738-
ndpi_get_current_time(flow));
739-
}
740772

741773
ndpi_check_dga_name(ndpi_struct, flow, flow->host_server_name, 1, 0, proto->app_protocol != NDPI_PROTOCOL_UNKNOWN);
742774
}
@@ -747,25 +779,18 @@ static int process_hostname(struct ndpi_detection_module_struct *ndpi_struct,
747779
static void search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) {
748780
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
749781
int payload_offset = 0;
750-
u_int8_t is_query, is_mdns;
751-
u_int16_t s_port = 0, d_port = 0;
782+
u_int8_t is_query;
752783
struct ndpi_dns_packet_header dns_header;
753784
u_int off;
754785
ndpi_master_app_protocol proto;
755786
int rc;
756787

757788
if(packet->udp != NULL) {
758-
s_port = ntohs(packet->udp->source);
759-
d_port = ntohs(packet->udp->dest);
760789
payload_offset = 0;
761-
} else if(packet->tcp != NULL) /* pkt size > 512 bytes */ {
762-
s_port = ntohs(packet->tcp->source);
763-
d_port = ntohs(packet->tcp->dest);
790+
} else if(packet->tcp != NULL) {
764791
payload_offset = 2;
765792
}
766793

767-
is_mdns = ((s_port == MDNS_PORT) || (d_port == MDNS_PORT)) ? 1 : 0;
768-
769794
if(!is_valid_dns(ndpi_struct, flow, &dns_header, payload_offset, &is_query)) {
770795
#ifdef DNS_DEBUG
771796
printf("[DNS] invalid packet\n");
@@ -778,6 +803,8 @@ static void search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct
778803
return;
779804
}
780805

806+
process_hostname(ndpi_struct, flow, &proto);
807+
781808
off = sizeof(struct ndpi_dns_packet_header) + payload_offset;
782809

783810
if(is_query) {
@@ -812,7 +839,7 @@ static void search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct
812839
#endif
813840
} else {
814841
off = rc;
815-
rc = process_answers(ndpi_struct, flow, &dns_header, off, is_mdns);
842+
rc = process_answers(ndpi_struct, flow, &dns_header, off, &proto);
816843
if(rc == -1) {
817844
#ifdef DNS_DEBUG
818845
printf("[DNS] Error answers\n");
@@ -828,8 +855,6 @@ static void search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct
828855
}
829856
}
830857

831-
process_hostname(ndpi_struct, flow, &proto);
832-
833858
/* Report if this is a DNS query or reply */
834859
flow->protos.dns.is_query = is_query;
835860

0 commit comments

Comments
 (0)