From: Pieter Lexis Date: Thu, 30 Jul 2020 11:22:43 +0000 (+0200) Subject: iputils: Add function to print comma-separated CA containers X-Git-Tag: auth-4.4.0-alpha1~2^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=27a82613d02920c815d426a40415bc177d1a3825;p=thirdparty%2Fpdns.git iputils: Add function to print comma-separated CA containers --- diff --git a/pdns/iputils.hh b/pdns/iputils.hh index b8ea84fab3..1283921d7f 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -360,6 +360,25 @@ union ComboAddress { } return false; } + + /*! Returns a comma-separated string of IP addresses + * + * \param c An stl container with ComboAddresses + * \param withPort Also print the port (default true) + * \param portExcept Print the port, except when this is the port (default 53) + */ + template < template < class ... > class Container, class ... Args > + static string caContainerToString(const Container& c, const bool withPort = true, const uint16_t portExcept = 53) { + vector strs; + for (const auto& ca : c) { + if (withPort) { + strs.push_back(ca.toStringWithPortExcept(portExcept)); + continue; + } + strs.push_back(ca.toString()); + } + return boost::join(strs, ","); + }; }; /** This exception is thrown by the Netmask class and by extension by the NetmaskGroup class */ diff --git a/pdns/test-iputils_hh.cc b/pdns/test-iputils_hh.cc index 04a010f229..cc54392953 100644 --- a/pdns/test-iputils_hh.cc +++ b/pdns/test-iputils_hh.cc @@ -795,4 +795,29 @@ BOOST_AUTO_TEST_CASE(test_iterator) { } } +BOOST_AUTO_TEST_CASE(test_ComboAddress_caContainerToString) { + ComboAddress ca1("192.0.2.1:53"); + ComboAddress ca2("192.0.2.2:5300"); + ComboAddress ca3("[2001:db8:53::3]:53"); + ComboAddress ca4("[2001:db8:53::4]:5300"); + + set caSet({ca1, ca2, ca3, ca4}); + vector caVector({ca1, ca2, ca3, ca4}); + + string caSetStr = ComboAddress::caContainerToString(caSet, false); + string caVectorStr = ComboAddress::caContainerToString(caVector, false); + BOOST_CHECK_EQUAL(caSetStr, "192.0.2.1,192.0.2.2,2001:db8:53::3,2001:db8:53::4"); + BOOST_CHECK_EQUAL(caVectorStr, "192.0.2.1,192.0.2.2,2001:db8:53::3,2001:db8:53::4"); + + caSetStr = ComboAddress::caContainerToString(caSet, true); + caVectorStr = ComboAddress::caContainerToString(caVector, true); + BOOST_CHECK_EQUAL(caSetStr, "192.0.2.1,192.0.2.2:5300,2001:db8:53::3,[2001:db8:53::4]:5300"); + BOOST_CHECK_EQUAL(caVectorStr, "192.0.2.1,192.0.2.2:5300,2001:db8:53::3,[2001:db8:53::4]:5300"); + + caSetStr = ComboAddress::caContainerToString(caSet, true, 0); + caVectorStr = ComboAddress::caContainerToString(caVector, true, 0); + BOOST_CHECK_EQUAL(caSetStr, "192.0.2.1:53,192.0.2.2:5300,[2001:db8:53::3]:53,[2001:db8:53::4]:5300"); + BOOST_CHECK_EQUAL(caVectorStr, "192.0.2.1:53,192.0.2.2:5300,[2001:db8:53::3]:53,[2001:db8:53::4]:5300"); +} + BOOST_AUTO_TEST_SUITE_END()