Skip to content

Commit 2cd0996

Browse files
author
Aaron Meihm
committed
iputils: use lpeg for octet split
1 parent 93c6251 commit 2cd0996

File tree

4 files changed

+8
-22
lines changed

4 files changed

+8
-22
lines changed

lpeg/CMakeLists.txt.lpeg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
6-
project(lpeg VERSION 1.0.10 LANGUAGES C)
6+
project(lpeg VERSION 1.0.11 LANGUAGES C)
77

88
set(CPACK_PACKAGE_NAME luasandbox-${PROJECT_NAME})
99
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Lua LPeg Module (Parsing Expression Grammars)")

lpeg/modules/lpeg/ip_address.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
* `host_field` - returns the host as a Heka message field table `{value="127.0.0.1", representation="ipv4"}`
1515
* 'v4_field' - converts a version 4 ip address into a Heka message field
1616
* `v6_field` - converts a version 6 ip address into a Heka message field
17+
* 'v4_octets' - converts a version 4 ip address into an array of octets
1718
--]]
1819

1920
-- Imports
2021
local string = require "string"
2122
local l = require "lpeg"
23+
local tonumber = tonumber
2224
l.locale(l)
2325

2426
local M = {}
@@ -31,6 +33,8 @@ local d8 = "1" * l.digit * l.digit
3133
+ l.digit
3234
v4 = d8 * "." * d8 * "." * d8 * "." * d8
3335

36+
local octet = d8 / tonumber
37+
v4_octets = l.Ct(octet * "." * octet * "." * octet * "." * octet)
3438

3539
local h16 = l.xdigit * l.xdigit^-3
3640
local hg = h16 * ":"

moz_security/modules/iputils.lua

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ local tonumber = tonumber
5555
local tostring = tostring
5656
local type = type
5757

58+
local ipa = require("lpeg.ip_address")
5859
local bit = require("bit")
5960
local string = require("string")
6061

@@ -78,27 +79,7 @@ end
7879

7980

8081
local function split_octets(input)
81-
local pos = 0
82-
local prev = 0
83-
local octs = {}
84-
85-
for i=1,4 do
86-
pos = string.find(input, ".", prev, true)
87-
if pos then
88-
if i == 4 then
89-
return nil
90-
end
91-
octs[i] = string.sub(input, prev, pos-1)
92-
elseif i == 4 then
93-
octs[i] = string.sub(input, prev, -1)
94-
break
95-
else
96-
return nil
97-
end
98-
prev = pos + 1
99-
end
100-
101-
return octs
82+
return ipa.v4_octets:match(input)
10283
end
10384

10485

moz_security/tests/iputils.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ chk_ip_in_cidr(true, "10.0.0.1", pc)
4747
chk_ip_in_cidr(true, "10.0.254.254", pc)
4848
chk_ip_in_cidr(false, "10.2.0.0", pc)
4949
chk_ip_in_cidr(false, "172.16.25.25", pc)
50+
chk_ip_in_cidr(nil, "1.2", pc)
5051
chk_ip_in_cidr(nil, "invalid", pc)
5152
chk_ip_in_cidr(nil, nil, pc)
5253
chk_ip_in_cidr(nil, nil, nil)

0 commit comments

Comments
 (0)