From 86da8e795c98139f22d6afc6dbb9112d8a21fe8a Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 20 Jan 2015 15:16:56 +0500 Subject: [PATCH] Fix IPv4 address ranges parsing. --- bin/lua/wipf/util/ip.lua | 15 ++++++--------- test/wipf_test.lua | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/bin/lua/wipf/util/ip.lua b/bin/lua/wipf/util/ip.lua index c4227e4d..2d26bc0a 100644 --- a/bin/lua/wipf/util/ip.lua +++ b/bin/lua/wipf/util/ip.lua @@ -10,7 +10,10 @@ local sock = require"sys.sock" local ip4range_to_numbers do 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) if not from_ip then @@ -41,14 +44,8 @@ do local iprange_from, iprange_to = {}, {} local index = 0 - for line in string.gmatch(text, "%s*(%S+)%s*\n?") do - local from, to - - if line == "*" then - from, to = 0, 0xFFFFFFFF - else - from, to = parse_address_mask(line) - end + for line in string.gmatch(text, "%s*([^\n]+)") do + local from, to = parse_address_mask(line) if from then index = index + 1 diff --git a/test/wipf_test.lua b/test/wipf_test.lua index bd937657..a841abbd 100644 --- a/test/wipf_test.lua +++ b/test/wipf_test.lua @@ -1,6 +1,7 @@ -- WIPF Log Tests local sys = require"sys" +local sock = require"sys.sock" local wipf = require"wipflua" local wipf_fs = require"wipf/util/fs" @@ -40,6 +41,23 @@ do 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" do local log_blocked = true @@ -63,11 +81,9 @@ do local iprange_from_inc, iprange_to_inc = wipf_ip.ip4range_to_numbers(ip_include) - assert(iprange_from_inc.n == iprange_to_inc.n) local iprange_from_exc, iprange_to_exc = wipf_ip.ip4range_to_numbers(ip_exclude) - assert(iprange_from_exc.n == iprange_to_exc.n) print("OK") end