Kea 2.5.4
ncr_udp.h
Go to the documentation of this file.
1// Copyright (C) 2013-2020 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
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*)>
132
134typedef boost::shared_array<uint8_t> RawBufferPtr;
135
136typedef boost::shared_ptr<asiolink::UDPEndpoint> UDPEndpointPtr;
137
148
149public:
157 struct Data {
158
166 Data(RawBufferPtr& buffer, const size_t buf_size,
167 UDPEndpointPtr& data_source)
168 : buffer_(buffer), buf_size_(buf_size), data_source_(data_source),
170 };
171
174
176 size_t buf_size_;
177
180
183 size_t put_len_;
184
186 boost::system::error_code error_code_;
187
194
195 };
196
209 UDPCallback (RawBufferPtr& buffer, const size_t buf_size,
210 UDPEndpointPtr& data_source,
211 const UDPCompletionHandler& handler);
212
222 void operator ()(const boost::system::error_code error_code,
223 const size_t bytes_transferred);
224
231 size_t getBytesTransferred() const {
232 return (data_->bytes_transferred_);
233 }
234
238 void setBytesTransferred(const size_t value) {
239 data_->bytes_transferred_ = value;
240 }
241
243 boost::system::error_code getErrorCode() const {
244 return (data_->error_code_);
245 }
246
250 void setErrorCode(const boost::system::error_code value) {
251 data_->error_code_ = value;
252 }
253
256 return (data_->buffer_);
257 }
258
260 size_t getBufferSize() const {
261 return (data_->buf_size_);
262 }
263
265 const uint8_t* getData() const {
266 return (data_->buffer_.get());
267 }
268
282 void putData(const uint8_t* src, size_t len);
283
286 size_t getPutLen() const {
287 return (data_->put_len_);
288 }
289
294 data_->data_source_ = endpoint;
295 }
296
299 return (data_->data_source_);
300 }
301
302 private:
304 UDPCompletionHandler handler_;
305
307 boost::shared_ptr<Data> data_;
308};
309
312
320public:
322 static const size_t RECV_BUF_MAX = isc::asiolink::
324
338 const uint32_t port,
340 RequestReceiveHandler& ncr_recv_handler,
341 const bool reuse_address = false);
342
344 virtual ~NameChangeUDPListener();
345
354 virtual void open(isc::asiolink::IOService& io_service);
355
363 virtual void close();
364
372 void doReceive();
373
398 void receiveCompletionHandler(const bool successful,
399 const UDPCallback* recv_callback);
400private:
402 isc::asiolink::IOAddress ip_address_;
403
405 uint32_t port_;
406
408 NameChangeFormat format_;
409
411 boost::shared_ptr<boost::asio::ip::udp::socket> asio_socket_;
412
414 boost::shared_ptr<NameChangeUDPSocket> socket_;
415
417 boost::shared_ptr<UDPCallback> recv_callback_;
418
420 bool reuse_address_;
421
428private:
430 NameChangeUDPListener& operator=(const NameChangeUDPListener& source);
432};
433
434
442public:
443
446
463 const uint32_t port, const isc::asiolink::IOAddress& server_address,
464 const uint32_t server_port, const NameChangeFormat format,
465 RequestSendHandler& ncr_send_handler,
466 const size_t send_que_max = NameChangeSender::MAX_QUEUE_DEFAULT,
467 const bool reuse_address = false);
468
470 virtual ~NameChangeUDPSender();
471
472
481 virtual void open(isc::asiolink::IOService& io_service);
482
483
491 virtual void close();
492
501 virtual void doSend(NameChangeRequestPtr& ncr);
502
520 void sendCompletionHandler(const bool successful,
521 const UDPCallback* send_callback);
522
536 virtual int getSelectFd();
537
541 virtual bool ioReady();
542
543private:
544
549 void closeWatchSocket();
550
552 isc::asiolink::IOAddress ip_address_;
553
555 uint32_t port_;
556
558 isc::asiolink::IOAddress server_address_;
559
561 uint32_t server_port_;
562
564 NameChangeFormat format_;
565
567 boost::shared_ptr<boost::asio::ip::udp::socket> asio_socket_;
568
570 boost::shared_ptr<NameChangeUDPSocket> socket_;
571
573 boost::shared_ptr<isc::asiolink::UDPEndpoint> server_endpoint_;
574
576 boost::shared_ptr<UDPCallback> send_callback_;
577
579 bool reuse_address_;
580
582 util::WatchSocketPtr watch_socket_;
583};
584
585} // namespace isc::dhcp_ddns
586} // namespace isc
587
588#endif
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Abstract class for defining application layer receive callbacks.
Definition: ncr_io.h:183
Abstract interface for receiving NameChangeRequests.
Definition: ncr_io.h:167
Abstract class for defining application layer send callbacks.
Definition: ncr_io.h:488
Abstract interface for sending NameChangeRequests.
Definition: ncr_io.h:466
static const size_t MAX_QUEUE_DEFAULT
Defines a default maximum number of entries in the send queue.
Definition: ncr_io.h:473
Provides the ability to receive NameChangeRequests via UDP socket.
Definition: ncr_udp.h:319
virtual void close()
Closes the UDPSocket.
Definition: ncr_udp.cc:127
virtual ~NameChangeUDPListener()
Destructor.
Definition: ncr_udp.cc:83
static const size_t RECV_BUF_MAX
Defines the maximum size packet that can be received.
Definition: ncr_udp.h:322
virtual void open(isc::asiolink::IOService &io_service)
Opens a UDP socket using the given IOService.
Definition: ncr_udp.cc:89
void receiveCompletionHandler(const bool successful, const UDPCallback *recv_callback)
Implements the NameChangeRequest level receive completion handler.
Definition: ncr_udp.cc:153
void doReceive()
Initiates an asynchronous read on the socket.
Definition: ncr_udp.cc:118
NameChangeUDPListener(const isc::asiolink::IOAddress &ip_address, const uint32_t port, const NameChangeFormat format, RequestReceiveHandler &ncr_recv_handler, const bool reuse_address=false)
Constructor.
Definition: ncr_udp.cc:67
Provides the ability to send NameChangeRequests via UDP socket.
Definition: ncr_udp.h:441
void sendCompletionHandler(const bool successful, const UDPCallback *send_callback)
Implements the NameChangeRequest level send completion handler.
Definition: ncr_udp.cc:317
virtual void open(isc::asiolink::IOService &io_service)
Opens a UDP socket using the given IOService.
Definition: ncr_udp.cc:229
virtual bool ioReady()
Returns whether or not the sender has IO ready to process.
Definition: ncr_udp.cc:365
static const size_t SEND_BUF_MAX
Defines the maximum size packet that can be sent.
Definition: ncr_udp.h:445
virtual int getSelectFd()
Returns a file descriptor suitable for use with select.
Definition: ncr_udp.cc:355
virtual void close()
Closes the UDPSocket.
Definition: ncr_udp.cc:265
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, RequestSendHandler &ncr_send_handler, const size_t send_que_max=NameChangeSender::MAX_QUEUE_DEFAULT, const bool reuse_address=false)
Constructor.
Definition: ncr_udp.cc:203
virtual void doSend(NameChangeRequestPtr &ncr)
Sends a given request asynchronously over the socket.
Definition: ncr_udp.cc:294
virtual ~NameChangeUDPSender()
Destructor.
Definition: ncr_udp.cc:223
Thrown when a UDP level exception occurs.
Definition: ncr_udp.h:122
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:147
size_t getBytesTransferred() const
Returns the number of bytes transferred by the completed IO service.
Definition: ncr_udp.h:231
void setDataSource(UDPEndpointPtr &endpoint)
Sets the data source to the given endpoint.
Definition: ncr_udp.h:293
void setErrorCode(const boost::system::error_code value)
Sets the completed IO layer service outcome status.
Definition: ncr_udp.h:250
size_t getBufferSize() const
Returns the data transfer buffer capacity.
Definition: ncr_udp.h:260
RawBufferPtr getBuffer() const
Returns the data transfer buffer.
Definition: ncr_udp.h:255
const uint8_t * getData() const
Returns a pointer the data transfer buffer content.
Definition: ncr_udp.h:265
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:238
const UDPEndpointPtr & getDataSource()
Returns the UDP endpoint that provided the transferred data.
Definition: ncr_udp.h:298
size_t getPutLen() const
Returns the number of bytes manually written into the transfer buffer.
Definition: ncr_udp.h:286
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:243
NameChangeFormat
Defines the list of data wire formats supported.
Definition: ncr_msg.h:60
std::function< void(const bool, const UDPCallback *)> UDPCompletionHandler
Defines a function pointer for NameChangeRequest completion handlers.
Definition: ncr_udp.h:131
isc::asiolink::UDPSocket< UDPCallback > NameChangeUDPSocket
Convenience type for UDP socket based listener.
Definition: ncr_udp.h:311
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:242
boost::shared_array< uint8_t > RawBufferPtr
Defines a dynamically allocated shared array.
Definition: ncr_udp.h:134
boost::shared_ptr< asiolink::UDPEndpoint > UDPEndpointPtr
Definition: ncr_udp.h:136
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Definition: watch_socket.h:138
Defines the logger used by the top-level component of kea-lfc.
This file defines abstract classes for exchanging NameChangeRequests.
Container class which stores service invocation related data.
Definition: ncr_udp.h:157
boost::system::error_code error_code_
Stores the IO layer result code of the completed IO service.
Definition: ncr_udp.h:186
size_t put_len_
Stores this size of the data within the buffer when written there manually.
Definition: ncr_udp.h:183
RawBufferPtr buffer_
A pointer to the data transfer buffer.
Definition: ncr_udp.h:173
size_t buf_size_
Storage capacity of the buffer.
Definition: ncr_udp.h:176
Data(RawBufferPtr &buffer, const size_t buf_size, UDPEndpointPtr &data_source)
Constructor.
Definition: ncr_udp.h:166
UDPEndpointPtr data_source_
The UDP endpoint that is the origin of the data transferred.
Definition: ncr_udp.h:179
size_t bytes_transferred_
Stores the number of bytes transferred by completed IO service.
Definition: ncr_udp.h:193
Defines the class, WatchSocket.