ucspi-ssl  0.99e
TLS encryption for IPv6 communication
dns_ip6.c
Go to the documentation of this file.
1 #include "stralloc.h"
2 #include "uint16.h"
3 #include "byte.h"
4 #include "dns.h"
5 #include "ip4.h"
6 #include "ip6.h"
7 
8 static int dns_ip6_packet_add(stralloc *out,const char *buf,unsigned int len)
9 {
10  unsigned int pos;
11  char header[16];
12  uint16 numanswers;
13  uint16 datalen;
14 
15  pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return -1;
16  uint16_unpack_big(header + 6,&numanswers);
17  pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1;
18  pos += 4;
19 
20  while (numanswers--) {
21  pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1;
22  pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return -1;
23  uint16_unpack_big(header + 8,&datalen);
24  if (byte_equal(header,2,DNS_T_AAAA)) {
25  if (byte_equal(header + 2,2,DNS_C_IN))
26  if (datalen == 16) {
27  if (!dns_packet_copy(buf,len,pos,header,16)) return -1;
28  if (!stralloc_catb(out,header,16)) return -1;
29  }
30  } else if (byte_equal(header,2,DNS_T_A))
31  if (byte_equal(header + 2,2,DNS_C_IN))
32  if (datalen == 4) {
33  byte_copy(header,12,V4mappedprefix);
34  if (!dns_packet_copy(buf,len,pos,header+12,4)) return -1;
35  if (!stralloc_catb(out,header,16)) return -1;
36  }
37  pos += datalen;
38  }
39 
40  dns_sortip6(out->s,out->len);
41  return 0;
42 }
43 
44 int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len) {
45  if (!stralloc_copys(out,"")) return -1;
46  return dns_ip6_packet_add(out,buf,len);
47 }
48 
49 static char *q = 0;
50 
51 int dns_ip6(stralloc *out,stralloc *fqdn)
52 {
53  unsigned int i;
54  char code;
55  char ch;
56  char ip[16];
57 
58  if (!stralloc_copys(out,"")) return -1;
59  if (!stralloc_readyplus(fqdn,1)) return -1;
60  fqdn->s[fqdn->len]=0;
61  if ((i=ip6_scan(fqdn->s,ip))) {
62  if (fqdn->s[i]) return -1;
63  stralloc_copyb(out,ip,16);
64  return 0;
65  }
66  code = 0;
67  for (i = 0;i <= fqdn->len;++i) {
68  if (i < fqdn->len)
69  ch = fqdn->s[i];
70  else
71  ch = '.';
72 
73  if ((ch == '[') || (ch == ']')) continue;
74  if (ch == '.') {
75  if (!stralloc_append(out,&code)) return -1;
76  code = 0;
77  continue;
78  }
79  if ((ch >= '0') && (ch <= '9')) {
80  code *= 10;
81  code += ch - '0';
82  continue;
83  }
84 
85  if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
86  if (!stralloc_copys(out,"")) return -1;
87  if (dns_resolve(q,DNS_T_AAAA) != -1)
88  if (dns_ip6_packet_add(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) != -1) {
90  dns_domain_free(&q);
91  }
92  if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
93  if (dns_resolve(q,DNS_T_A) != -1)
94  if (dns_ip6_packet_add(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) != -1) {
96  dns_domain_free(&q);
97  }
98  return out->a>0?0:-1;
99  }
100 
101  out->len &= ~3;
102  return 0;
103 }
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:79
void byte_copy(void *, unsigned int, const void *)
char buf[SSL_NAME_LEN]
Definition: sslhandle.c:125
void dns_domain_free(char **)
Definition: dns_domain.c:18
#define DNS_C_IN
Definition: dns.h:8
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_dfd.c:6
int stralloc_append(stralloc *, const char *)
int stralloc_readyplus(stralloc *, unsigned int)
unsigned int pos
Definition: dns.h:36
#define DNS_T_A
Definition: dns.h:11
int dns_ip6(stralloc *out, stralloc *fqdn)
Definition: dns_ip6.c:51
unsigned int packetlen
Definition: dns.h:30
void dns_sortip6(char *, unsigned int)
Definition: dns_sortip6.c:7
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:7
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:18
int stralloc_copyb(stralloc *, const char *, unsigned int)
Definition: stralloc_opyb.c:6
unsigned int ip6_scan(const char *src, char *ip)
int dns_ip6_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_ip6.c:44
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int)
Definition: dns_packet.c:8
#define DNS_T_AAAA
Definition: dns.h:22
int stralloc_catb(stralloc *, const char *, unsigned int)
Definition: stralloc_catb.c:6
const unsigned char V4mappedprefix[12]
char * packet
Definition: dns.h:29
unsigned short uint16
Definition: uint16.h:4
int stralloc_copys(stralloc *, const char *)
Definition: stralloc_opys.c:7
#define byte_equal(s, n, t)
Definition: byte.h:13
void uint16_unpack_big(const char *, uint16 *)
int dns_resolve(const char *, const char *)