ucspi-ssl  0.12.7
ucspi-ssl
rules.c
Go to the documentation of this file.
1 #include "alloc.h"
2 #include "stralloc.h"
3 #include "open.h"
4 #include "cdbread.h"
5 #include "byte.h"
6 #include "fmt.h"
7 #include "getln.h"
8 #include "ip.h"
9 #include "str.h"
10 #include "ip_bit.h"
11 #include "rules.h"
12 
13 stralloc rules_name = {0};
14 stralloc ipstring = {0};
15 
16 static struct cdb c;
17 
18 static int dorule(void (*callback)(char *,unsigned int)) {
19  char *data;
20  unsigned int datalen;
21 
22  switch (cdb_find(&c,rules_name.s,rules_name.len)) {
23  case -1: return -1;
24  case 0: return 0;
25  }
26 
27  datalen = cdb_datalen(&c);
28  data = alloc(datalen);
29  if (!data) return -1;
30  if (cdb_read(&c,data,datalen,cdb_datapos(&c)) == -1) {
31  alloc_free(data);
32  return -1;
33  }
34 
35  callback(data, datalen);
36  alloc_free(data);
37  return 1;
38 }
39 
40 static int doit(void (*callback)(char *, unsigned int), char *ip, char *host, char *info) {
41  int p;
42  int r;
43  int ipv6 = str_len(ip) - byte_chr(ip,str_len(ip),':');
44 
45  if (info) { /* 1. info@ip */
46  if (!stralloc_copys(&rules_name,info)) return -1;
47  if (!stralloc_cats(&rules_name,"@")) return -1;
48  if (ipv6) {
49  if (!ip6_fmt_str(&ipstring,ip))
50  if (!stralloc_catb(&rules_name,ipstring.s,ipstring.len)) return -1;
51  }
52  else
53  if (!stralloc_cats(&rules_name,ip)) return -1;
54  r = dorule(callback);
55  if (r) return r;
56 
57  if (host) { /* 2. info@=host */
58  if (!stralloc_copys(&rules_name,info)) return -1;
59  if (!stralloc_cats(&rules_name,"@=")) return -1;
60  if (!stralloc_cats(&rules_name,host)) return -1;
61  r = dorule(callback);
62  if (r) return r;
63  }
64  }
65 
66  if (ipv6) { /* 3. IPv6/IPv4 */
67  if (!ip6_fmt_str(&ipstring,ip)) {
68  if (!stralloc_copyb(&rules_name,ipstring.s,ipstring.len)) return -1;
69  r = dorule(callback);
70  if (r) return r;
71  }
72  } else {
73  if (!stralloc_copys(&rules_name,ip)) return -1;
74  r = dorule(callback);
75  if (r) return r;
76  }
77 
78  if (host) { /* 4. =host */
79  if (!stralloc_copys(&rules_name,"=")) return -1;
80  if (!stralloc_cats(&rules_name,host)) return -1;
81  r = dorule(callback);
82  if (r) return r;
83  }
84 
85  if (!ipv6) { /* 5. IPv4 class-based */
86  if (!stralloc_copys(&rules_name,ip)) return -1;
87  while (rules_name.len > 0) {
88  if (ip[rules_name.len - 1] == '.') {
89  r = dorule(callback);
90  if (r) return r;
91  }
92  --rules_name.len;
93  }
94  }
95 
96  if (ipv6) { /* 6. IPv6/IPv4 CIDR */
97  if (!ip6_bitstring(&ipstring,ip,128)) {
98  for (p = 129; p > 1; p--) {
99  if (!stralloc_copys(&rules_name,"^")) return -1;
100  if (!stralloc_catb(&rules_name,ipstring.s,p)) return -1;
101  r = dorule(callback);
102  if (r) return r;
103  }
104  }
105  } else {
106  if (!ip4_bitstring(&ipstring,ip,32)) {
107  for (p = 33; p > 1; p--) {
108  if (!stralloc_copys(&rules_name,"_")) return -1;
109  if (!stralloc_catb(&rules_name,ipstring.s,p)) return -1;
110  r = dorule(callback);
111  if (r) return r;
112  }
113  }
114  }
115 
116  if (host) { /* 7. =host. */
117  while (*host) {
118  if (*host == '.') {
119  if (!stralloc_copys(&rules_name,"=")) return -1;
120  if (!stralloc_cats(&rules_name,host)) return -1;
121  r = dorule(callback);
122  if (r) return r;
123  }
124  ++host;
125  }
126  if (!stralloc_copys(&rules_name,"=")) return -1; /* 8. = rule */
127  r = dorule(callback);
128  if (r) return r;
129  }
130 
131  rules_name.len = 0;
132  return dorule(callback);
133 }
134 
135 int rules(void (*callback)(char *,unsigned int), int fd, char *ip, char *host, char *info) {
136  int r;
137  cdb_init(&c,fd);
138  r = doit(callback,ip,host,info);
139  cdb_free(&c);
140  return r;
141 }
int ip4_bitstring(stralloc *ip4string, char *ip, unsigned int prefix)
Definition: ip4_bit.c:26
int ip6_bitstring(stralloc *ip6string, char *ip6addr, unsigned int prefix)
Definition: ip6_bit.c:48
unsigned int ip6_fmt_str(stralloc *dest, char *src)
Definition: ip6_bit.c:160
int rules(void(*callback)(char *, unsigned int), int fd, char *ip, char *host, char *info)
Definition: rules.c:135
stralloc ipstring
Definition: rules.c:14
stralloc rules_name
Definition: rules.c:13