Kea 2.6.2
ncr_udp.h
Go to the documentation of this file.
1// Copyright (C) 2013-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef NCR_UDP_LISTENER_H
8#define NCR_UDP_LISTENER_H
9
101
103#include <asiolink/io_address.h>
104#include <asiolink/io_service.h>
106#include <asiolink/udp_socket.h>
107#include <dhcp_ddns/ncr_io.h>
108#include <util/buffer.h>
109#include <util/watch_socket.h>
110
111#include <boost/shared_array.hpp>
112#include <boost/enable_shared_from_this.hpp>
113
118namespace isc {
119namespace dhcp_ddns {
120
123public:
124 NcrUDPError(const char* file, size_t line, const char* what) :
125 isc::Exception(file, line, what) { };
126};
127
128class UDPCallback;
130typedef std::function<void(const bool, const UDPCallback*)> UDPCompletionHandler;
131
133typedef boost::shared_array<uint8_t> RawBufferPtr;
134
135typedef boost::shared_ptr<asiolink::UDPEndpoint> UDPEndpointPtr;
136
147
148public:
156 struct Data {
157
165 Data(RawBufferPtr& buffer, const size_t buf_size, UDPEndpointPtr& data_source)
166 : buffer_(buffer), buf_size_(buf_size), data_source_(data_source),
168 };
169
172
174 size_t buf_size_;
175
178
181 size_t put_len_;
182
184 boost::system::error_code error_code_;
185
192
193 };
194
207 UDPCallback(RawBufferPtr& buffer, const size_t buf_size,
208 UDPEndpointPtr& data_source,
209 const UDPCompletionHandler& handler);
210
220 void operator ()(const boost::system::error_code error_code,
221 const size_t bytes_transferred);
222
229 size_t getBytesTransferred() const {
230 return (data_->bytes_transferred_);
231 }
232
236 void setBytesTransferred(const size_t value) {
237 data_->bytes_transferred_ = value;
238 }
239
241 boost::system::error_code getErrorCode() const {
242 return (data_->error_code_);
243 }
244
248 void setErrorCode(const boost::system::error_code value) {
249 data_->error_code_ = value;
250 }
251
254 return (data_->buffer_);
255 }
256
258 size_t getBufferSize() const {
259 return (data_->buf_size_);
260 }
261
263 const uint8_t* getData() const {
264 return (data_->buffer_.get());
265 }
266
280 void putData(const uint8_t* src, size_t len);
281
284 size_t getPutLen() const {
285 return (data_->put_len_);
286 }
287
292 data_->data_source_ = endpoint;
293 }
294
297 return (data_->data_source_);
298 }
299
300 private:
302 UDPCompletionHandler handler_;
303
305 boost::shared_ptr<Data> data_;
306};
307
310
318public:
321
335 const uint32_t port,
336 const NameChangeFormat format,
337 RequestReceiveHandlerPtr ncr_recv_handler,
338 const bool reuse_address = false);
339
341 virtual ~NameChangeUDPListener();
342
351 virtual void open(const isc::asiolink::IOServicePtr& io_service);
352
360 virtual void close();
361
369 void doReceive();
370
395 void receiveCompletionHandler(const bool successful,
396 const UDPCallback* recv_callback);
397
398private:
399
401 isc::asiolink::IOServicePtr io_service_;
402
404 isc::asiolink::IOAddress ip_address_;
405
407 uint32_t port_;
408
410 NameChangeFormat format_;
411
413 boost::shared_ptr<boost::asio::ip::udp::socket> asio_socket_;
414
416 boost::shared_ptr<NameChangeUDPSocket> socket_;
417
419 boost::shared_ptr<UDPCallback> recv_callback_;
420
422 bool reuse_address_;
423
430private:
432 NameChangeUDPListener& operator=(const NameChangeUDPListener& source);
434};
435
443public:
444
447
464 const uint32_t port, const isc::asiolink::IOAddress& server_address,
465 const uint32_t server_port, const NameChangeFormat format,
466 RequestSendHandlerPtr ncr_send_handler,
467 const size_t send_que_max = NameChangeSender::MAX_QUEUE_DEFAULT,
468 const bool reuse_address = false);
469
471 virtual ~NameChangeUDPSender();
472
481 virtual void open(const isc::asiolink::IOServicePtr& io_service);
482
490 virtual void close();
491
500 virtual void doSend(NameChangeRequestPtr& ncr);
501
519 void sendCompletionHandler(const bool successful,
520 const UDPCallback* send_callback);
521
535 virtual int getSelectFd();
536
540 virtual bool ioReady();
541
542private:
543
548 void closeWatchSocket();
549
551 isc::asiolink::IOAddress ip_address_;
552
554 uint32_t port_;
555
557 isc::asiolink::IOAddress server_address_;
558
560 uint32_t server_port_;
561
563 NameChangeFormat format_;
564
566 boost::shared_ptr<boost::asio::ip::udp::socket> asio_socket_;
567
569 boost::shared_ptr<NameChangeUDPSocket> socket_;
570
572 boost::shared_ptr<isc::asiolink::UDPEndpoint> server_endpoint_;
573
575 boost::shared_ptr<UDPCallback> send_callback_;
576
578 bool reuse_address_;
579
581 util::WatchSocketPtr watch_socket_;
582};
583
584} // namespace isc::dhcp_ddns
585} // namespace isc
586
587#endif
then both cases could be regarded as being with the completion being signalled by the posting of the completion event However UDP is the most common case and that would involve extra overhead So the open() returns a status indicating whether the operation completed asynchronously. If it did
This is a base class for exceptions thrown from the DNS library module.
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
boost::shared_ptr< RequestReceiveHandler > RequestReceiveHandlerPtr
Defines a smart pointer to an instance of a request receive handler.
Definition ncr_io.h:206
NameChangeListener(RequestReceiveHandlerPtr recv_handler)
Constructor.
Definition ncr_io.cc:53
NameChangeSender(RequestSendHandlerPtr send_handler, size_t send_queue_max=MAX_QUEUE_DEFAULT)
Constructor.
Definition ncr_io.cc:157
boost::shared_ptr< RequestSendHandler > RequestSendHandlerPtr
Defines a smart pointer to an instance of a request send handler.
Definition ncr_io.h:512
static const size_t MAX_QUEUE_DEFAULT
Defines a default maximum number of entries in the send queue.
Definition ncr_io.h:475
NameChangeUDPListener(const isc::asiolink::IOAddress &ip_address, const uint32_t port, const NameChangeFormat format, RequestReceiveHandlerPtr ncr_recv_handler, const bool reuse_address=false)
Constructor.
Definition ncr_udp.cc:66
virtual void close()
Closes the UDPSocket.
Definition ncr_udp.cc:129
virtual ~NameChangeUDPListener()
Destructor.
Definition ncr_udp.cc:82
static const size_t RECV_BUF_MAX
Defines the maximum size packet that can be received.
Definition ncr_udp.h:320
void receiveCompletionHandler(const bool successful, const UDPCallback *recv_callback)
Implements the NameChangeRequest level receive completion handler.
Definition ncr_udp.cc:159
void doReceive()
Initiates an asynchronous read on the socket.
Definition ncr_udp.cc:120
void sendCompletionHandler(const bool successful, const UDPCallback *send_callback)
Implements the NameChangeRequest level send completion handler.
Definition ncr_udp.cc:328
virtual bool ioReady()
Returns whether or not the sender has IO ready to process.
Definition ncr_udp.cc:379
static const size_t SEND_BUF_MAX
Defines the maximum size packet that can be sent.
Definition ncr_udp.h:446
virtual int getSelectFd()
Returns a file descriptor suitable for use with select.
Definition ncr_udp.cc:369
virtual void close()
Closes the UDPSocket.
Definition ncr_udp.cc:274
virtual void doSend(NameChangeRequestPtr &ncr)
Sends a given request asynchronously over the socket.
Definition ncr_udp.cc:306
NameChangeUDPSender(const isc::asiolink::IOAddress &ip_address, const uint32_t port, const isc::asiolink::IOAddress &server_address, const uint32_t server_port, const NameChangeFormat format, RequestSendHandlerPtr ncr_send_handler, const size_t send_que_max=NameChangeSender::MAX_QUEUE_DEFAULT, const bool reuse_address=false)
Constructor.
Definition ncr_udp.cc:208
virtual ~NameChangeUDPSender()
Destructor.
Definition ncr_udp.cc:228
NcrUDPError(const char *file, size_t line, const char *what)
Definition ncr_udp.h:124
Implements the callback class passed into UDPSocket calls.
Definition ncr_udp.h:146
size_t getBytesTransferred() const
Returns the number of bytes transferred by the completed IO service.
Definition ncr_udp.h:229
void setDataSource(UDPEndpointPtr &endpoint)
Sets the data source to the given endpoint.
Definition ncr_udp.h:291
void setErrorCode(const boost::system::error_code value)
Sets the completed IO layer service outcome status.
Definition ncr_udp.h:248
size_t getBufferSize() const
Returns the data transfer buffer capacity.
Definition ncr_udp.h:258
RawBufferPtr getBuffer() const
Returns the data transfer buffer.
Definition ncr_udp.h:253
const uint8_t * getData() const
Returns a pointer the data transfer buffer content.
Definition ncr_udp.h:263
void putData(const uint8_t *src, size_t len)
Copies data into the data transfer buffer.
Definition ncr_udp.cc:51
UDPCallback(RawBufferPtr &buffer, const size_t buf_size, UDPEndpointPtr &data_source, const UDPCompletionHandler &handler)
Used as the callback object for UDPSocket services.
Definition ncr_udp.cc:21
void setBytesTransferred(const size_t value)
Sets the number of bytes transferred.
Definition ncr_udp.h:236
const UDPEndpointPtr & getDataSource()
Returns the UDP endpoint that provided the transferred data.
Definition ncr_udp.h:296
size_t getPutLen() const
Returns the number of bytes manually written into the transfer buffer.
Definition ncr_udp.h:284
void operator()(const boost::system::error_code error_code, const size_t bytes_transferred)
Operator that will be invoked by the asiolink layer.
Definition ncr_udp.cc:35
boost::system::error_code getErrorCode() const
Returns the completed IO layer service outcome status.
Definition ncr_udp.h:241
NameChangeFormat
Defines the list of data wire formats supported.
Definition ncr_msg.h:59
std::function< void(const bool, const UDPCallback *)> UDPCompletionHandler
Defines a function pointer for NameChangeRequest completion handlers.
Definition ncr_udp.h:130
isc::asiolink::UDPSocket< UDPCallback > NameChangeUDPSocket
Convenience type for UDP socket based listener.
Definition ncr_udp.h:309
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition ncr_msg.h:241
boost::shared_array< uint8_t > RawBufferPtr
Defines a dynamically allocated shared array.
Definition ncr_udp.h:133
boost::shared_ptr< asiolink::UDPEndpoint > UDPEndpointPtr
Definition ncr_udp.h:135
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Defines the logger used by the top-level component of kea-lfc.
This file defines abstract classes for exchanging NameChangeRequests.
boost::system::error_code error_code_
Stores the IO layer result code of the completed IO service.
Definition ncr_udp.h:184
size_t put_len_
Stores this size of the data within the buffer when written there manually.
Definition ncr_udp.h:181
RawBufferPtr buffer_
A pointer to the data transfer buffer.
Definition ncr_udp.h:171
size_t buf_size_
Storage capacity of the buffer.
Definition ncr_udp.h:174
Data(RawBufferPtr &buffer, const size_t buf_size, UDPEndpointPtr &data_source)
Constructor.
Definition ncr_udp.h:165
UDPEndpointPtr data_source_
The UDP endpoint that is the origin of the data transferred.
Definition ncr_udp.h:177
size_t bytes_transferred_
Stores the number of bytes transferred by completed IO service.
Definition ncr_udp.h:191
Defines the class, WatchSocket.