]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnslabel.hh
add OpenSSL exception to PowerDNS, Netherlabs, van Dijk and Hubert copyrights
[thirdparty/pdns.git] / pdns / dnslabel.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2010 Netherlabs Computer Consulting BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <string>
24 #include <stdio.h>
25 #include <iostream>
26 #include <string.h>
27 #include <stdexcept>
28 using std::string;
29 using std::cerr;
30 using std::endl;
31
32
33 /* the idea of dnslabel is that we guard our input, and from that point
34 * onwards, trust the contents of d_storage.
35 *
36 * On input we deal with escapes etc, on output we re-escape.
37 * This can be slow since we hope with all our might not to be
38 * using the 'human' interfaces too much, and keep everything as a
39 * native DNS label all the time.
40 *
41 * The goal for DNSLabel is to be 'holier than thou' and adhere
42 * to all relevant RFCs. This means implementing the really odd DNS case
43 * sensitivity rules, doing all the escaping properly and deal
44 * with embedded nuls.
45 *
46 * Design
47 * As a special speedup, we implement 'chopping' by having an offset
48 * counter. This means that the oft-repeated 'www.powerdns.com.'
49 * 'powerdns.com.', 'com.', '.' sequence does not involve any mallocs.
50 */
51 class DNSLabel
52 {
53 public:
54 explicit DNSLabel(const char* human);
55 explicit DNSLabel(const std::string& human);
56 DNSLabel(const char* raw, unsigned int length);
57 DNSLabel(const DNSLabel& rhs);
58 DNSLabel(const char* raw, const char* beginPacket, unsigned int packetLength, unsigned int* len);
59 DNSLabel();
60 ~DNSLabel();
61 string human() const;
62 string binary() const;
63 bool endsOn(const DNSLabel& rhs) const;
64 bool chopOff();
65 bool operator<(const DNSLabel& rhs) const;
66 bool operator==(const DNSLabel& rhs) const;
67 DNSLabel& operator=(const DNSLabel& rhs);
68 int project(char* target, unsigned int length);
69 static int validateConsume(const char* raw, unsigned int len);
70 static bool validateStrict(const char* raw, unsigned int len);
71
72 static DNSLabel createFromBuffer(const char* raw, unsigned int* len);
73 private:
74 char* d_storage;
75 unsigned int d_fulllen;
76 unsigned int d_offset;
77 unsigned int d_capacity;
78 void init(unsigned int len=64);
79 unsigned int getLength() const
80 {
81 return d_fulllen - d_offset;
82 }
83
84 const char* getStart() const
85 {
86 return d_storage + d_offset;
87 }
88
89 void appendChar(char c)
90 {
91 if(d_fulllen == d_capacity)
92 expandCapacity();
93 d_storage[d_fulllen++]= c;
94 }
95 void appendRange(const char* ptr, unsigned int len)
96 {
97 if(d_fulllen + len > d_capacity)
98 expandCapacity(len);
99 memcpy(d_storage + d_fulllen, ptr, len);
100 d_fulllen += len;
101 }
102
103 void expandCapacity(unsigned int len=0);
104 void chaseLabel(const char* raw, const char* beginPacket, unsigned int packetLength, unsigned int* len, bool updateLen);
105 };