# HG changeset patch # User Matthew Wild # Date 1368798831 -3600 # Node ID 8c1a3243d16ff20045295ed64cfcfc0b244cb00b # Parent e108c3f97f26fd7c02b72ad3a852c5cac8696c22 util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV) diff -r e108c3f97f26 -r 8c1a3243d16f util/ip.lua --- a/util/ip.lua Fri May 17 14:52:52 2013 +0100 +++ b/util/ip.lua Fri May 17 14:53:51 2013 +0100 @@ -193,5 +193,20 @@ 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};