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), _resume_offset(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);
215 _keepalive_timeout = header._keepalive * 1000;
219 initiateExtendedHandshake();
229 if (_peer._keepalive > 0)
233 timerclear(&timeout);
234 timeout.tv_sec = header._keepalive * 2;
235 _socket_stream->setTimeout(timeout);
241 if (_idle_timeout > 0)
243 (*getProtocolStream()).enableIdleTimeout(_idle_timeout);
257 _keepalive_sender.stop();
325 _callback.addTrafficIn(amount);
330 _callback.addTrafficOut(amount);
364 if (_socket_stream != NULL) _socket_stream->
close();
373 _keepalive_sender.stop();
377 }
catch (
const std::exception&) { };
380 if (_socket_stream != NULL) _socket_stream->
close();
383 _callback.connectionDown(
this);
421 if (_protocol_stream != NULL)
delete _protocol_stream;
423 _protocol_stream->exceptions(std::ios::badbit | std::ios::eofbit);
429 if (_socket != NULL)
return;
432 if (_socket_stream != NULL)
return;
435 std::string address =
"0.0.0.0";
436 unsigned int port = 0;
442 for (std::list<dtn::core::Node::URI>::const_iterator iter = uri_list.begin(); iter != uri_list.end(); ++iter)
450 uri.
decode(address, port);
460 tv.tv_sec = _timeout;
470 __setup_socket(client,
false);
494 }
catch (
const bad_cast&) { };
500 if (_socket == NULL) {
505 __setup_socket(_socket,
true);
509 std::iostream &stream = (*sc);
518 _keepalive_sender.start();
536 deserializer >> bundle;
539 if ( ( bundle.destination ==
EID() ) || ( bundle.source ==
EID() ) )
571 }
catch (
const std::exception &ex) {
581 return safe_streamconnection(_protocol_stream, _protocol_stream_mutex);
584 TCPConnection::KeepaliveSender::KeepaliveSender(
TCPConnection &connection,
size_t &keepalive_timeout)
585 : _connection(connection), _keepalive_timeout(keepalive_timeout)
590 TCPConnection::KeepaliveSender::~KeepaliveSender()
594 void TCPConnection::KeepaliveSender::run() throw ()
601 _wait.wait(_keepalive_timeout);
606 _connection.keepalive();
614 }
catch (
const std::exception&) { };
617 void TCPConnection::KeepaliveSender::__cancellation() throw ()
624 : _connection(connection)
628 TCPConnection::Sender::~Sender()
632 void TCPConnection::Sender::__cancellation() throw ()
638 void TCPConnection::Sender::run() throw ()
644 std::iostream &stream = (*sc);
649 while (stream.good())
660 #ifdef WITH_BUNDLE_SECURITY
683 _connection._resume_offset = 0;
687 _connection._sentqueue.push(transfer);
693 if (_connection._resume_offset > 0)
703 serializer << bundle;
707 stream << std::flush;
726 }
catch (
const std::exception &ex) {
759 void dtn::net::TCPConnection::enableTLS()
772 return _socket_stream->good();
775 void TCPConnection::Sender::finally() throw ()