sync_udp_server.h 6.35 KB
Newer Older
1
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// 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 __SYNC_UDP_SERVER_H
#define __SYNC_UDP_SERVER_H 1

#ifndef ASIO_HPP
#error "asio.hpp must be included before including this, see asiolink.h as to why"
#endif

22
23
24
25
#include "dns_answer.h"
#include "dns_lookup.h"
#include "dns_server.h"

26
#include <dns/message.h>
27
#include <asiolink/simple_callback.h>
28
#include <util/buffer.h>
29
#include <exceptions.h>
30

31
#include <boost/noncopyable.hpp>
32
33
34
35

namespace isc {
namespace asiodns {

36
/// \brief An UDP server that doesn't asynchronous lookup handlers.
37
///
38
39
40
41
/// That means, the lookup handler must provide the answer right away.
/// This allows for implementation with less overhead, compared with
/// the UDPClass.
class SyncUDPServer : public DNSServer, public boost::noncopyable {
42
43
44
45
46
47
48
49
50
public:
    /// \brief Constructor
    /// \param io_service the asio::io_service to work with
    /// \param addr the IP address to listen for queries on
    /// \param port the port to listen for queries on
    /// \param checkin the callbackprovider for non-DNS events
    /// \param lookup the callbackprovider for DNS lookup events
    /// \param answer the callbackprovider for DNS answer events
    explicit SyncUDPServer(asio::io_service& io_service,
51
52
53
54
                           const asio::ip::address& addr, const uint16_t port,
                           isc::asiolink::SimpleCallback* checkin = NULL,
                           DNSLookup* lookup = NULL,
                           DNSAnswer* answer = NULL);
55
56
57
58
59
60
61
62
63
64
65

    /// \brief Constructor
    /// \param io_service the asio::io_service to work with
    /// \param fd the file descriptor of opened UDP socket
    /// \param af address family, either AF_INET or AF_INET6
    /// \param checkin the callbackprovider for non-DNS events
    /// \param lookup the callbackprovider for DNS lookup events
    /// \param answer the callbackprovider for DNS answer events
    /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6
    /// \throw isc::asiolink::IOError when a low-level error happens, like the
    ///     fd is not a valid descriptor.
66
67
68
    SyncUDPServer(asio::io_service& io_service, const int fd, const int af,
                  isc::asiolink::SimpleCallback* checkin = NULL,
                  DNSLookup* lookup = NULL, DNSAnswer* answer = NULL);
69

70
71
72
73
74
    /// \brief Start the SyncUDPServer.
    ///
    /// This is the function operator to keep interface with other server
    /// classes. They need that because they're coroutines.
    virtual void operator()(asio::error_code ec = asio::error_code(),
75
76
77
                    size_t length = 0);

    /// \brief Calls the lookup callback
78
79
80
81
82
    virtual void asyncLookup() {
        isc_throw(Unexpected,
                  "SyncUDPServer doesn't support asyncLookup by design, use "
                  "UDPServer if you need it.");
    }
83
84
85

    /// \brief Stop the running server
    /// \note once the server stopped, it can't restart
86
    virtual void stop();
87
88
89

    /// \brief Resume operation
    ///
90
91
92
93
94
95
    /// Note that unlike other servers, this one expects it to be called
    /// directly from the lookup callback. If it isn't, the server will
    /// throw an Unexpected exception (probably to the event loop, which
    /// would usually lead to termination of the program, but that's OK,
    /// as it would be serious programmer error).
    ///
96
97
    /// \param done Set this to true if the lookup action is done and
    ///        we have an answer
98
    virtual void resume(const bool done);
99
100
101
102

    /// \brief Check if we have an answer
    ///
    /// \return true if we have an answer
103
    virtual bool hasAnswer();
104
105
106

    /// \brief Clones the object
    ///
107
108
109
110
111
    /// Since cloning is for the use of coroutines, the synchronous UDP server
    /// does not need to be cloned. Therefore supporting it would be needless
    /// work, and trying to clone it would be a programmer error anyway, this
    /// throws Unexpected.
    ///
112
    /// \return a newly allocated copy of this object
113
114
    virtual DNSServer* clone() {
        isc_throw(Unexpected, "SyncUDPServer can't be cloned.");
115
116
    }
private:
117
118
119
120
    // Internal state & buffers. We don't use the PIMPL idiom, as this class
    // isn't usually used directly anyway.

    // Maximum size of incoming UDP packet
121
    static const size_t MAX_LENGTH = 4096;
122
    // Buffer for incoming data
123
    uint8_t data_[MAX_LENGTH];
124
125
126
    // The buffer to render the output to and send it.
    // If it was OK to have just a buffer, not the wrapper class,
    // we could reuse the data_
127
    isc::util::OutputBufferPtr output_buffer_;
128
    // Objects to hold the query message and the answer
129
    isc::dns::MessagePtr query_, answer_;
130
    // The socket used for the communication
131
    std::auto_ptr<asio::ip::udp::socket> socket_;
132
    // The event loop we use
133
    asio::io_service& io_;
134
    // Place the socket puts the sender of a packet when it is received
135
    asio::ip::udp::endpoint sender_;
136
    // Callbacks
137
138
139
    const asiolink::SimpleCallback* checkin_callback_;
    const DNSLookup* lookup_callback_;
    const DNSAnswer* answer_callback_;
140
141
142
143
144
145
146
147
    // Answers from the lookup callback (not sent directly, but signalled
    // through resume()
    bool resume_called_, done_;
    // This turns true when the server stops. Allows for not sending the
    // answer after we closed the socket.
    bool stopped_;

    // Auxiliary functions
148

149
150
    // Schedule next read on the socket. Just a wrapper around
    // socket_->async_read_from with the correct parameters.
151
    void scheduleRead();
152
153
    // Callback from the socket's read call (called when there's an error or
    // when a new packet comes).
154
    void handleRead(const asio::error_code& ec, const size_t length);
155
156
157
158
159
};

} // namespace asiodns
} // namespace isc
#endif // __SYNC_UDP_SERVER_H