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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* ndpi_domain_classify.c
*
* Copyright (C) 2011-23 - 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 <stdlib.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include "ndpi_config.h"
#include "ndpi_api.h"
#if 0
#define DEBUG_ADD
#define DEBUG_CONTAINS
#endif
ndpi_domain_classify* ndpi_domain_classify_alloc() {
int i;
ndpi_domain_classify *cat = (ndpi_domain_classify*)ndpi_malloc(sizeof(ndpi_domain_classify));
if(!cat)
return NULL;
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++)
cat->classes[i].class_id = 0, cat->classes[i].domains = NULL;
return((ndpi_domain_classify*)cat);
}
/* ********************************************************** */
void ndpi_domain_classify_free(ndpi_domain_classify *s) {
u_int32_t i;
if(!s)
return;
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].domains != NULL) {
ndpi_bitmap64_free(s->classes[i].domains);
} else
break;
}
ndpi_free(s);
}
/* ********************************************************** */
u_int32_t ndpi_domain_classify_size(ndpi_domain_classify *s) {
u_int32_t i, tot_len = sizeof(ndpi_domain_classify);
if(!s)
return(0);
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].domains != NULL) {
tot_len += ndpi_bitmap64_size(s->classes[i].domains);
} else
break;
}
return(tot_len);
}
/* ********************************************************** */
bool ndpi_domain_classify_add(ndpi_domain_classify *s,
u_int8_t class_id,
const char *domain) {
u_int32_t i;
char *dot;
if(!s || !domain)
return(false);
/* Skip initial string . in domain names */
while(domain[0] == '.') domain++;
dot = strrchr(domain, '.');
if(!dot) return(false);
if((!strcmp(dot, ".arpa")) || (!strcmp(dot, ".local")))
return(false);
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].class_id == class_id) {
break;
} else if(s->classes[i].class_id == 0) {
s->classes[i].class_id = class_id;
s->classes[i].domains = ndpi_bitmap64_alloc();
if(!s->classes[i].domains)
s->classes[i].class_id = 0;
break;
}
}
if(i == MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS)
return(false);
return(ndpi_bitmap64_set(s->classes[i].domains,
ndpi_quick_hash64(domain, strlen(domain))));
}
/* ********************************************************** */
u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *s,
u_int8_t class_id,
char *file_path) {
u_int32_t i, num_added = 0;
char buf[256];
FILE *fd;
char *line;
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].class_id == class_id) {
break;
} else if(s->classes[i].class_id == 0) {
s->classes[i].class_id = class_id;
s->classes[i].domains = ndpi_bitmap64_alloc();
if(!s->classes[i].domains)
s->classes[i].class_id = 0;
break;
}
}
if(i == MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS)
return(false);
/* *************************************** */
fd = fopen(file_path, "r");
if(fd == NULL)
return(false);
while((line = fgets(buf, sizeof(buf), fd)) != NULL) {
u_int len;
u_int64_t hash;
if((line[0] == '#') || (line[0] == '\0'))
continue;
else {
len = strlen(line) - 1;
if(len == 0)
continue;
else
line[len] = '\0';
}
hash = ndpi_quick_hash64(line, strlen(line));
if(ndpi_bitmap64_set(s->classes[i].domains, hash))
num_added++;
}
fclose(fd);
return(num_added);
}
/* ********************************************************** */
static bool is_valid_domain_char(u_char c) {
if(((c >= 'A')&& (c <= 'Z'))
|| ((c >= 'a')&& (c <= 'z'))
|| ((c >= '0')&& (c <= '9'))
|| (c == '_')
|| (c == '-')
|| (c == '.'))
return(true);
else
return(false);
}
/* ********************************************************** */
bool ndpi_domain_classify_contains(ndpi_domain_classify *s,
u_int8_t *class_id /* out */,
const char *domain) {
u_int32_t i, len;
const char *dot, *elem;
if(!domain || !s) return(false);
if((len = strlen(domain)) == 0) return(false);
if((dot = strrchr(domain, '.')) == NULL) return(false);
if((!strcmp(dot, ".arpa")) || (!strcmp(dot, ".local"))) return(false);
/* This is a number or a numeric IP or similar */
if(isdigit(domain[len-1]) && isdigit(domain[0])) {
#ifdef DEBUG_CONTAINS
printf("[contains] %s INVALID\n", domain);
#endif
return(false);
}
if(!is_valid_domain_char(domain[0])) {
#ifdef DEBUG_CONTAINS
printf("[contains] %s INVALID\n", domain);
#endif
return(false);
}
elem = domain;
while(elem != NULL) {
u_int64_t hash = ndpi_quick_hash64(elem, strlen(elem));
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].class_id != 0) {
if(ndpi_bitmap64_isset(s->classes[i].domains, hash)) {
#ifdef DEBUG_CONTAINS
printf("[contains] %s = %d\n", domain, s->classes[i].class_id);
#endif
*class_id = s->classes[i].class_id;
return(true);
}
} else
break;
}
elem = strchr(elem, '.');
if((elem == NULL) || (elem == dot))
break;
else
elem = &elem[1];
} /* while */
#ifdef DEBUG_CONTAINS
printf("[contains] %s NOT FOUND\n", domain);
#endif
return(false);
}
|