summaryrefslogtreecommitdiff
path: root/attic/historic/anode/libanode/impl/dns_txt.c
blob: b5cf1318425290111cbeb9cd17755d9bb21c39f4 (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
/* libanode: the Anode C reference implementation
 * Copyright (C) 2009-2010 Adam Ierymenko <adam.ierymenko@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <netdb.h>
#include "dns_txt.h"

#ifndef C_IN
#define C_IN ns_c_in
#endif
#ifndef T_TXT
#define T_TXT ns_t_txt
#endif

static volatile int Anode_resolver_initialized = 0;

int Anode_sync_resolve_txt(const char *host,char *txt,unsigned int txt_len)
{
  unsigned char answer[16384],*pptr,*end;
  char name[16384];
  int len,explen,i;

  if (!Anode_resolver_initialized) {
    Anode_resolver_initialized = 1;
    res_init();
  }

  /* Do not taunt happy fun ball. */

  len = res_search(host,C_IN,T_TXT,answer,sizeof(answer));
  if (len > 12) {
    pptr = answer + 12;
    end = answer + len;

    explen = dn_expand(answer,end,pptr,name,sizeof(name));
    if (explen > 0) {
      pptr += explen;

      if ((pptr + 2) >= end) return 2;
      if (ntohs(*((uint16_t *)pptr)) == T_TXT) {
        pptr += 4;
        if (pptr >= end) return 2;

        explen = dn_expand(answer,end,pptr,name,sizeof(name));
        if (explen > 0) {
          pptr += explen;

          if ((pptr + 2) >= end) return 2;
          if (ntohs(*((uint16_t *)pptr)) == T_TXT) {
            pptr += 10;
            if (pptr >= end) return 2;

            len = *(pptr++);
            if (len <= 0) return 2;
            if ((pptr + len) > end) return 2;

            if (txt_len < (len + 1))
              return 4;
            else {
              for(i=0;i<len;++i)
                txt[i] = pptr[i];
              txt[len] = (char)0;
              return 0;
            }
          }
        }
      }
    }
  }

  return 1;
}