Longest Prefix Match (LPM) library
Longest Prefix Match (LPM) library supporting IPv4 and IPv6. The implementation is written in C99 and is distributed under the 2-clause BSD license.
Additionally, bindings are available for Lua and Java. Specifications to build RPM and DEB packages are also provided.
lpm_t *lpm_create(void)
void lpm_destroy(lpm_t *lpm)
void lpm_clear(lpm_t *lpm, lpm_dtor_t *dtor, void *arg)
NULL
, as it traverses the entries. The destructor
function prototype:typedef void (*lpm_dtor_t)(void *arg, const void *key, size_t len, void *val);
int lpm_insert(lpm_t *lpm, const void *addr, size_t len, unsigned preflen, void *val)
int lpm_remove(lpm_t *lpm, const void *addr, size_t len, unsigned preflen)
void *lpm_lookup_prefix(lpm_t *lpm, const void *addr, size_t len, unsigned preflen)
NULL
on failure.void *lpm_lookup(lpm_t *lpm, const void *addr, size_t len)
NULL
on failure.int lpm_strtobin(const char *cidr, void *addr, size_t *len, unsigned *preflen)
addr
buffer and its length in len
, as well as the prefix length (if
not specified, then the maximum length of the address family will be set).
The address will be stored in the network byte order. Its buffer must
provide at least 4 or 16 bytes (depending on the address family). Returns
zero on success and -1 on failure.local lpm = require("lpm")
local acl = lpm.new()
local some_info = { val = "test" }
local addr, preflen = lpm.tobin("10.0.0.0/24")
if not acl:insert(addr, preflen, some_info) then
print("acl:insert() failed")
return -1
end
local ret = acl:lookup(lpm.tobin("10.0.0.100"))
print(ret.val)
See README how to build the JAR and the test case as example how to use the Java API
Just build the package, install it and link the library using the
-llpm
flag.
cd pkg && make rpm
cd pkg && make deb