TrinityCore
Loading...
Searching...
No Matches
IpAddress.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "IpAddress.h"
19#include "StringFormat.h"
20#include "Util.h"
21
22template <typename FormatContext>
23typename FormatContext::iterator Trinity::Net::Impl::AddressFormatter::format(boost::asio::ip::address_v4 const& address, FormatContext& ctx) const
24{
25 boost::system::error_code ec;
26 std::array<char, boost::asio::detail::max_addr_v4_str_len> addr_str;
27 if (boost::asio::detail::socket_ops::inet_ntop(AF_INET, address.to_bytes().data(), addr_str.data(), addr_str.size(), 0, ec) && !ec)
28 return std::ranges::copy(addr_str.begin(), CStringSentinel.Checked(addr_str.end()), ctx.out()).out;
29
30 return ctx.out();
31}
32
33template <typename FormatContext>
34typename FormatContext::iterator Trinity::Net::Impl::AddressFormatter::format(boost::asio::ip::address_v6 const& address, FormatContext& ctx) const
35{
36 boost::system::error_code ec;
37 std::array<char, boost::asio::detail::max_addr_v6_str_len> addr_str;
38 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)
39 return std::ranges::copy(addr_str.begin(), CStringSentinel.Checked(addr_str.end()), ctx.out()).out;
40
41 return ctx.out();
42}
43
44template <typename FormatContext>
45typename FormatContext::iterator Trinity::Net::Impl::AddressFormatter::format(boost::asio::ip::address const& address, FormatContext& ctx) const
46{
47 if (address.is_v4())
48 return this->format(address.to_v4(), ctx);
49
50 if (address.is_v6())
51 return this->format(address.to_v6(), ctx);
52
53 return ctx.out();
54}
55
56template TC_NETWORK_API fmt::appender Trinity::Net::Impl::AddressFormatter::format<fmt::format_context>(boost::asio::ip::address_v4 const&, fmt::format_context&) const;
57template TC_NETWORK_API fmt::appender Trinity::Net::Impl::AddressFormatter::format<fmt::format_context>(boost::asio::ip::address_v6 const&, fmt::format_context&) const;
58template TC_NETWORK_API fmt::appender Trinity::Net::Impl::AddressFormatter::format<fmt::format_context>(boost::asio::ip::address const&, fmt::format_context&) const;
#define TC_NETWORK_API
Definition Define.h:117
struct CStringSentinel_T CStringSentinel
constexpr CStringBoundedSentinel< Iterator > Checked(Iterator end) const
Definition Util.h:394
FormatContext::iterator format(boost::asio::ip::address_v4 const &address, FormatContext &ctx) const
Definition IpAddress.cpp:23