You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2023/02/24 17:16:57 UTC

[trafficserver] branch master updated: libswoc: Remove TextView from inet.cc (#9438)

This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 396c49762 libswoc: Remove TextView from inet.cc (#9438)
396c49762 is described below

commit 396c49762f93e52072aabef779d9798632c354c0
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Fri Feb 24 11:16:50 2023 -0600

    libswoc: Remove TextView from inet.cc (#9438)
---
 iocore/net/SSLCertLookup.cc            | 11 ++--
 src/tscore/ink_inet.cc                 | 25 ++++-----
 src/tscore/unit_tests/test_ink_inet.cc | 94 +++++++++++++++++-----------------
 3 files changed, 64 insertions(+), 66 deletions(-)

diff --git a/iocore/net/SSLCertLookup.cc b/iocore/net/SSLCertLookup.cc
index e2fd207a2..f736f84db 100644
--- a/iocore/net/SSLCertLookup.cc
+++ b/iocore/net/SSLCertLookup.cc
@@ -29,8 +29,10 @@
 #include "tscore/Regex.h"
 #include "tscore/Trie.h"
 #include "tscore/ink_config.h"
-#include "tscore/BufferWriter.h"
-#include "tscore/bwf_std_format.h"
+
+#include "swoc/BufferWriter.h"
+#include "swoc/bwf_base.h"
+
 #include "tscore/TestBox.h"
 
 #include "I_EventSystem.h"
@@ -51,8 +53,9 @@ struct SSLAddressLookupKey {
     // lookup insensitive to address formatting and also allow the longest match semantic to produce
     // different matches if there is a certificate on the port.
 
-    ts::FixedBufferWriter w{key, sizeof(key)};
-    w.print("{}", ts::bwf::Hex_Dump(ip)); // dump as raw hex bytes, don't format as IP address.
+    swoc::FixedBufferWriter w{key, sizeof(key)};
+    w.print("{}", ip.isIp6() ? swoc::bwf::As_Hex(ip.sin6.sin6_addr) :
+                               swoc::bwf::As_Hex(ip.sin.sin_addr)); // dump as raw hex bytes, don't format as IP address.
     if (in_port_t port = ip.host_order_port(); port) {
       sep = static_cast<unsigned char>(w.size());
       w.print(".{:x}", port);
diff --git a/src/tscore/ink_inet.cc b/src/tscore/ink_inet.cc
index 09e0fe06a..d8ef8d8b8 100644
--- a/src/tscore/ink_inet.cc
+++ b/src/tscore/ink_inet.cc
@@ -33,6 +33,8 @@
 #include "tscpp/util/TextView.h"
 #include "tscore/ink_inet.h"
 
+#include "swoc/TextView.h"
+
 IpAddr const IpAddr::INVALID;
 
 using namespace std::literals;
@@ -178,7 +180,7 @@ ats_ip_nptop(sockaddr const *addr, char *dst, size_t size)
 int
 ats_ip_parse(std::string_view str, std::string_view *addr, std::string_view *port, std::string_view *rest)
 {
-  ts::TextView src(str); /// Easier to work with for parsing.
+  swoc::TextView src(str); /// Easier to work with for parsing.
   // In case the incoming arguments are null, set them here and only check for null once.
   // it doesn't matter if it's all the same, the results will be thrown away.
   std::string_view local;
@@ -223,10 +225,10 @@ ats_ip_parse(std::string_view str, std::string_view *addr, std::string_view *por
         ++src;
       }
     } else {
-      ts::TextView::size_type last = src.rfind(':');
-      if (last != ts::TextView::npos && last == src.find(':')) {
+      swoc::TextView::size_type last = src.rfind(':');
+      if (last != swoc::TextView::npos && last == src.find(':')) {
         // Exactly one colon - leave post colon stuff in @a src.
-        *addr   = src.take_prefix_at(last);
+        *addr   = src.take_prefix(last);
         colon_p = true;
       } else { // presume no port, use everything.
         *addr = src;
@@ -234,7 +236,7 @@ ats_ip_parse(std::string_view str, std::string_view *addr, std::string_view *por
       }
     }
     if (colon_p) {
-      ts::TextView tmp{src};
+      swoc::TextView tmp{src};
       src.ltrim_if(&ParseRules::is_digit);
 
       if (tmp.data() == src.data()) {               // no digits at all
@@ -304,9 +306,9 @@ ats_ip_range_parse(std::string_view src, IpAddr &lower, IpAddr &upper)
       zret = TS_ERROR;
     } else if ('/' == src[idx]) {
       if (TS_SUCCESS == addr.load(src.substr(0, idx))) { // load the address
-        ts::TextView parsed;
+        swoc::TextView parsed;
         src.remove_prefix(idx + 1); // drop address and separator.
-        int cidr = ts::svtoi(src, &parsed);
+        int cidr = swoc::svtoi(src, &parsed);
         if (parsed.size() && 0 <= cidr) { // a cidr that's a positive integer.
           // Special case the cidr sizes for 0, maximum, and for IPv6 64 bit boundaries.
           if (addr.isIp4()) {
@@ -966,13 +968,4 @@ bwformat(BufferWriter &w, BWFSpec const &spec, sockaddr const *addr)
   return w;
 }
 
-namespace bwf
-{
-  detail::MemDump
-  Hex_Dump(IpEndpoint const &addr)
-  {
-    return detail::MemDump(ats_ip_addr8_cast(&addr), ats_ip_addr_size(&addr));
-  }
-} // namespace bwf
-
 } // namespace ts
diff --git a/src/tscore/unit_tests/test_ink_inet.cc b/src/tscore/unit_tests/test_ink_inet.cc
index 6fed519ea..b022b133d 100644
--- a/src/tscore/unit_tests/test_ink_inet.cc
+++ b/src/tscore/unit_tests/test_ink_inet.cc
@@ -21,14 +21,16 @@
     limitations under the License.
 */
 
-#include "tscpp/util/TextView.h"
-#include "tscore/BufferWriter.h"
+#include "swoc/TextView.h"
+#include "swoc/BufferWriter.h"
+#include "swoc/bwf_base.h"
+#include "swoc/swoc_ip.h"
+#include "swoc/bwf_ip.h"
 #include "tscore/ink_inet.h"
 #include <catch.hpp>
 #include <iostream>
 #include "ts/apidefs.h"
 #include "tscore/ink_inet.h"
-#include "tscore/BufferWriter.h"
 
 using namespace std::literals;
 
@@ -36,10 +38,10 @@ TEST_CASE("ink_inet", "[libts][inet][ink_inet]")
 {
   // Use TextView because string_view(nullptr) fails. Gah.
   struct ip_parse_spec {
-    ts::TextView hostspec;
-    ts::TextView host;
-    ts::TextView port;
-    ts::TextView rest;
+    swoc::TextView hostspec;
+    swoc::TextView host;
+    swoc::TextView port;
+    swoc::TextView rest;
   };
 
   constexpr ip_parse_spec names[] = {
@@ -159,100 +161,100 @@ TEST_CASE("inet formatting", "[libts][ink_inet][bwformat]")
   std::string_view addr_6{"[1337:0:0:ded:BEEF:0:0:0]:53874"};
   std::string_view addr_7{"172.19.3.105:4951"};
   std::string_view addr_null{"[::]:53874"};
-  ts::LocalBufferWriter<1024> w;
+  swoc::LocalBufferWriter<1024> w;
 
   REQUIRE(0 == ats_ip_pton(addr_1, &ep.sa));
-  w.print("{}", ep);
+  w.print("{}", swoc::IPEndpoint(ep));
   REQUIRE(w.view() == addr_1);
-  w.reset().print("{::p}", ep);
+  w.clear().print("{::p}", ep);
   REQUIRE(w.view() == "8080");
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == addr_1.substr(1, 24)); // check the brackets are dropped.
-  w.reset().print("[{::a}]", ep);
+  w.clear().print("[{::a}]", ep);
   REQUIRE(w.view() == addr_1.substr(0, 26)); // check the brackets are dropped.
-  w.reset().print("[{0::a}]:{0::p}", ep);
+  w.clear().print("[{0::a}]:{0::p}", ep);
   REQUIRE(w.view() == addr_1); // check the brackets are dropped.
-  w.reset().print("{::=a}", ep);
+  w.clear().print("{::=a}", ep);
   REQUIRE(w.view() == "ffee:0000:0000:0000:24c3:3349:3cee:0143");
-  w.reset().print("{:: =a}", ep);
+  w.clear().print("{:: =a}", ep);
   REQUIRE(w.view() == "ffee:   0:   0:   0:24c3:3349:3cee: 143");
   ep.setToLoopback(AF_INET6);
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == "::1");
   REQUIRE(0 == ats_ip_pton(addr_3, &ep.sa));
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == "1337:ded:beef::");
   REQUIRE(0 == ats_ip_pton(addr_4, &ep.sa));
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == "1337::ded:beef");
 
   REQUIRE(0 == ats_ip_pton(addr_5, &ep.sa));
-  w.reset().print("{:X:a}", ep);
+  w.clear().print("{:X:a}", ep);
   REQUIRE(w.view() == "1337::DED:BEEF:0:0:956");
 
   REQUIRE(0 == ats_ip_pton(addr_6, &ep.sa));
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == "1337:0:0:ded:beef::");
 
   REQUIRE(0 == ats_ip_pton(addr_null, &ep.sa));
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == "::");
 
   REQUIRE(0 == ats_ip_pton(addr_2, &ep.sa));
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == addr_2.substr(0, 13));
-  w.reset().print("{0::a}", ep);
+  w.clear().print("{0::a}", ep);
   REQUIRE(w.view() == addr_2.substr(0, 13));
-  w.reset().print("{::ap}", ep);
+  w.clear().print("{::ap}", ep);
   REQUIRE(w.view() == addr_2);
-  w.reset().print("{::f}", ep);
+  w.clear().print("{::f}", ep);
   REQUIRE(w.view() == IP_PROTO_TAG_IPV4);
-  w.reset().print("{::fpa}", ep);
+  w.clear().print("{::fpa}", ep);
   REQUIRE(w.view() == "172.17.99.231:23995 ipv4");
-  w.reset().print("{0::a} .. {0::p}", ep);
+  w.clear().print("{0::a} .. {0::p}", ep);
   REQUIRE(w.view() == "172.17.99.231 .. 23995");
-  w.reset().print("<+> {0::a} <+> {0::p}", ep);
+  w.clear().print("<+> {0::a} <+> {0::p}", ep);
   REQUIRE(w.view() == "<+> 172.17.99.231 <+> 23995");
-  w.reset().print("<+> {0::a} <+> {0::p} <+>", ep);
+  w.clear().print("<+> {0::a} <+> {0::p} <+>", ep);
   REQUIRE(w.view() == "<+> 172.17.99.231 <+> 23995 <+>");
-  w.reset().print("{:: =a}", ep);
+  w.clear().print("{:: =a}", ep);
   REQUIRE(w.view() == "172. 17. 99.231");
-  w.reset().print("{::=a}", ep);
+  w.clear().print("{::=a}", ep);
   REQUIRE(w.view() == "172.017.099.231");
-  w.reset().print("{}", ts::bwf::Hex_Dump(ep));
+  w.clear().print("{}", swoc::bwf::As_Hex(ats_ip4_addr_cast(ep)));
   REQUIRE(w.view() == "ac1163e7");
-  w.reset().print("{:#X}", ts::bwf::Hex_Dump(ep));
+  w.clear().print("{:#X}", swoc::bwf::As_Hex(ats_ip4_addr_cast(ep)));
   REQUIRE(w.view() == "0XAC1163E7");
 
   // Documentation examples
   REQUIRE(0 == ats_ip_pton(addr_7, &ep.sa));
-  w.reset().print("To {}", ep);
+  w.clear().print("To {}", ep);
   REQUIRE(w.view() == "To 172.19.3.105:4951");
-  w.reset().print("To {0::a} on port {0::p}", ep); // no need to pass the argument twice.
+  w.clear().print("To {0::a} on port {0::p}", ep); // no need to pass the argument twice.
   REQUIRE(w.view() == "To 172.19.3.105 on port 4951");
-  w.reset().print("To {::=}", ep);
+  w.clear().print("To {::=}", ep);
   REQUIRE(w.view() == "To 172.019.003.105:04951");
-  w.reset().print("{::a}", ep);
+  w.clear().print("{::a}", ep);
   REQUIRE(w.view() == "172.19.3.105");
-  w.reset().print("{::=a}", ep);
+  w.clear().print("{::=a}", ep);
   REQUIRE(w.view() == "172.019.003.105");
-  w.reset().print("{::0=a}", ep);
+  w.clear().print("{::0=a}", ep);
   REQUIRE(w.view() == "172.019.003.105");
-  w.reset().print("{:: =a}", ep);
+  w.clear().print("{:: =a}", ep);
   REQUIRE(w.view() == "172. 19.  3.105");
-  w.reset().print("{:>20:a}", ep);
+  w.clear().print("{:>20:a}", ep);
   REQUIRE(w.view() == "        172.19.3.105");
-  w.reset().print("{:>20:=a}", ep);
+  w.clear().print("{:>20:=a}", ep);
   REQUIRE(w.view() == "     172.019.003.105");
-  w.reset().print("{:>20: =a}", ep);
+  w.clear().print("{:>20: =a}", ep);
   REQUIRE(w.view() == "     172. 19.  3.105");
-  w.reset().print("{:<20:a}", ep);
+  w.clear().print("{:<20:a}", ep);
   REQUIRE(w.view() == "172.19.3.105        ");
 
-  w.reset().print("{:p}", reinterpret_cast<sockaddr const *>(0x1337beef));
+  w.clear().print("{:p}", reinterpret_cast<sockaddr const *>(0x1337beef));
   REQUIRE(w.view() == "0x1337beef");
 
   ats_ip_pton(addr_1, &ep.sa);
-  w.reset().print("{}", ts::bwf::Hex_Dump(ep));
+  w.clear().print("{}", swoc::bwf::As_Hex(ats_ip6_addr_cast(ep)));
   REQUIRE(w.view() == "ffee00000000000024c333493cee0143");
 }