aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_domains.c
blob: aeb34b010b21c62600cd370cc0023858577e0435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * ndpi_domains.c
 *
 * Copyright (C) 2011-24 - ntop.org and contributors
 *
 * nDPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nDPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ndpi_config.h"
#include "ndpi_api.h"
#include "ndpi_includes.h"
#include "ndpi_private.h"

/* ******************************* */

int ndpi_load_domain_suffixes(struct ndpi_detection_module_struct *ndpi_str,
			      char *public_suffix_list_path) {
  char buf[256], *line;
  FILE *fd;
  bool do_trace = false;
  u_int num_domains = 0;
  
  if(public_suffix_list_path == NULL)
    return(-1);

  if((fd = fopen(public_suffix_list_path, "r")) == NULL)
    return(-2);
  
  if(ndpi_str->public_domain_suffixes != NULL) {
    /* An existing license was aleady loaded: free it and start over */
    ndpi_domain_classify_free(ndpi_str->public_domain_suffixes);
  }

  if((ndpi_str->public_domain_suffixes = ndpi_domain_classify_alloc()) == NULL)
    return(-3);

  while((line = fgets(buf, sizeof(buf), fd)) != NULL) {
    u_int offset, len;
    
    /* Skip empty lines or comments */
    if((line[0] == '\0') || (line[0] == '/') || (line[0] == '\n') || (line[0] == '\r'))
      continue;

    if((line[0] == '*') && (line[1] == '.') && (line[2] != '\0'))
      offset = 2;
    else
      offset = 0;

    len = strlen(line) - 1;
    while((len > 0) && (line[len] == '\n'))
      line[len--] = '\0';

    if(!ndpi_domain_classify_add(ndpi_str->public_domain_suffixes,
				 1 /* dummy */, &line[offset])) {
      if(do_trace) NDPI_LOG_ERR(ndpi_str, "Error while processing domain %s\n", &line[offset]);
    } else
      num_domains++;
  }

  if(!ndpi_domain_classify_finalize(ndpi_str->public_domain_suffixes)) {
    if(do_trace) NDPI_LOG_ERR(ndpi_str, "Error while finalizing domain processing\n");
  }

  if(do_trace) NDPI_LOG_ERR(ndpi_str, "Loaded %u domains\n", num_domains);
  
  return(0);
}

/* ******************************* */

const char* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndpi_str,
					const char *hostname) {
  if(ndpi_str->public_domain_suffixes == NULL)
    return(hostname);
  else {
    u_int8_t class_id;
    
    return(ndpi_domain_classify_longest_prefix(ndpi_str->public_domain_suffixes,
					       &class_id, hostname, false));
  }
}

/* ******************************* */

const char* ndpi_get_host_domain(struct ndpi_detection_module_struct *ndpi_str,
				 const char *hostname) {
  if(ndpi_str->public_domain_suffixes == NULL)
    return(hostname);
  else {
    u_int8_t class_id;
    
    return(ndpi_domain_classify_longest_prefix(ndpi_str->public_domain_suffixes,
					       &class_id, hostname, true));
  }
}