ucspi-ssl  0.99e
TLS encryption for IPv6 communication
dns_ip.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 
6 int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len)
7 {
8  unsigned int pos;
9  char header[12];
10  uint16 numanswers;
11  uint16 datalen;
12 
13  if (!stralloc_copys(out,"")) return -1;
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_A))
25  if (byte_equal(header + 2,2,DNS_C_IN))
26  if (datalen == 4) {
27  if (!dns_packet_copy(buf,len,pos,header,4)) return -1;
28  if (!stralloc_catb(out,header,4)) return -1;
29  }
30  pos += datalen;
31  }
32 
33  dns_sortip(out->s,out->len);
34  return 0;
35 }
36 
37 static char *q = 0;
38 
39 int dns_ip4(stralloc *out,const stralloc *fqdn)
40 {
41  unsigned int i;
42  char code;
43  char ch;
44 
45  if (!stralloc_copys(out,"")) return -1;
46  code = 0;
47  for (i = 0;i <= fqdn->len;++i) {
48  if (i < fqdn->len)
49  ch = fqdn->s[i];
50  else
51  ch = '.';
52 
53  if ((ch == '[') || (ch == ']')) continue;
54  if (ch == '.') {
55  if (!stralloc_append(out,&code)) return -1;
56  code = 0;
57  continue;
58  }
59  if ((ch >= '0') && (ch <= '9')) {
60  code *= 10;
61  code += ch - '0';
62  continue;
63  }
64 
65  if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
66  if (dns_resolve(q,DNS_T_A) == -1) return -1;
69  dns_domain_free(&q);
70  return 0;
71  }
72 
73  out->len &= ~3;
74  return 0;
75 }
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:79
char buf[SSL_NAME_LEN]
Definition: sslhandle.c:125
void dns_sortip(char *, unsigned int)
Definition: dns_sortip.c:7
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 *)
unsigned int pos
Definition: dns.h:36
#define DNS_T_A
Definition: dns.h:11
unsigned int packetlen
Definition: dns.h:30
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:7
int dns_ip4(stralloc *out, const stralloc *fqdn)
Definition: dns_ip.c:39
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:18
int dns_ip4_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_ip.c:6
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int)
Definition: dns_packet.c:8
int stralloc_catb(stralloc *, const char *, unsigned int)
Definition: stralloc_catb.c:6
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 *)