diff --git a/lpmwrapper.hh b/lpmwrapper.hh new file mode 100644 index 0000000..a83b059 --- /dev/null +++ b/lpmwrapper.hh @@ -0,0 +1,38 @@ +extern "C" { +#include "ext/lpm.h" +} + +class LPMWrapper +{ +public: + LPMWrapper() + { + d_lpm = lpm_create(); + } + ~LPMWrapper() + { + lpm_destroy(d_lpm); + } + + void insert(const std::string& str, void* val=(void*)1) + { + char addr[16]; + size_t len=sizeof(addr); + unsigned preflen=0; + lpm_strtobin(str.c_str(), addr, &len, &preflen); + if(lpm_insert(d_lpm, addr, len, preflen, val) < 0) + throw std::runtime_error("Error inserting prefix"); + } + + void* lookup(const char*str) + { + char addr[16]; + size_t len=sizeof(addr); + unsigned preflen=0; + lpm_strtobin(str, addr, &len, &preflen); + return lpm_lookup(d_lpm, addr, len); + } + +private: + lpm_t* d_lpm; +}; diff --git a/teller.cc b/teller.cc index 4e4e02c..5ce6582 100644 --- a/teller.cc +++ b/teller.cc @@ -6,46 +6,10 @@ #include #include #include "ext/toml.hpp" -extern "C" { -#include "ext/lpm.h" -} +#include "lpmwrapper.hh" using namespace std; -class LPMWrapper -{ -public: - LPMWrapper() - { - d_lpm = lpm_create(); - } - ~LPMWrapper() - { - lpm_destroy(d_lpm); - } - - void insert(const std::string& str, void* val=(void*)1) - { - char addr[16]; - size_t len=sizeof(addr); - unsigned preflen=0; - lpm_strtobin(str.c_str(), addr, &len, &preflen); - if(lpm_insert(d_lpm, addr, len, preflen, val) < 0) - throw std::runtime_error("Error inserting prefix"); - } - - void* lookup(const char*str) - { - char addr[16]; - size_t len=sizeof(addr); - unsigned preflen=0; - lpm_strtobin(str, addr, &len, &preflen); - return lpm_lookup(d_lpm, addr, len); - } - -private: - lpm_t* d_lpm; -}; struct TrackerConf { diff --git a/testrunner.cc b/testrunner.cc index 5ab59e3..6796eca 100644 --- a/testrunner.cc +++ b/testrunner.cc @@ -1,10 +1,15 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "ext/doctest.h" -#include "ext/lpm.h" +#include "lpmwrapper.hh" using namespace std; TEST_CASE("basic test") { - CHECK(1==1); + LPMWrapper t; + void* ptr = (void*)1; + t.insert("127.0.0.1/32", ptr); + + CHECK(t.lookup("127.0.0.1") == ptr); + CHECK(t.lookup("127.0.0.2") == 0); }