changeset 5587 | e108c3f97f26 |
parent 5552 | 40e7a6cf15ff |
child 5588 | 8c1a3243d16f |
5586:7e1264bf7af8 | 5587:e108c3f97f26 |
---|---|
10 __tostring = function (ip) return ip.addr; end, |
10 __tostring = function (ip) return ip.addr; end, |
11 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end}; |
11 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end}; |
12 local hex2bits = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011", ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111", ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011", ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111" }; |
12 local hex2bits = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011", ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111", ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011", ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111" }; |
13 |
13 |
14 local function new_ip(ipStr, proto) |
14 local function new_ip(ipStr, proto) |
15 if proto ~= "IPv4" and proto ~= "IPv6" then |
15 if not proto then |
16 local sep = ipStr:match("^%x+(.)"); |
|
17 if sep == ":" then proto = "IPv6" |
|
18 elseif sep == "." then proto = "IPv4" |
|
19 end |
|
20 if not proto then |
|
21 return nil, "invalid address"; |
|
22 end |
|
23 elseif proto ~= "IPv4" and proto ~= "IPv6" then |
|
16 return nil, "invalid protocol"; |
24 return nil, "invalid protocol"; |
17 end |
25 end |
18 |
26 |
19 return setmetatable({ addr = ipStr, proto = proto }, ip_mt); |
27 return setmetatable({ addr = ipStr, proto = proto }, ip_mt); |
20 end |
28 end |