diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-06-02 18:38:41 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-06-02 18:38:41 +0200 |
commit | 74d9da7f3257c510b4f99fca635c5a9971053300 (patch) | |
tree | 7a12179eb0b716e82ff755e1f0e90594070f2a4e /src/common | |
parent | 2d984fcade155bf860ed20713a95251ad0103318 (diff) |
Core/Network: Make ip address formattable with fmt
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/network/AsyncAcceptor.h | 4 | ||||
-rw-r--r-- | src/common/network/Http/HttpService.cpp | 6 | ||||
-rw-r--r-- | src/common/network/IpAddress.cpp | 58 | ||||
-rw-r--r-- | src/common/network/IpAddress.h | 20 | ||||
-rw-r--r-- | src/common/network/Socket.h | 5 |
5 files changed, 86 insertions, 7 deletions
diff --git a/src/common/network/AsyncAcceptor.h b/src/common/network/AsyncAcceptor.h index be9c8d21e41..aaaa9410584 100644 --- a/src/common/network/AsyncAcceptor.h +++ b/src/common/network/AsyncAcceptor.h @@ -98,14 +98,14 @@ public: _acceptor.bind(_endpoint, errorCode); if (errorCode) { - TC_LOG_INFO("network", "Could not bind to {}:{} {}", _endpoint.address().to_string(), _endpoint.port(), errorCode.message()); + TC_LOG_INFO("network", "Could not bind to {}:{} {}", _endpoint.address(), _endpoint.port(), errorCode.message()); return false; } _acceptor.listen(TRINITY_MAX_LISTEN_CONNECTIONS, errorCode); if (errorCode) { - TC_LOG_INFO("network", "Failed to start listening on {}:{} {}", _endpoint.address().to_string(), _endpoint.port(), errorCode.message()); + TC_LOG_INFO("network", "Failed to start listening on {}:{} {}", _endpoint.address(), _endpoint.port(), errorCode.message()); return false; } diff --git a/src/common/network/Http/HttpService.cpp b/src/common/network/Http/HttpService.cpp index 6115fd6b6d2..3ba983ac10e 100644 --- a/src/common/network/Http/HttpService.cpp +++ b/src/common/network/Http/HttpService.cpp @@ -135,7 +135,7 @@ void SessionService::InitAndStoreSessionState(std::shared_ptr<SessionState> stat while (state->Id.is_nil() || _sessions.contains(state->Id)) std::copy_n(Trinity::Crypto::GetRandomBytes<16>().begin(), 16, state->Id.begin()); - TC_LOG_DEBUG(_logger, "Client at {} created new session {}", address.to_string(), boost::uuids::to_string(state->Id)); + TC_LOG_DEBUG(_logger, "Client at {} created new session {}", address, boost::uuids::to_string(state->Id)); _sessions[state->Id] = std::move(state); } } @@ -175,7 +175,7 @@ std::shared_ptr<SessionState> SessionService::FindAndRefreshSessionState(std::st auto itr = _sessions.find(boost::uuids::string_generator()(id.begin(), id.end())); if (itr == _sessions.end()) { - TC_LOG_DEBUG(_logger, "Client at {} attempted to use a session {} that was expired", address.to_string(), id); + TC_LOG_DEBUG(_logger, "Client at {} attempted to use a session {} that was expired", address, id); return nullptr; // no session } @@ -185,7 +185,7 @@ std::shared_ptr<SessionState> SessionService::FindAndRefreshSessionState(std::st if (state->RemoteAddress != address) { TC_LOG_ERROR(_logger, "Client at {} attempted to use a session {} that was last accessed from {}, denied access", - address.to_string(), id, state->RemoteAddress.to_string()); + address, id, state->RemoteAddress); return nullptr; } diff --git a/src/common/network/IpAddress.cpp b/src/common/network/IpAddress.cpp new file mode 100644 index 00000000000..40b3148daa5 --- /dev/null +++ b/src/common/network/IpAddress.cpp @@ -0,0 +1,58 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "IpAddress.h" +#include "StringFormat.h" +#include "Util.h" + +template <typename FormatContext> +typename FormatContext::iterator Trinity::Net::Impl::AddressFormatter::format(boost::asio::ip::address_v4 const& address, FormatContext& ctx) const +{ + boost::system::error_code ec; + std::array<char, boost::asio::detail::max_addr_v4_str_len> addr_str; + if (boost::asio::detail::socket_ops::inet_ntop(AF_INET, address.to_bytes().data(), addr_str.data(), addr_str.size(), 0, ec) && !ec) + return std::ranges::copy(addr_str.begin(), CStringSentinel.Checked(addr_str.end()), ctx.out()).out; + + return ctx.out(); +} + +template <typename FormatContext> +typename FormatContext::iterator Trinity::Net::Impl::AddressFormatter::format(boost::asio::ip::address_v6 const& address, FormatContext& ctx) const +{ + boost::system::error_code ec; + std::array<char, boost::asio::detail::max_addr_v6_str_len> addr_str; + if (boost::asio::detail::socket_ops::inet_ntop(AF_INET6, address.to_bytes().data(), addr_str.data(), addr_str.size(), address.scope_id(), ec) && !ec) + return std::ranges::copy(addr_str.begin(), CStringSentinel.Checked(addr_str.end()), ctx.out()).out; + + return ctx.out(); +} + +template <typename FormatContext> +typename FormatContext::iterator Trinity::Net::Impl::AddressFormatter::format(boost::asio::ip::address const& address, FormatContext& ctx) const +{ + if (address.is_v4()) + return this->format(address.to_v4(), ctx); + + if (address.is_v6()) + return this->format(address.to_v6(), ctx); + + return ctx.out(); +} + +template TC_NETWORK_API fmt::appender Trinity::Net::Impl::AddressFormatter::format<fmt::format_context>(boost::asio::ip::address_v4 const&, fmt::format_context&) const; +template TC_NETWORK_API fmt::appender Trinity::Net::Impl::AddressFormatter::format<fmt::format_context>(boost::asio::ip::address_v6 const&, fmt::format_context&) const; +template TC_NETWORK_API fmt::appender Trinity::Net::Impl::AddressFormatter::format<fmt::format_context>(boost::asio::ip::address const&, fmt::format_context&) const; diff --git a/src/common/network/IpAddress.h b/src/common/network/IpAddress.h index b856d7f6340..c19feedf84d 100644 --- a/src/common/network/IpAddress.h +++ b/src/common/network/IpAddress.h @@ -19,6 +19,7 @@ #define TRINITYCORE_IP_ADDRESS_H #include "Define.h" +#include "StringFormatFwd.h" #include <boost/asio/ip/address.hpp> namespace Trinity::Net @@ -28,6 +29,25 @@ namespace Trinity::Net using boost::asio::ip::make_address_v6; using boost::asio::ip::v4_mapped_t::v4_mapped; inline uint32 address_to_uint(boost::asio::ip::address_v4 const& address) { return address.to_uint(); } + + namespace Impl + { + struct AddressFormatter : NoArgFormatterBase + { + template <typename FormatContext> + typename FormatContext::iterator format(boost::asio::ip::address_v4 const& address, FormatContext& ctx) const; + + template <typename FormatContext> + typename FormatContext::iterator format(boost::asio::ip::address_v6 const& address, FormatContext& ctx) const; + + template <typename FormatContext> + typename FormatContext::iterator format(boost::asio::ip::address const& address, FormatContext& ctx) const; + }; + } } +template <> struct fmt::formatter<boost::asio::ip::address_v4, char, void> : Trinity::Net::Impl::AddressFormatter { }; +template <> struct fmt::formatter<boost::asio::ip::address_v6, char, void> : Trinity::Net::Impl::AddressFormatter { }; +template <> struct fmt::formatter<boost::asio::ip::address, char, void> : Trinity::Net::Impl::AddressFormatter { }; + #endif // TRINITYCORE_IP_ADDRESS_H diff --git a/src/common/network/Socket.h b/src/common/network/Socket.h index e91d3198b58..b7542c9c70d 100644 --- a/src/common/network/Socket.h +++ b/src/common/network/Socket.h @@ -19,6 +19,7 @@ #define TRINITYCORE_SOCKET_H #include "Concepts.h" +#include "IpAddress.h" #include "Log.h" #include "MessageBuffer.h" #include "SocketConnectionInitializer.h" @@ -203,7 +204,7 @@ public: boost::system::error_code shutdownError; _socket.shutdown(boost::asio::socket_base::shutdown_send, shutdownError); if (shutdownError) - TC_LOG_DEBUG("network", "Socket::CloseSocket: {} errored when shutting down socket: {} ({})", GetRemoteIpAddress().to_string(), + TC_LOG_DEBUG("network", "Socket::CloseSocket: {} errored when shutting down socket: {} ({})", GetRemoteIpAddress(), shutdownError.value(), shutdownError.message()); this->OnClose(); @@ -262,7 +263,7 @@ protected: _socket.set_option(boost::asio::ip::tcp::no_delay(enable), err); if (err) TC_LOG_DEBUG("network", "Socket::SetNoDelay: failed to set_option(boost::asio::ip::tcp::no_delay) for {} - {} ({})", - GetRemoteIpAddress().to_string(), err.value(), err.message()); + GetRemoteIpAddress(), err.value(), err.message()); } private: |