Browse Source

Fix #815

pull/820/head
Max Lv 8 years ago
parent
commit
e5c3254450
4 changed files with 33 additions and 30 deletions
  1. 24
      src/acl.c
  2. 3
      src/http.c
  3. 20
      src/rule.c
  4. 16
      src/rule.h

24
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)

3
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;

20
src/rule.c

@ -26,7 +26,6 @@
*/
#include <stdio.h>
#include <string.h>
#include <sys/queue.h>
#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);
}

16
src/rule.h

@ -31,7 +31,8 @@
#include "config.h"
#endif
#include <sys/queue.h>
#include <libcork/core.h>
#include <libcork/ds.h>
#ifdef HAVE_PCRE_H
#include <pcre.h>
@ -39,22 +40,19 @@
#include <pcre/pcre.h>
#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 *);

Loading…
Cancel
Save