| 1 | |||
| 2 | |||
| 3 | package GlbDNS::Resolver::Base; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use List::Util qw(shuffle); | ||
| 7 | |||
| 8 | use Geo::IP; | ||
| 9 | my $gi = Geo::IP->open_type( GEOIP_CITY_EDITION_REV1, GEOIP_STANDARD); | ||
| 10 | |||
| 11 | our %known_broken_servers = ( | ||
| 12 | # opendns chicago server | ||
| 13 | '208.69.36.11' => { lat => '41.980905', lon => '-87.906654' }, | ||
| 14 | '208.69.36.12' => { lat => '41.980905', lon => '-87.906654' }, | ||
| 15 | '208.69.36.14' => { lat => '41.980905', lon => '-87.906654' }, | ||
| 16 | ); | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | return bless {}, $class | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub request { | ||
| 27 | my ($self, $glbdns, $qname, $qclass, $qtype, $peerhost, $query) = @_; | ||
| 28 | my ($rcode, $ans, $auth, $add) = (undef, [], [], []); | ||
| 29 | my $response_incudes_ns = 0; | ||
| 30 | |||
| 31 | $qname = lc($qname); | ||
| 32 | |||
| 33 | my @query = split(/\./, $qname); | ||
| 34 | |||
| 35 | # do a direct lookup here | ||
| 36 | # should probably be a hook | ||
| 37 | my $host = $glbdns->{hosts}->{$qname}; | ||
| 38 | |||
| 39 | # if the host doesnt know | ||
| 40 | # try to find something in the tree that matches | ||
| 41 | |||
| 42 | # xxx this needs wilcard support | ||
| 43 | unless($host) { | ||
| 44 | my $domain = $glbdns->get_host($qname); | ||
| 45 | |||
| 46 | # if we find something, we find out the domain and return not found | ||
| 47 | if ($domain) { | ||
| 48 | $domain = $glbdns->{hosts}->{$domain->{__DOMAIN__}}; | ||
| 49 | return ("NXDOMAIN", [], $domain->{SOA}, [],{ aa => 1}); | ||
| 50 | } | ||
| 51 | |||
| 52 | # we find nothing we refuse | ||
| 53 | return ("REFUSED", [], [], [],{ aa => 0}); | ||
| 54 | } | ||
| 55 | |||
| 56 | # unclear why go through get_host again here | ||
| 57 | my $domain = $glbdns->get_host($host->{domain}); | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | # if it is a ANY|CNAME|A|AAAA query and we have CNAME we resolve the CNAME | ||
| 62 | if (($qtype eq 'ANY' || $qtype eq 'CNAME' || $qtype eq 'A' || $qtype eq 'AAAA') && $host->{CNAME}) { | ||
| 63 | push @$ans, $self->lookup($qname, "CNAME", $host, $peerhost); | ||
| 64 | # CNAMES are only allowed to point to one resource | ||
| 65 | $qname = $host->{CNAME}->[0]->cname; | ||
| 66 | $host = $glbdns->{hosts}->{$qname}; | ||
| 67 | } | ||
| 68 | |||
| 69 | # after we resolved the CNAME, we just continue | ||
| 70 | |||
| 71 | if ($qtype eq 'ANY' || $qtype eq 'A' || $qtype eq 'PTR') { | ||
| 72 | push @$ans, $self->lookup($qname, $qtype, $host, $peerhost); | ||
| 73 | } | ||
| 74 | |||
| 75 | if ($qtype eq 'ANY' || $qtype eq 'AAAA') { | ||
| 76 | my @answer = $self->lookup($qname, $qtype, $host, $peerhost); | ||
| 77 | # if we get a specific AAAA query | ||
| 78 | # and this host exists (otherwise we wouldnt have come this far | ||
| 79 | # then we have to return to SOA in auth 0 ANS and NO ERROR | ||
| 80 | # RFC 4074 4.1 and 4.2 | ||
| 81 | |||
| 82 | # This probably applies for more things | ||
| 83 | # should we be returning glue? | ||
| 84 | if($qtype eq 'AAAA' && !@answer && !@$ans) { | ||
| 85 | return ("NOERROR", [], [@{$domain->{SOA}}], [], { aa => 1 }); | ||
| 86 | } | ||
| 87 | if($answer[0] && $answer[0]->type eq 'CNAME') { | ||
| 88 | push @$ans, $answer[0]; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | if ($qtype eq 'ANY' || $qtype eq 'NS') { | ||
| 95 | push @$ans, @{$domain->{NS}}; | ||
| 96 | $response_incudes_ns++; | ||
| 97 | } | ||
| 98 | if ($qtype eq 'ANY' || $qtype eq 'SOA') { | ||
| 99 | push @$ans, @{$domain->{SOA}}; | ||
| 100 | } | ||
| 101 | |||
| 102 | # XXX negative MX should probably be treated like AAAA | ||
| 103 | if ($qtype eq 'ANY' || $qtype eq 'MX') { | ||
| 104 | if($host->{MX}) { | ||
| 105 | push @$ans, @{$host->{MX}}; | ||
| 106 | foreach my $mx (@{$host->{MX}}) { | ||
| 107 | my $mx_host = $glbdns->get_host($mx->exchange); | ||
| 108 | push @$add, $self->lookup($mx->exchange, "A", $mx_host, $peerhost); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | if($qtype eq 'MX' && !@$ans) { | ||
| 112 | return ("NOERROR", [], [@{$domain->{SOA}}], [], { aa => 1 }); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | $auth = $domain->{NS} unless($response_incudes_ns); | ||
| 119 | foreach my $ns (@{$domain->{NS}}) { | ||
| 120 | my $ns_domain = $glbdns->get_host($ns->nsdname); | ||
| 121 | if ($ns_domain) { | ||
| 122 | push @$add, $self->lookup($ns->nsdname, "A", $ns_domain, $peerhost); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | $rcode = "NOERROR"; | ||
| 128 | |||
| 129 | return ($rcode, $ans, $auth, $add, { aa => 1 }); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub lookup { | ||
| 133 | my $self = shift; | ||
| 134 | my $qname = shift; | ||
| 135 | my $qtype = shift; | ||
| 136 | my $host = shift; | ||
| 137 | my $peerhost = shift; | ||
| 138 | my @answer; | ||
| 139 | |||
| 140 | return unless $host; | ||
| 141 | |||
| 142 | if (my $geo = $host->{__GEO__}) { | ||
| 143 | my ($lat, $lon) = (undef,undef); | ||
| 144 | if (exists($known_broken_servers{$peerhost})) { | ||
| 145 | $GblDNS::counters{"Broken|$peerhost|$qname"}++; | ||
| 146 | $lat = $known_broken_servers{$peerhost}->{lat}; | ||
| 147 | $lon = $known_broken_servers{$peerhost}->{lon}; | ||
| 148 | } else { | ||
| 149 | my $record = $gi->record_by_addr($peerhost); | ||
| 150 | if($record) { | ||
| 151 | $lat = $record->latitude; | ||
| 152 | $lon = $record->longitude; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | if (defined($lat)) { | ||
| 156 | my %distance; | ||
| 157 | foreach my $server (keys %$geo) { | ||
| 158 | |||
| 159 | $distance{$server} = $self->distance($geo->{$server}->{lat}, $geo->{$server}->{lon}, $lat, $lon); | ||
| 160 | } | ||
| 161 | |||
| 162 | my @answer; | ||
| 163 | foreach my $server (@{[sort { $distance{$a} <=> $distance{$b} } keys %distance ]}) { | ||
| 164 | next if ($geo->{$server}->{radius} && | ||
| 165 | $geo->{$server}->{radius} < $distance{$server}); | ||
| 166 | $GlbDNS::counters{"Location|$qname|$server"}++; | ||
| 167 | foreach my $host (@{$geo->{$server}->{hosts}}) { | ||
| 168 | my $key = $host->type eq 'A' ? $host->address : $host->cname; | ||
| 169 | push @answer, $host if (!exists $GlbDNS::status{$key} || $GlbDNS::status{$key}); | ||
| 170 | |||
| 171 | } | ||
| 172 | if(@answer) { | ||
| 173 | @answer = shuffle(@answer); | ||
| 174 | unshift @answer, $geo->{$server}->{source}->{$qname} if($geo->{$server}->{source}->{$qname}); | ||
| 175 | return @answer; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | $GlbDNS::counters{Failed_geo_look}++; | ||
| 180 | } | ||
| 181 | |||
| 182 | if ($qtype eq 'ANY') { | ||
| 183 | push @answer, @{$host->{A}} if $host->{A}; | ||
| 184 | push @answer, @{$host->{AAAA}} if $host->{AAAA}; | ||
| 185 | push @answer, @{$host->{CNAME}} if $host->{CNAME}; | ||
| 186 | } else { | ||
| 187 | push @answer, @{$host->{$qtype}} if ($host->{$qtype}); | ||
| 188 | } | ||
| 189 | my @filtered; | ||
| 190 | |||
| 191 | foreach my $answer (@answer) { | ||
| 192 | my $key; | ||
| 193 | if($answer->type eq 'A') { | ||
| 194 | $key = $answer->address; | ||
| 195 | } elsif($answer->type eq 'CNAME') { | ||
| 196 | $key = $answer->cname; | ||
| 197 | } else { | ||
| 198 | push @filtered, $answer; | ||
| 199 | next; | ||
| 200 | } | ||
| 201 | push @filtered, $answer if (!exists $GlbDNS::status{$key} || $GlbDNS::status{$key}); | ||
| 202 | } | ||
| 203 | return @filtered if(@filtered); #only return the filtered list if it contains SOMETHING | ||
| 204 | return @answer; | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | my $pi = atan2(1,1) * 4; | ||
| 209 | my $earth_radius = 6378; | ||
| 210 | |||
| 211 | sub distance { | ||
| 212 | my ($self, $tlat, $tlon, $slat, $slon) = @_; | ||
| 213 | |||
| 214 | my $tlat_r = int($tlat) * ($pi/180); | ||
| 215 | my $tlon_r = int($tlon) * ($pi/180); | ||
| 216 | my $slat_r = int($slat) * ($pi/180); | ||
| 217 | my $slon_r = int($slon) * ($pi/180); | ||
| 218 | |||
| 219 | # print "$tlat $tlon => $slat $slon\n"; | ||
| 220 | # print "$tlat_r $tlon_r => $slat_r $slon_r\n"; | ||
| 221 | |||
| 222 | my $delta_lat = $slat_r - $tlat_r; | ||
| 223 | my $delta_lon = $slon_r - $tlon_r; | ||
| 224 | |||
| 225 | my $temp = sin($delta_lat/2.0)**2 + cos($tlat_r) * cos($slat_r) * sin($delta_lon/2.0)**2; | ||
| 226 | |||
| 227 | return (atan2(sqrt($temp),sqrt(1-$temp)) * 12756.32); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | 1; |