LuaJIT FFI bindings to libcidr. Provides CIDR calculations for IPv4 and IPv6.
Lua Makefile
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib Bump to v0.1.3 to try and fix OPM upload issue. Nov 7, 2016
spec Fix memory leak. Add tests. Update docs. Oct 26, 2015
.gitignore Let's try to fix build in CI environment. Oct 26, 2015
CHANGELOG.md Bump to v0.1.3 to try and fix OPM upload issue. Nov 7, 2016
LICENSE.txt
Makefile Let's try to fix build in CI environment. Oct 26, 2015
README.md Add installation instructions. Jan 21, 2016
circle.yml Another try at CI environment fixes. Oct 26, 2015
dist.ini
libcidr-ffi-0.1.3-1.rockspec Bump to v0.1.3 to try and fix OPM upload issue. Nov 7, 2016
libcidr-ffi-git-1.rockspec Fix rockspec syntax. Jun 22, 2015

README.md

lua-libcidr-ffi

Circle CI

LuaJIT FFI bindings to libcidr. Provides CIDR calculations for IPv4 and IPv6.

Installation

The libcidr library must first be installed on your system. Then you can install lua-libcidr-ffi through LuaRocks:

$ luarocks install libcidr-ffi

Usage

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

Functions

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.

from_str

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.

to_str

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.

contains

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.

Alternatives

  • 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.