flagfly / liblpm

Longest Prefix Match (LPM) library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

Build Status

API

  • lpm_t *lpm_create(void)

    • Construct a new LPM object.
  • void lpm_destroy(lpm_t *lpm)

    • Destroy the LPM object and any entries in it.
  • void lpm_clear(lpm_t *lpm, lpm_dtor_t *dtor, void *arg)

    • Remove all entries in the LPM object. It calls the passed destructor function, if it is not 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)

    • Insert the network address of a given length and prefix length into the LPM object and associate the entry with specified pointer value. The address must be in the network byte order. Returns 0 on success or -1 on failure.
  • int lpm_remove(lpm_t *lpm, const void *addr, size_t len, unsigned preflen)

    • Remove the network address of a given length and prefix length from the LPM object. Returns 0 on success or -1 on failure.
  • void *lpm_lookup(lpm_t *lpm, const void *addr, size_t len)

    • Lookup the given address performing the longest prefix match. Returns the associated pointer value on success or NULL on failure.
  • int lpm_strtobin(const char *cidr, void *addr, size_t *len, unsigned *preflen)

    • Convert a string in CIDR notation to a binary address, to be stored in the 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.

Lua example

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)

About

Longest Prefix Match (LPM) library

License:BSD 2-Clause "Simplified" License


Languages

Language:C 69.2%Language:Java 19.7%Language:Makefile 9.5%Language:Lua 1.6%