10 #ifndef BOOST_ASIO_HPP
11 #error "asio.hpp must be included before including this, see asiolink.h as to why"
17 #include <boost/noncopyable.hpp>
52 #if BOOST_VERSION < 106600
53 return (socket_.native());
55 return (socket_.native_handle());
81 if (socket_.is_open()) {
83 const bool non_blocking_orig = socket_.non_blocking();
88 socket_.non_blocking(
true);
94 int cc = recv(
getNative(), data,
sizeof(data), MSG_PEEK);
104 socket_.non_blocking(non_blocking_orig);
113 return ((err == 0) || (err == EAGAIN) || (err == EWOULDBLOCK));
149 virtual void asyncSend(
const void* data,
size_t length,
164 void asyncSend(
const void* data,
size_t length, C& callback);
177 virtual void asyncReceive(
void* data,
size_t length,
size_t offset,
196 size_t& cumulative,
size_t& offset,
204 virtual void close();
232 std::unique_ptr<TlsStream<C>> stream_ptr_;
235 TlsStream<C>& stream_;
238 typename TlsStream<C>::lowest_layer_type& socket_;
260 template <
typename C>
262 stream_ptr_(), stream_(stream),
263 socket_(stream_.lowest_layer()), send_buffer_() {
268 template <
typename C>
270 stream_ptr_(new TlsStream<C>(service, context)),
271 stream_(*stream_ptr_), socket_(stream_.lowest_layer()), send_buffer_()
277 template <
typename C>
void
282 if (!socket_.is_open()) {
284 socket_.open(boost::asio::ip::tcp::v4());
286 socket_.open(boost::asio::ip::tcp::v6());
293 socket_.set_option(boost::asio::socket_base::reuse_address(
true));
307 socket_.async_connect(tcp_endpoint->getASIOEndpoint(), callback);
312 template <
typename C>
void
314 if (!socket_.is_open()) {
316 "a TLS socket that is not open");
318 stream_.handshake(callback);
324 template <
typename C>
void
327 if (!socket_.is_open()) {
329 "attempt to send on a TLS socket that is not open");
334 send_buffer_->writeData(data, length);
337 boost::asio::async_write(stream_,
338 boost::asio::buffer(send_buffer_->getData(),
339 send_buffer_->getLength()),
341 }
catch (
const boost::numeric::bad_numeric_cast&) {
343 "attempt to send buffer larger than 64kB");
347 template <
typename C>
void
351 if (!socket_.is_open()) {
353 "attempt to send on a TLS socket that is not open");
361 uint16_t count = boost::numeric_cast<uint16_t>(length);
365 send_buffer_->writeUint16(count);
366 send_buffer_->writeData(data, length);
369 boost::asio::async_write(stream_,
370 boost::asio::buffer(send_buffer_->getData(),
371 send_buffer_->getLength()),
373 }
catch (
const boost::numeric::bad_numeric_cast&) {
375 "attempt to send buffer larger than 64kB");
382 template <
typename C>
void
386 if (!socket_.is_open()) {
388 "attempt to receive from a TLS socket that is not open");
409 if (offset >= length) {
411 "TCP receive buffer");
414 static_cast<void*
>(
static_cast<uint8_t*
>(data) + offset);
417 stream_.async_read_some(boost::asio::buffer(buffer_start, length - offset),
423 template <
typename C>
bool
425 size_t& cumulative,
size_t& offset,
430 const uint8_t* data =
static_cast<const uint8_t*
>(staging);
431 size_t data_length = length;
435 if (cumulative < 2) {
439 cumulative += length;
440 if (cumulative < 2) {
459 data_length = cumulative - 2;
463 cumulative += length;
473 if (expected >= outbuff->getLength()) {
477 size_t copy_amount = std::min(expected - outbuff->getLength(),
479 outbuff->writeData(data, copy_amount);
483 return (expected == outbuff->getLength());
488 template <
typename C>
void
490 if (socket_.is_open()) {
497 template <
typename C>
void
499 if (!socket_.is_open()) {
501 "a TLS socket that is not open");
503 stream_.shutdown(callback);
509 template <
typename C>
void
511 if (socket_.is_open() && stream_ptr_) {
519 #endif // TLS_SOCKET_H
virtual int getNative() const
Return file descriptor of underlying socket.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
TLSSocket(TlsStream< C > &stream)
Constructor from a TLS stream.
virtual void close()
Close socket.
const boost::asio::ip::tcp::endpoint & getASIOEndpoint() const
virtual void open(const IOEndpoint *endpoint, C &callback)
Open Socket.
virtual void handshake(C &callback)
Perform Handshake.
virtual TlsStream< C > & getTlsStream() const
Returns reference to the underlying TLS stream.
bool isUsable() const
Checks if the connection is usable.
virtual bool isOpenSynchronous() const
Is "open()" synchronous predicate.
virtual short getProtocol() const =0
Returns the protocol number of the endpoint (TCP, UDP...)
The IOService class is a wrapper for the ASIO io_service class.
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
virtual void asyncSend(const void *data, size_t length, const IOEndpoint *endpoint, C &callback)
Send Asynchronously.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
The TCPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a TCP ...
virtual short getFamily() const =0
Returns the address family of the endpoint.
virtual void cancel()
Cancel I/O On Socket.
virtual void asyncReceive(void *data, size_t length, size_t offset, IOEndpoint *endpoint, C &callback)
Receive Asynchronously.
virtual ~TLSSocket()
Destructor.
virtual int getProtocol() const
Return protocol of socket.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
virtual TlsStream< C >::lowest_layer_type & getASIOSocket() const
Returns reference to the underlying ASIO socket.
Defines the logger used by the top-level component of kea-dhcp-ddns.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
A wrapper interface for the ASIO library.
I/O Socket with asynchronous operations.
virtual bool processReceivedData(const void *staging, size_t length, size_t &cumulative, size_t &offset, size_t &expected, isc::util::OutputBufferPtr &outbuff)
Process received data packet.
boost::shared_ptr< OutputBuffer > OutputBufferPtr
virtual void shutdown(C &callback)
TLS shutdown.
The TLSSocket class is a concrete derived class of IOAsioSocket that represents a TLS socket...
The IOEndpoint class is an abstract base class to represent a communication endpoint.