Subject: | SRV rrsort is incorrect |
The current SRV record sorting algorithm is this:
my ( $a, $b ) = ( $Net::DNS::a, $Net::DNS::b );
$a->{priority} <=> $b->{priority}
|| $b->{weight} <=> $a->{weight};
Alas, this just makes weight a less significant priority.
RFC2782 defines how to deal with SRV records. Here's the section on sorting SRV records by weight (it first says to sort by priority; this section is just for RRs of the same weight):
To select a target to be contacted next, arrange all SRV RRs
(that have not been ordered yet) in any order, except that all
those with weight 0 are placed at the beginning of the list.
Compute the sum of the weights of those RRs, and with each RR
associate the running sum in the selected order. Then choose a
uniform random number between 0 and the sum computed
(inclusive), and select the RR whose running sum value is the
first in the selected order which is greater than or equal to
the random number selected. The target host specified in the
selected SRV RR is the next one to be contacted by the client.
Remove this SRV RR from the set of the unordered SRV RRs and
apply the described algorithm to the unordered SRV RRs to select
the next target host. Continue the ordering process until there
are no unordered SRV RRs. This process is repeated for each
Priority.
This seems like an intrisically N^2 algorithm, but luckily the number of records is expected to be small.
I'll take a stab as preparing a patch, but I wanted to see if already had thought about this issue.
Thanks.