rrset.h 15.7 KB
Newer Older
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
// Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id$

#ifndef __RRSET_H
#define __RRSET_H 1

#include <netinet/in.h>

#include <vector>
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>

#include <dns/exceptions.h>
#include <dns/buffer.h>
#include <dns/name.h>

32 33
namespace isc {
namespace dns {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class Message;
class NameCompressor;

// XXX: this should be defined somewhere else.
typedef enum { SECTION_QUESTION = 0,
               SECTION_ANSWER = 1,
               SECTION_AUTHORITY = 2,
               SECTION_ADDITIONAL = 3,
               SECTION_MAX = 4 } section_t;

class RRClass {
public:
    RRClass() {}
    explicit RRClass(uint16_t classval) : classval_(classval) {}
    explicit RRClass(const std::string& classstr);
49 50 51
    const std::string toText() const;
    void toWire(Buffer& b) const;
    uint16_t getValue() const { return (classval_); }
52 53 54 55
    bool operator==(const RRClass& other) const
    { return (classval_ == other.classval_); }
    bool operator!=(const RRClass& other) const
    { return (classval_ != other.classval_); }
56 57
    bool operator<(const RRClass& other) const
    { return (classval_ < other.classval_); }
58

59
    // (Some) well-known RRclass constants
60 61 62 63 64 65 66 67 68 69 70
    static const RRClass IN;
    static const RRClass CH;
private:
    uint16_t classval_;
};

class RRType {
public:
    RRType() {}
    explicit RRType(uint16_t typeval) : typeval_(typeval) {}
    explicit RRType(const std::string& typestr);
71 72 73
    const std::string toText() const;
    void toWire(Buffer& b) const;
    uint16_t getValue() const { return (typeval_); }
74 75 76 77
    bool operator==(const RRType& other) const
    { return (typeval_ == other.typeval_); }
    bool operator!=(const RRType& other) const
    { return (typeval_ != other.typeval_); }
78 79
    bool operator<(const RRType& other) const
    { return (typeval_ < other.typeval_); }
80

81
    // (Some) Well-known RRtype constants
82 83
    static const RRType A;
    static const RRType NS;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
84
    static const RRType SOA;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
85
    static const RRType MX;
86
    static const RRType TXT;
87 88 89 90 91 92 93 94 95 96 97
    static const RRType AAAA;
    // more to follow...

private:
    uint16_t typeval_;
};

class TTL {
public:
    TTL() {}
    explicit TTL(uint32_t ttlval) : ttlval_(ttlval) {}
98
    std::string toText() const
99
    { return (boost::lexical_cast<std::string>(ttlval_)); }
100 101
    void toWire(Buffer& b) const;
    uint32_t getValue() { return (ttlval_); }
102 103 104 105
    bool operator==(const TTL& other) const
    { return (ttlval_ == other.ttlval_); }
    bool operator!=(const TTL& other) const
    { return (ttlval_ != other.ttlval_); }
106 107 108 109 110 111 112 113
    bool operator<=(const TTL &other) const
    { return (ttlval_ <= other.ttlval_); }
    bool operator>=(const TTL &other) const
    { return (ttlval_ >= other.ttlval_); }
    bool operator<(const TTL &other) const
    { return (ttlval_ < other.ttlval_); }
    bool operator>(const TTL &other) const
    { return (ttlval_ > other.ttlval_); }
114 115 116 117 118 119 120 121 122
private:
    uint32_t ttlval_;
};

namespace Rdata {
// Standard DNS Rdata Subclasses
// if we worry about data copy, we may have to use
// shared pointers at the cost of depending on boost.
class Rdata;
123 124
typedef boost::shared_ptr<Rdata> RDATAPTR; // for compatibility
typedef boost::shared_ptr<Rdata> RdataPtr;
125 126
// if we want to avoid the dependency, use this; but we'll have
// to care much more about resource leak.
127
//typedef Rdata * RdataPtr;
128 129 130 131 132 133

// Abstract RDATA class
class Rdata {
public:
    virtual ~Rdata() {};
    virtual unsigned int count() const = 0;
134 135 136
    virtual const RRType& getType() const = 0;
    virtual std::string toText() const = 0;
    virtual void toWire(Buffer& b, NameCompressor& c) const = 0;
137 138
    // need generic method for getting n-th field? c.f. ldns
    // e.g. string getField(int n);
139

140
    // semi-polymorphic factories.
141 142
    static Rdata* fromText(const RRClass& rrclass, const RRType& rrtype,
                           const std::string& text_rdata);
143 144
    static Rdata* fromWire(const RRClass& rrclass, const RRType& rrtype,
                           Buffer& b, NameDecompressor& d);
145

146 147
    // polymorphic copy constructor (XXX should revisit it)
    virtual Rdata* copy() const = 0;
148 149 150 151 152 153 154 155
};

namespace Generic {
class NS : public Rdata::Rdata {
public:
    NS() {}
    explicit NS(const std::string& namestr) : nsname_(namestr) {}
    explicit NS(const Name& nsname) : nsname_(nsname) {}
156
    explicit NS(Buffer& buffer, NameDecompressor& decompressor);
157
    unsigned int count() const { return (1); }
158 159 160 161 162
    const RRType& getType() const { return (RRType::NS); }
    static const RRType& getTypeStatic() { return (RRType::NS); }
    std::string toText() const;
    void toWire(Buffer& b, NameCompressor& c) const;
    const std::string getNsname() const { return (nsname_.toText(false)); }
163 164 165
    bool operator==(const NS &other) const
    { return (nsname_ == other.nsname_); }
    virtual bool operator!=(const NS &other) const { return !(*this == other); }
166
    virtual Rdata* copy() const;
167 168 169
private:
    Name nsname_;
};
170

JINMEI Tatuya's avatar
JINMEI Tatuya committed
171 172 173 174 175 176 177 178 179
class MX : public Rdata::Rdata {
public:
    MX() {}
    explicit MX(uint16_t preference, const std::string& namestr) :
        preference_(preference), mxname_(namestr) {}
    explicit MX(Buffer& buffer, NameDecompressor& decompressor);
    unsigned int count() const { return (1); }
    const RRType& getType() const { return (RRType::MX); }
    static const RRType& getTypeStatic() { return (RRType::MX); }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
180 181
    uint16_t getPreference() const { return (preference_); }
    const Name getMXName() const { return (mxname_); }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
182 183 184 185 186 187 188 189 190 191 192
    std::string toText() const;
    void toWire(Buffer& buffer, NameCompressor& compressor) const;
    bool operator==(const MX& other) const
    { return (preference_ == other.preference_ && mxname_ == other.mxname_); }
    virtual bool operator!=(const MX &other) const { return !(*this == other); }
    virtual Rdata* copy() const;
private:
    uint16_t preference_;
    Name mxname_;
};

JINMEI Tatuya's avatar
JINMEI Tatuya committed
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
class SOA : public Rdata::Rdata {
public:
    SOA() {}
    explicit SOA(const std::string& mname, const std::string& rname,
                 uint32_t serial, uint32_t refresh, uint32_t retry,
                 uint32_t expire, const TTL& ttl) :
        mname_(mname), rname_(rname), serial_(serial), refresh_(refresh),
        retry_(retry), expire_(expire), ttl_(ttl) {}
    explicit SOA(Buffer& buffer, NameDecompressor& decompressor);
    unsigned int count() const { return (1); }
    const RRType& getType() const { return (RRType::SOA); }
    static const RRType& getTypeStatic() { return (RRType::SOA); }
    std::string toText() const;
    void toWire(Buffer& buffer, NameCompressor& compressor) const;
    bool operator==(const SOA &other) const;
    virtual bool operator!=(const SOA &other) const
    { return !(*this == other); }
    virtual Rdata* copy() const;
private:
    Name mname_;
    Name rname_;
    uint32_t serial_;
    uint32_t refresh_;
    uint32_t retry_;
    uint32_t expire_;
    TTL ttl_;
};

inline bool
SOA::operator==(const SOA& other) const
{
    return (mname_ == other.mname_ && rname_ == other.rname_ &&
            serial_ == other.serial_ && refresh_ == other.refresh_ &&
            retry_ == other.retry_ && expire_ == other.expire_ &&
            ttl_ == other.ttl_);
}

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
// A quick hack implementation of TXT RR.  It currently supports only one
// "character-string".
class TXT : public Rdata::Rdata {
public:
    TXT() {}
    explicit TXT(const std::string& text_data);
    unsigned int count() const { return (string_list.size()); }
    const RRType& getType() const { return (RRType::TXT); }
    static const RRType& getTypeStatic() { return (RRType::TXT); }
    std::string toText() const;
    void fromWire(Buffer& b, NameDecompressor& c);
    void toWire(Buffer& b, NameCompressor& c) const;
    bool operator==(const TXT &other) const;
    virtual bool operator!=(const TXT &other) const
    { return !(*this == other); }
    virtual Rdata* copy() const;
private:
    static const unsigned int MAX_CHARACTER_STRING = 255;
    std::vector<std::string> string_list;
};
250 251 252 253 254 255 256 257 258
// add MXRdata, etc...
}

namespace IN {
class A : public Rdata::Rdata {
public:
    A() {}
    // constructor from a textual IPv4 address
    explicit A(const std::string& addrstr);
259
    explicit A(Buffer& buffer, NameDecompressor& decompressor);
260
    unsigned int count() const { return (1); }
261 262 263 264
    const RRType& getType() const { return (RRType::A); }
    static const RRType& getTypeStatic() { return (RRType::A); }
    std::string toText() const;
    void toWire(Buffer& b, NameCompressor& c) const;
265 266 267 268 269
    const struct in_addr& getAddress() const { return (addr_); }
    bool operator==(const A &other) const
    { return (addr_.s_addr == other.addr_.s_addr); }
    virtual bool operator!=(const A &other) const
    { return !(*this == other); }
270
    virtual Rdata* copy() const;
271 272 273 274 275 276 277 278 279 280
private:
    // XXX: should probably define an "address class" and use it.
    struct in_addr addr_;
};

class AAAA : public Rdata::Rdata {
public:
    AAAA() {}
    // constructor from a textual IPv6 address
    explicit AAAA(const std::string& addrstr);
281
    explicit AAAA(Buffer& buffer, NameDecompressor& decompressor);
282
    unsigned int count() const { return (1); }
283 284 285 286
    std::string toText() const;
    const RRType& getType() const { return (RRType::AAAA); }
    static const RRType& getTypeStatic() { return (RRType::AAAA); }
    void toWire(Buffer& b, NameCompressor& c) const;
287 288 289 290 291
    const struct in6_addr& getAddress() const { return (addr_); }
    bool operator==(const AAAA &other) const
    { return (IN6_ARE_ADDR_EQUAL(&addr_, &other.addr_)); }
    virtual bool operator!=(const AAAA &other) const
    { return !(*this == other); }
292
    virtual Rdata* copy() const;
293 294 295 296 297 298 299
private:
    // XXX: should probably define an "address class" and use it.
    struct in6_addr addr_;
};
}
}

300 301
// An RRset.  Conceptually it's a named RdataSet.  A (section
// of) DNS message would consist of a list of RRsets.
302 303 304
// This is a primary class internally used in our major software such as name
// servers.
//
305 306 307 308 309 310
// Note about terminology: there has been a discussion at the IETF namedroppers
// ML about RRset vs RRSet (case of "s").  While RFC2181 uses the latter,
// many other RFCs use the former, and most of the list members who showed
// their opinion seem to prefer RRset.  We follow that preference in this
// implementation.
//
311 312 313 314 315 316 317 318 319 320 321
// Open Issues:
//   - add more set-like operations, e.g, merge?
//   - add a "sort" method?  "search(find)" method?
//   - BIND9 libdns has some special DNSSEC-related methods
//     such as addnoqname(), addclosest().  do we need these?
//   - need to check duplicate rdata in addrdata()?
//   - need a comparison method?  if so, should it compare
//     rdata's as a set or as a list (compare each rdata one
//     by one)?  ldns has ldns_rr_list_compare(), which takes
//     the latter approach (assuming the caller sorts the lists
//     beforehand?).
322
class AbstractRRset {
323
public:
324
    virtual ~AbstractRRset() {}
325 326
    virtual std::string toText() const = 0;
    virtual int toWire(Buffer& buffer, NameCompressor& compressor,
327
                        section_t section) = 0;
328
    virtual void addRdata(Rdata::RdataPtr rdata) = 0;
329 330 331 332 333 334
    virtual unsigned int countRdata() const = 0;
    virtual const Name& getName() const = 0;
    virtual const RRClass& getClass() const = 0;
    virtual const RRType& getType() const = 0;
    virtual const TTL& getTtl() const = 0;
    virtual void setTtl(const TTL& ttl) = 0;
335
    virtual const std::vector<Rdata::RdataPtr>& getRdatalist() const = 0;
336 337
};

338
class RRset : public AbstractRRset {
339
public:
340
    RRset() {}
341 342 343
    explicit RRset(const Name &name, const RRClass &rrclass,
                   const RRType &rrtype, const TTL &ttl) :
        name_(name), rrclass_(rrclass), rrtype_(rrtype), ttl_(ttl) {}
344
    unsigned int countRdata() const { return (rdatalist_.size()); }
345
    void addRdata(Rdata::RdataPtr rdata);
346 347 348 349 350 351 352
    void removeRdata(const Rdata::Rdata& rdata);
    std::string toText() const;
    int toWire(Buffer& buffer, NameCompressor& compressor, section_t section);
    const Name& getName() const { return (name_); } 
    const RRClass& getClass() const { return (rrclass_); }
    const RRType& getType() const { return (rrtype_); }
    const TTL& getTtl() const { return (ttl_); }
353
    // once constructed, only TTL and rdatalist can be modified.
354
    void setTtl(const TTL& ttl) { ttl_ = ttl; }
355
    const std::vector<Rdata::RdataPtr>& getRdatalist() const
356
    { return (rdatalist_); }
357
    template <typename T> void getRdatalist(std::vector<T>&) const;
358 359
private:
    Name name_;
360 361
    RRClass rrclass_;
    RRType rrtype_;
362
    TTL ttl_;
363
    std::vector<Rdata::RdataPtr> rdatalist_;
364 365 366 367
};

//
// Generic Question section entry
368 369
// XXX: it seems to be not a good idea to inherit from RRset.  We should
// probably re-define it as a separate class.
370
//
371
class Question : public AbstractRRset {
372
public:
373 374 375
    explicit Question(const Name& name, const RRClass& rrclass,
             const RRType& rrtype) :
        name_(name), rrclass_(rrclass), rrtype_(rrtype), ttl_(0) {}
376 377 378 379 380 381 382 383
    std::string toText() const;
    int toWire(Buffer& buffer, NameCompressor& compressor, section_t section);
    unsigned int countRdata() const { return (0); }
    const Name& getName() const { return (name_); }
    const RRClass& getClass() const { return (rrclass_); }
    const RRType& getType() const { return (rrtype_); }
    const TTL& getTtl() const { return (ttl_); } // XXX
    void setTtl(const TTL& ttl) {}              // XXX
384
    void addRdata(Rdata::RdataPtr rdata) {}     // XXX
385
    const std::vector<Rdata::RdataPtr>& getRdatalist() const {} // XXX
386 387
private:
    Name name_;
388 389 390
    RRClass rrclass_;
    RRType rrtype_;
    TTL ttl_;
391 392 393 394 395
};

// TBD: this interface should be revisited.
template <typename T>
void
396
RRset::getRdatalist(std::vector<T>& v) const
397
{
398
    std::vector<Rdata::RdataPtr>::const_iterator it;
399 400
    for (it = rdatalist_.begin(); it != rdatalist_.end(); ++it) {
        const T& concreteRdata = static_cast<const T&>(**it); // XXX
401
        if (T::getTypeStatic() != (**it).getType()) {
402
            throw DNSRRtypeMismatch();
403 404 405 406 407 408 409 410 411 412 413 414 415 416
        }
        v.push_back(concreteRdata);
    }
}

// A single RR.  Architecturally this is redundant, but it
// would often be convenient if we can handle each RR
// separately.
// Note: an RR *is-not-a* set, so conceptually it cannot be a
// derived class of RdataSet.
class RR {
public:
    RR() {}
    explicit RR(const std::string& rrstr);
417 418
    explicit RR(const Name& name, const RRClass& rrclass,
                const RRType& rrtype, const TTL& ttl,
419
                const Rdata::Rdata& rdata);
420 421 422
    explicit RR(const Name& name, const RRClass& rrclass,
                const RRType& rrtype, const TTL& ttl,
                Rdata::RdataPtr rdata);
423 424 425 426 427
    std::string toText() const { return (rrset_.toText()); }
    const Name& getName() const { return (rrset_.getName()); }
    const RRClass& getClass() const { return (rrset_.getClass()); }
    const RRType& getType() const { return (rrset_.getType()); }
    const TTL& getTtl() const { return (rrset_.getTtl()); }
428
    const Rdata::RdataPtr getRdata() const
429 430
    { return (*rrset_.getRdatalist().begin()); }
    void setTtl(const TTL& ttl) { rrset_.setTtl(ttl); }
431
private:
432 433 434
    // An RR is (could be) actually implemented as an RRset containing at most
    // one RR.
    RRset rrset_;
435 436 437 438 439 440 441 442
};
}
}
#endif  // __RRSET_H

// Local Variables: 
// mode: c++
// End: