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/network/IpAddress.cpp | |
parent | 2d984fcade155bf860ed20713a95251ad0103318 (diff) |
Core/Network: Make ip address formattable with fmt
Diffstat (limited to 'src/common/network/IpAddress.cpp')
-rw-r--r-- | src/common/network/IpAddress.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
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; |