Annotate

util/ip.lua @ 5587:e108c3f97f26

util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 May 2013 14:52:52 +0100
parent 5552:40e7a6cf15ff
child 5588:8c1a3243d16f
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
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
26
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27 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
28 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30 local function toBits(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
31 local result = "";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
32 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33 if ip.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34 ip = ip.toV4mapped;
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 ip = (ip.addr):upper();
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37 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
38 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
39 for i, field in ipairs(fields) do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40 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
41 for i = 1, 16 * (9 - #fields) do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
42 result = result .. "0";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
45 for i = 1, 4 - field:len() do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
46 result = result .. "0000";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
47 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
48 for i = 1, field:len() do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 result = result .. hex2bits[field:sub(i,i)];
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 end
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 return result;
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
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 local function commonPrefixLength(ipA, ipB)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 ipA, ipB = toBits(ipA), toBits(ipB);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 for i = 1, 128 do
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 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
60 return i-1;
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 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
63 return 128;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66 local function v4scope(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
67 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
68 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
69 -- Loopback:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
70 if fields[1] == 127 then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
71 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
72 -- Link-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
73 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
74 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
75 -- Global unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
76 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
77 return 0xE;
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 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
80
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
81 local function v6scope(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
82 -- Loopback:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
83 if ip:match("^[0:]*1$") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
84 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
85 -- Link-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
86 elseif ip:match("^[Ff][Ee][89ABab]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
87 return 0x2;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
88 -- Site-local unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
89 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
90 return 0x5;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
91 -- Multicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
92 elseif ip:match("^[Ff][Ff]") then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
93 return tonumber("0x"..ip:sub(4,4));
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
94 -- Global unicast:
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
95 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
96 return 0xE;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
97 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
98 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
99
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
100 local function label(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
101 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
102 return 0;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
103 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
104 return 2;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
105 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
106 return 5;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
107 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
108 return 13;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
109 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
110 return 11;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
111 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
112 return 12;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
113 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
114 return 3;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
115 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
116 return 4;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
117 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
118 return 1;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
119 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
120 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
121
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
122 local function precedence(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
123 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
124 return 50;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
125 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
126 return 30;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
127 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
128 return 5;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
129 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
130 return 3;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
131 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
132 return 1;
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 4433
diff changeset
133 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
134 return 1;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
135 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
136 return 1;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
137 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
138 return 35;
4419
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
139 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
140 return 40;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
141 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
142 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
143
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
144 local function toV4mapped(ip)
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
145 local fields = {};
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
146 local ret = "::ffff:";
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
147 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
148 ret = ret .. ("%02x"):format(fields[1]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
149 ret = ret .. ("%02x"):format(fields[2]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
150 ret = ret .. ":"
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
151 ret = ret .. ("%02x"):format(fields[3]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
152 ret = ret .. ("%02x"):format(fields[4]);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
153 return new_ip(ret, "IPv6");
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
154 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
155
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
156 function ip_methods:toV4mapped()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
157 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
158 local value = toV4mapped(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
159 self.toV4mapped = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
160 return value;
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:label()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
164 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
165 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
166 value = label(self.toV4mapped);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
167 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
168 value = label(self);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
169 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
170 self.label = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
171 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
172 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
173
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
174 function ip_methods:precedence()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
175 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
176 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
177 value = precedence(self.toV4mapped);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
178 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
179 value = precedence(self);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
180 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
181 self.precedence = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
182 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
183 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
184
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
185 function ip_methods:scope()
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
186 local value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
187 if self.proto == "IPv4" then
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
188 value = v4scope(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
189 else
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
190 value = v6scope(self.addr);
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
191 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
192 self.scope = value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
193 return value;
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
194 end
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
195
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
196 return {new_ip = new_ip,
b1e49cc314cb util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
197 commonPrefixLength = commonPrefixLength};