@@ -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+
218244static 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,
324350static 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,
747779static 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