summaryrefslogtreecommitdiffstats
path: root/qtype.cc
blob: a0d5732cd10ea4363c198d55974ea06e546d31e2 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "dns.hh"
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <sstream>
#include "qtype.hh"
#include "misc.hh"

static_assert(sizeof(QType) == 2, "QType is not 2 bytes in size, something is wrong!");

const map<const string, uint16_t> QType::names = {
  {"A", 1},
  {"NS", 2},
  {"CNAME", 5},
  {"SOA", 6},
  {"MB", 7},
  {"MG", 8},
  {"MR", 9},
  {"PTR", 12},
  {"HINFO", 13},
  {"MINFO", 14},
  {"MX", 15},
  {"TXT", 16},
  {"RP", 17},
  {"AFSDB", 18},
  {"SIG", 24},
  {"KEY", 25},
  {"AAAA", 28},
  {"LOC", 29},
  {"SRV", 33},
  {"NAPTR", 35},
  {"KX", 36},
  {"CERT", 37},
  {"A6", 38},
  {"DNAME", 39},
  {"OPT", 41},
  {"APL", 42},
  {"DS", 43},
  {"SSHFP", 44},
  {"IPSECKEY", 45},
  {"RRSIG", 46},
  {"NSEC", 47},
  {"DNSKEY", 48},
  {"DHCID", 49},
  {"NSEC3", 50},
  {"NSEC3PARAM", 51},
  {"TLSA", 52},
  {"SMIMEA", 53},
  {"RKEY", 57},
  {"CDS", 59},
  {"CDNSKEY", 60},
  {"OPENPGPKEY", 61},
  {"CSYNC", 62},
  {"ZONEMD", 63},
  {"SVCB", 64},
  {"HTTPS", 65},
  {"SPF", 99},
  {"NID", 104},
  {"L32", 105},
  {"L64", 106},
  {"LP", 107},
  {"EUI48", 108},
  {"EUI64", 109},
  {"TKEY", 249},
  {"TSIG", 250},
  {"IXFR", 251},
  {"AXFR", 252},
  {"MAILB", 253},
  {"MAILA", 254},
  {"ANY", 255},
  {"URI", 256},
  {"CAA", 257},
  {"DLV", 32769},
  {"ADDR", 65400},
#if !defined(RECURSOR)
  {"ALIAS", 65401},
  {"LUA", 65402},
#endif
};

static map<uint16_t, const string> swapElements(const map<const string, uint16_t>& names) {
  map<uint16_t, const string> ret;

  for (const auto& n : names) {
    ret.emplace(n.second, n.first);
  }
  return ret;
}

const map<uint16_t, const string> QType::numbers = swapElements(names);


bool QType::isSupportedType() const
{
  return numbers.count(code) == 1;
}

bool QType::isMetadataType() const
{
  // rfc6895 section 3.1, note ANY is 255 and falls outside the range
  if (code == QType::OPT || (code >= rfc6895MetaLowerBound && code <= rfc6895MetaUpperBound)) {
    return true;
  }
  return false;
}

const string QType::toString() const
{
  const auto& name = numbers.find(code);
  if (name != numbers.cend()) {
    return name->second;
  }
  return "TYPE" + std::to_string(code);
}

uint16_t QType::chartocode(const char *p)
{
  string P = toUpper(p);

  const auto& num = names.find(P);
  if (num != names.cend()) {
    return num->second;
  }
  if (*p == '#') {
    return static_cast<uint16_t>(atoi(p + 1));
  }

  if (boost::starts_with(P, "TYPE")) {
    return static_cast<uint16_t>(atoi(p + 4));
  }

  return 0;
}

QType &QType::operator=(const char *p)
{
  code = chartocode(p);
  return *this;
}

QType &QType::operator=(const string &s)
{
  code = chartocode(s.c_str());
  return *this;
}

const std::string QClass::toString() const
{
  switch (qclass) {
  case IN:
    return "IN";
  case CHAOS:
    return "CHAOS";
  case NONE:
    return "NONE";
  case ANY:
    return "ANY";
  default :
    return "CLASS" + std::to_string(qclass);
  }
}