Kea  2.1.7-git
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 
102 #include <asiolink/asio_wrapper.h>
103 #include <asiolink/io_address.h>
104 #include <asiolink/io_service.h>
105 #include <asiolink/udp_endpoint.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 
118 namespace isc {
119 namespace dhcp_ddns {
120 
122 class NcrUDPError : public isc::Exception {
123 public:
124  NcrUDPError(const char* file, size_t line, const char* what) :
125  isc::Exception(file, line, what) { };
126 };
127 
130 typedef std::function<void(const bool, const UDPCallback*)>
132 
134 typedef boost::shared_array<uint8_t> RawBufferPtr;
135 
136 typedef boost::shared_ptr<asiolink::UDPEndpoint> UDPEndpointPtr;
137 
147 class UDPCallback {
148 
149 public:
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),
169  put_len_(0), error_code_(), bytes_transferred_(0) {
170  };
171 
173  RawBufferPtr buffer_;
174 
176  size_t buf_size_;
177 
179  UDPEndpointPtr data_source_;
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 
255  RawBufferPtr getBuffer() const {
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 
293  void setDataSource(UDPEndpointPtr& endpoint) {
294  data_->data_source_ = endpoint;
295  }
296 
298  const UDPEndpointPtr& getDataSource() {
299  return (data_->data_source_);
300  }
301 
302  private:
304  UDPCompletionHandler handler_;
305 
307  boost::shared_ptr<Data> data_;
308 };
309 
312 
320 public:
322  static const size_t RECV_BUF_MAX = isc::asiolink::
324 
338  const uint32_t port,
339  const NameChangeFormat format,
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);
400 private:
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 
428 private:
430  NameChangeUDPListener& operator=(const NameChangeUDPListener& source);
432 };
433 
434 
442 public:
443 
445  static const size_t SEND_BUF_MAX = NameChangeUDPListener::RECV_BUF_MAX;
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 
543 private:
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
size_t buf_size_
Storage capacity of the buffer.
Definition: ncr_udp.h:176
const UDPEndpointPtr & getDataSource()
Returns the UDP endpoint that provided the transferred data.
Definition: ncr_udp.h:298
size_t getBufferSize() const
Returns the data transfer buffer capacity.
Definition: ncr_udp.h:260
std::function< void(const bool, const UDPCallback *)> UDPCompletionHandler
Defines a function pointer for NameChangeRequest completion handlers.
Definition: ncr_udp.h:128
This file defines abstract classes for exchanging NameChangeRequests.
Provides the ability to receive NameChangeRequests via UDP socket.
Definition: ncr_udp.h:319
static const size_t RECV_BUF_MAX
Defines the maximum size packet that can be received.
Definition: ncr_udp.h:322
Implements the callback class passed into UDPSocket calls.
Definition: ncr_udp.h:147
size_t put_len_
Stores this size of the data within the buffer when written there manually.
Definition: ncr_udp.h:183
NameChangeFormat
Defines the list of data wire formats supported.
Definition: ncr_msg.h:60
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Definition: watch_socket.h:138
NcrUDPError(const char *file, size_t line, const char *what)
Definition: ncr_udp.h:124
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:212
Data(RawBufferPtr &buffer, const size_t buf_size, UDPEndpointPtr &data_source)
Constructor.
Definition: ncr_udp.h:166
RawBufferPtr getBuffer() const
Returns the data transfer buffer.
Definition: ncr_udp.h:255
UDPEndpointPtr data_source_
The UDP endpoint that is the origin of the data transferred.
Definition: ncr_udp.h:179
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
size_t getPutLen() const
Returns the number of bytes manually written into the transfer buffer.
Definition: ncr_udp.h:286
Defines the class, WatchSocket.
boost::system::error_code getErrorCode() const
Returns the completed IO layer service outcome status.
Definition: ncr_udp.h:243
void setBytesTransferred(const size_t value)
Sets the number of bytes transferred.
Definition: ncr_udp.h:238
Provides the ability to send NameChangeRequests via UDP socket.
Definition: ncr_udp.h:441
Abstract class for defining application layer send callbacks.
Definition: ncr_io.h:488
boost::system::error_code error_code_
Stores the IO layer result code of the completed IO service.
Definition: ncr_udp.h:186
Thrown when a UDP level exception occurs.
Definition: ncr_udp.h:122
void setErrorCode(const boost::system::error_code value)
Sets the completed IO layer service outcome status.
Definition: ncr_udp.h:250
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
Container class which stores service invocation related data.
Definition: ncr_udp.h:157
const uint8_t * getData() const
Returns a pointer the data transfer buffer content.
Definition: ncr_udp.h:265
boost::shared_array< uint8_t > RawBufferPtr
Defines a dynamically allocated shared array.
Definition: ncr_udp.h:134
isc::asiolink::UDPSocket< UDPCallback > NameChangeUDPSocket
Convenience type for UDP socket based listener.
Definition: ncr_udp.h:311
void setDataSource(UDPEndpointPtr &endpoint)
Sets the data source to the given endpoint.
Definition: ncr_udp.h:293
RawBufferPtr buffer_
A pointer to the data transfer buffer.
Definition: ncr_udp.h:170
size_t getBytesTransferred() const
Returns the number of bytes transferred by the completed IO service.
Definition: ncr_udp.h:231
Abstract interface for sending NameChangeRequests.
Definition: ncr_io.h:466
size_t bytes_transferred_
Stores the number of bytes transferred by completed IO service.
Definition: ncr_udp.h:193
boost::shared_ptr< asiolink::UDPEndpoint > UDPEndpointPtr
Definition: ncr_udp.h:136
Abstract interface for receiving NameChangeRequests.
Definition: ncr_io.h:167
Abstract class for defining application layer receive callbacks.
Definition: ncr_io.h:183
static const size_t MAX_QUEUE_DEFAULT
Defines a default maximum number of entries in the send queue.
Definition: ncr_io.h:473
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157