Permalink
Browse files

generalize blacklist internals

  • Loading branch information...
1 parent b3566b9 commit e7ddb2ae9ca7e467db0197f00927a071f94143a4 @thejh committed May 29, 2012
Showing with 32 additions and 17 deletions.
  1. +30 −15 src/blacklist.c
  2. +1 −1 src/blacklist.h
  3. +1 −1 src/main.c
View
@@ -6,18 +6,40 @@
#include <stdlib.h>
-char **blacklist = NULL;
+struct bl_entry {
+ char *domain;
+ char forbidden;
+}
+
+
+struct bl_entry *blacklist = NULL;
int blacklist_size = 0;
-void reload_domain_blacklist() {
+
+static bl_entry *get_bl_entry(char *host) {
+ char predotted_bl_entry[1030];
+ predotted_bl_entry[0] = '.';
+
+ for (int i=0; i<blacklist_size; i++) {
+ if (strcmp(host, blacklist[i]->domain) == 0) return blacklist[i];
+
+ strcpy(predotted_bl_entry+1, blacklist[i]->domain);
+ if (memcmp(host + (strlen(host) - strlen(predotted_bl_entry)), predotted_bl_entry, strlen(predotted_bl_entry)+1) == 0)
+ return blacklist[i];
+ }
+
+ return NULL;
+}
+
+void reload_blacklist() {
FILE *f = fopen("../conf/domains.blacklist", "r");
char line[1024];
blacklist_size = 0;
while (fgets(line, 1024, f) != NULL) {
if (strlen(line) < 3 || line[0] == '#') continue;
blacklist_size++;
}
- blacklist = malloc(sizeof(char *) * blacklist_size);
+ blacklist = malloc(sizeof(struct bl_entry) * blacklist_size);
rewind(f);
int i = 0;
while (fgets(line, 1024, f) != NULL) {
@@ -26,22 +48,15 @@ void reload_domain_blacklist() {
char *n_i = strchr(line, '\n');
if (r_i != NULL) *r_i = '\0';
if (n_i != NULL) *n_i = '\0';
- blacklist[i++] = strdup(line);
+ blacklist[i]->forbidden = 1;
+ blacklist[i++]->domain = strdup(line);
}
assert(i == blacklist_size);
fclose(f);
}
int bl_check(char *host) {
- char predotted_bl_entry[1030];
- predotted_bl_entry[0] = '.';
-
- for (int i=0; i<blacklist_size; i++) {
- if (strcmp(host, blacklist[i]) == 0) return 1;
-
- strcpy(predotted_bl_entry+1, blacklist[i]);
- if (memcmp(host + (strlen(host) - strlen(predotted_bl_entry)), predotted_bl_entry, strlen(predotted_bl_entry)+1) == 0) return 1;
- }
-
- return 0;
+ struct bl_entry *e = get_bl_entry(host);
+ if (e == NULL) return 0;
+ return e->forbidden;
}
View
@@ -1,2 +1,2 @@
-void reload_domain_blacklist();
+void reload_blacklist();
int bl_check(char *host);
View
@@ -667,7 +667,7 @@ int main(int argc, char **argv) {
};
sigaction(SIGPIPE, &action_ignore, NULL);
idle_agents_by_host = ht_create();
- reload_domain_blacklist();
+ reload_blacklist();
// start listening
int listen_fd = socket(AF_INET6, SOCK_STREAM, 0);

0 comments on commit e7ddb2a

Please sign in to comment.