46 #include <openssl/x509.h>
51 #ifdef WITH_BUNDLE_SECURITY
59 const std::string TCPConnection::TAG =
"TCPConnection";
65 : _peer(), _node(node), _socket(sock), _socket_stream(NULL), _sec_stream(NULL), _protocol_stream(NULL), _sender(*this),
66 _keepalive_sender(*this, _keepalive_timeout), _timeout(timeout), _lastack(0), _keepalive_timeout(0),
67 _callback(tcpsrv), _flags(0), _aborted(false)
74 _keepalive_sender.join();
82 delete _protocol_stream;
83 _protocol_stream = NULL;
86 if (_sec_stream != NULL) {
90 if ((_socket != NULL) && (_socket_stream == NULL)) {
92 }
else if (_socket_stream != NULL) {
93 delete _socket_stream;
114 (*getProtocolStream()).reject();
160 if (weak_cn.find_first_of(
"//") == 0) {
161 weak_cn = weak_cn.substr(2, weak_cn.length() - 2);
174 }
catch (
const std::exception&) {
202 _node =
Node(header._localeid);
205 _keepalive_timeout = header._keepalive * 1000;
209 initiateExtendedHandshake();
219 if (_peer._keepalive > 0)
223 timerclear(&timeout);
224 timeout.tv_sec = header._keepalive * 2;
225 _socket_stream->setTimeout(timeout);
231 if (_idle_timeout > 0)
233 (*getProtocolStream()).enableIdleTimeout(_idle_timeout);
247 _keepalive_sender.stop();
344 _socket_stream->
close();
353 _keepalive_sender.stop();
357 }
catch (
const std::exception&) { };
360 if (_socket_stream != NULL) _socket_stream->
close();
363 _callback.connectionDown(
this);
401 if (_protocol_stream != NULL)
delete _protocol_stream;
403 _protocol_stream->exceptions(std::ios::badbit | std::ios::eofbit);
414 if (_socket != NULL)
return;
417 if (_socket_stream != NULL)
return;
420 std::string address =
"0.0.0.0";
421 unsigned int port = 0;
427 for (std::list<dtn::core::Node::URI>::const_iterator iter = uri_list.begin(); iter != uri_list.end(); ++iter)
435 uri.
decode(address, port);
445 tv.tv_sec = _timeout;
455 __setup_socket(client,
false);
477 }
catch (
const bad_cast&) { };
483 if (_socket == NULL) {
488 __setup_socket(_socket,
true);
492 std::iostream &stream = (*sc);
501 _keepalive_sender.start();
519 deserializer >> bundle;
522 if ( ( bundle.destination ==
EID() ) || ( bundle.source ==
EID() ) )
554 }
catch (
const std::exception &ex) {
564 return safe_streamconnection(_protocol_stream, _protocol_stream_mutex);
567 TCPConnection::KeepaliveSender::KeepaliveSender(
TCPConnection &connection,
size_t &keepalive_timeout)
568 : _connection(connection), _keepalive_timeout(keepalive_timeout)
573 TCPConnection::KeepaliveSender::~KeepaliveSender()
577 void TCPConnection::KeepaliveSender::run() throw ()
584 _wait.wait(_keepalive_timeout);
589 _connection.keepalive();
597 }
catch (
const std::exception&) { };
600 void TCPConnection::KeepaliveSender::__cancellation() throw ()
607 : _connection(connection)
611 TCPConnection::Sender::~Sender()
615 void TCPConnection::Sender::__cancellation() throw ()
621 void TCPConnection::Sender::run() throw ()
627 std::iostream &stream = (*sc);
632 while (stream.good())
640 #ifdef WITH_BUNDLE_SECURITY
664 _connection._sentqueue.push(transfer);
678 serializer << bundle;
682 stream << std::flush;
701 }
catch (
const std::exception &ex) {
734 void dtn::net::TCPConnection::enableTLS()
747 return _socket_stream->good();
750 void TCPConnection::Sender::finally() throw ()
774 return (*sc).getMonitorStat(index);
784 return (*sc).resetMonitorStats();