zeroclickinfo-goodies/lib/DDG/Goodie/Teredo.pm

64 lines
2.3 KiB
Perl
Raw Normal View History

2014-10-28 11:34:36 -07:00
package DDG::Goodie::Teredo;
# ABSTRACT: Provides the Teredo server IPv4 address,
# NAT IPv4 address, and port number encoded in a given
# Teredo tunnel IPv6 address.
use strict;
2014-10-28 11:34:36 -07:00
use DDG::Goodie;
use Net::IP;
use Math::BaseConvert;
triggers start => 'teredo';
primary_example_queries 'teredo 2001:0000:4136:e378:8000:63bf:3fff:fdd2';
description 'Teredo address analyzer';
name 'Teredo extractor';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Teredo.pm';
topics 'sysadmin';
category 'transformations';
attribution github => ['seanheaton','Sean Heaton'],
twitter => ['seanograph','Sean Heaton'],
2015-12-29 06:03:50 -08:00
email => ['seanoftime@gmail.com','seanoftime@gmail.com'],
github => ["https://github.com/Mailkov", "Melchiorre Alastra"];
2014-10-28 11:34:36 -07:00
zci answer_type => 'teredo';
zci is_cached => 1;
2015-12-29 06:09:42 -08:00
# Params: server, port, client
sub to_text {
return "Teredo Server IPv4: " . $_[0]->ip() . "\nNAT Public IPv4: " . $_[2]->ip()
. "\nClient Port: " . $_[1];
}
2014-10-28 11:34:36 -07:00
handle remainder => sub {
my @output = ();
2014-10-28 11:36:15 -07:00
2014-10-28 11:34:36 -07:00
# Create an IPv6 address from the query value
my $ip = new Net::IP ($_,6) if $_;
# Verify the query value is a valid Teredo IPv6 address
if ((defined $ip) && ($ip->version() == 6) && (substr($ip->ip(),0,9) eq "2001:0000")) {
2015-12-29 06:07:35 -08:00
my $binip = $ip->binip();
2014-10-28 11:36:15 -07:00
2015-12-29 06:07:35 -08:00
# bits 32 to 64 designate IPv4 address of the Teredo server used
push @output, (new Net::IP (Net::IP::ip_bintoip((substr $binip, 32, 32),4)));
2014-10-28 11:34:36 -07:00
2015-12-29 06:07:35 -08:00
# negation of bits 80 to 96, converted to decimal, designate NAT port number
push @output, (65535 - cnv((substr $binip, 80, 16),2,10));
2014-10-28 11:34:36 -07:00
2015-12-29 06:07:35 -08:00
# negation of bits 96 to 128 designate IPv4 address of NAT device
push @output, (new Net::IP (Net::IP::ip_bintoip(~(substr $binip, 96, 32),4)));
return answer => to_text(@output), #html => to_html(@output);
2014-10-28 11:34:36 -07:00
}
return;
# Params: server, port, client
2015-12-29 06:05:55 -08:00
#sub to_html {
#return "<div><span class=\"teredo__label text--secondary\">Teredo Server IPv4: </span><span class=\"text--primary\">" . $_[0]->ip()
#. "</span></div><div><span class=\"teredo__label text--secondary\">NAT Public IPv4: </span><span class=\"text--primary\">" . $_[2]->ip()
#. "</span></div><div><span class=\"teredo__label text--secondary\">Client Port: </span><span class=\"text--primary\">" . $_[1] . "</span></div>";
#}
2014-10-28 11:34:36 -07:00
};
1;