Software /
code /
prosody
Annotate
util/ip.lua @ 8432:da807f4f706c
util.ip: Simplify bitstring method
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 01 Dec 2017 04:32:20 +0100 |
parent | 8431:a5a03d40a20c |
child | 8433:ca7f8e60410a |
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 |
8430
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
8 local net = require "util.net"; |
8432
da807f4f706c
util.ip: Simplify bitstring method
Kim Alvefur <zash@zash.se>
parents:
8431
diff
changeset
|
9 local hex = require "util.hex"; |
8430
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
10 |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
11 local ip_methods = {}; |
8428 | 12 |
13 local ip_mt = { | |
14 __index = function (ip, key) | |
15 return ip_methods[key](ip); | |
16 end, | |
17 __tostring = function (ip) return ip.addr; end, | |
18 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end | |
19 }; | |
20 | |
8382
e5d00bf4a4d5
util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7486
diff
changeset
|
21 local hex2bits = { |
e5d00bf4a4d5
util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7486
diff
changeset
|
22 ["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
|
23 ["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
|
24 ["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
|
25 ["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
|
26 }; |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
27 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
28 local function new_ip(ipStr, proto) |
8430
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
29 local zone; |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
30 if (not proto or proto == "IPv6") and ipStr:find('%', 1, true) then |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
31 ipStr, zone = ipStr:match("^(.-)%%(.*)"); |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
32 end |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
33 |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
34 local packed, err = net.pton(ipStr); |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
35 if not packed then return packed, err end |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
36 if proto == "IPv6" and #packed ~= 16 then |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
37 return nil, "invalid-ipv6"; |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
38 elseif proto == "IPv4" and #packed ~= 4 then |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
39 return nil, "invalid-ipv4"; |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
40 elseif not proto then |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
41 if #packed == 16 then |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
42 proto = "IPv6"; |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
43 elseif #packed == 4 then |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
44 proto = "IPv4"; |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
45 else |
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
46 return nil, "unknown protocol"; |
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
|
47 end |
8430
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
48 elseif proto ~= "IPv6" and proto ~= "IPv4" then |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
49 return nil, "invalid protocol"; |
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 |
8430
a58d560aa8d5
util.ip: Parse IP address using inet_pton
Kim Alvefur <zash@zash.se>
parents:
8429
diff
changeset
|
52 return setmetatable({ addr = ipStr, packed = packed, proto = proto, zone = zone }, ip_mt); |
4419
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 |
8431
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
55 function ip_methods.bits(ip) |
8432
da807f4f706c
util.ip: Simplify bitstring method
Kim Alvefur <zash@zash.se>
parents:
8431
diff
changeset
|
56 return hex.to(ip.packed):upper():gsub(".", hex2bits); |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
57 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
58 |
8431
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
59 function ip_methods.bits_full(ip) |
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
60 if ip.proto == "IPv4" then |
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
61 ip = ip.toV4mapped; |
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
62 end |
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
63 return ip.bits; |
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
64 end |
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
65 |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
66 local function commonPrefixLength(ipA, ipB) |
8431
a5a03d40a20c
util.ip: Make bit string function into a method
Kim Alvefur <zash@zash.se>
parents:
8430
diff
changeset
|
67 ipA, ipB = ipA.bits_full, ipB.bits_full; |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
68 for i = 1, 128 do |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
69 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
|
70 return i-1; |
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 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
73 return 128; |
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 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
76 local function v4scope(ip) |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
77 local fields = {}; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
78 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
|
79 -- Loopback: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
80 if fields[1] == 127 then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
81 return 0x2; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
82 -- Link-local unicast: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
83 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
|
84 return 0x2; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
85 -- Global unicast: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
86 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
87 return 0xE; |
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 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
90 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
91 local function v6scope(ip) |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
92 -- Loopback: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
93 if ip:match("^[0:]*1$") then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
94 return 0x2; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
95 -- Link-local unicast: |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5608
diff
changeset
|
96 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
|
97 return 0x2; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
98 -- Site-local unicast: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
99 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
100 return 0x5; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
101 -- Multicast: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
102 elseif ip:match("^[Ff][Ff]") then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
103 return tonumber("0x"..ip:sub(4,4)); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
104 -- Global unicast: |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
105 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
106 return 0xE; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
107 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
108 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
109 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
110 local function label(ip) |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
111 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
|
112 return 0; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
113 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
|
114 return 2; |
5552
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
115 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
|
116 return 5; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
117 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
|
118 return 13; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
119 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
|
120 return 11; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
121 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
|
122 return 12; |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
123 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
|
124 return 3; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
125 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
|
126 return 4; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
127 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
128 return 1; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
129 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
130 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
131 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
132 local function precedence(ip) |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
133 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
|
134 return 50; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
135 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
|
136 return 30; |
5552
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
137 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
|
138 return 5; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
139 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
|
140 return 3; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
141 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
|
142 return 1; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4433
diff
changeset
|
143 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
|
144 return 1; |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
145 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
|
146 return 1; |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
147 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
|
148 return 35; |
4419
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
149 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
150 return 40; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
151 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
152 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
153 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
154 local function toV4mapped(ip) |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
155 local fields = {}; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
156 local ret = "::ffff:"; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
157 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
|
158 ret = ret .. ("%02x"):format(fields[1]); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
159 ret = ret .. ("%02x"):format(fields[2]); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
160 ret = ret .. ":" |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
161 ret = ret .. ("%02x"):format(fields[3]); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
162 ret = ret .. ("%02x"):format(fields[4]); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
163 return new_ip(ret, "IPv6"); |
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:toV4mapped() |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
167 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
|
168 local value = toV4mapped(self.addr); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
169 self.toV4mapped = value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
170 return value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
171 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
172 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
173 function ip_methods:label() |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
174 local value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
175 if self.proto == "IPv4" then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
176 value = label(self.toV4mapped); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
177 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
178 value = label(self); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
179 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
180 self.label = value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
181 return value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
182 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
183 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
184 function ip_methods:precedence() |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
185 local value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
186 if self.proto == "IPv4" then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
187 value = precedence(self.toV4mapped); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
188 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
189 value = precedence(self); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
190 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
191 self.precedence = value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
192 return value; |
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 |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
195 function ip_methods:scope() |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
196 local value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
197 if self.proto == "IPv4" then |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
198 value = v4scope(self.addr); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
199 else |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
200 value = v6scope(self.addr); |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
201 end |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
202 self.scope = value; |
b1e49cc314cb
util.ip: New module containing IP related functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
203 return value; |
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 |
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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 end |
8c1a3243d16f
util.ip: Add 'private' method/property to determine whether an IP address is generally expected to be internet-routeable (YMMV)
Matthew Wild <mwild1@gmail.com>
parents:
5587
diff
changeset
|
217 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
|
218 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
|
219 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
|
220 |
5603
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
221 local function parse_cidr(cidr) |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
222 local bits; |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
223 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
|
224 if ip_len then |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
225 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
|
226 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
|
227 end |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
228 return new_ip(cidr), bits; |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
229 end |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
230 |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
231 local function match(ipA, ipB, bits) |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
232 local common_bits = commonPrefixLength(ipA, ipB); |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 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
|
237 end |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
238 |
8429
b3562a1b1caa
util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents:
8428
diff
changeset
|
239 return { |
b3562a1b1caa
util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents:
8428
diff
changeset
|
240 new_ip = new_ip, |
5603
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
241 commonPrefixLength = commonPrefixLength, |
e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
Matthew Wild <mwild1@gmail.com>
parents:
5599
diff
changeset
|
242 parse_cidr = parse_cidr, |
8429
b3562a1b1caa
util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents:
8428
diff
changeset
|
243 match = match, |
b3562a1b1caa
util.ip: Reflow module export table
Kim Alvefur <zash@zash.se>
parents:
8428
diff
changeset
|
244 }; |