LuaJIT FFI bindings to libcidr. Provides CIDR calculations for IPv4 and IPv6.
The libcidr library must first be installed on your system. Then you can install lua-libcidr-ffi through LuaRocks:
$ luarocks install libcidr-ffi
local cidr = require "libcidr-ffi"
cidr.contains(cidr.from_str("10.10.10.10/8"), cidr.from_str("10.20.30.40")) -- true
cidr.contains(cidr.from_str("10.10.10.10/16"), cidr.from_str("10.20.30.40")) -- false
cidr.contains(cidr.from_str("2001:db8::/32"), cidr.from_str("2001:db8:1234::1")) -- true
cidr.contains(cidr.from_str("2001:db8::/32"), cidr.from_str("2001:db9:1234::1")) -- false
For more detailed documentation of function behavior, see libcidr's own documentation. Currently, only bindings to a few of libcidr's functions are available in this Lua library, but other bindings could easily be added.
struct, err = cidr.from_str(string)
Takes in a netblock description as a human-readable string, and creates a CIDR structure from it. In case of failures, returns nil
and a string describing the error.
string, err = cidr.to_str(struct)
Takes in a CIDR structure, and generates up a human-readable string describing the netblock. In case of failures, returns nil
and a string describing the error.
bool = cidr.contains(big, small)
This function is passed two CIDR structures describing a pair of netblocks. It then determines if the latter is wholly contained within the former. In case of failures, returns nil
and a string describing the error.
- lua-resty-iputils: A pure Lua library for CIDR comparisons in OpenResty. Provides a nice higher-level API with built-in caching. Currently lacks IPv6 support.