Net-DNS-QueryID

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

Artistic
Changes
Copying
Makefile.PL
MANIFEST			This list of files
MANIFEST.SKIP
README
QueryID.pm
t/notbusy.t
t/random.t
t/wrapover.t
META.yml                                 Module meta-data (added by MakeMaker)

QueryID.pm  view on Meta::CPAN


require Exporter;
@ISA = qw(Exporter);


$VERSION = do { my @r = (q$Revision: 0.02 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

@EXPORT_OK = qw(
	id_get
	id_clr
	id_busy
);

sub DESTROY {};

=head1	NAME

Net::DNS::QueryID - random Query ID numbers

=head1 SYNOPSIS

QueryID.pm  view on Meta::CPAN


The purpose of this module is to provide and unpredictable source of 16 bit
DNS Query ID numbers to help defeat cache poisoning using DNS Spoofing or "Man in the Middle"
attacks as describe in the Wikipedia article and its references:

	http://en.wikipedia.org/wiki/DNS_cache_poisoning

  use Net::DNS::QueryID qw(
	id_get
	id_clr
	id_busy
  );

  $queryID = id_get();
  $result  = id_clr($queryID);
  $result  = id_busy($queryID);

=cut

my $idvec = '';
foreach(0..2047) {	# set 65536 long vector string to zero
  vec($idvec,$_,32) = 0x0;
}

my $test = 0;

QueryID.pm  view on Meta::CPAN


=cut

sub id_clr($) {
  return 0 if $_[0] < 1 || $_[0] > 65535;
  return 0 unless vec($idvec,$_[0],1);
  vec($idvec,$_[0],1) = 0x0;
  return $_[0];
}

=item * $result  = id_busy($queryID);

  input:	Query ID
  returns:	true if Query ID is in the cache
		false if Query ID is not in the cache
		false if Query ID is out of range
		i.e. not 1 -165535

=cut

sub id_busy($) {
  return 0 if $_[0] < 1 or $_[0] > 65535;
  vec($idvec,$_[0],1);
}

sub _mode {
  $test = $_[0];
  return $idvec;
}

=head1 EXPORTS_OK

	id_get
	id_clr
	id_busy

=head1 AUTHOR

Michael Robinton <michael@bizsystems.com>

=head1 COPYRIGHT 2012-2014

Michael Robinton <michael@bizsystems.com>

All rights reserved.

README  view on Meta::CPAN

    The purpose of this module is to provide and unpredictable source of 16
    bit DNS Query ID numbers to help defeat cache poisoning using DNS
    Spoofing or "Man in the Middle" attacks as describe in the Wikipedia
    article and its references:

            http://en.wikipedia.org/wiki/DNS_cache_poisoning

      use Net::DNS::QueryID qw(
            id_get
            id_clr
            id_busy
      );

      $queryID = id_get();
      $result  = id_clr($queryID);
      $result  = id_busy($queryID);

    * $queryID = id_get();
      input:        none
      returns:      16 bit integer from 1 - 65535
                    that is not currently in the cache.
                    false (0) if all 65535 ID's are in use

    * $result = id_clr($queryID);
      input:        Query ID to clear
      returns:      true (the Query ID) on success
                    false if the Query ID is not in use
                    false if the Query ID is out of range
                    i.e. not 1 -1 65535

    * $result = id_busy($queryID);
      input:        Query ID
      returns:      true if Query ID is in the cache
                    false if Query ID is not in the cache
                    false if Query ID is out of range
                    i.e. not 1 -165535

EXPORTS_OK
            id_get
            id_clr
            id_busy

AUTHOR
    Michael Robinton <michael@bizsystems.com>

COPYRIGHT 2012-2014
    Michael Robinton <michael@bizsystems.com>

    All rights reserved.

    This program is free software; you can redistribute it and/or modify it

t/notbusy.t  view on Meta::CPAN

######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..16\n"; }
END {print "not ok 1\n" unless $loaded;}

use Net::DNS::QueryID qw(
	id_get
	id_clr
	id_busy
);

$loaded = 1;

print "ok 1\n";

*mode = \&Net::DNS::QueryID::_mode;

$test = 2;

t/notbusy.t  view on Meta::CPAN


## test 6		check that 4 were generated
my $idvec = mode(0);	# retrieve vector		set RANDOM mode
my $got = unpack("%32b*",$idvec);
print "got: $got, exp: $exp\nnot "
	unless $got == $exp;
&ok;

## test 7 - 10		check that ID's are in cache
foreach(@qid) {
  unless (id_busy($_)) {
    print "$_ not found in Query ID cache\nnot "
  }
  &ok;
}

my @exp = (86,87,88,89);

## test 11 - 14		check that overflow ID's were created sequentially
foreach(0..$#qid) {
  print "got: $qid[$_], exp: $exp[$_]\nnot "
	unless $qid[$_] == $exp[$_];
  &ok;
}

## test 15		check that leading ID' sre not in the cache
foreach (1..85) {
  if (id_busy($_)) {
    print "unexpected ID $_ in cache\nnot ";
    last;
  }
}
&ok;

## test 16
foreach (90..65535) {
  if (id_busy($_)) {
    print "unexpected ID $_ in cache\nnot ";
    last;
  }
}
&ok;

t/random.t  view on Meta::CPAN

######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..34\n"; }
END {print "not ok 1\n" unless $loaded;}

use Net::DNS::QueryID qw(
	id_get
	id_clr
	id_busy
);

$loaded = 1;

print "ok 1\n";

*mode = \&Net::DNS::QueryID::_mode;

$test = 2;

t/random.t  view on Meta::CPAN

my $idvec = mode(0);	# retrieve vector

my $got = unpack("%32b*",$idvec);

print "got: $got, exp: $exp\nnot "
	unless $got == $exp;
&ok;

## test 13 - 22		check that ID's are in cache
foreach(@qid) {
  unless (id_busy($_)) {
    print "$_ not found in Query ID cache\nnot "
  }
  &ok;
}

## test 23 - 27		clear half the ID's from cache

while ($_ = pop @qid) {
  print "failed to clear Query ID '$_'\nnot "
	unless id_clr($_);

t/wrapover.t  view on Meta::CPAN

######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..20\n"; }
END {print "not ok 1\n" unless $loaded;}

use Net::DNS::QueryID qw(
	id_get
	id_clr
	id_busy
);

$loaded = 1;

print "ok 1\n";

*mode = \&Net::DNS::QueryID::_mode;

$test = 2;

t/wrapover.t  view on Meta::CPAN


## test 6		check that 4 were generated
my $idvec = mode(0);	# retrieve vector		set RANDOM mode
my $got = unpack("%32b*",$idvec);
print "got: $got, exp: $exp\nnot "
	unless $got == $exp;
&ok;

## test 7 - 10		check that ID's are in cache
foreach(@qid) {
  unless (id_busy($_)) {
    print "$_ not found in Query ID cache\nnot "
  }
  &ok;
}

my @exp = (65534, 65535, 1, 2);

## test 11 - 14		check that overflow ID's were created sequentially
foreach(0..$#qid) {
  print "got: $qid[$_], exp: $exp[$_]\nnot "

t/wrapover.t  view on Meta::CPAN

print "failed to get one more Query ID\nnot "
	unless id_get();
&ok;

print STDERR "\tthis may take a while\n";
## test 18		check that cache is full
print "cache was NOT full and should have been\nnot "
	if id_get();
&ok;

## test 19		check busy underflow
print "ID zero present\nnot "
	if id_busy(0);
&ok;

## test 20		check busy overflow
print "ID 65536 present \nnot "
	if id_busy(65536);
&ok;



( run in 0.323 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )