rrset.h 30.3 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
// Copyright (C) 2010  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.

#ifndef __RRSET_H
#define __RRSET_H 1

#include <iostream>
#include <string>

#include <boost/shared_ptr.hpp>

JINMEI Tatuya's avatar
JINMEI Tatuya committed
23 24
#include <exceptions/exceptions.h>

25 26
#include <dns/rdata.h>
#include <dns/rrtype.h>
27 28

namespace isc {
29
namespace util {
30
namespace io {
31 32
class OututBuffer;
}
33
}
34

35 36
namespace dns {

JINMEI Tatuya's avatar
JINMEI Tatuya committed
37 38 39 40 41 42 43 44 45 46 47
///
/// \brief A standard DNS module exception that is thrown if an RRset object
/// does not contain any RDATA where required.
///
class EmptyRRset : public Exception {
public:
    EmptyRRset(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) {}
};

// forward declarations
48 49 50 51 52 53 54
class Name;
class RRType;
class RRClass;
class RRTTL;
class MessageRenderer;
class AbstractRRset;
class BasicRRset;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
55
class RdataIterator;
56
class BasicRRsetImpl;
57
class RRset;
58

59
/// \brief A pointer-like type pointing to an \c RRset object.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
60 61 62
///
/// This type is commonly used as an argument of various functions defined
/// in this library in order to handle RRsets in a polymorphic manner.
63
typedef boost::shared_ptr<RRset> RRsetPtr;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
64

65
/// \brief A pointer-like type pointing to an (immutable) \c RRset
JINMEI Tatuya's avatar
JINMEI Tatuya committed
66 67 68 69
/// object.
///
/// This type is commonly used as an argument of various functions defined
/// in this library in order to handle RRsets in a polymorphic manner.
70
typedef boost::shared_ptr<const RRset> ConstRRsetPtr;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
71 72

/// \brief A pointer-like type point to an \c RdataIterator object.
73 74
typedef boost::shared_ptr<RdataIterator> RdataIteratorPtr;

JINMEI Tatuya's avatar
JINMEI Tatuya committed
75 76
/// \brief The \c AbstractRRset class is an abstract base class that
/// models a DNS RRset.
77
///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
78 79 80
/// An object of (a specific derived class of) \c AbstractRRset
/// models an RRset as described in the DNS standard:
/// A set of DNS resource records (RRs) of the same type and class.
81 82
/// The standard requires the TTL of all RRs in an RRset be the same;
/// this class follows that requirement.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
83 84 85 86 87 88 89 90 91 92 93 94 95

/// Note about duplicate RDATA: RFC2181 states that it's meaningless that an
/// RRset contains two identical RRs and that name servers should suppress
/// such duplicates.
/// This class is not responsible for ensuring this requirement: For example,
/// \c addRdata() method doesn't check if there's already RDATA identical
/// to the one being added.
/// This is because such checks can be expensive, and it's often easy to
/// ensure the uniqueness requirement at the %data preparation phase
/// (e.g. when loading a zone).
/// When parsing an incoming DNS message, the uniqueness may not be guaranteed,
/// so the application needs to detect and ignore any duplicate RDATA
/// (the \c Message class of this library should provide this responsibility).
96
///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
97 98 99 100 101 102 103 104 105 106
/// Another point to note is that \c AbstractRRset and its derived classes
/// allow an object to have an empty set of RDATA.
/// Even though there's no corresponding notion in the protocol specification,
/// it would be more intuitive for a container-like %data structure
/// to allow an empty set.
///
/// Since \c AbstractRRset is an abstract class, it is generally used
/// via a pointer (or pointer like object) or a reference.
/// In particular, \c RRsetPtr, a pointer like type for \c AbstractRRset,
/// is used for polymorphic RRset operations throughout this library.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
107
///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
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
/// The \c AbstractRRset class is also intended to be a major customization
/// point.  For example, a high performance server implementation may want
/// to define an optimized "pre-compiled" RRset and provide an optimized
/// implementation of the \c toWire() method.
///
/// Note about design choice: In BIND9, a set of RDATA with a common tuple
/// of RR class, RR type, and TTL was represented in a structure named
/// \c rdataset.  Unlike the RRset classes, an \c rdataset did not contain
/// the information of the owner name.
/// This might be advantageous if we want to handle "RRsets", that is,
/// a set of different types of RRset for the same owner name, because
/// a single "name" structure can be used for multiple RRsets, minimizing
/// %data copy and memory footprint.
/// On the other hand, it's inconvenient for API users since in many cases
/// a pair of name and an \c rdataset must be maintained.  It's also counter
/// intuitive in implementing protocol operations as an RRset is often used
/// as an atomic entity in DNS protocols while an \c rdataset is a component
/// of an RRset.
///
/// We have therefore defined the notion of RRset explicitly in our initial
/// API design.  We believe memory footprint is not a big concern because
/// RRsets are generally expected to be used as temporary objects, e.g.
/// while parsing or constructing a DNS message, or searching a DNS %data
/// source; for longer term purposes such as in-memory %data source entries,
/// the corresponding %data would be represented in a different, memory
/// optimized format.  As for the concern about %data copy, we believe
/// it can be mitigated by using copy-efficient implementation for the
/// \c Name class implementation, such as reference counted objects.
/// Later, We plan to perform benchmark tests later to see if this assumption
/// is valid and to revisit the design if necessary.
///
/// Note about terminology: there has been a discussion at the IETF
/// namedroppers ML about RRset vs RRSet (case of "s")
/// [http://ops.ietf.org/lists/namedroppers/namedroppers.2009/msg02737.html].
/// 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.
///
/// The current design of \c AbstractRRset is still in flux.
/// There are many open questions in design details:
///   - support more set-like operations, e.g, merge two RRsets of the same
///     type?
///   - more convenient methods or non member utility functions, e.g.
///      "sort" and "search(find)" method?
///   - what about comparing two RRsets of the same type?  If we need this,
///     should it compare rdata's as a set or as a list (i.e. compare
154
///     each rdata one by one or as a whole)?  c.f. NLnet Labs' ldns
JINMEI Tatuya's avatar
JINMEI Tatuya committed
155
///     (http://www.nlnetlabs.nl/projects/ldns/doc/index.html)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
156 157 158 159 160 161 162 163
///     has \c ldns_rr_list_compare(), which takes the latter approach
///     (seemingly assuming the caller sorts the lists beforehand).
///   - BIND9 libdns has some special DNSSEC-related methods
///     such as \c addnoqname() or \c addclosest().  Do we need these?
///     (Probably not.  We wouldn't want to make the class design too
///     monolithic.)
///   - Do we need to allow the user to remove specific Rdata?
///     Probably not, according to the current usage of the BIND9 code.
164
class AbstractRRset {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    ///
    /// \name Constructors and Destructor
    ///
    /// Note: The copy constructor and the assignment operator are intentionally
    /// defined as private to make it explicit that this is a pure base class.
    //@{
private:
    AbstractRRset(const AbstractRRset& source);
    AbstractRRset& operator=(const AbstractRRset& source);
protected:
    /// \brief The default constructor.
    ///
    /// This is intentionally defined as \c protected as this base class should
    /// never be instantiated (except as part of a derived class).
    AbstractRRset() {}
180
public:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
181
    /// The destructor.
182
    virtual ~AbstractRRset() {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
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
    //@}

    ///
    /// \name Getter and Setter Methods
    ///
    /// These methods are generally expected to be exception free, but it's
    /// not guaranteed at the interface level;
    /// for example, some performance optimized derived class may manage
    /// the information corresponding to the class "attributes" to get or set,
    /// and may require dynamic memory allocation to execute the method.
    /// Consult the derived class description to see if a specific derived
    /// \c RRset class may throw an exception from these methods.
    ///
    /// Note that setter methods are not provided for \c RRClass and
    /// \c RRType.  This is intentional.  Since the format and semantics of
    /// \c Rdata are dependent on the RR type (and RR class for some RR types),
    /// allowing dynamically modify these attributes can easily lead to a
    /// bug where the RDATA and type and/or class become inconsistent.
    /// We want to avoid that situation by restricting the access.
    //@{
    /// \brief Returns the number of \c Rdata objects contained in the \c RRset.
    ///
    /// Note that an \c RRset with an empty set of \c Rdata can exist, so
    /// this method may return 0.
    ///
    /// \return The number of \c Rdata objects contained.
209
    virtual unsigned int getRdataCount() const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
210 211 212 213 214

    /// \brief Returns the owner name of the \c RRset.
    ///
    /// \return A reference to a \c Name class object corresponding to the
    /// \c RRset owner name.
215
    virtual const Name& getName() const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
216 217 218 219 220

    /// \brief Returns the RR Class of the \c RRset.
    ///
    /// \return A reference to a \c RRClass class object corresponding to the
    /// RR class of the \c RRset.
221
    virtual const RRClass& getClass() const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
222 223 224 225 226

    /// \brief Returns the RR Type of the \c RRset.
    ///
    /// \return A reference to a \c RRType class object corresponding to the
    /// RR type of the \c RRset.
227
    virtual const RRType& getType() const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
228 229 230 231 232

    /// \brief Returns the TTL of the RRset.
    ///
    /// \return A reference to a \c RRTTL class object corresponding to the
    /// TTL of the \c RRset.
233
    virtual const RRTTL& getTTL() const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
234 235

    /// \brief Updates the owner name of the \c RRset.
236
    ///
237 238
    /// \param name A reference to a \c Name class object to be copied as the
    /// new name.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
239 240 241
    virtual void setName(const Name& name) = 0;

    /// \brief Updates the TTL of the \c RRset.
242
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
243 244
    /// \param ttl A reference to a \c RRTTL class object to be copied as the
    /// new TTL.
245
    virtual void setTTL(const RRTTL& ttl) = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
246
    //@}
247 248

    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
249 250 251 252 253 254 255 256
    /// \name Converter Methods
    ///
    /// These methods have the default implementation that can be reused by
    /// derived classes.
    /// Since they are defined as pure virtual, derived classes
    /// that want to reuse the default implementation must explicitly
    /// invoke their base class version (see the description for
    /// <code>addRdata(const rdata::Rdata&)</code>).
257
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
258 259 260 261 262 263 264
    /// Design Note: the default implementations are defined only using
    /// other public methods of the \c AbstractRRset class, and could be
    /// implemented as non member functions (as some C++ textbooks suggest).
    /// However, since derived classes may want to provide customized versions
    /// (especially of the \c toWire() method for performance reasons)
    /// we chose to define them as virtual functions, and, as a result,
    /// member functions.
265
    //@{
JINMEI Tatuya's avatar
JINMEI Tatuya committed
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
    /// \brief Convert the RRset to a string.
    ///
    /// Unlike other similar methods of this library, this method terminates
    /// the resulting string with a trailing newline character.
    /// (following the BIND9 convention)
    ///
    /// The RRset must contain some RDATA; otherwise, an exception of class
    /// \c EmptyRRset will be thrown.
    /// If resource allocation fails, a corresponding standard exception
    /// will be thrown.
    /// The default implementation may throw other exceptions if the
    /// \c toText() method of the RDATA objects throws.
    /// If a derived class of \c AbstractRRset overrides the default
    /// implementation, the derived version may throw its own exceptions.
    ///
    /// Open issue: We may want to support multiple output formats as
    /// BIND9 does.  For example, we might want to allow omitting the owner
    /// name when possible in the context of zone dump.  This is a future
    /// TODO item.
    ///
    /// \return A string representation of the RRset.
    virtual std::string toText() const = 0;

    /// \brief Render the RRset in the wire format with name compression and
    /// truncation handling.
    ///
    /// This method compresses the owner name of the RRset and domain names
    /// used in RDATA that should be compressed.
    /// In addition, this method detects the case where rendering the entire
    /// RRset would cause truncation, and handles the case appropriately
    /// (this is a TODO item, and not implemented in this version).
    ///
    /// Note: perhaps we may want to add more arguments to convey optional
    /// information such as an "rrset-order" policy or how to handle truncation
    /// case.  This is a TODO item.
    ///
    /// If resource allocation fails, a corresponding standard exception
    /// will be thrown.
    /// The RRset must contain some RDATA; otherwise, an exception of class
    /// \c EmptyRRset will be thrown.
    /// The default implementation may throw other exceptions if the
    /// \c toWire() method of the RDATA objects throws.
    /// If a derived class of \c AbstractRRset overrides the default
    /// implementation, the derived version may throw its own exceptions.
    ///
    /// \param renderer DNS message rendering context that encapsulates the
    /// output buffer and name compression information.
    /// \return The number of RRs rendered.  If the truncation is necessary
    /// this value may be different from the number of RDATA objects contained
    /// in the RRset.
    virtual unsigned int toWire(MessageRenderer& renderer) const = 0;

    /// \brief Render the RRset in the wire format without any compression.
    ///
    /// See the other toWire() description about possible exceptions.
    ///
    /// \param buffer An output buffer to store the wire data.
    /// \return The number of RRs rendered.
324
    virtual unsigned int toWire(isc::util::io::OutputBuffer& buffer) const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
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
    //@}

    ///
    /// \name RDATA Manipulation Methods
    ///
    //@{
    /// \brief Add an RDATA to the RRset (pointer version).
    ///
    /// This method adds the given RDATA (as a pointer-like type to a
    /// derived class object of \c rdata::Rdata) to the \c RRset.
    ///
    /// \param rdata A pointer (like) type of \c rdata::RdataPtr to be added
    /// to the \c RRset.
    virtual void addRdata(rdata::ConstRdataPtr rdata) = 0;

    /// \brief Add an RDATA to the RRset (reference version).
    ///
    /// This method adds the given RDATA (as a reference to a
    /// derived class object of \c rdata::Rdata) to the \c RRset.
    ///
    /// This method has the default implementation that can be reused by
    /// derived classes.
    /// Since this method is defined as pure virtual, derived classes
    /// that want to reuse the default implementation must explicitly
    /// invoke this base class version.
    /// For example, if the class \c CustomizedRRset, a derived class of
    /// \c AbstractRRset, wants to reuse the default implementation of
    /// \c %addRdata() (reference version), it would be defined as follows:
    /// \code void
    /// CustomizedRRset::addRdata(const rdata::Rdata& rdata)
    /// {
    ///     AbstractRRset::addRdata(rdata);
    /// }
    /// \endcode
    ///
    /// This method is more strictly typed than the pointer version:
361
    /// If \c rdata does not refer to the appropriate derived
JINMEI Tatuya's avatar
JINMEI Tatuya committed
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
    /// \c Rdata class
    /// for the \c RRType for this \c RRset, it throws an exception of class
    /// \c std::bad_cast.
    /// If resource allocation fails, a corresponding standard exception
    /// will be thrown.
    /// The RRset must contain some RDATA; otherwise, an exception of class
    /// \c EmptyRRset will be thrown.
    /// The default implementation may throw other exceptions if the
    /// \c toWire() method of the RDATA objects throws.
    /// If a derived class of \c AbstractRRset overrides the default
    /// implementation, the derived version may throw its own exceptions.
    ///
    /// The default implementation simply constructs an \c rdata::RdataPtr
    /// object from a newly allocated RDATA object copying from parameter
    /// \c rdata, and calls the other version of
    /// \c addRdata(const rdata::RdataPtr).
    /// So it is inherently less efficient than the other version.
    /// Still, this version would offer a more intuitive interface and is
    /// provided as such.
    ///
    /// \param rdata A reference to a \c rdata::RdataPtr (derived) class
    /// object, a copy of which is to be added to the \c RRset.
    virtual void addRdata(const rdata::Rdata& rdata) = 0;

    /// \brief Return an iterator to go through all RDATA stored in the
    /// \c RRset.
    ///
389 390 391 392
    /// The rdata cursor of the returned iterator will point to the first
    /// RDATA, that is, it effectively calls \c RdataIterator::first()
    /// internally.
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
393 394 395 396 397 398 399 400 401 402
    /// Using the design pattern terminology, \c getRdataIterator()
    /// is an example of a <em>factory method</em>.
    ///
    /// Whether this method throws an exception depends on the actual
    /// implementation of the derived \c AbstractRRset class, but in general
    /// it will involve resource allocation and can throw a standard exception
    /// if it fails.
    ///
    /// \return A pointer-like object pointing to the derived \c RdataIterator
    /// object.
403 404 405 406
    virtual RdataIteratorPtr getRdataIterator() const = 0;
    //@}
};

JINMEI Tatuya's avatar
JINMEI Tatuya committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/// \brief The \c RdataIterator class is an abstract base class that
/// provides an interface for accessing RDATA objects stored in an RRset.
///
/// While different derived classes of \c AbstractRRset may maintain the RDATA
/// objects in different ways, the \c RdataIterator class provides a
/// unified interface to iterate over the RDATA objects in a polymorphic
/// manner.
///
/// Each derived class of \c AbstractRRset is expected to provide a concrete
/// derived class of \c RdataIterator, and each derived \c RdataIterator
/// class implements the unified interface in a way specific to the
/// implementation of the corresponding derived \c AbstractRRset class.
/// Using the design pattern terminology, this is a typical example of
/// the \e Iterator pattern.
///
/// The RDATA objects stored in the \c RRset are considered to form
/// a unidirectional list from the \c RdataIterator point of view (while
/// the actual implementation in the derived \c RRset may not use a list).
425
/// We call this unidirectional list the <em>rdata list</em>.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
426 427
///
/// An \c RdataIterator object internally (and conceptually) holds a
428
/// <em>rdata cursor</em>, which points to a specific item of the rdata list.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
///
/// Note about design choice: as is clear from the interface, \c RdataIterator
/// is not compatible with the standard iterator classes.
/// Although it would be useful (for example, we could then use STL algorithms)
/// and is not necessarily impossible, it would make the iterator implementation
/// much more complicated.
/// For instance, any standard iterator must be assignable and
/// copy-constructible.
/// So we'd need to implement \c RdataIterator::operator=() in a polymorphic
/// way.  This will require non-trivial implementation tricks.
/// We believe the simplified iterator interface as provided by the
/// \c RdataIterator class is sufficient in practice:
/// Most applications will simply go through the RDATA objects contained in
/// an RRset, examining (and possibly using) each object, as one path
/// operation.
444
class RdataIterator {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
445 446 447 448 449 450 451 452 453 454 455 456
    ///
    /// \name Constructors and Destructor
    ///
    /// Note: The copy constructor and the assignment operator are intentionally
    /// defined as private to make it explicit that this is a pure base class.
    //@{
protected:
    /// \brief The default constructor.
    ///
    /// This is intentionally defined as \c protected as this base class should
    /// never be instantiated (except as part of a derived class).
    RdataIterator() {}
457
public:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
458
    /// \brief Destructor
459
    virtual ~RdataIterator() {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
460 461 462 463 464 465
private:
    RdataIterator(const RdataIterator& source);
    RdataIterator& operator=(const RdataIterator& source);
    //@}

public:
466
    /// \brief Move the rdata cursor to the first RDATA in the rdata list
JINMEI Tatuya's avatar
JINMEI Tatuya committed
467 468 469
    /// (if any).
    ///
    /// This method can safely be called multiple times, even after moving
470
    /// the rdata cursor forward by the \c next() method.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
471 472
    ///
    /// This method should never throw an exception.
473
    virtual void first() = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
474

475
    /// \brief Move the rdata cursor to the next RDATA in the rdata list
JINMEI Tatuya's avatar
JINMEI Tatuya committed
476 477 478
    /// (if any).
    ///
    /// This method should never throw an exception.
479
    virtual void next() = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
480

481
    /// \brief Return the current \c Rdata corresponding to the rdata cursor.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
482 483
    ///
    /// \return A reference to an \c rdata::::Rdata object corresponding
484
    /// to the rdata cursor.
485
    virtual const rdata::Rdata& getCurrent() const = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
486

487 488
    /// \brief Return true iff the rdata cursor has reached the end of the
    /// rdata list.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
489 490 491 492 493 494 495 496
    ///
    /// Once this method returns \c true, the behavior of any subsequent
    /// call to \c next() or \c getCurrent() is undefined.
    /// Likewise, the result of \c isLast() call followed by such undefined
    /// operations is also undefined.
    ///
    /// This method should never throw an exception.
    ///
497 498
    /// \return \c true if the rdata cursor has reached the end of the
    /// rdata list; otherwise \c false.
499 500 501
    virtual bool isLast() const = 0;
};

JINMEI Tatuya's avatar
JINMEI Tatuya committed
502 503 504
/// \brief The \c BasicRRset class is a concrete derived class of
/// \c AbstractRRset that defines a straightforward RRset implementation.
///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
505 506 507
/// This class is designed to be as portable as possible, and so it adopts
/// the Pimpl idiom to hide as many details as possible.
/// Performance is a secondary concern for this class.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
508
///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
509 510 511 512 513 514
/// This class is intended to be used by applications that only need
/// moderate level of performance with full functionality provided by
/// the \c AbstractRRset interfaces.
/// Highly performance-sensitive applications, such as a large scale
/// authoritative or caching name servers will implement and use a customized
/// version of derived \c AbstractRRset class.
515
class BasicRRset : public AbstractRRset {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
516 517 518 519 520 521 522 523
    ///
    /// \name Constructors and Destructor
    ///
    /// Note: The copy constructor and the assignment operator are intentionally
    /// defined as private.  The intended use case wouldn't require copies of
    /// a \c BasicRRset object; once created, it would normally be used
    /// as a \c const object (via references).
    //@{
524 525
private:
    BasicRRset(const BasicRRset& source);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
526
    BasicRRset& operator=(const BasicRRset& source);
527
public:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
528 529 530 531 532 533 534 535 536 537
    /// \brief Constructor from (mostly) fixed parameters of the RRset.
    ///
    /// This constructor is normally expected to be exception free, but
    /// copying the name may involve resource allocation, and if it fails
    /// the corresponding standard exception will be thrown.
    ///
    /// \param name The owner name of the RRset.
    /// \param rrclass The RR class of the RRset.
    /// \param rrtype The RR type of the RRset.
    /// \param ttl The TTL of the RRset.
538 539
    BasicRRset(const Name& name, const RRClass& rrclass,
               const RRType& rrtype, const RRTTL& ttl);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
540
    /// \brief The destructor.
541
    virtual ~BasicRRset();
JINMEI Tatuya's avatar
JINMEI Tatuya committed
542 543
    //@}

544
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
545
    /// \name Getter and Setter Methods
546
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
547 548
    //@{
    /// \brief Returns the number of \c Rdata objects contained in the \c RRset.
549
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
550 551 552 553
    /// This method never throws an exception.
    ///
    /// \return The number of \c Rdata objects contained.
    virtual unsigned int getRdataCount() const;
554

JINMEI Tatuya's avatar
JINMEI Tatuya committed
555
    /// \brief Returns the owner name of the \c RRset.
556
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
557
    /// This method never throws an exception.
558
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
559 560
    /// \return A reference to a \c Name class object corresponding to the
    /// \c RRset owner name.
561
    virtual const Name& getName() const;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
562 563 564 565 566 567 568

    /// \brief Returns the RR Class of the \c RRset.
    ///
    /// This method never throws an exception.
    ///
    /// \return A reference to a \c RRClass class object corresponding to the
    /// RR class of the \c RRset.
569
    virtual const RRClass& getClass() const;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
570 571 572 573 574 575 576

    /// \brief Returns the RR Type of the \c RRset.
    ///
    /// This method never throws an exception.
    ///
    /// \return A reference to a \c RRType class object corresponding to the
    /// RR type of the \c RRset.
577
    virtual const RRType& getType() const;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
578 579 580 581 582 583 584

    /// \brief Returns the TTL of the \c RRset.
    ///
    /// This method never throws an exception.
    ///
    /// \return A reference to a \c RRTTL class object corresponding to the
    /// TTL of the \c RRset.
585
    virtual const RRTTL& getTTL() const;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
586 587 588 589 590 591 592 593

    /// \brief Updates the owner name of the \c RRset.
    ///
    /// This method normally does not throw an exception, but could throw
    /// some standard exception on resource allocation failure if the
    /// internal copy of the \c name involves resource allocation and it
    /// fails.
    ///
594 595
    /// \param name A reference to a \c Name class object to be copied as the
    /// new name.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
596 597 598 599 600 601 602 603
    virtual void setName(const Name& name);

    /// \brief Updates the TTL of the \c RRset.
    ///
    /// This method never throws an exception.
    ///
    /// \param ttl A reference to a \c RRTTL class object to be copied as the
    /// new TTL.
604 605
    virtual void setTTL(const RRTTL& ttl);
    //@}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

    ///
    /// \name Converter Methods
    ///
    //@{
    /// \brief Convert the RRset to a string.
    ///
    /// This method simply uses the default implementation.
    /// See \c AbstractRRset::toText().
    virtual std::string toText() const;

    /// \brief Render the RRset in the wire format with name compression and
    /// truncation handling.
    ///
    /// This method simply uses the default implementation.
    /// See \c AbstractRRset::toWire(MessageRenderer&)const.
    virtual unsigned int toWire(MessageRenderer& renderer) const;

    /// \brief Render the RRset in the wire format without any compression.
    ///
    /// This method simply uses the default implementation.
    /// See \c AbstractRRset::toWire(OutputBuffer&)const.
628
    virtual unsigned int toWire(isc::util::io::OutputBuffer& buffer) const;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
629 630
    //@}

631
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    /// \name RDATA manipulation methods
    ///
    //@{
    /// \brief Add an RDATA to the RRset (pointer version).
    ///
    /// This method is normally expected to be exception free, but it may
    /// involve resource allocation, and if it fails the corresponding
    /// standard exception will be thrown.
    ///
    /// \param rdata A pointer (like) type of \c rdata::RdataPtr to be added
    /// to the \c BasicRRset.
    virtual void addRdata(rdata::ConstRdataPtr rdata);

    /// \brief Add an RDATA to the RRset (reference version).
    ///
    /// This method simply uses the default implementation.
    /// See \c AbstractRRset::addRdata(const rdata::Rdata&).
    virtual void addRdata(const rdata::Rdata& rdata);

    /// \brief Return an iterator to go through all RDATA stored in the
    /// \c BasicRRset.
    ///
    /// This is a concrete derived implementation of
    /// \c AbstractRRset::getRdataIterator().
    ///
    /// This method dynamically allocates resources.  If it fails it will
    /// throw the corresponding standard exception.
    /// The iterator methods for the \c BasicRRset class are exception free.
    ///
    /// \return A pointer-like object pointing to the derived \c RdataIterator
    /// object for the \c BasicRRset class.
663
    virtual RdataIteratorPtr getRdataIterator() const;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
664
    //@}
665 666 667 668
private:
    BasicRRsetImpl* impl_;
};

669 670 671 672 673 674 675
/// \brief The \c RRset class is a concrete derived class of
/// \c BasicRRset which contains a pointer to an additional RRset
/// containing associated RRSIG records.  This allows DNSSEC aware
/// applications to treat data associated with a particular
/// QNAME/QTYPE/QCLASS as a single object.
class RRset : public BasicRRset {
public:
676
    RRset(const Name& name, const RRClass& rrclass,
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
          const RRType& rrtype, const RRTTL& ttl);

    virtual ~RRset();

    /// \brief Updates the owner name of the \c RRset, including RRSIGs if any
    virtual void setName(const Name& n) {
        BasicRRset::setName(n);
        if (rrsig_) {
            rrsig_->setName(n);
        }
    }

    /// \brief Updates the owner name of the \c RRset, including RRSIGs if any
    virtual void setTTL(const RRTTL& ttl) {
        BasicRRset::setTTL(ttl);
        if (rrsig_) {
            rrsig_->setTTL(ttl);
        }
    }

    /// \brief Adds an RRSIG RR to this RRset's signatures
    virtual void addRRsig(const rdata::RdataPtr rdata) {
        if (!rrsig_) {
700 701
            rrsig_ = RRsetPtr(new RRset(getName(), getClass(),
                                        RRType::RRSIG(), getTTL()));
702 703 704 705 706 707 708 709 710
        }
        rrsig_->addRdata(rdata);
    }

    /// \brief Adds an RRSIG RRset to this RRset
    void addRRsig(AbstractRRset& sigs) {
        RdataIteratorPtr it = sigs.getRdataIterator();

        if (!rrsig_) {
711 712
            rrsig_ = RRsetPtr(new RRset(getName(), getClass(),
                                        RRType::RRSIG(), getTTL()));
713 714 715 716 717 718 719 720 721 722 723 724 725
        }

        for (it->first(); !it->isLast(); it->next()) {
            rrsig_->addRdata(it->getCurrent());
        }
    }

    void addRRsig(RRsetPtr sigs) { addRRsig(*sigs); }

    /// \brief Clear the RRSIGs for this RRset
    void removeRRsig() { rrsig_ = RRsetPtr(); }

    /// \brief Return a pointer to this RRset's RRSIG RRset
726
    RRsetPtr getRRsig() const { return (rrsig_); }
727 728 729 730 731
private:
    RRsetPtr rrsig_;
};


JINMEI Tatuya's avatar
JINMEI Tatuya committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745
/// \brief Insert the \c RRset as a string into stream.
///
/// This method convert the \c rrset into a string and inserts it into the
/// output stream \c os.
///
/// This function overloads the global \c operator<< to behave as described in
/// \c %ostream::%operator<< but applied to RRset objects.
///
/// \param os A \c std::ostream object on which the insertion operation is
/// performed.
/// \param rrset A reference to a (derived class of) \c AbstractRRset object
/// output by the operation.
/// \return A reference to the same \c std::ostream object referenced by
/// parameter \c os after the insertion operation.
746 747 748 749 750 751 752 753
std::ostream& operator<<(std::ostream& os, const AbstractRRset& rrset);
} // end of namespace dns
} // end of namespace isc
#endif  // __RRSET_H

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