author | Kim Alvefur <zash@zash.se> |
Fri, 21 Sep 2018 21:19:44 +0200 | |
changeset 9339 | 9e8d7d461c7d |
parent 7262 | d8300985f2bb |
child 12979 | d10957394a3c |
permissions | -rw-r--r-- |
4423
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
1 |
-- Prosody IM |
5552
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
2 |
-- Copyright (C) 2011-2013 Florian Zeitz |
4423
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
3 |
-- |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
4 |
-- This project is MIT/X11 licensed. Please see the |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
5 |
-- COPYING file in the source package for more information. |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
6 |
-- |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
7 |
|
5552
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
8 |
-- This is used to sort destination addresses by preference |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
9 |
-- during S2S connections. |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
10 |
-- We can't hand this off to getaddrinfo, since it blocks |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
11 |
|
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
12 |
local ip_commonPrefixLength = require"util.ip".commonPrefixLength |
4423
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
13 |
|
5552
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
14 |
local function commonPrefixLength(ipA, ipB) |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
15 |
local len = ip_commonPrefixLength(ipA, ipB); |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
16 |
return len < 64 and len or 64; |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
17 |
end |
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
18 |
|
4426
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
19 |
local function t_sort(t, comp) |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
20 |
for i = 1, (#t - 1) do |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
21 |
for j = (i + 1), #t do |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
22 |
local a, b = t[i], t[j]; |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
23 |
if not comp(a,b) then |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
24 |
t[i], t[j] = b, a; |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
25 |
end |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
26 |
end |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
27 |
end |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
28 |
end |
ded726418b16
util.rfc3484: Use a stable sorting algorithm
Florian Zeitz <florob@babelmonkeys.de>
parents:
4423
diff
changeset
|
29 |
|
4830
ea907059a90e
util.rfc3484: Don't pollute the global scope.
Kim Alvefur <zash@zash.se>
parents:
4426
diff
changeset
|
30 |
local function source(dest, candidates) |
4423
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
31 |
local function comp(ipA, ipB) |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
32 |
-- Rule 1: Prefer same address |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
33 |
if dest == ipA then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
34 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
35 |
elseif dest == ipB then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
36 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
37 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
38 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
39 |
-- Rule 2: Prefer appropriate scope |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
40 |
if ipA.scope < ipB.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
41 |
if ipA.scope < dest.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
42 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
43 |
else |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
44 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
45 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
46 |
elseif ipA.scope > ipB.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
47 |
if ipB.scope < dest.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
48 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
49 |
else |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
50 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
51 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
52 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
53 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
54 |
-- Rule 3: Avoid deprecated addresses |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
55 |
-- XXX: No way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
56 |
-- Rule 4: Prefer home addresses |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
57 |
-- XXX: Mobility Address related, no way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
58 |
-- Rule 5: Prefer outgoing interface |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
59 |
-- XXX: Interface to address relation. No way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
60 |
-- Rule 6: Prefer matching label |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
61 |
if ipA.label == dest.label and ipB.label ~= dest.label then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
62 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
63 |
elseif ipB.label == dest.label and ipA.label ~= dest.label then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
64 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
65 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
66 |
|
5552
40e7a6cf15ff
util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents:
4830
diff
changeset
|
67 |
-- Rule 7: Prefer temporary addresses (over public ones) |
4423
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
68 |
-- XXX: No way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
69 |
-- Rule 8: Use longest matching prefix |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
70 |
if commonPrefixLength(ipA, dest) > commonPrefixLength(ipB, dest) then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
71 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
72 |
else |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
73 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
74 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
75 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
76 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
77 |
t_sort(candidates, comp); |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
78 |
return candidates[1]; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
79 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
80 |
|
4830
ea907059a90e
util.rfc3484: Don't pollute the global scope.
Kim Alvefur <zash@zash.se>
parents:
4426
diff
changeset
|
81 |
local function destination(candidates, sources) |
4423
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
82 |
local sourceAddrs = {}; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
83 |
local function comp(ipA, ipB) |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
84 |
local ipAsource = sourceAddrs[ipA]; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
85 |
local ipBsource = sourceAddrs[ipB]; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
86 |
-- Rule 1: Avoid unusable destinations |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
87 |
-- XXX: No such information |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
88 |
-- Rule 2: Prefer matching scope |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
89 |
if ipA.scope == ipAsource.scope and ipB.scope ~= ipBsource.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
90 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
91 |
elseif ipA.scope ~= ipAsource.scope and ipB.scope == ipBsource.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
92 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
93 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
94 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
95 |
-- Rule 3: Avoid deprecated addresses |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
96 |
-- XXX: No way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
97 |
-- Rule 4: Prefer home addresses |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
98 |
-- XXX: Mobility Address related, no way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
99 |
-- Rule 5: Prefer matching label |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
100 |
if ipAsource.label == ipA.label and ipBsource.label ~= ipB.label then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
101 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
102 |
elseif ipBsource.label == ipB.label and ipAsource.label ~= ipA.label then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
103 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
104 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
105 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
106 |
-- Rule 6: Prefer higher precedence |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
107 |
if ipA.precedence > ipB.precedence then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
108 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
109 |
elseif ipA.precedence < ipB.precedence then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
110 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
111 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
112 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
113 |
-- Rule 7: Prefer native transport |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
114 |
-- XXX: No way to determine this |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
115 |
-- Rule 8: Prefer smaller scope |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
116 |
if ipA.scope < ipB.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
117 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
118 |
elseif ipA.scope > ipB.scope then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
119 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
120 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
121 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
122 |
-- Rule 9: Use longest matching prefix |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
123 |
if commonPrefixLength(ipA, ipAsource) > commonPrefixLength(ipB, ipBsource) then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
124 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
125 |
elseif commonPrefixLength(ipA, ipAsource) < commonPrefixLength(ipB, ipBsource) then |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
126 |
return false; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
127 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
128 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
129 |
-- Rule 10: Otherwise, leave order unchanged |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
130 |
return true; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
131 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
132 |
for _, ip in ipairs(candidates) do |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
133 |
sourceAddrs[ip] = source(ip, sources); |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
134 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
135 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
136 |
t_sort(candidates, comp); |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
137 |
return candidates; |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
138 |
end |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
139 |
|
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
140 |
return {source = source, |
4314eeeed394
util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
141 |
destination = destination}; |