Fix IPv4 address ranges parsing.

This commit is contained in:
Nodir Temirkhodjaev 2015-01-20 15:16:56 +05:00
parent 8875b3cd18
commit 86da8e795c
2 changed files with 24 additions and 11 deletions

View File

@ -10,7 +10,10 @@ local sock = require"sys.sock"
local ip4range_to_numbers local ip4range_to_numbers
do do
local function parse_address_mask(line) local function parse_address_mask(line)
local from, sep, mask = string.match(line, "([%d%.]+)%s*(%S)%s*(%S+)") local from, sep, mask = string.match(line, "([%d%.]+)%s*([/%-])%s*(%S+)")
if not from then
return
end
local from_ip = sock.inet_pton(from, true) local from_ip = sock.inet_pton(from, true)
if not from_ip then if not from_ip then
@ -41,14 +44,8 @@ do
local iprange_from, iprange_to = {}, {} local iprange_from, iprange_to = {}, {}
local index = 0 local index = 0
for line in string.gmatch(text, "%s*(%S+)%s*\n?") do for line in string.gmatch(text, "%s*([^\n]+)") do
local from, to local from, to = parse_address_mask(line)
if line == "*" then
from, to = 0, 0xFFFFFFFF
else
from, to = parse_address_mask(line)
end
if from then if from then
index = index + 1 index = index + 1

View File

@ -1,6 +1,7 @@
-- WIPF Log Tests -- WIPF Log Tests
local sys = require"sys" local sys = require"sys"
local sock = require"sys.sock"
local wipf = require"wipflua" local wipf = require"wipflua"
local wipf_fs = require"wipf/util/fs" local wipf_fs = require"wipf/util/fs"
@ -40,6 +41,23 @@ do
end end
print"-- IPv4 Conversions"
do
local ip_range = [[
172.16.0.0/20
192.168.0.0 - 192.168.255.255
]]
local from, to = wipf_ip.ip4range_to_numbers(ip_range)
assert(from.n == 2 and to.n == 2)
assert(from[1] == sock.inet_pton("172.16.0.0", true))
assert(to[1] == sock.inet_pton("172.31.255.255", true))
assert(from[2] == sock.inet_pton("192.168.0.0", true))
assert(to[2] == sock.inet_pton("192.168.255.255", true))
print("OK")
end
print"-- Conf Read/Write" print"-- Conf Read/Write"
do do
local log_blocked = true local log_blocked = true
@ -63,11 +81,9 @@ do
local iprange_from_inc, iprange_to_inc = local iprange_from_inc, iprange_to_inc =
wipf_ip.ip4range_to_numbers(ip_include) wipf_ip.ip4range_to_numbers(ip_include)
assert(iprange_from_inc.n == iprange_to_inc.n)
local iprange_from_exc, iprange_to_exc = local iprange_from_exc, iprange_to_exc =
wipf_ip.ip4range_to_numbers(ip_exclude) wipf_ip.ip4range_to_numbers(ip_exclude)
assert(iprange_from_exc.n == iprange_to_exc.n)
print("OK") print("OK")
end end