Commit afc4be09 authored by Jelte Jansen's avatar Jelte Jansen
Browse files

Merge branch 'trac569'

parents 696cec5b 5e357877
......@@ -573,6 +573,9 @@ AC_SUBST(MULTITHREADING_FLAG)
#
CPPFLAGS="$CPPFLAGS -I\$(top_srcdir)/ext/asio"
#
# Use our 'coroutine' header from ext
CPPFLAGS="$CPPFLAGS -I\$(top_srcdir)/ext/coroutine"
#
# Disable threads: Currently we don't use them.
CPPFLAGS="$CPPFLAGS -DASIO_DISABLE_THREADS=1"
#
......@@ -666,8 +669,6 @@ AC_CONFIG_FILES([Makefile
src/lib/Makefile
src/lib/asiolink/Makefile
src/lib/asiolink/tests/Makefile
src/lib/asiolink/internal/Makefile
src/lib/asiolink/internal/tests/Makefile
src/lib/bench/Makefile
src/lib/bench/example/Makefile
src/lib/bench/tests/Makefile
......
......@@ -21,7 +21,6 @@
#include <cassert>
#include <asiolink/asiolink.h>
#include <asiolink/ioaddress.h>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
......
......@@ -243,7 +243,7 @@
/// scrubbed.
#include <config.h>
#include <asiolink/ioendpoint.h>
#include <asiolink/io_endpoint.h>
#include <dns/message.h>
#include <dns/name.h>
......
......@@ -21,8 +21,8 @@
#include <config.h>
#include <asiolink/ioendpoint.h>
#include <asiolink/ioaddress.h>
#include <asiolink/io_endpoint.h>
#include <asiolink/io_address.h>
#include <netinet/in.h>
#include <dns/name.h>
......
SUBDIRS = . tests internal
SUBDIRS = . tests
AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES)
......@@ -12,14 +12,25 @@ CLEANFILES = *.gcno *.gcda
# have some code fragments that would hit gcc's unused-parameter warning,
# which would make the build fail with -Werror (our default setting).
lib_LTLIBRARIES = libasiolink.la
libasiolink_la_SOURCES = asiolink.cc asiolink.h
libasiolink_la_SOURCES += iosocket.cc iosocket.h
libasiolink_la_SOURCES += iomessage.h
libasiolink_la_SOURCES += ioaddress.cc ioaddress.h
libasiolink_la_SOURCES += ioendpoint.cc ioendpoint.h
libasiolink_la_SOURCES += udpdns.cc internal/udpdns.h
libasiolink_la_SOURCES += tcpdns.cc internal/tcpdns.h
libasiolink_la_SOURCES += internal/coroutine.h
libasiolink_la_SOURCES = asiolink.h
libasiolink_la_SOURCES += io_service.cc io_service.h
libasiolink_la_SOURCES += dns_service.cc dns_service.h
libasiolink_la_SOURCES += dns_server.h
libasiolink_la_SOURCES += dns_lookup.h
libasiolink_la_SOURCES += dns_answer.h
libasiolink_la_SOURCES += simple_callback.h
libasiolink_la_SOURCES += interval_timer.h interval_timer.cc
libasiolink_la_SOURCES += recursive_query.h recursive_query.cc
libasiolink_la_SOURCES += io_socket.cc io_socket.h
libasiolink_la_SOURCES += io_message.h
libasiolink_la_SOURCES += io_address.cc io_address.h
libasiolink_la_SOURCES += io_endpoint.cc io_endpoint.h
libasiolink_la_SOURCES += udp_endpoint.h udp_socket.h
libasiolink_la_SOURCES += udp_server.h udp_server.cc
libasiolink_la_SOURCES += udp_query.h udp_query.cc
libasiolink_la_SOURCES += tcp_endpoint.h tcp_socket.h
libasiolink_la_SOURCES += tcp_server.h tcp_server.cc
libasiolink_la_SOURCES += coroutine.h
# Note: the ordering matters: -Wno-... must follow -Wextra (defined in
# B10_CXXFLAGS)
libasiolink_la_CXXFLAGS = $(AM_CXXFLAGS)
......
This diff is collapsed.
// Copyright (C) 2011 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 __ASIOLINK_DNS_ANSWER_H
#define __ASIOLINK_DNS_ANSWER_H 1
#include <asiolink/io_message.h>
namespace asiolink {
/// \brief The \c DNSAnswer class is an abstract base class for a DNS
/// Answer provider function.
///
/// Specific derived class implementations are hidden within the
/// implementation. Instances of the derived classes can be called
/// as functions via the operator() interface. Pointers to these
/// instances can then be provided to the \c IOService class
/// via its constructor.
///
/// A DNS Answer provider function takes answer data that has been obtained
/// from a DNS Lookup provider functon and readies it to be sent to the
/// client. After it has run, the OutputBuffer object passed to it should
/// contain the answer to the query rendered into wire format.
class DNSAnswer {
///
/// \name Constructors and Destructor
///
/// Note: The copy constructor and the assignment operator are
/// intentionally defined as private, making this class non-copyable.
//@{
private:
DNSAnswer(const DNSAnswer& source);
DNSAnswer& operator=(const DNSAnswer& 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).
DNSAnswer() {}
public:
/// \brief The destructor
virtual ~DNSAnswer() {}
//@}
/// \brief The function operator
///
/// This makes its call indirectly via the "self" pointer, ensuring
/// that the function ultimately invoked will be the one in the derived
/// class.
///
/// \param io_message The event message to handle
/// \param query_message The DNS MessagePtr of the original query
/// \param answer_message The DNS MessagePtr of the answer we are
/// building
/// \param buffer Intermediate data results are put here
virtual void operator()(const IOMessage& io_message,
isc::dns::MessagePtr query_message,
isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer) const = 0;
};
} // namespace asiolink
#endif // __ASIOLINK_DNS_ANSWER_H
// Copyright (C) 2011 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 __ASIOLINK_DNS_LOOKUP_H
#define __ASIOLINK_DNS_LOOKUP_H 1
#include <asiolink/io_message.h>
#include <asiolink/dns_server.h>
#include <dns/buffer.h>
#include <dns/message.h>
namespace asiolink {
/// \brief The \c DNSLookup class is an abstract base class for a DNS
/// Lookup provider function.
///
/// Specific derived class implementations are hidden within the
/// implementation. Instances of the derived classes can be called
/// as functions via the operator() interface. Pointers to these
/// instances can then be provided to the \c IOService class
/// via its constructor.
///
/// A DNS Lookup provider function obtains the data needed to answer
/// a DNS query (e.g., from authoritative data source, cache, or upstream
/// query). After it has run, the OutputBuffer object passed to it
/// should contain the answer to the query, in an internal representation.
class DNSLookup {
///
/// \name Constructors and Destructor
///
/// Note: The copy constructor and the assignment operator are
/// intentionally defined as private, making this class non-copyable.
//@{
private:
DNSLookup(const DNSLookup& source);
DNSLookup& operator=(const DNSLookup& 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).
DNSLookup() : self_(this) {}
public:
/// \brief The destructor
virtual ~DNSLookup() {}
//@}
/// \brief The function operator
///
/// This makes its call indirectly via the "self" pointer, ensuring
/// that the function ultimately invoked will be the one in the derived
/// class.
///
/// \param io_message The event message to handle
/// \param message The DNS MessagePtr that needs handling
/// \param buffer The final answer is put here
/// \param DNSServer DNSServer object to use
virtual void operator()(const IOMessage& io_message,
isc::dns::MessagePtr message,
isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer,
DNSServer* server) const
{
(*self_)(io_message, message, answer_message, buffer, server);
}
private:
DNSLookup* self_;
};
} // namespace asiolink
#endif // __ASIOLINK_DNS_LOOKUP_H
// Copyright (C) 2011 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 __ASIOLINK_DNS_SERVER_H
#define __ASIOLINK_DNS_SERVER_H 1
#include <asiolink/io_message.h>
namespace asiolink {
/// \brief The \c DNSServer class is a wrapper (and base class) for
/// classes which provide DNS server functionality.
///
/// The classes derived from this one, \c TCPServer and \c UDPServer,
/// act as the interface layer between clients sending queries, and
/// functions defined elsewhere that provide answers to those queries.
/// Those functions are described in more detail below under
/// \c SimpleCallback, \c DNSLookup, and \c DNSAnswer.
///
/// Notes to developers:
/// When constructed, this class (and its derived classes) will have its
/// "self_" member set to point to "this". Objects of this class (as
/// instantiated through a base class) are sometimes passed by
/// reference (as this superclass); calls to methods in the base
/// class are then rerouted via this pointer to methods in the derived
/// class. This allows code from outside asiolink, with no specific
/// knowledge of \c TCPServer or \c UDPServer, to access their methods.
///
/// This class is both assignable and copy-constructable. Its subclasses
/// use the "stackless coroutine" pattern, meaning that it will copy itself
/// when "forking", and that instances will be posted as ASIO handler
/// objects, which are always copied.
///
/// Because these objects are frequently copied, it is recommended
/// that derived classes be kept small to reduce copy overhead.
class DNSServer {
protected:
///
/// \name Constructors and destructors
///
/// This is intentionally defined as \c protected, as this base class
/// should never be instantiated except as part of a derived class.
//@{
DNSServer() : self_(this) {}
public:
/// \brief The destructor
virtual ~DNSServer() {}
//@}
///
/// \name Class methods
///
/// These methods all make their calls indirectly via the "self_"
/// pointer, ensuring that the functions ultimately invoked will be
/// the ones in the derived class. This makes it possible to pass
/// instances of derived classes as references to this base class
/// without losing access to derived class data.
///
//@{
/// \brief The funtion operator
virtual void operator()(asio::error_code ec = asio::error_code(),
size_t length = 0)
{
(*self_)(ec, length);
}
/// \brief Resume processing of the server coroutine after an
/// asynchronous call (e.g., to the DNS Lookup provider) has completed.
///
/// \param done If true, this signals the system there is an answer
/// to return.
virtual void resume(const bool done) { self_->resume(done); }
/// \brief Indicate whether the server is able to send an answer
/// to a query.
///
/// This is presently used only for testing purposes.
virtual bool hasAnswer() { return (self_->hasAnswer()); }
/// \brief Returns the current value of the 'coroutine' object
///
/// This is a temporary method, intended to be used for debugging
/// purposes during development and removed later. It allows
/// callers from outside the coroutine object to retrieve information
/// about its current state.
///
/// \return The value of the 'coroutine' object
virtual int value() { return (self_->value()); }
/// \brief Returns a pointer to a clone of this DNSServer object.
///
/// When a \c DNSServer object is copied or assigned, the result will
/// normally be another \c DNSServer object containing a copy
/// of the original "self_" pointer. Calling clone() guarantees
/// that the underlying object is also correctly copied.
///
/// \return A deep copy of this DNSServer object
virtual DNSServer* clone() { return (self_->clone()); }
//@}
protected:
/// \brief Lookup handler object.
///
/// This is a protected class; it can only be instantiated
/// from within a derived class of \c DNSServer.
///
/// A server object that has received a query creates an instance
/// of this class and scheudles it on the ASIO service queue
/// using asio::io_service::post(). When the handler executes, it
/// calls the asyncLookup() method in the server object to start a
/// DNS lookup. When the lookup is complete, the server object is
/// scheduled to resume, again using io_service::post().
///
/// Note that the calling object is copied into the handler object,
/// not referenced. This is because, once the calling object yields
/// control to the handler, it falls out of scope and may disappear
template <typename T>
class AsyncLookup {
public:
AsyncLookup(T& caller) : caller_(caller) {}
void operator()() { caller_.asyncLookup(); }
private:
T caller_;
};
/// \brief Carries out a DNS lookup.
///
/// This function calls the \c DNSLookup object specified by the
/// DNS server when the \c IOService was created, passing along
/// the details of the query and a pointer back to the current
/// server object. It is called asynchronously via the AsyncLookup
/// handler class.
virtual void asyncLookup() { self_->asyncLookup(); }
private:
DNSServer* self_;
};
} // asiolink
#endif // __ASIOLINK_DNS_SERVER_H
// Copyright (C) 2011 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.
#include <config.h>
#include <asiolink/io_service.h>
#include <asio/ip/address.hpp>
#include <asio.hpp>
#include <asiolink/tcp_server.h>
#include <asiolink/udp_server.h>
#include <log/dummylog.h>
#include <boost/lexical_cast.hpp>
using isc::log::dlog;
namespace asiolink {
class SimpleCallback;
class DNSLookup;
class DNSAnswer;
namespace {
asio::ip::address
convertAddr(const std::string& address) {
asio::error_code err;
asio::ip::address addr = asio::ip::address::from_string(address, err);
if (err) {
isc_throw(IOError, "Invalid IP address '" << &address << "': "
<< err.message());
}
return (addr);
}
}
class DNSServiceImpl {
public:
DNSServiceImpl(IOService& io_service, const char& port,
const asio::ip::address* v4addr,
const asio::ip::address* v6addr,
SimpleCallback* checkin, DNSLookup* lookup,
DNSAnswer* answer);
IOService& io_service_;
typedef boost::shared_ptr<UDPServer> UDPServerPtr;
typedef boost::shared_ptr<TCPServer> TCPServerPtr;
typedef boost::shared_ptr<DNSServer> DNSServerPtr;
std::vector<DNSServerPtr> servers_;
SimpleCallback *checkin_;
DNSLookup *lookup_;
DNSAnswer *answer_;
void addServer(uint16_t port, const asio::ip::address& address) {
try {
dlog(std::string("Initialize TCP server at ") + address.to_string() + ":" + boost::lexical_cast<std::string>(port));
TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(),
address, port, checkin_, lookup_, answer_));
(*tcpServer)();
servers_.push_back(tcpServer);
dlog(std::string("Initialize UDP server at ") + address.to_string() + ":" + boost::lexical_cast<std::string>(port));
UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(),
address, port, checkin_, lookup_, answer_));
(*udpServer)();
servers_.push_back(udpServer);
}
catch (const asio::system_error& err) {
// We need to catch and convert any ASIO level exceptions.
// This can happen for unavailable address, binding a privilege port
// without the privilege, etc.
isc_throw(IOError, "Failed to initialize network servers: " <<
err.what());
}
}
void addServer(const char& port, const asio::ip::address& address) {
uint16_t portnum;
try {
// XXX: SunStudio with stlport4 doesn't reject some invalid
// representation such as "-1" by lexical_cast<uint16_t>, so
// we convert it into a signed integer of a larger size and perform
// range check ourselves.
const int32_t portnum32 = boost::lexical_cast<int32_t>(&port);
if (portnum32 < 0 || portnum32 > 65535) {
isc_throw(IOError, "Invalid port number '" << &port);
}
portnum = portnum32;
} catch (const boost::bad_lexical_cast& ex) {
isc_throw(IOError, "Invalid port number '" << &port << "': " <<
ex.what());
}
addServer(portnum, address);
}
};
DNSServiceImpl::DNSServiceImpl(IOService& io_service,
const char& port,
const asio::ip::address* const v4addr,
const asio::ip::address* const v6addr,
SimpleCallback* checkin,
DNSLookup* lookup,
DNSAnswer* answer) :
io_service_(io_service),
checkin_(checkin),
lookup_(lookup),
answer_(answer)
{
if (v4addr) {
addServer(port, *v4addr);
}
if (v6addr) {
addServer(port, *v6addr);
}
}
DNSService::DNSService(IOService& io_service,
const char& port, const char& address,
SimpleCallback* checkin,
DNSLookup* lookup,
DNSAnswer* answer) :
impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup,
answer)), io_service_(io_service)
{
addServer(port, &address);
}
DNSService::DNSService(IOService& io_service,
const char& port,
const bool use_ipv4, const bool use_ipv6,
SimpleCallback* checkin,
DNSLookup* lookup,
DNSAnswer* answer) :
impl_(NULL), io_service_(io_service)
{
const asio::ip::address v4addr_any =
asio::ip::address(asio::ip::address_v4::any());
const asio::ip::address* const v4addrp = use_ipv4 ? &v4addr_any : NULL;
const asio::ip::address v6addr_any =
asio::ip::address(asio::ip::address_v6::any());
const asio::ip::address* const v6addrp = use_ipv6 ? &v6addr_any : NULL;
impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin, lookup, answer);
}
DNSService::DNSService(IOService& io_service, SimpleCallback* checkin,
DNSLookup* lookup, DNSAnswer *answer) :
impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup,
answer)), io_service_(io_service)
{
}
DNSService::~DNSService() {
delete impl_;
}
void
DNSService::addServer(const char& port, const std::string& address) {
impl_->addServer(port, convertAddr(address));
}
void
DNSService::addServer(uint16_t port, const std::string& address) {
impl_->addServer(port, convertAddr(address));
}
void
DNSService::clearServers() {
// FIXME: This does not work, it does not close the socket.
// How is it done?
impl_->servers_.clear();
}
} // namespace asiolink
// Copyright (C) 2011 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 __ASIOLINK_DNS_SERVICE_H
#define __ASIOLINK_DNS_SERVICE_H 1
#include <resolve/resolver_interface.h>
#include <asiolink/io_service.h>
namespace asiolink {
class SimpleCallback;
class DNSLookup;