diff --git a/ChangeLog b/ChangeLog index 92c510d03..9195b578f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * add incoming_connection_alert for logging all successful incoming connections * feature to encrypt peer connections with a secret AES-256 key stored in .torrent file * deprecated compact storage allocation * close files in separate thread on systems where close() may block (Mac OS X for instance) diff --git a/docs/manual.rst b/docs/manual.rst index 2a52851b2..23c0e6912 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -7319,6 +7319,56 @@ having to call ``get_settings()``. ``error`` is an error code used for when an error occurs on the feed. +incoming_connection_alert +------------------------- + +The incoming connection alert is posted every time we successfully accept +an incoming connection, through any mean. The most straigh-forward ways +of accepting incoming connections are through the TCP listen socket and +the UDP listen socket for uTP sockets. However, connections may also be +accepted ofer a Socks5 or i2p listen socket, or via a torrent specific +listen socket for SSL torrents. + +:: + + struct incoming_connection_alert: alert + { + // ... + virtual std::string message() const; + + int socket_type; + tcp::endpoint ip; + }; + +``socket_type`` tells you what kind of socket the connection was accepted +as: + ++==========+=====================================+ +| value | type | ++==========+=====================================+ +| 0 | none (no socket instantiated) | ++----------+-------------------------------------+ +| 1 | TCP | ++----------+-------------------------------------+ +| 2 | Socks5 | ++----------+-------------------------------------+ +| 3 | HTTP | ++----------+-------------------------------------+ +| 4 | uTP | ++----------+-------------------------------------+ +| 5 | i2p | ++----------+-------------------------------------+ +| 6 | SSL/TCP | ++----------+-------------------------------------+ +| 7 | SSL/Socks5 | ++----------+-------------------------------------+ +| 8 | HTTPS (SSL/HTTP) | ++----------+-------------------------------------+ +| 9 | SSL/uTP | ++----------+-------------------------------------+ + +``ip`` is the IP address and port the connection came from. + alert dispatcher ================ diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 5ee578fcc..8b3ffdfd9 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1278,6 +1278,22 @@ namespace libtorrent error_code error; }; + struct TORRENT_EXPORT incoming_connection_alert: alert + { + incoming_connection_alert(int type_, tcp::endpoint const& ip_) + : socket_type(type_) + , ip(ip_) + {} + + TORRENT_DEFINE_ALERT(incoming_connection_alert); + + const static int static_category = alert::peer_notification; + virtual std::string message() const; + + int socket_type; + tcp::endpoint ip; + }; + } diff --git a/include/libtorrent/socket_type.hpp b/include/libtorrent/socket_type.hpp index 47c98daf6..4cd3aea6b 100644 --- a/include/libtorrent/socket_type.hpp +++ b/include/libtorrent/socket_type.hpp @@ -196,6 +196,7 @@ namespace libtorrent endpoint_type remote_endpoint(error_code& ec) const; void bind(endpoint_type const& endpoint, error_code& ec); std::size_t available(error_code& ec) const; + int type(); template diff --git a/src/alert.cpp b/src/alert.cpp index 9cefb72e2..9efa13533 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -566,5 +566,26 @@ namespace libtorrent { return torrent_alert::message() + " needs SSL certificate"; } + std::string incoming_connection_alert::message() const + { + char msg[600]; + char const* type_str[] = { + "null", + "TCP", + "Socks5/TCP", + "HTTP", + "uTP", + "i2p", + "SSL/TCP", + "SSL/Socks5", + "HTTPS", + "SSL/uTP" + }; + error_code ec; + snprintf(msg, sizeof(msg), "incoming connection from %s (%s)" + , print_endpoint(ip).c_str(), type_str[socket_type]); + return msg; + } + } // namespace libtorrent diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 67eee0630..d136ab7f3 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2243,6 +2243,11 @@ namespace aux { (*m_logger) << time_now_string() << " <== INCOMING CONNECTION " << endp << "\n"; #endif + if (m_alerts.should_post()) + { + m_alerts.post_alert(incoming_connection_alert(s->type(), endp)); + } + if (!m_settings.enable_incoming_utp && s->get()) { diff --git a/src/socket_type.cpp b/src/socket_type.cpp index 59a0c93f1..4fbd34147 100644 --- a/src/socket_type.cpp +++ b/src/socket_type.cpp @@ -164,6 +164,8 @@ namespace libtorrent std::size_t socket_type::available(error_code& ec) const { TORRENT_SOCKTYPE_FORWARD_RET(available(ec), 0) } + int socket_type::type() { return m_type; } + #ifndef BOOST_NO_EXCEPTIONS void socket_type::open(protocol_type const& p) { TORRENT_SOCKTYPE_FORWARD(open(p)) } diff --git a/src/torrent.cpp b/src/torrent.cpp index af6a6af91..5fdc7048f 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1462,6 +1462,11 @@ ctx->set_verify_callback(verify_function, ec); return; } + if (alerts().should_post()) + { + alerts().post_alert(incoming_connection_alert(s->type(), endp)); + } + if (!settings().enable_incoming_tcp) { if (alerts().should_post())