abstract out lpmwrapper, add test
This commit is contained in:
parent
2744d576a7
commit
e7897a9832
38
lpmwrapper.hh
Normal file
38
lpmwrapper.hh
Normal file
@ -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;
|
||||
};
|
38
teller.cc
38
teller.cc
@ -6,46 +6,10 @@
|
||||
#include <atomic>
|
||||
#include <pcaudiolib/audio.h>
|
||||
#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
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user