Annotate

util/ip.lua @ 8429:b3562a1b1caa

util.ip: Reflow module export table
author Kim Alvefur <zash@zash.se>
date Fri, 01 Dec 2017 01:55:40 +0100
parent 8428:ca44e462322c
child 8430:a58d560aa8d5
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 = {};
8428
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
9
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
10 local ip_mt = {
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
11 __index = function (ip, key)
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
12 return ip_methods[key](ip);
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
13 end,
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
14 __tostring = function (ip) return ip.addr; end,
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
15 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
16 };
ca44e462322c util.ip: Reflow metatable
Kim Alvefur <zash@zash.se>
parents: 8382
diff changeset
17
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7486
diff changeset
18 local hex2bits = {
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7486
diff changeset
19 ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011",
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7486
diff changeset
20 ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111",
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7486
diff changeset
21 ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011",
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7486
diff changeset
22 ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111",
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7486
diff changeset
23 };
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25 local function new_ip(ipStr, proto)
7052
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
26 if not proto then
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
27 local sep = ipStr:match("^%x+(.)");
7053
3049137d14b6 util.ip: Fix protocol detection of IPv6 addresses beginning with : [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 7052
diff changeset
28 if sep == ":" or (not(sep) and ipStr:sub(1,1) == ":") then
3049137d14b6 util.ip: Fix protocol detection of IPv6 addresses beginning with : [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 7052
diff changeset
29 proto = "IPv6"
3049137d14b6 util.ip: Fix protocol detection of IPv6 addresses beginning with : [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 7052
diff changeset
30 elseif sep == "." then
3049137d14b6 util.ip: Fix protocol detection of IPv6 addresses beginning with : [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 7052
diff changeset
31 proto = "IPv4"
7052
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
32 end
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
33 if not proto then
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
34 return nil, "invalid address";
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
35 end
306aabf2d57d util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Matthew Wild <mwild1@gmail.com>
parents: 5597
diff changeset
36 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
37 return nil, "invalid protocol";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38 end
7055
23de70d19e77 util.ip: Support zone id syntax in IPv6 addresses
Kim Alvefur <zash@zash.se>
parents: 7053
diff changeset
39 local zone;
23de70d19e77 util.ip: Support zone id syntax in IPv6 addresses
Kim Alvefur <zash@zash.se>
parents: 7053
diff changeset
40 if proto == "IPv6" and ipStr:find('%', 1, true) then
23de70d19e77 util.ip: Support zone id syntax in IPv6 addresses
Kim Alvefur <zash@zash.se>
parents: 7053
diff changeset
41 ipStr, zone = ipStr:match("^(.-)%%(.*)");
23de70d19e77 util.ip: Support zone id syntax in IPv6 addresses
Kim Alvefur <zash@zash.se>
parents: 7053
diff changeset
42 end
5597
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
43 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
44 local changed;
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
45 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
46 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
47 end);
6fe09707c73b util.ip: Convert IPv4 mapped addresses to hex.
Kim Alvefur <zash@zash.se>
parents: 5552
diff changeset
48 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
49 end
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50
7055
23de70d19e77 util.ip: Support zone id syntax in IPv6 addresses
Kim Alvefur <zash@zash.se>
parents: 7053
diff changeset
51 return setmetatable({ addr = ipStr, proto = proto, zone = zone }, ip_mt);
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
53
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
54 local function toBits(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
55 local result = "";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 if ip.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 ip = ip.toV4mapped;
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 ip = (ip.addr):upper();
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61 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
62 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
63 for i, field in ipairs(fields) do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 if field:len() == 0 and i ~= 1 and i ~= #fields then
7481
b3a864df32ef util.ip: remove unused one-letter loop variables [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7061
diff changeset
65 for _ = 1, 16 * (9 - #fields) do
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66 result = result .. "0";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
67 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
68 else
7481
b3a864df32ef util.ip: remove unused one-letter loop variables [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7061
diff changeset
69 for _ = 1, 4 - field:len() do
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
70 result = result .. "0000";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
71 end
7486
c415a3fd4485 util.ip: rename variable (i is already defined) [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7481
diff changeset
72 for j = 1, field:len() do
c415a3fd4485 util.ip: rename variable (i is already defined) [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7481
diff changeset
73 result = result .. hex2bits[field:sub(j, j)];
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
74 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
75 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
76 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
77 return result;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
78 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
79
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
80 local function commonPrefixLength(ipA, ipB)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
81 ipA, ipB = toBits(ipA), toBits(ipB);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
82 for i = 1, 128 do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
83 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
84 return i-1;
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 return 128;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
88 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
89
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
90 local function v4scope(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
91 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
92 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
93 -- Loopback:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
94 if fields[1] == 127 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
95 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
96 -- Link-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
97 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
98 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
99 -- Global unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
100 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
101 return 0xE;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
102 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
103 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
104
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
105 local function v6scope(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
106 -- Loopback:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
107 if ip:match("^[0:]*1$") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
108 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
109 -- Link-local unicast:
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5608
diff changeset
110 elseif ip:match("^[Ff][Ee][89ABab]") then
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
111 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
112 -- Site-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
113 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
114 return 0x5;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
115 -- Multicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
116 elseif ip:match("^[Ff][Ff]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
117 return tonumber("0x"..ip:sub(4,4));
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
118 -- Global unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
119 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
120 return 0xE;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
121 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
122 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
123
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
124 local function label(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
125 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
126 return 0;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
127 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
128 return 2;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
129 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
130 return 5;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
131 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
132 return 13;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
133 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
134 return 11;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
135 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
136 return 12;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
137 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
138 return 3;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
139 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
140 return 4;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
141 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
142 return 1;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
143 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
144 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
145
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
146 local function precedence(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
147 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
148 return 50;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
149 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
150 return 30;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
151 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
152 return 5;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
153 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
154 return 3;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
155 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
156 return 1;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
157 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
158 return 1;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
159 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
160 return 1;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
161 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
162 return 35;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
163 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
164 return 40;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
165 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
166 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
167
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
168 local function toV4mapped(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
169 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
170 local ret = "::ffff:";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
171 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
172 ret = ret .. ("%02x"):format(fields[1]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
173 ret = ret .. ("%02x"):format(fields[2]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
174 ret = ret .. ":"
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
175 ret = ret .. ("%02x"):format(fields[3]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
176 ret = ret .. ("%02x"):format(fields[4]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
177 return new_ip(ret, "IPv6");
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
178 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
179
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
180 function ip_methods:toV4mapped()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
181 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
182 local value = toV4mapped(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
183 self.toV4mapped = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
184 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
185 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
186
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
187 function ip_methods:label()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
188 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
189 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
190 value = label(self.toV4mapped);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
191 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
192 value = label(self);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
193 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
194 self.label = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
195 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
196 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
197
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
198 function ip_methods:precedence()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
199 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
200 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
201 value = precedence(self.toV4mapped);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
202 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
203 value = precedence(self);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
204 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
205 self.precedence = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
206 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
207 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
208
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
209 function ip_methods:scope()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
210 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
211 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
212 value = v4scope(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
213 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
214 value = v6scope(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
215 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
216 self.scope = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
217 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
218 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
219
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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234
5603
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
235 local function parse_cidr(cidr)
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
236 local bits;
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
237 local ip_len = cidr:find("/", 1, true);
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
238 if ip_len then
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
239 bits = tonumber(cidr:sub(ip_len+1, -1));
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
240 cidr = cidr:sub(1, ip_len-1);
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
241 end
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
242 return new_ip(cidr), bits;
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
243 end
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
244
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
245 local function match(ipA, ipB, bits)
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
246 local common_bits = commonPrefixLength(ipA, ipB);
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
247 if bits and ipB.proto == "IPv4" then
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
248 common_bits = common_bits - 96; -- v6 mapped addresses always share these bits
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
249 end
6926
d96b2aa7a11d util.ip: Improve comparison, == doesn't necessarily handle IPv6 addresses correctly if they aren't normalized (case, ::, etc.)
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
250 return common_bits >= (bits or 128);
5603
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
251 end
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
252
8429
b3562a1b1caa util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents: 8428
diff changeset
253 return {
b3562a1b1caa util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents: 8428
diff changeset
254 new_ip = new_ip,
5603
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
255 commonPrefixLength = commonPrefixLength,
e07f4f02e4f9 util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents: 5599
diff changeset
256 parse_cidr = parse_cidr,
8429
b3562a1b1caa util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents: 8428
diff changeset
257 match = match,
b3562a1b1caa util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents: 8428
diff changeset
258 };