diff options
author | Matthew Wild <mwild1@gmail.com> | 2013-05-17 14:53:51 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2013-05-17 14:53:51 +0100 |
commit | 0fa89e6a22c699d1bb3133e8cc998fa981d1daf6 (patch) | |
tree | 509beaf4882130e8f8debec822975776533f4a91 /util/ip.lua | |
parent | e2c0ebf93d7caef842f8310411c7e5b3cce51456 (diff) | |
download | prosody-0fa89e6a22c699d1bb3133e8cc998fa981d1daf6.tar.gz prosody-0fa89e6a22c699d1bb3133e8cc998fa981d1daf6.zip |
util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Diffstat (limited to 'util/ip.lua')
-rw-r--r-- | util/ip.lua | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/util/ip.lua b/util/ip.lua index 9e668d1b..6ebc023b 100644 --- a/util/ip.lua +++ b/util/ip.lua @@ -193,5 +193,20 @@ function ip_methods:scope() return value; end +function ip_methods:private() + local private = self.scope ~= 0xE; + if not private and self.proto == "IPv4" then + local ip = self.addr; + local fields = {}; + ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end); + if fields[1] == 127 or fields[1] == 10 or (fields[1] == 192 and fields[2] == 168) + or (fields[1] == 172 and (fields[2] >= 16 or fields[2] <= 32)) then + private = true; + end + end + self.private = private; + return private; +end + return {new_ip = new_ip, commonPrefixLength = commonPrefixLength}; |