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