From e5c325445092dc80d3c8bba2d8ab07beee6e4d4d Mon Sep 17 00:00:00 2001 From: Max Lv Date: Fri, 9 Sep 2016 10:11:44 +0800 Subject: [PATCH] Fix #815 --- src/acl.c | 24 +++++++++++++----------- src/http.c | 3 +++ src/rule.c | 20 ++++++++++---------- src/rule.h | 16 +++++++--------- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/acl.c b/src/acl.c index 38a234ac..e891b59c 100644 --- a/src/acl.c +++ b/src/acl.c @@ -33,8 +33,8 @@ static struct ip_set white_list_ipv6; static struct ip_set black_list_ipv4; static struct ip_set black_list_ipv6; -rule_head_t black_list_rules; -rule_head_t white_list_rules; +static struct cork_dllist black_list_rules; +static struct cork_dllist white_list_rules; static int acl_mode = BLACK_LIST; @@ -89,12 +89,12 @@ int init_acl(const char *path) ipset_init(&black_list_ipv4); ipset_init(&black_list_ipv6); - STAILQ_INIT(&black_list_rules); - STAILQ_INIT(&white_list_rules); + cork_dllist_init(&black_list_rules); + cork_dllist_init(&white_list_rules); - struct ip_set *list_ipv4 = &black_list_ipv4; - struct ip_set *list_ipv6 = &black_list_ipv6; - rule_head_t *rules = &black_list_rules; + struct ip_set *list_ipv4 = &black_list_ipv4; + struct ip_set *list_ipv6 = &black_list_ipv6; + struct cork_dllist *rules = &black_list_rules; FILE *f = fopen(path, "r"); if (f == NULL) { @@ -177,11 +177,13 @@ int init_acl(const char *path) return 0; } -void free_rules(rule_head_t *rules) +void free_rules(struct cork_dllist *rules) { - rule_t *iter; - while ((iter = STAILQ_FIRST(rules)) != NULL) - remove_rule(rules, iter); + struct cork_dllist_item *iter; + while ((iter = cork_dllist_head(rules)) != NULL) { + rule_t *rule = cork_container_of(iter, rule_t, entries); + remove_rule(rule); + } } void free_acl(void) diff --git a/src/http.c b/src/http.c index 366d8f27..77c3beb1 100644 --- a/src/http.c +++ b/src/http.c @@ -63,6 +63,9 @@ parse_http_header(const char* data, size_t data_len, char **hostname) { if (hostname == NULL) return -3; + if (data_len == 0) + return -1; + result = get_header("Host:", data, data_len, hostname); if (result < 0) return result; diff --git a/src/rule.c b/src/rule.c index 88870c4b..e07481b6 100644 --- a/src/rule.c +++ b/src/rule.c @@ -26,7 +26,6 @@ */ #include #include -#include #include "rule.h" #include "utils.h" @@ -63,8 +62,8 @@ accept_rule_arg(rule_t *rule, const char *arg) { } void -add_rule(rule_head_t *rules, rule_t *rule) { - STAILQ_INSERT_TAIL(rules, rule, entries); +add_rule(struct cork_dllist *rules, rule_t *rule) { + cork_dllist_add(rules, &rule->entries); } int @@ -86,26 +85,27 @@ init_rule(rule_t *rule) { } rule_t * -lookup_rule(const rule_head_t *head, const char *name, size_t name_len) { - rule_t *iter; +lookup_rule(const struct cork_dllist *rules, const char *name, size_t name_len) { + struct cork_dllist_item *curr, *next; if (name == NULL) { name = ""; name_len = 0; } - STAILQ_FOREACH(iter, head, entries) { - if (pcre_exec(iter->pattern_re, NULL, + cork_dllist_foreach_void(rules, curr, next) { + rule_t *rule = cork_container_of(curr, rule_t, entries); + if (pcre_exec(rule->pattern_re, NULL, name, name_len, 0, 0, NULL, 0) >= 0) - return iter; + return rule; } return NULL; } void -remove_rule(rule_head_t *head, rule_t *rule) { - STAILQ_REMOVE(head, rule, rule, entries); +remove_rule(rule_t *rule) { + cork_dllist_remove(&rule->entries); free_rule(rule); } diff --git a/src/rule.h b/src/rule.h index df233f5e..72a6bae7 100644 --- a/src/rule.h +++ b/src/rule.h @@ -31,7 +31,8 @@ #include "config.h" #endif -#include +#include +#include #ifdef HAVE_PCRE_H #include @@ -39,22 +40,19 @@ #include #endif -STAILQ_HEAD(rule_head, rule); - -typedef struct rule_head rule_head_t; - typedef struct rule { char *pattern; /* Runtime fields */ pcre *pattern_re; - STAILQ_ENTRY(rule) entries; + + struct cork_dllist_item entries; } rule_t; -void add_rule(rule_head_t *, rule_t *); +void add_rule(struct cork_dllist *, rule_t *); int init_rule(rule_t *); -rule_t *lookup_rule(const rule_head_t *, const char *, size_t); -void remove_rule(rule_head_t *, rule_t *); +rule_t *lookup_rule(const struct cork_dllist *, const char *, size_t); +void remove_rule(rule_t *); rule_t *new_rule(); int accept_rule_arg(rule_t *, const char *);