ucspi-ssl  0.99e
TLS encryption for IPv6 communication
dns_ipq6.c
Go to the documentation of this file.
1 #include "stralloc.h"
2 #include "case.h"
3 #include "byte.h"
4 #include "str.h"
5 #include "dns.h"
6 
7 static int doit(stralloc *work,const char *rule)
8 {
9  char ch;
10  unsigned int colon;
11  unsigned int prefixlen;
12 
13  ch = *rule++;
14  if ((ch != '?') && (ch != '=') && (ch != '*') && (ch != '-')) return 1;
15  colon = str_chr(rule,':');
16  if (!rule[colon]) return 1;
17 
18  if (work->len < colon) return 1;
19  prefixlen = work->len - colon;
20  if ((ch == '=') && prefixlen) return 1;
21  if (case_diffb(rule,colon,work->s + prefixlen)) return 1;
22  if (ch == '?') {
23  if (byte_chr(work->s,prefixlen,'.') < prefixlen) return 1;
24  if (byte_chr(work->s,prefixlen,':') < prefixlen) return 1;
25  if (byte_chr(work->s,prefixlen,'[') < prefixlen) return 1;
26  if (byte_chr(work->s,prefixlen,']') < prefixlen) return 1;
27  }
28 
29  work->len = prefixlen;
30  if (ch == '-') work->len = 0;
31  return stralloc_cats(work,rule + colon + 1);
32 }
33 
34 int dns_ip6_qualify_rules(stralloc *out,stralloc *fqdn,const stralloc *in,const stralloc *rules)
35 {
36  unsigned int i;
37  unsigned int j;
38  unsigned int plus;
39  unsigned int fqdnlen;
40 
41  if (!stralloc_copy(fqdn,in)) return -1;
42 
43  for (j = i = 0;j < rules->len;++j)
44  if (!rules->s[j]) {
45  if (!doit(fqdn,rules->s + i)) return -1;
46  i = j + 1;
47  }
48 
49  fqdnlen = fqdn->len;
50  plus = byte_chr(fqdn->s,fqdnlen,'+');
51  if (plus >= fqdnlen)
52  return dns_ip6(out,fqdn);
53 
54  i = plus + 1;
55  for (;;) {
56  j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
57  byte_copy(fqdn->s + plus,j,fqdn->s + i);
58  fqdn->len = plus + j;
59  if (dns_ip6(out,fqdn) == -1) return -1;
60  if (out->len) return 0;
61  i += j;
62  if (i >= fqdnlen) return 0;
63  ++i;
64  }
65 }
66 
67 int dns_ip6_qualify(stralloc *out,stralloc *fqdn,const stralloc *in)
68 {
69  static stralloc rules;
70  if (dns_resolvconfrewrite(&rules) == -1) return -1;
71  return dns_ip6_qualify_rules(out,fqdn,in,&rules);
72 }
int stralloc_copy(stralloc *, const stralloc *)
Definition: stralloc_copy.c:4
void byte_copy(void *, unsigned int, const void *)
int stralloc_cats(stralloc *, const char *)
Definition: stralloc_cats.c:7
int case_diffb(const char *, unsigned int, const char *)
int dns_ip6(stralloc *, stralloc *)
Definition: dns_ip6.c:51
int dns_ip6_qualify_rules(stralloc *out, stralloc *fqdn, const stralloc *in, const stralloc *rules)
Definition: dns_ipq6.c:34
int rules(void(*callback)(char *, unsigned int), int fd, char *ip, char *host, char *info)
Definition: rules.c:137
unsigned int str_chr(const char *, int)
unsigned int byte_chr(const char *, unsigned int n, int)
int dns_ip6_qualify(stralloc *out, stralloc *fqdn, const stralloc *in)
Definition: dns_ipq6.c:67
int dns_resolvconfrewrite(stralloc *)
Definition: dns_rcrw.c:112