summaryrefslogtreecommitdiff
path: root/src/rules.c
blob: 279ffae9ba8f1f0867ce7a3863e42f77f959e201 (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
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
#include "alloc.h"
#include "stralloc.h"
#include "open.h"
#include "cdbread.h"
#include "byte.h"
#include "fmt.h"
#include "getln.h"
#include "ip.h"
#include "str.h"
#include "ip_bit.h"
#include "rules.h"

stralloc rules_name = {0};
stralloc ipstring = {0};

static struct cdb c;

static int dorule(void (*callback)(char *,unsigned int)) {
  char *data;
  unsigned int datalen;

  switch (cdb_find(&c,rules_name.s,rules_name.len)) {
    case -1: return -1;
    case  0: return 0;
  }

  datalen = cdb_datalen(&c);
  data = alloc(datalen);
  if (!data) return -1;
  if (cdb_read(&c,data,datalen,cdb_datapos(&c)) == -1) {
    alloc_free(data);
    return -1;
  }

  callback(data, datalen);
  alloc_free(data);
  return 1;
}

static int doit(void (*callback)(char *,unsigned int),char *ip,char *host,char *info) {
  int p;
  int r;
  int ipv6 = str_len(ip) - byte_chr(ip,str_len(ip),':');

  if (info) {							/* 1. info@ip */
    if (!stralloc_copys(&rules_name,info)) return -1;
    if (!stralloc_cats(&rules_name,"@")) return -1;
    if (ipv6) {
      if (!ip6_fmt_str(&ipstring,ip))
        if (!stralloc_catb(&rules_name,ipstring.s,ipstring.len)) return -1;
    }
    else
      if (!stralloc_cats(&rules_name,ip)) return -1;
    r = dorule(callback);
    if (r) return r;

    if (host) {							/* 2. info@=host */
      if (!stralloc_copys(&rules_name,info)) return -1;
      if (!stralloc_cats(&rules_name,"@=")) return -1;
      if (!stralloc_cats(&rules_name,host)) return -1;
      r = dorule(callback);
      if (r) return r;
    }
  }

  if (ipv6) {							/* 3. IPv6/IPv4 */
    if (!ip6_fmt_str(&ipstring,ip)) {		
        if (!stralloc_copyb(&rules_name,ipstring.s,ipstring.len)) return -1;
        r = dorule(callback);
        if (r) return r;
    }
  } else {
    if (!stralloc_copys(&rules_name,ip)) return -1;
    r = dorule(callback);
    if (r) return r;
  }

  if (host) {							/* 4. =host */
    if (!stralloc_copys(&rules_name,"=")) return -1;
    if (!stralloc_cats(&rules_name,host)) return -1;
    r = dorule(callback);
    if (r) return r;
  }

  if (!ipv6) {							/* 5. IPv4 class-based */
    if (!stralloc_copys(&rules_name,ip)) return -1;
    while (rules_name.len > 0) {
      if (ip[rules_name.len - 1] == '.') {
        r = dorule(callback);
        if (r) return r;
      }
      --rules_name.len;
    } 
  }

  if (ipv6) {							/* 6. IPv6/IPv4 CIDR */
    if (!ip6_bitstring(&ipstring,ip,128)) {
      for (p = 129; p > 1; p--) {
        if (!stralloc_copys(&rules_name,"^")) return -1;
        if (!stralloc_catb(&rules_name,ipstring.s,p)) return -1;
        r = dorule(callback);
        if (r) return r;
      }
    }
  } else {
    if (!ip4_bitstring(&ipstring,ip,32)) {	
      for (p = 33; p > 1; p--) {
        if (!stralloc_copys(&rules_name,"_")) return -1;
        if (!stralloc_catb(&rules_name,ipstring.s,p)) return -1;
        r = dorule(callback);
        if (r) return r;
      }
    }
  } 

  if (host) {							/* 7. =host. */
    while (*host) {
      if (*host == '.') {
        if (!stralloc_copys(&rules_name,"=")) return -1;
        if (!stralloc_cats(&rules_name,host)) return -1;
        r = dorule(callback);
        if (r) return r;
      }
      ++host;
    }
    if (!stralloc_copys(&rules_name,"=")) return -1;		/* 8. = rule */
    r = dorule(callback);
    if (r) return r;
  } 

  rules_name.len = 0;
  return dorule(callback);
}

int rules(void (*callback)(char *,unsigned int),int fd,char *ip,char *host,char *info) {
  int r;
  cdb_init(&c,fd);
  r = doit(callback,ip,host,info);
  cdb_free(&c);
  return r;
}