Annotate

util/ip.lua @ 5599:34e9f237b915

Merge 0.9->trunk
author Kim Alvefur <zash@zash.se>
date Sat, 18 May 2013 13:20:46 +0200
parent 5588:8c1a3243d16f
parent 5597:6fe09707c73b
child 5603:e07f4f02e4f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
1 -- Prosody IM
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2 -- Copyright (C) 2008-2011 Florian Zeitz
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3 --
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
6 --
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8 local ip_methods = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
9 local ip_mt = { __index = function (ip, key) return (ip_methods[key])(ip); end,
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10 __tostring = function (ip) return ip.addr; end,
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
11 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
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" };
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 local function new_ip(ipStr, proto)
5587
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
15 if not proto then
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
16 local sep = ipStr:match("^%x+(.)");
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
17 if sep == ":" then proto = "IPv6"
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
18 elseif sep == "." then proto = "IPv4"
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
19 end
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
20 if not proto then
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
21 return nil, "invalid address";
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
22 end
e108c3f97f26 util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
23 elseif proto ~= "IPv4" and proto ~= "IPv6" then
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24 return nil, "invalid protocol";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25 end
5597
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
26 if proto == "IPv6" and ipStr:find('.', 1, true) then
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
27 local changed;
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
28 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d)
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
29 return (":%04X:%04X"):format(a*256+b,c*256+d);
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
30 end);
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
31 if changed ~= 1 then return nil, "invalid-address"; end
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
32 end
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34 return setmetatable({ addr = ipStr, proto = proto }, ip_mt);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
36
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37 local function toBits(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38 local result = "";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
39 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40 if ip.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
41 ip = ip.toV4mapped;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
42 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43 ip = (ip.addr):upper();
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
45 if not ip:match(":$") then fields[#fields] = nil; end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
46 for i, field in ipairs(fields) do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
47 if field:len() == 0 and i ~= 1 and i ~= #fields then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
48 for i = 1, 16 * (9 - #fields) do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 result = result .. "0";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 for i = 1, 4 - field:len() do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
53 result = result .. "0000";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
54 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
55 for i = 1, field:len() do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 result = result .. hex2bits[field:sub(i,i)];
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60 return result;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
62
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
63 local function commonPrefixLength(ipA, ipB)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 ipA, ipB = toBits(ipA), toBits(ipB);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65 for i = 1, 128 do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66 if ipA:sub(i,i) ~= ipB:sub(i,i) then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
67 return i-1;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
68 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
69 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
70 return 128;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
71 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
72
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
73 local function v4scope(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
74 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
75 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
76 -- Loopback:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
77 if fields[1] == 127 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
78 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
79 -- Link-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
80 elseif fields[1] == 169 and fields[2] == 254 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
81 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
82 -- Global unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
83 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
84 return 0xE;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
85 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
86 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
87
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
88 local function v6scope(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
89 -- Loopback:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
90 if ip:match("^[0:]*1$") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
91 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
92 -- Link-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
93 elseif ip:match("^[Ff][Ee][89ABab]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
94 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
95 -- Site-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
96 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
97 return 0x5;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
98 -- Multicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
99 elseif ip:match("^[Ff][Ff]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
100 return tonumber("0x"..ip:sub(4,4));
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
101 -- Global unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
102 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
103 return 0xE;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
104 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
105 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
106
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
107 local function label(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
108 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
109 return 0;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
110 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
111 return 2;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
112 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
113 return 5;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
114 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
115 return 13;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
116 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
117 return 11;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
118 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
119 return 12;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
120 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
121 return 3;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
122 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
123 return 4;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
124 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
125 return 1;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
126 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
127 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
128
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
129 local function precedence(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
130 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
131 return 50;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
132 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
133 return 30;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
134 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
135 return 5;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
136 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
137 return 3;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
138 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
139 return 1;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
140 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
141 return 1;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
142 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
143 return 1;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
144 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
145 return 35;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
146 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
147 return 40;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
148 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
149 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
150
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
151 local function toV4mapped(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
152 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
153 local ret = "::ffff:";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
154 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
155 ret = ret .. ("%02x"):format(fields[1]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
156 ret = ret .. ("%02x"):format(fields[2]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
157 ret = ret .. ":"
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
158 ret = ret .. ("%02x"):format(fields[3]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
159 ret = ret .. ("%02x"):format(fields[4]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
160 return new_ip(ret, "IPv6");
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
161 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
162
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
163 function ip_methods:toV4mapped()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
164 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
165 local value = toV4mapped(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
166 self.toV4mapped = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
167 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
168 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
169
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
170 function ip_methods:label()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
171 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
172 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
173 value = label(self.toV4mapped);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
174 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
175 value = label(self);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
176 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
177 self.label = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
178 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
179 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
180
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
181 function ip_methods:precedence()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
182 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
183 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
184 value = precedence(self.toV4mapped);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
185 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
186 value = precedence(self);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
187 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
188 self.precedence = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
189 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
190 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
191
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
192 function ip_methods:scope()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
193 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
194 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
195 value = v4scope(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
196 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
197 value = v6scope(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
198 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
199 self.scope = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
200 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
201 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
202
5588
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
203 function ip_methods:private()
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
204 local private = self.scope ~= 0xE;
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
205 if not private and self.proto == "IPv4" then
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
206 local ip = self.addr;
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
207 local fields = {};
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
208 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
209 if fields[1] == 127 or fields[1] == 10 or (fields[1] == 192 and fields[2] == 168)
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
210 or (fields[1] == 172 and (fields[2] >= 16 or fields[2] <= 32)) then
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
211 private = true;
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
212 end
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
213 end
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
214 self.private = private;
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
215 return private;
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
216 end
8c1a3243d16f util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents: 5587
diff changeset
217
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
218 return {new_ip = new_ip,
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
219 commonPrefixLength = commonPrefixLength};