sync_udp_server.cc 6.73 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
//
// 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 <asio.hpp>
#include <asio/error.hpp>
19
20
21
22

#include "sync_udp_server.h"
#include "logger.h"

23
24
25
26
#include <asiolink/dummy_io_cb.h>
#include <asiolink/udp_endpoint.h>
#include <asiolink/udp_socket.h>

27
#include <boost/bind.hpp>
28

29
#include <sys/types.h>
30
31
32
33
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>             // for some IPC/network system calls
#include <errno.h>
34
35
36
37
38
39
40

using namespace std;
using namespace isc::asiolink;

namespace isc {
namespace asiodns {

41
42
43
SyncUDPServer::SyncUDPServer(asio::io_service& io_service, const int fd,
                             const int af, asiolink::SimpleCallback* checkin,
                             DNSLookup* lookup, DNSAnswer* answer) :
44
45
46
    output_buffer_(new isc::util::OutputBuffer(0)),
    query_(new isc::dns::Message(isc::dns::Message::PARSE)),
    answer_(new isc::dns::Message(isc::dns::Message::RENDER)),
47
    checkin_callback_(checkin), lookup_callback_(lookup),
48
    answer_callback_(answer), stopped_(false)
49
50
51
52
{
    if (af != AF_INET && af != AF_INET6) {
        isc_throw(InvalidParameter, "Address family must be either AF_INET "
                  "or AF_INET6, not " << af);
53
    }
54
55
56
57
58
59
60
61
62
63
64
    LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_FD_ADD_UDP).arg(fd);
    try {
        socket_.reset(new asio::ip::udp::socket(io_service));
        socket_->assign(af == AF_INET6 ? asio::ip::udp::v6() :
                        asio::ip::udp::v4(), fd);
    } catch (const std::exception& exception) {
        // Whatever the thing throws, it is something from ASIO and we
        // convert it
        isc_throw(IOError, exception.what());
    }
}
65
66

void
67
68
69
70
71
SyncUDPServer::scheduleRead() {
    socket_->async_receive_from(asio::buffer(data_, MAX_LENGTH), sender_,
                                boost::bind(&SyncUDPServer::handleRead, this,
                                            _1, _2));
}
72

73
74
75
76
77
78
79
80
void
SyncUDPServer::handleRead(const asio::error_code& ec, const size_t length) {
    // Abort on fatal errors
    if (ec) {
        using namespace asio::error;
        if (ec.value() != would_block && ec.value() != try_again &&
            ec.value() != interrupted) {
            return;
81
        }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    }
    // Some kind of interrupt, spurious wakeup, or like that. Just try reading
    // again.
    if (ec || length == 0) {
        scheduleRead();
        return;
    }
    // OK, we have a real packet of data. Let's dig into it!

    // XXX: This is taken (and ported) from UDPSocket class. What the hell does
    // it really mean?

    // The UDP socket class has been extended with asynchronous functions
    // and takes as a template parameter a completion callback class.  As
    // UDPServer does not use these extended functions (only those defined
    // in the IOSocket base class) - but needs a UDPSocket to get hold of
    // the underlying Boost UDP socket - DummyIOCallback is used.  This
    // provides the appropriate operator() but is otherwise functionless.
    UDPSocket<DummyIOCallback> socket(*socket_);
    UDPEndpoint endpoint(sender_);
    IOMessage message(data_, length, socket, endpoint);
    if (checkin_callback_ != NULL) {
        (*checkin_callback_)(message);
105
106
107
        if (stopped_) {
            return;
        }
108
    }
109

110
111
112
113
114
115
    // If we don't have a DNS Lookup provider, there's no point in
    // continuing; we exit the coroutine permanently.
    if (lookup_callback_ == NULL) {
        scheduleRead();
        return;
    }
116

117
118
119
120
121
122
    // Make sure the buffers are fresh.  Note that we don't touch query_
    // because it's supposed to be cleared in lookup_callback_.  We should
    // eventually even remove this member variable (and remove it from
    // the lookup_callback_ interface, but until then, any callback
    // implementation should be careful that it's the responsibility of
    // the callback implementation.  See also #2239).
123
124
    output_buffer_->clear();
    answer_->clear(isc::dns::Message::RENDER);
125

126
127
128
    // Mark that we don't have an answer yet.
    done_ = false;
    resume_called_ = false;
129

130
    // Call the actual lookup
131
    (*lookup_callback_)(message, query_, answer_, output_buffer_, this);
132

133
134
135
136
137
    if (!resume_called_) {
        isc_throw(isc::Unexpected,
                  "No resume called from the lookup callback");
    }

138
139
140
141
    if (stopped_) {
        return;
    }

142
143
144
    if (done_) {
        // Good, there's an answer.
        // Call the answer callback to render it.
145
        (*answer_callback_)(message, query_, answer_, output_buffer_);
146

147
148
149
150
        if (stopped_) {
            return;
        }

151
        asio::error_code ec;
152
153
        socket_->send_to(asio::buffer(output_buffer_->getData(),
                                      output_buffer_->getLength()),
154
155
156
157
158
159
                         sender_, 0, ec);
        if (ec) {
            LOG_ERROR(logger, ASIODNS_UDP_SYNC_SEND_FAIL).
                      arg(sender_.address().to_string()).
                      arg(ec.message());
        }
160
    }
161
162
163

    // And schedule handling another socket.
    scheduleRead();
164
165
166
}

void
167
168
169
170
SyncUDPServer::operator()(asio::error_code, size_t) {
    // To start the server, we just schedule reading of data when they
    // arrive.
    scheduleRead();
171
172
173
174
175
176
177
178
179
180
181
182
183
}

/// Stop the UDPServer
void
SyncUDPServer::stop() {
    /// Using close instead of cancel, because cancel
    /// will only cancel the asynchornized event already submitted
    /// to io service, the events post to io service after
    /// cancel still can be scheduled by io service, if
    /// the socket is cloesed, all the asynchronized event
    /// for it won't be scheduled by io service not matter it is
    /// submit to io serice before or after close call. And we will
    //. get bad_descriptor error
184
    socket_->close();
185
    stopped_ = true;
186
187
188
189
190
191
192
}

/// Post this coroutine on the ASIO service queue so that it will
/// resume processing where it left off.  The 'done' parameter indicates
/// whether there is an answer to return to the client.
void
SyncUDPServer::resume(const bool done) {
193
194
    resume_called_ = true;
    done_ = done;
195
196
197
198
}

bool
SyncUDPServer::hasAnswer() {
199
    return (done_);
200
201
202
203
}

} // namespace asiodns
} // namespace isc