summaryrefslogtreecommitdiff
path: root/src/domain.c
blob: 7d97ab7c8b380f84afeb43db4dbb3ed21436507f (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
/*
 * Copyright (c) 2010-2011 Stefan Bolte <portix@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef DWB_DOMAIN_SERVICE
#include <glib-2.0/glib.h>
#include <string.h>
#include "util.h"

static GHashTable *_tld_table;
static char **_effective_tlds;

const char *
domain_get_base_for_host(const char *host) {
  const char *cur_domain = host;
  const char *prev_domain = host;
  const char *pprev_domain = host;
  const char *ret = NULL;
  char *nextdot = strchr(cur_domain, '.');
  char *entry = NULL;
  while (1) {
    entry = g_hash_table_lookup(_tld_table, cur_domain);
    if (entry != NULL) {
      if (*entry == '*') {
        ret = pprev_domain;
        break;
      }
      else if (*entry == '!' && nextdot) {
        ret = nextdot + 1;
        break;
      }
      else {
        ret = prev_domain;
        break;
      }
    }
    if (nextdot == NULL)
      break;
    pprev_domain = prev_domain;
    prev_domain = cur_domain;
    cur_domain = nextdot + 1;
    nextdot = strchr(cur_domain, '.');
  }
  return ret;
}
void
domain_end() {
  if (_tld_table)
    g_hash_table_unref(_tld_table);
  if (_effective_tlds) {
    g_strfreev(_effective_tlds);
  }
}

void 
domain_init() {
  _tld_table = g_hash_table_new((GHashFunc)g_str_hash, (GEqualFunc)g_str_equal);
  char *eff_tld;

  char *tld_file = util_get_data_file("tlds.txt");
  char *content = util_get_file_content(tld_file);
  _effective_tlds = g_strsplit(content, "\n", -1);
  for (int i=0; (eff_tld = _effective_tlds[i]); i++) {
    if (*eff_tld == '*' || *eff_tld == '!') 
      eff_tld++;
    if (*eff_tld == '.')
      eff_tld++;
    g_hash_table_insert(_tld_table, eff_tld, _effective_tlds[i]);
  }
  g_free(tld_file);
}
#endif