All Projects → GUI → Lua Libcidr Ffi

GUI / Lua Libcidr Ffi

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

Programming Languages

lua
6591 projects

Projects that are alternatives of or similar to Lua Libcidr Ffi

lua-resty-ipcidr
A simple and very fast function to check against CIDR
Stars: ✭ 17 (-60.47%)
Mutual labels:  ip, openresty
Lua Resty Ctxdump
Stash and apply the old ngx.ctx for avoiding being destoried after Nginx internal redirect happens.
Stars: ✭ 35 (-18.6%)
Mutual labels:  openresty
Openwaf
Web security protection system based on openresty
Stars: ✭ 563 (+1209.3%)
Mutual labels:  openresty
Jxwaf
JXWAF(锦衣盾)是一款开源web应用防火墙
Stars: ✭ 768 (+1686.05%)
Mutual labels:  openresty
Cidranger
Fast IP to CIDR lookup in Golang
Stars: ✭ 586 (+1262.79%)
Mutual labels:  ip
Lor
a fast, minimalist web framework for lua based on OpenResty
Stars: ✭ 930 (+2062.79%)
Mutual labels:  openresty
Ip Location Zh
获取 IP 地址的真实地理位置
Stars: ✭ 556 (+1193.02%)
Mutual labels:  ip
Koa Ip
Ip filter middleware for koa, support whitelist and blacklist.
Stars: ✭ 37 (-13.95%)
Mutual labels:  ip
Rcluster.lua
lua redis client driver that support redis cluster
Stars: ✭ 31 (-27.91%)
Mutual labels:  openresty
Lua Nginx Redis
🌺 Redis、Lua、Nginx、OpenResty 笔记和资料
Stars: ✭ 757 (+1660.47%)
Mutual labels:  openresty
Api
姬长信API For Docker 一个基于多种编程语言开源免费不限制提供生活常用,出行服务,开发工具,金融服务,通讯服务和公益大数据的平台.
Stars: ✭ 743 (+1627.91%)
Mutual labels:  ip
Hostile
Simple, programmatic `/etc/hosts` manipulation (in node.js)
Stars: ✭ 654 (+1420.93%)
Mutual labels:  ip
Snoop
Snoop — инструмент разведки на основе открытых данных (OSINT world)
Stars: ✭ 886 (+1960.47%)
Mutual labels:  ip
Janus Webrtc Gateway Docker
Perfect Docker Image for Media Streaming Expert User ( https://github.com/meetecho/janus-gateway )
Stars: ✭ 582 (+1253.49%)
Mutual labels:  openresty
Cdn dig
用于在linux 终端快速查询全国 CDN 调度到哪些区域节点的工具
Stars: ✭ 36 (-16.28%)
Mutual labels:  ip
Qqwry.dat
自动更新的纯真ip库,每天自动更新
Stars: ✭ 559 (+1200%)
Mutual labels:  ip
Docker Openresty
Docker tooling for OpenResty
Stars: ✭ 678 (+1476.74%)
Mutual labels:  openresty
Dpdk Ans
ANS(Accelerated Network Stack) on DPDK, DPDK native TCP/IP stack.
Stars: ✭ 925 (+2051.16%)
Mutual labels:  ip
Vanilla
An OpenResty Lua MVC Web Framework
Stars: ✭ 1,018 (+2267.44%)
Mutual labels:  openresty
Laravel Ip Gateway
Laravel IP Gateway
Stars: ✭ 37 (-13.95%)
Mutual labels:  ip

lua-libcidr-ffi

Circle CI

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

Installation

To use lua-libcidr-ffi, libcidr must first be installed on your system. You can then install lua-libcidr-ffi via LuaRocks or OPM:

Via LuaRocks:

luarocks install libcidr-ffi

Or via OPM:

opm get GUI/lua-libcidr-ffi

Dependencies

libcidr must be installed on your system. It can be installed via system packages (if available) or manually built from source:

Packages: If binary packages are available for your distribution (available on Ubuntu 18.04 Bionic and newer or Debian 10 Buster and newer):

apt-get install libcidr-dev

Source: For other distributions where binary packages are not available, libcidr can be installed from source:

curl -OL "https://www.over-yonder.net/~fullermd/projects/libcidr/libcidr-1.2.3.tar.xz"
tar -xf libcidr-1.2.3.tar.xz
cd libcidr-1.2.3
make
sudo make install

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[, flags])

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.

Flags

An optional second argument accepts flags that can be used to control the string output format. Constants for each flag are available under cidr.flags. Multiple flags can be combined as a bitmask. The available flags are:

  • NOFLAGS: A stand-in for when you just want the default output
  • NOCOMPACT: Don't do ::-style IPv6 compaction
  • VERBOSE: Show leading 0's in octets [v6 only]
  • USEV6: Use IPv4-mapped address form for IPv4 addresses (::ffff:a.b.c.d)
  • USEV4COMPAT: Use IPv4-compat form (::a.b.c.d) instead of IPv4-mapped form (only meaningful in combination with CIDR_USEV6)
  • NETMASK: Return a netmask in standard form after the slash, instead of the prefix length. Note that the form of the netmask can thus be altered by the various flags that alter how the address is displayed.
  • ONLYADDR: Show only the address, without the prefix/netmask
  • ONLYPFLEN: Show only the prefix length (or netmask, when combined with CIDR_NETMASK), without the address.
  • WILDCARD: Show a Cisco-style wildcard mask instead of the netmask (only meaningful in combination with CIDR_NETMASK)
  • FORCEV6: Forces treating the CIDR as an IPv6 address, no matter what it really is. This doesn't do any conversion or translation; just treats the raw data as if it were IPv6.
  • FORCEV4: Forces treating the CIDR as an IPv4 address, no matter what it really is. This doesn't do any conversion or translation; just treats the raw data as if it were IPv4.
  • REVERSE: Generates a .in-addr.arpa or .ip6.arpa-style PTR record name for the given block. Note that this always treats it solely as an address; the netmask is ignored. See some notes in cidr_from_str() for details of the asymmetric treatment of this form of address representation relating to the netmask.

Examples:

local cidr = require "libcidr-ffi"
local bit = require "bit"

cidr.to_str(cidr.from_str("127.0.0.1"))
-- "127.0.0.1/32"

cidr.to_str(cidr.from_str("127.0.0.1"), cidr.flags.ONLYADDR)
-- "127.0.0.1"

cidr.to_str(cidr.from_str("127.0.0.1"), cidr.flags.USEV6)
-- "::ffff:127.0.0.1/128"

cidr.to_str(cidr.from_str("127.0.0.1"), bit.bor(cidr.flags.ONLYADDR, cidr.flags.USEV6))
-- "::ffff:127.0.0.1"

cidr.to_str(cidr.from_str("2001:db8::2:1"))
-- "2001:db8::2:1/128"

cidr.to_str(cidr.from_str("2001:db8::2:1"), cidr.flags.VERBOSE)
-- "2001:0db8::0002:0001/128"

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.

Troubleshooting

LuaRocks Installation: Could not find library

When performing the luarocks install libcidr-ffi command, if you receive an error indicating the libcidr library could not be found (Could not find library file for CIDR), then you can manually specify the location of the lib directory that contains the libcidr.so file by using the CIDR_LIBDIR argument. For example, if the library is installed in /usr/local/lib/libcidr.so:

luarocks install libcidr-ffi CIDR_LIBDIR=/usr/local/lib

Runtime: Could not find library

When requiring the libcidr-ffi module in your Lua code, if you receive an error indicating the libcidr library could not be found (libcidr.so: cannot open shared object file: No such file or directory), then you can manually specify the location of the lib directory that contains the libcidr.so file by using the LD_LIBRARY_PATH environment variable. For example, if the library is installed in /usr/local/lib/libcidr.so:

export LD_LIBRARY_PATH=/usr/local/lib
luajit -e 'require "libcidr-ffi"'

Development

After checking out the repo, Docker can be used to run the test suite:

docker-compose run --rm app make test

Release Process

To release a new version to LuaRocks and OPM:

  • Ensure CHANGELOG.md is up to date.
  • Update the _VERSION in lib/libcidr-ffi.lua.
  • Update the version in dist.ini.
  • Move the rockspec file to the new version number (git mv libcidr-ffi-X.X.X-1.rockspec libcidr-ffi-X.X.X-1.rockspec), and update the version and tag variables in the rockspec file.
  • Commit and tag the release (git tag -a vX.X.X -m "Tagging vX.X.X" && git push origin vX.X.X).
  • Run make release VERSION=X.X.X.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].