From: Peter van Dijk Date: Wed, 5 Feb 2020 12:10:41 +0000 (+0100) Subject: sdig: add basic TCP support to stdin packet parsing X-Git-Tag: dnsdist-1.5.0-alpha1~12^2~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=530e3e1a093bfc80639596527c604c7c989ada6a;p=thirdparty%2Fpdns.git sdig: add basic TCP support to stdin packet parsing --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index d9a3955a73..64d3ad8fad 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -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 \ diff --git a/pdns/sdig.cc b/pdns/sdig.cc index 0918dc9ddc..22f759e1ca 100644 --- a/pdns/sdig.cc +++ b/pdns/sdig.cc @@ -317,6 +317,7 @@ try { } else if (fromstdin) { std::istreambuf_iterator 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 index 0000000000..80683492d3 --- /dev/null +++ b/pdns/test-proxy_protocol_cc.cc @@ -0,0 +1,50 @@ +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_MAIN +#include + +#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()