IP / CIDR Utility Module

iputils can be used for verifying if an IP address is part of a specific set of CIDR / subnets.

This module is based on lua-resty-iputils, which can be found at https://github.com/hamishforbes/lua-resty-iputils/.

1. Functions

1.1. parse_cidr

Parse an array of CIDR subnet specifications where each entry is a string. Returns a list of the subnets in binary form for later use in ip_in_cidrs.

If a subnet mask is not specified with a subnet entry, it will default to 32.

local ipu = require("iputils")

local subnets = ipu.parse_cidrs({ "10.0.0.0/24", "192.168.1.0/24" })

Arguments

  • cidrs (table) - Array of subnets to parse, each in string format

Return

  • bincidrs (table) - Parsed subnets in binary format

1.2. ip_in_cidrs

Returns true if an IP address is in a list of binary CIDR representations as returned by parse_cidrs. Returns false if not present, or nil if an error occurs.

local ipu = require("iputils")

local subnets = ipu.parse_cidrs({ "10.0.0.0/24", "192.168.1.0/24" })
inlist = ipu.ip_in_cidrs("10.0.0.1", subnets)

Arguments

  • ip (string) - IP address to verify
  • bincidrs (table) - Parsed subnets returned from parse_cidrs

Returns

  • flag (boolean) - True if in subnets, false if not, nil if error

source code: iputils.lua

results matching ""

    No results matching ""