2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/network/tcp_socket.hpp>
|
|
|
|
|
#include <fc/network/ip.hpp>
|
2014-04-17 23:39:15 +00:00
|
|
|
#include <fc/network/tcp_socket_io_hooks.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/fwd_impl.hpp>
|
|
|
|
|
#include <fc/asio.hpp>
|
|
|
|
|
#include <fc/log/logger.hpp>
|
|
|
|
|
#include <fc/io/stdio.hpp>
|
2013-07-19 02:19:19 +00:00
|
|
|
#include <fc/exception/exception.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2014-04-09 22:29:59 +00:00
|
|
|
#if defined _WIN32 || defined WIN32 || defined OS_WIN64 || defined _WIN64 || defined WIN64 || defined WINNT
|
|
|
|
|
# include <MSTcpIP.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
namespace fc {
|
|
|
|
|
|
2014-04-17 23:39:15 +00:00
|
|
|
class tcp_socket::impl : public tcp_socket_io_hooks{
|
2013-06-05 19:19:00 +00:00
|
|
|
public:
|
2014-04-17 23:39:15 +00:00
|
|
|
impl() :
|
|
|
|
|
_sock(fc::asio::default_io_service()),
|
|
|
|
|
_io_hooks(this)
|
|
|
|
|
{}
|
|
|
|
|
~impl()
|
|
|
|
|
{
|
2014-04-29 23:54:43 +00:00
|
|
|
if( _sock.is_open() )
|
|
|
|
|
_sock.close();
|
2014-07-17 21:03:25 +00:00
|
|
|
if( _read_in_progress.valid() ) try { _read_in_progress.wait(); } catch ( ... ) {}
|
|
|
|
|
if( _write_in_progress.valid() ) try { _write_in_progress.wait(); } catch ( ... ) {}
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
2014-04-17 23:39:15 +00:00
|
|
|
virtual size_t readsome(boost::asio::ip::tcp::socket& socket, char* buffer, size_t length) override;
|
|
|
|
|
virtual size_t writesome(boost::asio::ip::tcp::socket& socket, const char* buffer, size_t length) override;
|
|
|
|
|
|
2014-07-17 21:03:25 +00:00
|
|
|
fc::future<size_t> _write_in_progress;
|
|
|
|
|
fc::future<size_t> _read_in_progress;
|
2013-06-05 19:19:00 +00:00
|
|
|
boost::asio::ip::tcp::socket _sock;
|
2014-04-17 23:39:15 +00:00
|
|
|
tcp_socket_io_hooks* _io_hooks;
|
2013-06-05 19:19:00 +00:00
|
|
|
};
|
2014-04-17 23:39:15 +00:00
|
|
|
|
|
|
|
|
size_t tcp_socket::impl::readsome(boost::asio::ip::tcp::socket& socket, char* buffer, size_t length)
|
|
|
|
|
{
|
2014-07-17 21:03:25 +00:00
|
|
|
return (_read_in_progress = fc::asio::read_some(socket, boost::asio::buffer(buffer, length))).wait();
|
2014-04-17 23:39:15 +00:00
|
|
|
}
|
|
|
|
|
size_t tcp_socket::impl::writesome(boost::asio::ip::tcp::socket& socket, const char* buffer, size_t length)
|
|
|
|
|
{
|
2014-07-17 21:03:25 +00:00
|
|
|
return (_write_in_progress = fc::asio::write_some(socket, boost::asio::buffer(buffer, length))).wait();
|
2014-04-17 23:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-08 13:04:45 +00:00
|
|
|
|
2014-04-29 23:54:43 +00:00
|
|
|
void tcp_socket::open()
|
|
|
|
|
{
|
|
|
|
|
my->_sock.open(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0).protocol());
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
bool tcp_socket::is_open()const {
|
|
|
|
|
return my->_sock.is_open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tcp_socket::tcp_socket(){};
|
|
|
|
|
|
|
|
|
|
tcp_socket::~tcp_socket(){};
|
|
|
|
|
|
|
|
|
|
void tcp_socket::flush() {}
|
|
|
|
|
void tcp_socket::close() {
|
2013-07-19 02:19:19 +00:00
|
|
|
try {
|
|
|
|
|
if( is_open() )
|
|
|
|
|
{
|
|
|
|
|
my->_sock.close();
|
|
|
|
|
}
|
|
|
|
|
} FC_RETHROW_EXCEPTIONS( warn, "error closing tcp socket" );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool tcp_socket::eof()const {
|
|
|
|
|
return !my->_sock.is_open();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-17 23:39:15 +00:00
|
|
|
size_t tcp_socket::writesome(const char* buf, size_t len) {
|
|
|
|
|
return my->_io_hooks->writesome(my->_sock, buf, len);
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-29 23:54:43 +00:00
|
|
|
fc::ip::endpoint tcp_socket::remote_endpoint()const
|
|
|
|
|
{
|
|
|
|
|
auto rep = my->_sock.remote_endpoint();
|
|
|
|
|
return fc::ip::endpoint(rep.address().to_v4().to_ulong(), rep.port() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2014-04-29 23:54:43 +00:00
|
|
|
fc::ip::endpoint tcp_socket::local_endpoint() const
|
|
|
|
|
{
|
|
|
|
|
auto boost_local_endpoint = my->_sock.local_endpoint();
|
|
|
|
|
return fc::ip::endpoint(boost_local_endpoint.address().to_v4().to_ulong(), boost_local_endpoint.port() );
|
|
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
size_t tcp_socket::readsome( char* buf, size_t len ) {
|
2014-04-17 23:39:15 +00:00
|
|
|
return my->_io_hooks->readsome(my->_sock, buf, len);
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-27 18:12:27 +00:00
|
|
|
void tcp_socket::connect_to( const fc::ip::endpoint& remote_endpoint ) {
|
|
|
|
|
fc::asio::tcp::connect(my->_sock, fc::asio::tcp::endpoint( boost::asio::ip::address_v4(remote_endpoint.get_address()), remote_endpoint.port() ) );
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-01 22:07:56 +00:00
|
|
|
void tcp_socket::bind(const fc::ip::endpoint& local_endpoint)
|
|
|
|
|
{
|
2014-04-29 23:54:43 +00:00
|
|
|
try
|
|
|
|
|
{
|
2014-06-01 22:07:56 +00:00
|
|
|
my->_sock.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(local_endpoint.get_address()),
|
|
|
|
|
local_endpoint.port()));
|
2014-04-29 23:54:43 +00:00
|
|
|
}
|
2014-06-01 22:07:56 +00:00
|
|
|
catch (const std::exception& except)
|
2014-04-29 23:54:43 +00:00
|
|
|
{
|
|
|
|
|
elog("Exception binding outgoing connection to desired local endpoint: ${what}", ("what", except.what()));
|
2014-06-01 22:07:56 +00:00
|
|
|
FC_THROW("error binding to ${endpoint}: ${what}", ("endpoint", local_endpoint)("what", except.what()));
|
2014-04-29 23:54:43 +00:00
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-09 22:29:59 +00:00
|
|
|
void tcp_socket::enable_keep_alives(const fc::microseconds& interval)
|
|
|
|
|
{
|
|
|
|
|
if (interval.count())
|
|
|
|
|
{
|
|
|
|
|
boost::asio::socket_base::keep_alive option(true);
|
|
|
|
|
my->_sock.set_option(option);
|
|
|
|
|
#if defined _WIN32 || defined WIN32 || defined OS_WIN64 || defined _WIN64 || defined WIN64 || defined WINNT
|
|
|
|
|
struct tcp_keepalive keepalive_settings;
|
|
|
|
|
keepalive_settings.onoff = 1;
|
2014-04-17 16:00:52 +00:00
|
|
|
keepalive_settings.keepalivetime = (ULONG)(interval.count() / fc::milliseconds(1).count());
|
|
|
|
|
keepalive_settings.keepaliveinterval = (ULONG)(interval.count() / fc::milliseconds(1).count());
|
2014-04-09 22:29:59 +00:00
|
|
|
|
|
|
|
|
DWORD dwBytesRet = 0;
|
|
|
|
|
if (WSAIoctl(my->_sock.native(), SIO_KEEPALIVE_VALS, &keepalive_settings, sizeof(keepalive_settings),
|
|
|
|
|
NULL, 0, &dwBytesRet, NULL, NULL) == SOCKET_ERROR)
|
|
|
|
|
wlog("Error setting TCP keepalive values");
|
2014-05-04 18:26:36 +00:00
|
|
|
#elif !defined(__clang__) || (__clang_major__ >= 6)
|
2014-04-09 22:29:59 +00:00
|
|
|
// This should work for modern Linuxes and for OSX >= Mountain Lion
|
|
|
|
|
int timeout_sec = interval.count() / fc::seconds(1).count();
|
2014-04-15 17:40:19 +00:00
|
|
|
if (setsockopt(my->_sock.native(), IPPROTO_TCP,
|
2014-05-06 21:20:04 +00:00
|
|
|
#if defined( __APPLE__ )
|
2014-04-15 17:40:19 +00:00
|
|
|
TCP_KEEPALIVE,
|
2014-05-04 18:26:36 +00:00
|
|
|
#else
|
2014-04-15 17:40:19 +00:00
|
|
|
TCP_KEEPIDLE,
|
2014-05-04 18:26:36 +00:00
|
|
|
#endif
|
2014-04-09 22:29:59 +00:00
|
|
|
(char*)&timeout_sec, sizeof(timeout_sec)) < 0)
|
|
|
|
|
wlog("Error setting TCP keepalive idle time");
|
2014-05-06 21:20:04 +00:00
|
|
|
# if !defined(__APPLE__) || defined(TCP_KEEPINTVL) // TCP_KEEPINTVL not defined before 10.9
|
2014-04-15 17:40:19 +00:00
|
|
|
if (setsockopt(my->_sock.native(), IPPROTO_TCP, TCP_KEEPINTVL,
|
2014-04-09 22:29:59 +00:00
|
|
|
(char*)&timeout_sec, sizeof(timeout_sec)) < 0)
|
|
|
|
|
wlog("Error setting TCP keepalive interval");
|
2014-05-06 21:20:04 +00:00
|
|
|
# endif // !__APPLE__ || TCP_KEEPINTVL
|
|
|
|
|
#endif // !WIN32
|
2014-04-09 22:29:59 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
boost::asio::socket_base::keep_alive option(false);
|
|
|
|
|
my->_sock.set_option(option);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-17 23:39:15 +00:00
|
|
|
void tcp_socket::set_io_hooks(tcp_socket_io_hooks* new_hooks)
|
|
|
|
|
{
|
|
|
|
|
my->_io_hooks = new_hooks ? new_hooks : &*my;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 23:54:43 +00:00
|
|
|
void tcp_socket::set_reuse_address(bool enable /* = true */)
|
|
|
|
|
{
|
|
|
|
|
FC_ASSERT(my->_sock.is_open());
|
|
|
|
|
boost::asio::socket_base::reuse_address option(enable);
|
|
|
|
|
my->_sock.set_option(option);
|
2014-05-14 20:02:41 +00:00
|
|
|
#if defined(__APPLE__) || (defined(__linux__) && defined(SO_REUSEPORT))
|
2014-05-14 04:14:40 +00:00
|
|
|
// OSX needs SO_REUSEPORT in addition to SO_REUSEADDR.
|
|
|
|
|
// This probably needs to be set for any BSD
|
|
|
|
|
int reuseport_value = 1;
|
|
|
|
|
if (setsockopt(my->_sock.native(), SOL_SOCKET, SO_REUSEPORT,
|
|
|
|
|
(char*)&reuseport_value, sizeof(reuseport_value)) < 0)
|
|
|
|
|
wlog("Error setting SO_REUSEPORT");
|
|
|
|
|
#endif // __APPLE__
|
2014-04-29 23:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
class tcp_server::impl {
|
|
|
|
|
public:
|
2014-04-29 23:54:43 +00:00
|
|
|
impl()
|
|
|
|
|
:_accept( fc::asio::default_io_service() )
|
|
|
|
|
{
|
|
|
|
|
_accept.open(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0).protocol());
|
|
|
|
|
}
|
2014-02-15 01:32:23 +00:00
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
~impl(){
|
2013-07-19 02:19:19 +00:00
|
|
|
try {
|
|
|
|
|
_accept.close();
|
|
|
|
|
}
|
2014-03-27 18:12:27 +00:00
|
|
|
catch ( boost::system::system_error& )
|
2013-07-19 02:19:19 +00:00
|
|
|
{
|
|
|
|
|
wlog( "unexpected exception ${e}", ("e", fc::except_str()) );
|
|
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::acceptor _accept;
|
|
|
|
|
};
|
|
|
|
|
void tcp_server::close() {
|
2014-04-29 23:54:43 +00:00
|
|
|
if( my && my->_accept.is_open() )
|
|
|
|
|
my->_accept.close();
|
|
|
|
|
delete my;
|
|
|
|
|
my = nullptr;
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
tcp_server::tcp_server()
|
|
|
|
|
:my(nullptr) {
|
|
|
|
|
}
|
|
|
|
|
tcp_server::~tcp_server() {
|
|
|
|
|
delete my;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-08-08 19:12:01 +00:00
|
|
|
void tcp_server::accept( tcp_socket& s )
|
|
|
|
|
{
|
2013-07-14 21:58:27 +00:00
|
|
|
try
|
|
|
|
|
{
|
2013-08-08 19:12:01 +00:00
|
|
|
FC_ASSERT( my != nullptr );
|
2013-07-14 21:58:27 +00:00
|
|
|
fc::asio::tcp::accept( my->_accept, s.my->_sock );
|
|
|
|
|
} FC_RETHROW_EXCEPTIONS( warn, "Unable to accept connection on socket." );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
2014-04-29 23:54:43 +00:00
|
|
|
void tcp_server::set_reuse_address(bool enable /* = true */)
|
|
|
|
|
{
|
|
|
|
|
if( !my )
|
|
|
|
|
my = new impl;
|
|
|
|
|
boost::asio::ip::tcp::acceptor::reuse_address option(enable);
|
|
|
|
|
my->_accept.set_option(option);
|
2014-05-14 20:02:41 +00:00
|
|
|
#if defined(__APPLE__) || (defined(__linux__) && defined(SO_REUSEPORT))
|
2014-05-14 04:14:40 +00:00
|
|
|
// OSX needs SO_REUSEPORT in addition to SO_REUSEADDR.
|
|
|
|
|
// This probably needs to be set for any BSD
|
|
|
|
|
int reuseport_value = 1;
|
|
|
|
|
if (setsockopt(my->_accept.native(), SOL_SOCKET, SO_REUSEPORT,
|
|
|
|
|
(char*)&reuseport_value, sizeof(reuseport_value)) < 0)
|
|
|
|
|
wlog("Error setting SO_REUSEPORT");
|
|
|
|
|
#endif // __APPLE__
|
2014-04-29 23:54:43 +00:00
|
|
|
}
|
2013-08-08 19:12:01 +00:00
|
|
|
void tcp_server::listen( uint16_t port )
|
|
|
|
|
{
|
2014-04-29 23:54:43 +00:00
|
|
|
if( !my )
|
|
|
|
|
my = new impl;
|
2014-06-01 22:07:56 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
my->_accept.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(), port));
|
|
|
|
|
my->_accept.listen();
|
|
|
|
|
}
|
|
|
|
|
FC_RETHROW_EXCEPTIONS(warn, "error listening on socket");
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
2014-02-15 01:32:23 +00:00
|
|
|
void tcp_server::listen( const fc::ip::endpoint& ep )
|
|
|
|
|
{
|
2014-04-29 23:54:43 +00:00
|
|
|
if( !my )
|
|
|
|
|
my = new impl;
|
2014-06-01 22:07:56 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
my->_accept.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string((string)ep.get_address()), ep.port()));
|
|
|
|
|
my->_accept.listen();
|
|
|
|
|
}
|
|
|
|
|
FC_RETHROW_EXCEPTIONS(warn, "error listening on socket");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fc::ip::endpoint tcp_server::get_local_endpoint() const
|
|
|
|
|
{
|
|
|
|
|
FC_ASSERT( my != nullptr );
|
|
|
|
|
return fc::ip::endpoint(my->_accept.local_endpoint().address().to_v4().to_ulong(),
|
|
|
|
|
my->_accept.local_endpoint().port() );
|
2014-02-15 01:32:23 +00:00
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2013-12-22 05:10:03 +00:00
|
|
|
uint16_t tcp_server::get_port()const
|
|
|
|
|
{
|
2014-06-01 22:07:56 +00:00
|
|
|
FC_ASSERT( my != nullptr );
|
|
|
|
|
return my->_accept.local_endpoint().port();
|
2013-12-22 05:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace fc
|