//
// dns.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 1998-2008 Andreas Haberstroh (andreas at ibusy dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_NET_DNS_HPP
#define BOOST_NET_DNS_HPP
#include <boost/asio/detail/push_options.hpp>
#include <vector>
#include <string>
#include <boost/asio.hpp>
#include <boost/net/network_array.hpp>
#include <boost/net/rfc1035_414.hpp>
using namespace std;
using namespace boost;
using namespace boost::algorithm;
namespace boost {
namespace net {
namespace dns {
// quick forward declarations
class message;
class request_base_t;
class resource_base_t;
//! Shared Resource Base Pointer
typedef shared_ptr<resource_base_t> shared_resource_base_t;
//! A list of resource records
typedef std::vector<shared_resource_base_t> rr_list_t;
//! A shared list of resource records
typedef shared_ptr<rr_list_t> shared_rr_list_t;
//! Shared Request Base Pointer
typedef shared_ptr<request_base_t> shared_request_base_t;
//! Class identification for resource records. Included is the QCLASS that is in the 0xF0 range.
typedef enum
{
class_none = 0x00, //!< No class definition
class_in, //!< Internet class. Default class
class_cs, //!< CSNET class, obsolete, used for examples.
class_ch, //!< CHAOS class
class_hs, //!< Hesiod [Dyer 87]
class_all = 0xff //!< Query all classes
} class_t;
//! Type identification for resource records. Included are the QTYPE's that are in the 0xF0 range
typedef enum
{
type_none = 0x00, //!< No type definition.
type_a = 0x01, //!< Address type
type_ns = 0x02, //!< Name Server type
type_cname = 0x05, //!< Canonical name type
type_soa = 0x06, //!< Start of Authority type
type_ptr = 0x0c, //!< Pointer type
type_hinfo = 0x0d, //!< Host Information type
type_mx = 0x0f, //!< Mail Exchanger type
type_txt = 0x10, //!< Text type
type_a6 = 0x1c, //!< Address (IP6) type
type_srv = 0x21, //!< Service type
type_ax = 0x68, //!< AX type zengfh 2012.4.15
type_cnamex= 0x69, //!< CNAMEX type zengfh 2012.4.15
type_tsig = 0xfa, //!< tsig type zengfh 2011.12.29
type_axfr = 0xfc, //!< Zone transfer type
type_all = 0xff //!< Query all types
} type_t;
/*!
Basic definition of a DNS request.
Shared by Questions and all the different resource record types
*/
class request_base_t
{
protected:
/// Domain name of the resource record
string rr_domain;
/// Resource record type
uint16_t rr_type;
/// Resource record class
uint16_t rr_class;
public:
/// Default constructor
request_base_t()
: rr_type(type_none), rr_class(class_none)
{}
/*!
Copy contructor
\param o request_base_t to copy
*/
request_base_t(const request_base_t & o)
: rr_domain(o.rr_domain), rr_type(o.rr_type), rr_class(o.rr_class)
{
}
/*!
Constructs a request_base_t
\param t Resource type to create object for
\param c Resource class to create object for
*/
request_base_t(const type_t t, const class_t c=class_in)
: rr_type(t), rr_class(c)
{
}
/*!
Constructs a request_base_t
\param d Domain name to create object for
\param t Resource type to create object for
\param c Resource class to create object for
*/
request_base_t(const string & d, const type_t t, const class_t c=class_in)
: rr_domain(d), rr_type(t), rr_class(c)
{}
/*!
Constructor that creates the request_base_t from a network buffer
\param buffer Memory buffer to read from
\param offset_map DNS label compression map for label/offset values
*/
request_base_t(dns_buffer_t& buffer, rfc1035_414_t& offset_map)
{
decode(buffer, offset_map);
}
/// Virtual Destructor
virtual ~request_base_t()
{
}
/*!
Sets the domain name
\param s Domain name to assign to the request_base_t
\return current domain name
*/
const string& domain(const string& s)
{
return domain(s.c_str());
}
/*!
Sets the domain name
\param s Domain name to assign to the request_base_t.
\return current domain name
*/
const string& domain(const char* s)
{
rr_domain = s;
return rr_domain;
}
/*!
Gets the domain name
\return current domain name
*/
const string& domain() const
{
return rr_domain;
}
/*!
Sets the resource record type
\param t resource type to assign to the request_base_t.
\return Current type
*/
const type_t rtype(const type_t t)
{
rr_type = t;
return (type_t)rr_type;
}
/*!
Gets the resource record type
\return Current type
*/
const type_t rtype() const
{
return (type_t)rr_type;
}
/*!
Sets the resource record class
\param c resource class to assign to the request_base_t.
\return current class
*/
const class_t rclass(const class_t c)
{
rr_class = c;
return (class_t)rr_class;
}
/*!
Gets the resource record class
\return current class
*/
const class_t rclass() const
{
return (class_t)rr_class;
}
/// Friend to tie to the containers in the message class.
friend class message;
protected:
/*!
Encodes the request_base_t into a memory buffer
\param buffer Buffer to encode the request into
\param offset_map DNS label compression map for label/offset values
*/
void encode(dns_buffer_t& buffer, rfc1035_414_t& offset_map)
{
offset_map.write_label(rr_domain, buffer);