File: dnsrecords.cc

package info (click to toggle)
pdns-recursor 3.2-4+deb6u1
  • links: PTS
  • area: main
  • in suites: squeeze-lts
  • size: 1,220 kB
  • ctags: 2,721
  • sloc: cpp: 12,564; ansic: 2,587; sh: 510; makefile: 133
file content (434 lines) | stat: -rw-r--r-- 11,586 bytes parent folder | download | duplicates (2)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
    PowerDNS Versatile Database Driven Nameserver
    Copyright (C) 2005 - 2009  PowerDNS.COM BV

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as 
    published by the Free Software Foundation

    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 St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "utility.hh"
#include "dnsrecords.hh"

boilerplate_conv(A, ns_t_a, conv.xfrIP(d_ip));

ARecordContent::ARecordContent(uint32_t ip) : DNSRecordContent(ns_t_a)
{
  d_ip = ip;
}

uint32_t ARecordContent::getIP() const
{
  return d_ip;
}

void ARecordContent::doRecordCheck(const DNSRecord& dr)
{  
  if(dr.d_clen!=4)
    throw MOADNSException("Wrong size for A record ("+lexical_cast<string>(dr.d_clen)+")");
}

class AAAARecordContent : public DNSRecordContent
{
public:
  AAAARecordContent() : DNSRecordContent(ns_t_aaaa)
  {}

  static void report(void)
  {
    regist(1, ns_t_aaaa, &make, &make, "AAAA");
  }

  static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr) 
  {
    if(dr.d_clen!=16)
      throw MOADNSException("Wrong size for AAAA record");

    AAAARecordContent* ret=new AAAARecordContent();
    pr.copyRecord((unsigned char*) &ret->d_ip6, 16);
    return ret;
  }

  static DNSRecordContent* make(const string& zone) 
  {
    AAAARecordContent *ar=new AAAARecordContent();
    if(Utility::inet_pton( AF_INET6, zone.c_str(), static_cast< void * >( ar->d_ip6 )) < 0)
      throw MOADNSException("Asked to encode '"+zone+"' as an IPv6 address, but does not parse");
    return ar;
  }

  void toPacket(DNSPacketWriter& pw)
  {
    string blob(d_ip6, d_ip6+16);
    pw.xfrBlob(blob);
  }
  
  string getZoneRepresentation() const
  {
    struct sockaddr_in6 addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin6_family=AF_INET6;
    memcpy(&addr.sin6_addr, d_ip6, 16);

    char tmp[128];
    tmp[0]=0;
    Utility::inet_ntop(AF_INET6, (const char*)& addr.sin6_addr, tmp, sizeof(tmp));
    return tmp;
  }

private:
  unsigned char d_ip6[16];
};


void NSECRecordContent::report(void)
{
  regist(1, 47, &make, &make, "NSEC");
}

DNSRecordContent* NSECRecordContent::make(const string& content)
{
  return new NSECRecordContent(content);
}

NSECRecordContent::NSECRecordContent(const string& content, const string& zone) : DNSRecordContent(47)
{
  RecordTextReader rtr(content, zone);
  rtr.xfrLabel(d_next);

  while(!rtr.eof()) {
    uint16_t type;
    rtr.xfrType(type);
    d_set.insert(type);
  }
}

void NSECRecordContent::toPacket(DNSPacketWriter& pw)
{
  pw.xfrLabel(d_next);

  uint8_t res[34];
  memset(res, 0, sizeof(res));

  set<uint16_t>::const_iterator i;
  for(i=d_set.begin(); i != d_set.end() && *i<255; ++i){
    res[2+*i/8] |= 1 << (7-(*i%8));
  }
  int len=0;
  if(!d_set.empty()) 
    len=1+*--i/8;

  res[1]=len;

  string tmp;
  tmp.assign(res, res+len+2);
  pw.xfrBlob(tmp);
}

NSECRecordContent::DNSRecordContent* NSECRecordContent::make(const DNSRecord &dr, PacketReader& pr) 
{
  NSECRecordContent* ret=new NSECRecordContent();
  pr.xfrLabel(ret->d_next);
  string bitmap;
  pr.xfrBlob(bitmap);
  
  // 00 06 20 00 00 00 00 03  -> NS RRSIG NSEC  ( 2, 46, 47 ) counts from left
  
  if(bitmap.size() < 2)
    throw MOADNSException("NSEC record with impossibly small bitmap");
  
  if(bitmap[0])
    throw MOADNSException("Can't deal with NSEC mappings > 255 yet");
  
  unsigned int len=bitmap[1];
  if(bitmap.size()!=2+len)
    throw MOADNSException("Can't deal with multi-part NSEC mappings yet");
  
  for(unsigned int n=0 ; n < len ; ++n) {
    uint8_t val=bitmap[2+n];
    for(int bit = 0; bit < 8 ; ++bit , val>>=1)
      if(val & 1) {
        ret->d_set.insert((7-bit) + 8*(n));
      }
  }
  
  return ret;
}

string NSECRecordContent::getZoneRepresentation() const
{
  string ret;
  RecordTextWriter rtw(ret);
  rtw.xfrLabel(d_next);
  
  for(set<uint16_t>::const_iterator i=d_set.begin(); i!=d_set.end(); ++i) {
    ret+=" ";
    ret+=NumberToType(*i);
  }
  
  return ret;
}

boilerplate_conv(NS, ns_t_ns, conv.xfrLabel(d_content, true));
boilerplate_conv(PTR, ns_t_ptr, conv.xfrLabel(d_content, true));
boilerplate_conv(CNAME, ns_t_cname, conv.xfrLabel(d_content, true));
boilerplate_conv(MR, ns_t_mr, conv.xfrLabel(d_alias, false));
boilerplate_conv(TXT, ns_t_txt, conv.xfrText(d_text, true));
boilerplate_conv(SPF, 99, conv.xfrText(d_text, true));
boilerplate_conv(HINFO, ns_t_hinfo,  conv.xfrText(d_cpu);   conv.xfrText(d_host));

boilerplate_conv(RP, ns_t_rp,
        	 conv.xfrLabel(d_mbox);   
        	 conv.xfrLabel(d_info)
        	 );


boilerplate_conv(OPT, ns_t_opt, 
        	   conv.xfrBlob(d_data)
        	 );

void OPTRecordContent::getData(vector<pair<uint16_t, string> >& options)
{
  string::size_type pos=0;
  uint16_t code, len;
  while(d_data.size() >= 4 + pos) {
    code = 0xff * d_data[pos] + d_data[pos+1];
    len = 0xff * d_data[pos+2] + d_data[pos+3];
    pos+=4;

    if(pos + len > d_data.size())
      break;

    string field(d_data.c_str() + pos, len);
    pos+=len;
    options.push_back(make_pair(code, field));
  }
}

boilerplate_conv(TSIG, ns_t_tsig, 
        	 conv.xfrLabel(d_algoName);
        	 conv.xfr48BitInt(d_time);
        	 conv.xfr16BitInt(d_fudge);
        	 uint16_t size=d_mac.size();
        	 conv.xfr16BitInt(size);
        	 conv.xfrBlob(d_mac, size);
        	 conv.xfr16BitInt(d_origID);
        	 conv.xfr16BitInt(d_eRcode);
        	 size=d_otherData.size();
        	 conv.xfr16BitInt(size);
        	 conv.xfrBlob(d_otherData, size);
        	 );

MXRecordContent::MXRecordContent(uint16_t preference, const string& mxname) : DNSRecordContent(ns_t_mx), d_preference(preference), d_mxname(mxname)
{
}

boilerplate_conv(MX, ns_t_mx, 
        	 conv.xfr16BitInt(d_preference);
        	 conv.xfrLabel(d_mxname, true);
        	 )

boilerplate_conv(KX, ns_t_kx, 
        	 conv.xfr16BitInt(d_preference);
        	 conv.xfrLabel(d_exchanger, false);
        	 )

boilerplate_conv(IPSECKEY, 45,  /* ns_t_ipsec */
        	 conv.xfr8BitInt(d_preference);
        	 conv.xfr8BitInt(d_gatewaytype);
        	 conv.xfr8BitInt(d_algorithm);
        	 conv.xfrLabel(d_gateway, false);
        	 conv.xfrBlob(d_publickey);
        	 )

boilerplate_conv(DHCID, 49, 
        	 conv.xfrBlob(d_content);
        	 )


boilerplate_conv(AFSDB, ns_t_afsdb, 
        	 conv.xfr16BitInt(d_subtype);
        	 conv.xfrLabel(d_hostname);
        	 )


boilerplate_conv(NAPTR, ns_t_naptr,
        	 conv.xfr16BitInt(d_order);    conv.xfr16BitInt(d_preference);
        	 conv.xfrText(d_flags);        conv.xfrText(d_services);         conv.xfrText(d_regexp);
        	 conv.xfrLabel(d_replacement);
        	 )


SRVRecordContent::SRVRecordContent(uint16_t preference, uint16_t weight, uint16_t port, const string& target) 
  : DNSRecordContent(ns_t_srv), d_preference(preference), d_weight(weight), d_port(port), d_target(target)
{}

boilerplate_conv(SRV, ns_t_srv, 
        	 conv.xfr16BitInt(d_preference);   conv.xfr16BitInt(d_weight);   conv.xfr16BitInt(d_port);
        	 conv.xfrLabel(d_target);
        	 )



SOARecordContent::SOARecordContent(const string& mname, const string& rname, const struct soatimes& st) 
  : DNSRecordContent(ns_t_soa), d_mname(mname), d_rname(rname)
{
  d_st=st;
}

boilerplate_conv(SOA, ns_t_soa, 
        	 conv.xfrLabel(d_mname, true);
        	 conv.xfrLabel(d_rname, true);
        	 conv.xfr32BitInt(d_st.serial);
        	 conv.xfr32BitInt(d_st.refresh);
        	 conv.xfr32BitInt(d_st.retry);
        	 conv.xfr32BitInt(d_st.expire);
        	 conv.xfr32BitInt(d_st.minimum);
        	 );
#undef KEY
boilerplate_conv(KEY, ns_t_key, 
        	 conv.xfr16BitInt(d_flags); 
        	 conv.xfr8BitInt(d_protocol); 
        	 conv.xfr8BitInt(d_algorithm); 
        	 conv.xfrBlob(d_certificate);
        	 );

boilerplate_conv(CERT, 37, 
        	 conv.xfr16BitInt(d_type); 
        	 conv.xfr16BitInt(d_tag); 
        	 conv.xfr8BitInt(d_algorithm); 
        	 conv.xfrBlob(d_certificate);
        	 )
#undef DS
boilerplate_conv(DS, 43, 
        	 conv.xfr16BitInt(d_tag); 
        	 conv.xfr8BitInt(d_algorithm); 
        	 conv.xfr8BitInt(d_digesttype); 
        	 conv.xfrHexBlob(d_digest);
        	 )

boilerplate_conv(SSHFP, 44, 
        	 conv.xfr8BitInt(d_algorithm); 
        	 conv.xfr8BitInt(d_fptype); 
        	 conv.xfrHexBlob(d_fingerprint);
        	 )

boilerplate_conv(RRSIG, 46, 
        	 conv.xfrType(d_type); 
          	 conv.xfr8BitInt(d_algorithm); 
          	 conv.xfr8BitInt(d_labels); 

          	 conv.xfr32BitInt(d_originalttl); 
          	 conv.xfrTime(d_sigexpire); 
          	 conv.xfrTime(d_siginception); 
        	 conv.xfr16BitInt(d_tag); 
        	 conv.xfrLabel(d_signer);
        	 conv.xfrBlob(d_signature);
        	 )
        	 
boilerplate_conv(DNSKEY, 48, 
        	 conv.xfr16BitInt(d_flags); 
        	 conv.xfr8BitInt(d_protocol); 
        	 conv.xfr8BitInt(d_algorithm); 
        	 conv.xfrBlob(d_key);
        	 )

// "fancy records" 
boilerplate_conv(URL, QType::URL, 
        	 conv.xfrLabel(d_url);
        	 )

boilerplate_conv(MBOXFW, QType::MBOXFW, 
        	 conv.xfrLabel(d_mboxfw);
        	 )

bool getEDNSOpts(const MOADNSParser& mdp, EDNSOpts* eo)
{
  if(mdp.d_header.arcount && !mdp.d_answers.empty() && 
     mdp.d_answers.back().first.d_type == QType::OPT) {
    eo->d_packetsize=mdp.d_answers.back().first.d_class;
    
    EDNS0Record stuff;
    uint32_t ttl=ntohl(mdp.d_answers.back().first.d_ttl);
    memcpy(&stuff, &ttl, sizeof(stuff));

    eo->d_extRCode=stuff.extRCode;
    eo->d_version=stuff.version;
    eo->d_Z = ntohs(stuff.Z);
    OPTRecordContent* orc = 
      dynamic_cast<OPTRecordContent*>(mdp.d_answers.back().first.d_content.get());
    if(!orc)
      return false;
    orc->getData(eo->d_options);

    return true;
  }
  else
    return false;
}


void reportBasicTypes()
{
  ARecordContent::report();
  AAAARecordContent::report();
  NSRecordContent::report();
  CNAMERecordContent::report();
  MXRecordContent::report();
  SOARecordContent::report();
  SRVRecordContent::report();
  PTRRecordContent::report();
  DNSRecordContent::regist(3, ns_t_txt, &TXTRecordContent::make, &TXTRecordContent::make, "TXT");
  TXTRecordContent::report();
  DNSRecordContent::regist(1, QType::ANY, 0, 0, "ANY");
}

void reportOtherTypes()
{
   AFSDBRecordContent::report();
   SPFRecordContent::report();
   NAPTRRecordContent::report();
   LOCRecordContent::report();
   HINFORecordContent::report();
   RPRecordContent::report();
   KEYRecordContent::report();
   DNSKEYRecordContent::report();
   RRSIGRecordContent::report();
   DSRecordContent::report();
   SSHFPRecordContent::report();
   CERTRecordContent::report();
   NSECRecordContent::report();
   DNSRecordContent::regist(0xff, QType::TSIG, &TSIGRecordContent::make, &TSIGRecordContent::make, "TSIG");
   OPTRecordContent::report();
}

void reportFancyTypes()
{
  URLRecordContent::report();
  MBOXFWRecordContent::report();
}

void reportAllTypes()
{
  reportBasicTypes();
  reportOtherTypes();
}

#if 0
static struct Reporter
{
  Reporter()
  {
    reportAllTypes();
  }
} reporter __attribute__((init_priority(65535)));
#endif