| 1 | package Zonemaster::Nameserver v1.0.1; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.14.2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Moose::Util::TypeConstraints; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Zonemaster::DNSName; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Zonemaster; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Zonemaster::Packet; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Zonemaster::Nameserver::Cache; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Zonemaster::Recursor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 1 | ▶ | use Net::LDNS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | use Net::IP::XS qw(:PROC); | ||
| 16 | use Time::HiRes qw[time]; | ||
| 17 | use JSON::XS; | ||
| 18 | use MIME::Base64; | ||
| 19 | use Module::Find qw[useall]; | ||
| 20 | use Carp; | ||
| 21 | use List::Util qw[max min sum]; | ||
| 22 | |||
| 23 | use overload | ||
| 24 | '""' => \&string, | ||
| 25 | 'cmp' => \&compare; | ||
| 26 | |||
| 27 | subtype 'Zonemaster::Net::IP::XS', as 'Object', where { $_->isa( 'Net::IP::XS' ) }; | ||
| 28 | coerce 'Zonemaster::Net::IP::XS', from 'Str', via { Net::IP::XS->new( $_ ) }; | ||
| 29 | |||
| 30 | has 'name' => ( is => 'ro', isa => 'Zonemaster::DNSName', coerce => 1, required => 0 ); | ||
| 31 | has 'address' => ( is => 'ro', isa => 'Zonemaster::Net::IP::XS', coerce => 1, required => 1 ); | ||
| 32 | |||
| 33 | has 'dns' => ( is => 'ro', isa => 'Net::LDNS', lazy_build => 1 ); | ||
| 34 | has 'cache' => ( is => 'ro', isa => 'Zonemaster::Nameserver::Cache', lazy_build => 1 ); | ||
| 35 | has 'times' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } ); | ||
| 36 | |||
| 37 | has 'source_address' => ( is => 'ro', isa => 'Maybe[Str]', lazy => 1, default => sub {return Zonemaster->config->resolver_source} ); | ||
| 38 | |||
| 39 | has 'fake_delegations' => ( is => 'ro', isa => 'HashRef', default => sub { {} } ); | ||
| 40 | has 'fake_ds' => ( is => 'ro', isa => 'HashRef', default => sub { {} } ); | ||
| 41 | |||
| 42 | ### | ||
| 43 | ### Variables | ||
| 44 | ### | ||
| 45 | |||
| 46 | our %object_cache; | ||
| 47 | |||
| 48 | ### | ||
| 49 | ### Build methods for attributes | ||
| 50 | ### | ||
| 51 | |||
| 52 | around 'new' => sub { | ||
| 53 | my $orig = shift; | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | my $obj = $self->$orig( @_ ); | ||
| 57 | my $name = uc( '' . $obj->name ); | ||
| 58 | $name = '$$$NONAME' unless $name; | ||
| 59 | |||
| 60 | if ( not exists $object_cache{$name}{ $obj->address->ip } ) { | ||
| 61 | Zonemaster->logger->add( NS_CREATED => { name => $name, ip => $obj->address->ip } ); | ||
| 62 | $object_cache{$name}{ $obj->address->ip } = $obj; | ||
| 63 | } | ||
| 64 | |||
| 65 | return $object_cache{$name}{ $obj->address->ip }; | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub _build_dns { | ||
| 69 | my ( $self ) = @_; | ||
| 70 | |||
| 71 | my $res = Net::LDNS->new( $self->address->ip ); | ||
| 72 | $res->recurse( 0 ); | ||
| 73 | |||
| 74 | my %defaults = %{ Zonemaster->config->resolver_defaults }; | ||
| 75 | foreach my $flag ( keys %defaults ) { | ||
| 76 | $res->$flag( $defaults{$flag} ); | ||
| 77 | } | ||
| 78 | |||
| 79 | if ( $self->source_address ) { | ||
| 80 | $res->source($self->source_address); | ||
| 81 | } | ||
| 82 | |||
| 83 | return $res; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _build_cache { | ||
| 87 | my ( $self ) = @_; | ||
| 88 | |||
| 89 | Zonemaster::Nameserver::Cache->new( { address => $self->address } ); | ||
| 90 | } | ||
| 91 | |||
| 92 | ### | ||
| 93 | ### Public Methods (and helpers) | ||
| 94 | ### | ||
| 95 | |||
| 96 | sub query { | ||
| 97 | my ( $self, $name, $type, $href ) = @_; | ||
| 98 | $type //= 'A'; | ||
| 99 | |||
| 100 | if ( $self->address->version == 4 and not Zonemaster->config->ipv4_ok ) { | ||
| 101 | Zonemaster->logger->add( IPV4_BLOCKED => { ns => $self->string } ); | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( $self->address->version == 6 and not Zonemaster->config->ipv6_ok ) { | ||
| 106 | Zonemaster->logger->add( IPV6_BLOCKED => { ns => $self->string } ); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | Zonemaster->logger->add( | ||
| 111 | 'query', | ||
| 112 | { | ||
| 113 | name => "$name", | ||
| 114 | type => $type, | ||
| 115 | flags => $href, | ||
| 116 | ip => $self->address->short | ||
| 117 | } | ||
| 118 | ); | ||
| 119 | |||
| 120 | my %defaults = %{ Zonemaster->config->resolver_defaults }; | ||
| 121 | |||
| 122 | my $class = $href->{class} // 'IN'; | ||
| 123 | my $dnssec = $href->{dnssec} // $defaults{dnssec}; | ||
| 124 | my $usevc = $href->{usevc} // $defaults{usevc}; | ||
| 125 | my $recurse = $href->{recurse} // $defaults{recurse}; | ||
| 126 | my $edns_size = $href->{edns_size} // $defaults{edns_size}; | ||
| 127 | |||
| 128 | # Fake a DS answer | ||
| 129 | if ( $type eq 'DS' and $class eq 'IN' and $self->fake_ds->{ lc( $name ) } ) { | ||
| 130 | my $p = Net::LDNS::Packet->new( $name, $type, $class ); | ||
| 131 | $p->aa( 1 ); | ||
| 132 | $p->do( $dnssec ); | ||
| 133 | $p->rd( $recurse ); | ||
| 134 | foreach my $rr ( @{ $self->fake_ds->{ lc( $name ) } } ) { | ||
| 135 | $p->unique_push( 'answer', $rr ); | ||
| 136 | } | ||
| 137 | my $res = Zonemaster::Packet->new( { packet => $p } ); | ||
| 138 | Zonemaster->logger->add( FAKE_DS_RETURNED => { name => "$name", from => "$self" } ); | ||
| 139 | return $res; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Fake a delegation | ||
| 143 | foreach my $fname ( sort keys %{ $self->fake_delegations } ) { | ||
| 144 | if ( $name =~ m/(\.|^)\Q$fname\E$/i ) { | ||
| 145 | my $p = Net::LDNS::Packet->new( $name, $type, $class ); | ||
| 146 | |||
| 147 | if ( lc( $name ) eq lc( $fname ) and $type eq 'NS' ) { | ||
| 148 | my $name = $self->fake_delegations->{$fname}{authority}; | ||
| 149 | my $addr = $self->fake_delegations->{$fname}{additional}; | ||
| 150 | $p->unique_push( 'answer', $_ ) for @$name; | ||
| 151 | $p->unique_push( 'additional', $_ ) for @$addr; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | while ( my ( $section, $aref ) = each %{ $self->fake_delegations->{$fname} } ) { | ||
| 155 | $p->unique_push( $section, $_ ) for @$aref; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | $p->aa( 0 ); | ||
| 160 | $p->do( $dnssec ); | ||
| 161 | $p->rd( $recurse ); | ||
| 162 | $p->answerfrom( $self->address->ip ); | ||
| 163 | Zonemaster->logger->add( | ||
| 164 | 'FAKE_DELEGATION', | ||
| 165 | { | ||
| 166 | name => "$name", | ||
| 167 | type => $type, | ||
| 168 | class => $class, | ||
| 169 | from => "$self", | ||
| 170 | } | ||
| 171 | ); | ||
| 172 | |||
| 173 | my $res = Zonemaster::Packet->new( { packet => $p } ); | ||
| 174 | Zonemaster->logger->add( FAKED_RETURN => { packet => $res->string } ); | ||
| 175 | return $res; | ||
| 176 | } ## end if ( $name =~ m/(\.|^)\Q$fname\E$/i) | ||
| 177 | } ## end foreach my $fname ( sort keys...) | ||
| 178 | |||
| 179 | if ( not exists( $self->cache->data->{"\U$name"}{"\U$type"}{"\U$class"}{$dnssec}{$usevc}{$recurse}{$edns_size} ) ) { | ||
| 180 | $self->cache->data->{"\U$name"}{"\U$type"}{"\U$class"}{$dnssec}{$usevc}{$recurse}{$edns_size} = | ||
| 181 | $self->_query( $name, $type, $href ); | ||
| 182 | } | ||
| 183 | |||
| 184 | my $p = $self->cache->data->{"\U$name"}{"\U$type"}{"\U$class"}{$dnssec}{$usevc}{$recurse}{$edns_size}; | ||
| 185 | Zonemaster->logger->add( CACHED_RETURN => { packet => ( $p ? $p->string : 'undef' ) } ); | ||
| 186 | |||
| 187 | return $p; | ||
| 188 | } ## end sub query | ||
| 189 | |||
| 190 | sub add_fake_delegation { | ||
| 191 | my ( $self, $domain, $href ) = @_; | ||
| 192 | my %delegation; | ||
| 193 | |||
| 194 | $domain = '' . Zonemaster::DNSName->new( $domain ); | ||
| 195 | foreach my $name ( keys %$href ) { | ||
| 196 | push @{ $delegation{authority} }, Net::LDNS::RR->new( sprintf( '%s IN NS %s', $domain, $name ) ); | ||
| 197 | foreach my $ip ( @{ $href->{$name} } ) { | ||
| 198 | if ( Net::IP::XS->new( $ip )->ip eq $self->address->ip ) { | ||
| 199 | Zonemaster->logger->add( | ||
| 200 | FAKE_DELEGATION_TO_SELF => { ns => "$self", domain => $domain, data => $href } ); | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | push @{ $delegation{additional} }, | ||
| 205 | Net::LDNS::RR->new( sprintf( '%s IN %s %s', $name, ( ip_is_ipv6( $ip ) ? 'AAAA' : 'A' ), $ip ) ); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | $self->fake_delegations->{$domain} = \%delegation; | ||
| 210 | Zonemaster->logger->add( ADDED_FAKE_DELEGATION => { ns => "$self", domain => $domain, data => $href } ); | ||
| 211 | |||
| 212 | # We're changing the world, so the cache can't be trusted | ||
| 213 | Zonemaster::Recursor->clear_cache; | ||
| 214 | |||
| 215 | return; | ||
| 216 | } ## end sub add_fake_delegation | ||
| 217 | |||
| 218 | sub add_fake_ds { | ||
| 219 | my ( $self, $domain, $aref ) = @_; | ||
| 220 | my @ds; | ||
| 221 | |||
| 222 | if ( not ref $domain ) { | ||
| 223 | $domain = Zonemaster::DNSName->new( $domain ); | ||
| 224 | } | ||
| 225 | |||
| 226 | Zonemaster->logger->add( FAKE_DS => { domain => lc( "$domain" ), data => $aref, ns => "$self" } ); | ||
| 227 | foreach my $href ( @$aref ) { | ||
| 228 | push @ds, | ||
| 229 | Net::LDNS::RR->new( | ||
| 230 | sprintf( | ||
| 231 | '%s IN DS %d %d %d %s', | ||
| 232 | "$domain", $href->{keytag}, $href->{algorithm}, $href->{type}, $href->{digest} | ||
| 233 | ) | ||
| 234 | ); | ||
| 235 | } | ||
| 236 | |||
| 237 | $self->fake_ds->{ lc( "$domain" ) } = \@ds; | ||
| 238 | |||
| 239 | # We're changing the world, so the cache can't be trusted | ||
| 240 | Zonemaster::Recursor->clear_cache; | ||
| 241 | |||
| 242 | return; | ||
| 243 | } ## end sub add_fake_ds | ||
| 244 | |||
| 245 | sub _query { | ||
| 246 | my ( $self, $name, $type, $href ) = @_; | ||
| 247 | my %flags; | ||
| 248 | |||
| 249 | $type //= 'A'; | ||
| 250 | $href->{class} //= 'IN'; | ||
| 251 | |||
| 252 | if ( Zonemaster->config->no_network ) { | ||
| 253 | croak sprintf | ||
| 254 | "External query for %s, %s attempted to %s while running with no_network", | ||
| 255 | $name, $type, $self->string; | ||
| 256 | } | ||
| 257 | |||
| 258 | Zonemaster->logger->add( | ||
| 259 | 'external_query', | ||
| 260 | { | ||
| 261 | name => "$name", | ||
| 262 | type => $type, | ||
| 263 | flags => $href, | ||
| 264 | ip => $self->address->short | ||
| 265 | } | ||
| 266 | ); | ||
| 267 | |||
| 268 | my %defaults = %{ Zonemaster->config->resolver_defaults }; | ||
| 269 | |||
| 270 | # Make sure we have a value for each flag | ||
| 271 | foreach my $flag ( keys %defaults ) { | ||
| 272 | $flags{$flag} = $href->{$flag} // $defaults{$flag}; | ||
| 273 | } | ||
| 274 | |||
| 275 | # Set flags for this query | ||
| 276 | foreach my $flag ( keys %flags ) { | ||
| 277 | $self->dns->$flag( $flags{$flag} ); | ||
| 278 | } | ||
| 279 | |||
| 280 | my $before = time(); | ||
| 281 | my $res = eval { $self->dns->query( "$name", $type, $href->{class} ) }; | ||
| 282 | if ( $@ ) { | ||
| 283 | my $msg = "$@"; | ||
| 284 | chomp( $msg ); | ||
| 285 | Zonemaster->logger->add( | ||
| 286 | LOOKUP_ERROR => { message => $msg, ns => "$self", name => "$name", type => $type, class => $href->{class} } | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | push @{ $self->times }, ( time() - $before ); | ||
| 290 | |||
| 291 | # Reset to defaults | ||
| 292 | foreach my $flag ( keys %flags ) { | ||
| 293 | $self->dns->$flag( $defaults{$flag} ); | ||
| 294 | } | ||
| 295 | |||
| 296 | if ( $res ) { | ||
| 297 | my $p = Zonemaster::Packet->new( { packet => $res } ); | ||
| 298 | Zonemaster->logger->add( EXTERNAL_RESPONSE => { packet => $p->string } ); | ||
| 299 | return $p; | ||
| 300 | } | ||
| 301 | else { | ||
| 302 | Zonemaster->logger->add( EMPTY_RETURN => {} ); | ||
| 303 | return; | ||
| 304 | } | ||
| 305 | } ## end sub _query | ||
| 306 | |||
| 307 | sub string { | ||
| 308 | my ( $self ) = @_; | ||
| 309 | |||
| 310 | return $self->name->string . '/' . $self->address->short; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub compare { | ||
| 314 | my ( $self, $other, $reverse ) = @_; | ||
| 315 | |||
| 316 | return $self->string cmp $other->string; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub save { | ||
| 320 | my ( $class, $filename ) = @_; | ||
| 321 | |||
| 322 | my $json = JSON::XS->new->allow_blessed->convert_blessed; | ||
| 323 | open my $fh, '>', $filename or die "Cache save failed: $!"; | ||
| 324 | foreach my $name ( keys %object_cache ) { | ||
| 325 | foreach my $addr ( keys %{ $object_cache{$name} } ) { | ||
| 326 | say $fh "$name $addr " . $json->encode( $object_cache{$name}{$addr}->cache->data ); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | close $fh or die $!; | ||
| 331 | |||
| 332 | Zonemaster->logger->add( SAVED_NS_CACHE => { file => $filename } ); | ||
| 333 | |||
| 334 | return; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub restore { | ||
| 338 | my ( $class, $filename ) = @_; | ||
| 339 | |||
| 340 | useall 'Net::LDNS::RR'; | ||
| 341 | my $decode = JSON::XS->new->filter_json_single_key_object( | ||
| 342 | 'Net::LDNS::Packet' => sub { | ||
| 343 | my ( $ref ) = @_; | ||
| 344 | ## no critic (Modules::RequireExplicitInclusion) | ||
| 345 | my $obj = Net::LDNS::Packet->new_from_wireformat( decode_base64( $ref->{data} ) ); | ||
| 346 | $obj->answerfrom( $ref->{answerfrom} ); | ||
| 347 | $obj->timestamp( $ref->{timestamp} ); | ||
| 348 | |||
| 349 | return $obj; | ||
| 350 | } | ||
| 351 | )->filter_json_single_key_object( | ||
| 352 | 'Zonemaster::Packet' => sub { | ||
| 353 | my ( $ref ) = @_; | ||
| 354 | |||
| 355 | return Zonemaster::Packet->new( { packet => $ref } ); | ||
| 356 | } | ||
| 357 | ); | ||
| 358 | |||
| 359 | open my $fh, '<', $filename or die "Failed to open restore data file: $!\n"; | ||
| 360 | while ( my $line = <$fh> ) { | ||
| 361 | my ( $name, $addr, $data ) = split( / /, $line, 3 ); | ||
| 362 | my $ref = $decode->decode( $data ); | ||
| 363 | my $ns = Zonemaster::Nameserver->new( | ||
| 364 | { | ||
| 365 | name => $name, | ||
| 366 | address => $addr, | ||
| 367 | cache => Zonemaster::Nameserver::Cache->new( { data => $ref, address => Net::IP::XS->new( $addr ) } ) | ||
| 368 | } | ||
| 369 | ); | ||
| 370 | } | ||
| 371 | close $fh; | ||
| 372 | |||
| 373 | Zonemaster->logger->add( RESTORED_NS_CACHE => { file => $filename } ); | ||
| 374 | |||
| 375 | return; | ||
| 376 | } ## end sub restore | ||
| 377 | |||
| 378 | sub max_time { | ||
| 379 | my ( $self ) = @_; | ||
| 380 | |||
| 381 | return max( @{ $self->times } ) // 0; | ||
| 382 | } | ||
| 383 | |||
| 384 | sub min_time { | ||
| 385 | my ( $self ) = @_; | ||
| 386 | |||
| 387 | return min( @{ $self->times } ) // 0; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub sum_time { | ||
| 391 | my ( $self ) = @_; | ||
| 392 | |||
| 393 | return sum( @{ $self->times } ) // 0; | ||
| 394 | } | ||
| 395 | |||
| 396 | sub average_time { | ||
| 397 | my ( $self ) = @_; | ||
| 398 | |||
| 399 | return 0 if @{ $self->times } == 0; | ||
| 400 | |||
| 401 | return ( $self->sum_time / scalar( @{ $self->times } ) ); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub median_time { | ||
| 405 | my ( $self ) = @_; | ||
| 406 | |||
| 407 | my @t = sort { $a <=> $b } @{ $self->times }; | ||
| 408 | my $c = scalar( @t ); | ||
| 409 | if ( $c % 2 == 0 ) { | ||
| 410 | return ( $t[ $c / 2 ] + $t[ ( $c / 2 ) - 1 ] ) / 2; | ||
| 411 | } | ||
| 412 | else { | ||
| 413 | return $t[ int( $c / 2 ) ]; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | sub stddev_time { | ||
| 418 | my ( $self ) = @_; | ||
| 419 | |||
| 420 | my $avg = $self->average_time; | ||
| 421 | my $c = scalar( @{ $self->times } ); | ||
| 422 | |||
| 423 | return 0 if $c == 0; | ||
| 424 | |||
| 425 | return sqrt( sum( map { ( $_ - $avg )**2 } @{ $self->times } ) / $c ); | ||
| 426 | } | ||
| 427 | |||
| 428 | sub all_known_nameservers { | ||
| 429 | my @res; | ||
| 430 | |||
| 431 | foreach my $n ( values %object_cache ) { | ||
| 432 | push @res, values %$n; | ||
| 433 | } | ||
| 434 | |||
| 435 | return @res; | ||
| 436 | } | ||
| 437 | |||
| 438 | sub axfr { | ||
| 439 | my ( $self, $domain, $callback, $class ) = @_; | ||
| 440 | $class //= 'IN'; | ||
| 441 | |||
| 442 | if ( Zonemaster->config->no_network ) { | ||
| 443 | croak sprintf | ||
| 444 | "External AXFR query for %s attempted to %s while running with no_network", | ||
| 445 | $domain, $self->string; | ||
| 446 | } | ||
| 447 | |||
| 448 | if ( $self->address->version == 4 and not Zonemaster->config->ipv4_ok ) { | ||
| 449 | Zonemaster->logger->add( IPV4_BLOCKED => { ns => $self->string } ); | ||
| 450 | return; | ||
| 451 | } | ||
| 452 | |||
| 453 | if ( $self->address->version == 6 and not Zonemaster->config->ipv6_ok ) { | ||
| 454 | Zonemaster->logger->add( IPV6_BLOCKED => { ns => $self->string } ); | ||
| 455 | return; | ||
| 456 | } | ||
| 457 | |||
| 458 | return $self->dns->axfr( $domain, $callback, $class ); | ||
| 459 | } ## end sub axfr | ||
| 460 | |||
| 461 | sub empty_cache { | ||
| 462 | %object_cache = (); | ||
| 463 | |||
| 464 | Zonemaster::Nameserver::Cache::empty_cache(); | ||
| 465 | |||
| 466 | return; | ||
| 467 | } | ||
| 468 | |||
| 469 | no Moose; | ||
| 470 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | ||
| 471 | |||
| 472 | 1; | ||
| 473 | |||
| 474 | =head1 NAME | ||
| 475 | |||
| 476 | Zonemaster::Nameserver - object representing a DNS nameserver | ||
| 477 | |||
| 478 | =head1 SYNOPSIS | ||
| 479 | |||
| 480 | my $ns = Zonemaster::Nameserver->new({ name => 'ns.nic.se', address => '212.247.7.228' }); | ||
| 481 | my $p = $ns->query('www.iis.se', 'AAAA'); | ||
| 482 | |||
| 483 | =head1 DESCRIPTION | ||
| 484 | |||
| 485 | This is a very central object in the L<Zonemaster> framework. All DNS | ||
| 486 | communications with the outside world pass through here, so we can do | ||
| 487 | things like synthezising and recording traffic. All the objects are | ||
| 488 | also unique per name/IP pair, and creating a new one with an already | ||
| 489 | existing pair will return the existing object instead of creating a | ||
| 490 | new one. Queries and their responses are cached by IP address, so that | ||
| 491 | a specific query will only be sent once to each address (even if there | ||
| 492 | are multiple objects for that address with different names). | ||
| 493 | |||
| 494 | Class methods on this class allows saving and loading cache contents. | ||
| 495 | |||
| 496 | =head1 ATTRIBUTES | ||
| 497 | |||
| 498 | =over | ||
| 499 | |||
| 500 | =item name | ||
| 501 | |||
| 502 | A L<Zonemaster::DNSName> object holding the nameserver's name. | ||
| 503 | |||
| 504 | =item address | ||
| 505 | |||
| 506 | A L<Net::IP::XS> object holding the nameserver's address. | ||
| 507 | |||
| 508 | =item dns | ||
| 509 | |||
| 510 | The L<Net::LDNS> object used to actually send and recieve DNS queries. | ||
| 511 | |||
| 512 | =item cache | ||
| 513 | |||
| 514 | A reference to a L<Zonemaster::Nameserver::Cache> object holding the cache of sent queries. Not meant for external use. | ||
| 515 | |||
| 516 | =item times | ||
| 517 | |||
| 518 | A reference to a list with elapsed time values for the queries made through this nameserver. | ||
| 519 | |||
| 520 | =back | ||
| 521 | |||
| 522 | =head1 CLASS METHODS | ||
| 523 | |||
| 524 | =over | ||
| 525 | |||
| 526 | =item save($filename) | ||
| 527 | |||
| 528 | Save the entire object cache to the given filename, using the | ||
| 529 | byte-order-independent Storable format. | ||
| 530 | |||
| 531 | =item restore($filename) | ||
| 532 | |||
| 533 | Replace the entire object cache with the contents of the named file. | ||
| 534 | |||
| 535 | =item all_known_nameservers() | ||
| 536 | |||
| 537 | Class method that returns a list of all nameserver objects in the global cache. | ||
| 538 | |||
| 539 | =item empty_cache() | ||
| 540 | |||
| 541 | Remove all cached nameserver objects and queries. | ||
| 542 | |||
| 543 | =back | ||
| 544 | |||
| 545 | =head1 INSTANCE METHODS | ||
| 546 | |||
| 547 | =over | ||
| 548 | |||
| 549 | =item query($name, $type, $flagref) | ||
| 550 | |||
| 551 | Send a DNS query to the nameserver the object represents. C<$name> and C<$type> are the name and type that will be queried for (C<$type> defaults | ||
| 552 | to 'A' if it's left undefined). C<$flagref> is a reference to a hash, the keys of which are flags and the values are their corresponding values. | ||
| 553 | The available flags are as follows. All but the first directly correspond to methods in the L<Net::LDNS::Resolver> object. | ||
| 554 | |||
| 555 | =over | ||
| 556 | |||
| 557 | =item class | ||
| 558 | |||
| 559 | Defaults to 'IN' if not set. | ||
| 560 | |||
| 561 | =item usevc | ||
| 562 | |||
| 563 | Send the query via TCP (only). | ||
| 564 | |||
| 565 | =item retrans | ||
| 566 | |||
| 567 | The retransmission interval | ||
| 568 | |||
| 569 | =item dnssec | ||
| 570 | |||
| 571 | Set the DO flag in the query. | ||
| 572 | |||
| 573 | =item debug | ||
| 574 | |||
| 575 | Set the debug flag in the resolver, producing output on STDERR as the query process proceeds. | ||
| 576 | |||
| 577 | =item recurse | ||
| 578 | |||
| 579 | Set the RD flag in the query. | ||
| 580 | |||
| 581 | =item udp_timeout | ||
| 582 | |||
| 583 | Set the UDP timeout for the outgoing UDP socket. May or may not be observed by the underlying network stack. | ||
| 584 | |||
| 585 | =item tcp_timeout | ||
| 586 | |||
| 587 | Set the TCP timeout for the outgoing TCP socket. May or may not be observed by the underlying network stack. | ||
| 588 | |||
| 589 | =item retry | ||
| 590 | |||
| 591 | Set the number of times the query is tried. | ||
| 592 | |||
| 593 | =item igntc | ||
| 594 | |||
| 595 | If set to true, incoming response packets with the TC flag set are not automatically retried over TCP. | ||
| 596 | |||
| 597 | =back | ||
| 598 | |||
| 599 | =item string() | ||
| 600 | |||
| 601 | Returns a string representation of the object. Normally this is just the name and IP address separated by a slash. | ||
| 602 | |||
| 603 | =item compare($other) | ||
| 604 | |||
| 605 | Used for overloading comparison operators. | ||
| 606 | |||
| 607 | =item sum_time() | ||
| 608 | |||
| 609 | Returns the total time spent sending queries and waiting for responses. | ||
| 610 | |||
| 611 | =item min_time() | ||
| 612 | |||
| 613 | Returns the shortest time spent on a query. | ||
| 614 | |||
| 615 | =item max_time() | ||
| 616 | |||
| 617 | Returns the longest time spent on a query. | ||
| 618 | |||
| 619 | =item average_time() | ||
| 620 | |||
| 621 | Returns the average time spent on queries. | ||
| 622 | |||
| 623 | =item median_time() | ||
| 624 | |||
| 625 | Returns the median query time. | ||
| 626 | |||
| 627 | =item stddev_time() | ||
| 628 | |||
| 629 | Returns the standard deviation for the whole set of query times. | ||
| 630 | |||
| 631 | =item add_fake_delegation($domain,$data) | ||
| 632 | |||
| 633 | Adds fake delegation information to this specific nameserver object. Takes the | ||
| 634 | same arguments as the similarly named method in L<Zonemaster>. This is | ||
| 635 | primarily used for internal information, and using it directly will likely give | ||
| 636 | confusing results (but may be useful to model certain kinds of | ||
| 637 | misconfigurations). | ||
| 638 | |||
| 639 | =item add_fake_ds($domain, $data) | ||
| 640 | |||
| 641 | Adds fake DS information to this nameserver object. Takes the same arguments as | ||
| 642 | the similarly named method in L<Zonemaster>. | ||
| 643 | |||
| 644 | =item axfr( $domain, $callback, $class ) | ||
| 645 | |||
| 646 | Does an AXFR for the requested domain from the nameserver. The callback | ||
| 647 | function will be called once for each received RR, with that RR as its only | ||
| 648 | argument. To continue getting more RRs, the callback must return a true value. | ||
| 649 | If it returns a true value, the AXFR will be aborted. See L<Net::LDNS::axfr> | ||
| 650 | for more details. | ||
| 651 | |||
| 652 | =back | ||
| 653 | |||
| 654 | =cut | ||