https://github.com/humbedooh/lua-cidr
Simple CIDR block matching library for Lua
https://github.com/humbedooh/lua-cidr
cidr ip lua network
Last synced: 10 months ago
JSON representation
Simple CIDR block matching library for Lua
- Host: GitHub
- URL: https://github.com/humbedooh/lua-cidr
- Owner: Humbedooh
- License: apache-2.0
- Created: 2021-07-24T12:41:43.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-24T17:27:48.000Z (almost 5 years ago)
- Last Synced: 2025-03-27T18:52:37.822Z (about 1 year ago)
- Topics: cidr, ip, lua, network
- Language: Lua
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lua-cidr
Simple CIDR block matching library for Lua 5.1 and above
## Usage:
`cidr` works with both IPv4 and Ipv6.
Requires the bitwise operator (`bitop`) library in order to work with Lua 5.1, 5.2 or LuaJIT. When Lua 5.3, it uses the native bitwise operators.
IPv4 example:
~~~lua
local cidr = import 'cidr'
local network = cidr:network('127.0.0.1/25')
local my_ip = '127.0.0.65'
local outside_ip = '1.2.3.4'
assert(network.ipv4) -- network is ipv4
assert(not network.ipv6) -- network is not ipv6
assert(network:match(my_ip)) -- my_ip is within network range
assert(not network:match(outside_ip)) -- outside_ip is not within network range
~~~
IPv6 example:
~~~lua
local cidr = require 'cidr'
local network = cidr:network("2001:dead:beef::1/48")
local my_ip = "2001:dead:beef::2"
assert(network.ipv6) -- network is ipv6
assert(not network.ipv4) -- is not ipv4
assert(network:match(my_ip)) -- my_ip is within network range
~~~