]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
sdig: add basic TCP support to stdin packet parsing
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Wed, 5 Feb 2020 12:10:41 +0000 (13:10 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Mar 2020 13:12:54 +0000 (14:12 +0100)
pdns/Makefile.am
pdns/sdig.cc
pdns/test-proxy_protocol_cc.cc [new file with mode: 0644]

index d9a3955a7304d30a451d52ee65932f25bfc4d93c..64d3ad8fad0d636ba093e2d804cfd9871971412f 100644 (file)
@@ -1293,6 +1293,7 @@ testrunner_SOURCES = \
        nsecrecords.cc \
        opensslsigners.cc opensslsigners.hh \
        pollmplexer.cc \
+       proxy-protocol.cc proxy-protocol.hh \
        qtype.cc \
        rcpgenerator.cc \
        responsestats.cc \
@@ -1323,6 +1324,7 @@ testrunner_SOURCES = \
        test-nameserver_cc.cc \
        test-packetcache_cc.cc \
        test-packetcache_hh.cc \
+       test-proxy_protocol_cc.cc \
        test-rcpgenerator_cc.cc \
        test-signers.cc \
        test-sha_hh.cc \
index 0918dc9ddc31f34bcedf0dc120b6af8b0c7806bf..22f759e1ca27b6b006705f7e02b52fac0217aae8 100644 (file)
@@ -317,6 +317,7 @@ try {
   } else if (fromstdin) {
     std::istreambuf_iterator<char> begin(std::cin), end;
     reply = string(begin, end);
+    if (tcp) reply = reply.substr(2);
     printReply(reply, showflags, hidesoadetails);
   } else if (tcp) {
     Socket sock(dest.sin4.sin_family, SOCK_STREAM);
diff --git a/pdns/test-proxy_protocol_cc.cc b/pdns/test-proxy_protocol_cc.cc
new file mode 100644 (file)
index 0000000..8068349
--- /dev/null
@@ -0,0 +1,50 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_NO_MAIN
+#include <boost/test/unit_test.hpp>
+
+#include "iputils.hh"
+#include "proxy-protocol.hh"
+
+using namespace boost;
+using std::string;
+
+
+BOOST_AUTO_TEST_SUITE(test_proxy_protocol_cc)
+
+#define BINARY(s) (std::string(s, sizeof(s) - 1))
+
+#define PROXYMAGIC "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
+#define PROXYMAGICLEN sizeof(PROXYMAGIC)-1
+
+static string proxymagic(PROXYMAGIC, PROXYMAGICLEN);
+
+BOOST_AUTO_TEST_CASE(test_roundtrip) {
+  string proxyheader;
+
+  bool ptcp = true;
+  ComboAddress src("65.66.67.68:18762");  // 18762 = 0x494a = "IJ"
+  ComboAddress dest("69.70.71.72:19276"); // 19276 = 0x4b4c = "KL"
+  proxyheader = makeProxyHeader(ptcp, src, dest);
+
+  BOOST_CHECK_EQUAL(proxyheader, BINARY(
+    PROXYMAGIC
+    "\x21"          // version | command
+    "\x11"          // ipv4=0x10 | TCP=0x1
+    "\x00\x0c"      // 4 bytes IPv4 * 2 + 2 port numbers = 8 + 2 * 2 =12 = 0xc
+    "ABCD"          // 65.66.67.68
+    "EFGH"          // 69.70.71.72
+    "IJ"            // src port
+    "KL"            // dst port
+    ));
+
+  bool ptcp2;
+  ComboAddress src2, dest2;
+
+  BOOST_CHECK_EQUAL(parseProxyHeader(proxyheader.c_str(), proxyheader.size(), src2, dest2, ptcp2), 28);
+
+  BOOST_CHECK_EQUAL(ptcp2, true);
+  BOOST_CHECK(src2 == ComboAddress("65.66.67.68:18762"));
+  BOOST_CHECK(dest2 == ComboAddress("69.70.71.72:19276"));
+}
+
+BOOST_AUTO_TEST_SUITE_END()