aboutsummaryrefslogtreecommitdiff
path: root/utils/hosts2domains.c
blob: e2e9b46281ffccd632f2ae379a61bfa0fc4c0ad9 (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
/*
 * ndpiReader.c
 *
 * Copyright (C) 2011-25 - ntop.org
 *
 * 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_api.h"
#include <assert.h>

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

int main(int argc, char *argv[]) {
  struct ndpi_detection_module_struct *ndpi_str;
  const char *lists_path = "../lists/public_suffix_list.dat";
  FILE *fd;
  char line[512];

  if(argc != 2) {
    printf("Usage: hosts2domains <filename>\n");
    return(0);
  }

  fd = fopen(argv[1], "r");
  if(fd == NULL) {
    printf("Unable to open %s\n", argv[1]);
    return(-1);
  }
  
  assert(ndpi_str = ndpi_init_detection_module(NULL));
  assert(ndpi_load_domain_suffixes(ndpi_str, (char*)lists_path) == 0);
  
  while (fgets(line, sizeof(line), fd) != NULL) {
    int len = strlen(line);

    if(len > 0) {
      if(line[len-1] == '\n')
	line[len-1] = '\0';
      
      printf("%s\n", ndpi_get_host_domain(ndpi_str, line));
    }
  }
  
  fclose(fd);
    
  ndpi_exit_detection_module(ndpi_str);

  return(0);
}