Software / code / prosody
Comparison
util/ip.lua @ 7052:306aabf2d57d
util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 17 May 2013 14:52:52 +0100 |
| parent | 5597:6fe09707c73b |
| child | 7053:3049137d14b6 |
comparison
equal
deleted
inserted
replaced
| 7051:ecfa474ff570 | 7052:306aabf2d57d |
|---|---|
| 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 if proto == "IPv6" and ipStr:find('.', 1, true) then | 26 if proto == "IPv6" and ipStr:find('.', 1, true) then |
| 19 local changed; | 27 local changed; |
| 20 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d) | 28 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d) |