| 1 | package DNS::Bananafonana; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $bananafonana_strings); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Math::BigInt qw(:constant); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | $VERSION = '0.1'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | @ISA = qw(Math::BigInt); | |||||||||||||||||||||||||||
| 13 | 1 | @EXPORT = qw(); | |||||||||||||||||||||||||||
| 14 | 1 | @EXPORT_OK = qw(from_bananafonana to_bananafonana bananafonana); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | DNS::Bananafonana - Perl extension for Bananafonana encoding / decoding | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use DNS::Bananafonana; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $bigint = from_bananafonana($number); | ||||||||||||||||||||||||||||
| 25 | $bafostr = to_bananafonana($bigint); | ||||||||||||||||||||||||||||
| 26 | $hostname = bananafonana($ptr, $domain, $prefix); | ||||||||||||||||||||||||||||
| 27 | $ip = bananafonana($hostname, $domain, $prefix); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | RFC 1924 describes a compact, fixed-size representation of IPv6 | ||||||||||||||||||||||||||||
| 32 | addresses which uses a base 85 number system. The base 85 numbers | ||||||||||||||||||||||||||||
| 33 | (from 0 to 84) are as follows: | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0..9 A..Z a..z ! # $ % & ( ) * + - ; < = > ? @ ^ _ ` { | } ~ | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | In order to let human beings pronounce the resulting string more easily and | ||||||||||||||||||||||||||||
| 38 | to be able to use base 85 encoding in DNS naming schemes, an alternative | ||||||||||||||||||||||||||||
| 39 | encoding scheme is used, based on 85 consonant-vowel pairs, as suggested by | ||||||||||||||||||||||||||||
| 40 | DGolden on Slashdot | ||||||||||||||||||||||||||||
| 41 | (http://tech.slashdot.org/comments.pl?sid=649579&cid=24654733). | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This module has a variable called C<$DNS::Bananafonana::bananafonana_strings>, | ||||||||||||||||||||||||||||
| 44 | which is a string containing the 85 two-character strings that make up | ||||||||||||||||||||||||||||
| 45 | the bananafonana "alphabet" from lowest to highest , in that order. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Additionally, the following three functions are defined for general | ||||||||||||||||||||||||||||
| 48 | use. (They will be exported upon request.) | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | $DNS::Bananafonana::bananafonana_strings = | |||||||||||||||||||||||||||
| 53 | "babebibobudadedidodufafefifofugagegigogu" . | ||||||||||||||||||||||||||||
| 54 | "hahehihohujajejijojukakekikokulalelilolu" . | ||||||||||||||||||||||||||||
| 55 | "mamemimomunaneninonupapepipopusasesisosu" . | ||||||||||||||||||||||||||||
| 56 | "tatetitotuvavevivovuxaxexixoxuwawewiwowu" . | ||||||||||||||||||||||||||||
| 57 | "zazezizozu"; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Maybe we can make this a little more general... | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | use constant BAFO_BASE => 85; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | use constant BAFO_BLOCKSIZE => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | use constant BAFO_MARKUP => '-'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =pod | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 from_bananafonana | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 Parameters | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | A string composed of valid bananafonana strings. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 Returns | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | A C<DNS::BigInt> object representing the number. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub from_bananafonana | ||||||||||||||||||||||||||||
| 81 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
from_bananafonana: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 6 | my $num = shift; | |||||||||||||||||||||||||||
| 83 | # Remove markup characters | ||||||||||||||||||||||||||||
| 84 | 6 | $num =~ s/[-_\.]//g; | |||||||||||||||||||||||||||
| 85 | 6 | my $answer = new Math::BigInt "0"; | |||||||||||||||||||||||||||
| 86 | 6 | my $n; | |||||||||||||||||||||||||||
| 87 | my $d; | ||||||||||||||||||||||||||||
| 88 | 6 | ▶ | while (length($d = substr($num,0,2)) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 67 | ▶ | if (length($d) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | croak __PACKAGE__ . "::from_bananafonana -- invalid bananafonana string $d"; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 67 | $num = substr($num,2); | |||||||||||||||||||||||||||
| 93 | 67 | $answer = $answer * BAFO_BASE; | |||||||||||||||||||||||||||
| 94 | 67 | $n = index($bananafonana_strings, $d)/2.0; | |||||||||||||||||||||||||||
| 95 | 67 | ▶ | if ($n < 0.0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 2 | croak __PACKAGE__ . "::from_bananafonana -- invalid bananafonana string $d"; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 65 | $answer = $answer + $n; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 4 | return $answer; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =pod | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 to_bananafonana | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 Parameters | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | A C<DNS::BigInt> object. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Optionally: | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | A markup character to split the string in more readable parts. | ||||||||||||||||||||||||||||
| 114 | Can be C<->, C<_> or C<.>. Defaults to C<->. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | A blocksize that determines the number of consonant-vowel combinations | ||||||||||||||||||||||||||||
| 117 | between each markup character. Defaults to 5. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 Returns | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | A string of bananafonana strings representing the number. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub to_bananafonana | ||||||||||||||||||||||||||||
| 126 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_bananafonana: called CC=10 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 4 | my $num = shift; | |||||||||||||||||||||||||||
| 128 | 4 | ▶ | my $markup = shift || BAFO_MARKUP; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || '-'
MC/DC0%0/1
shift() || '-'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | my $blocksize = (shift || BAFO_BLOCKSIZE) + 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {sign => '+', value => ['5']}
MC/DC0%0/1
shift() || {sign => '+', value => ['5']}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 130 | 4 | my @digits; | |||||||||||||||||||||||||||
| 131 | my $q; | ||||||||||||||||||||||||||||
| 132 | 4 | my $r; | |||||||||||||||||||||||||||
| 133 | 4 | my $d; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | ▶ | if (! $markup =~ /[-_\.]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | croak __PACKAGE__ . "::to_bananafonana -- invalid markup character ($markup)"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 4 | ▶ | if ($blocksize < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | croak __PACKAGE__ . "::to_bananafonana -- invalid blocksize ($blocksize)"; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 4 | ▶ | if ($num eq "NaN" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | croak __PACKAGE__ . "::to_bananafonana -- invalid number ($num)"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 3 | ▶ | while ($num > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 45 | $q = $num / BAFO_BASE; | |||||||||||||||||||||||||||
| 146 | 45 | $r = $num % BAFO_BASE; | |||||||||||||||||||||||||||
| 147 | 45 | $d = substr($bananafonana_strings, $r*2, 2); | |||||||||||||||||||||||||||
| 148 | 45 | ▶ | if ($blocksize > 0 && (($#digits + 1) % $blocksize == 0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$blocksize > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) and ($#digits + bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) % $blocksize == bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
MC/DC50%1/2
$blocksize > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) and ($#digits + bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) % $blocksize == bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
$blocksize > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
($#digits + bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) % $blocksize == bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table66%2/3
A and B
A= $blocksize > bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )B= ($#digits + bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) % $blocksize == bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
| |||||||||||||||||||||||||||||
| 149 | 9 | unshift @digits, $markup; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 45 | unshift @digits, $d; | |||||||||||||||||||||||||||
| 152 | 45 | $num = $q; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 3 | pop @digits; | |||||||||||||||||||||||||||
| 155 | 3 | ▶ | unshift @digits, 'ba' unless (@digits); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 3 | return join('', @digits); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =pod | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 bananafonana | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 Parameters | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | A string | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | A string containing the domain name of the record. It will be appended to the | ||||||||||||||||||||||||||||
| 168 | result when a pointer record is asked and removed from the input for hostname | ||||||||||||||||||||||||||||
| 169 | lookups. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Optionally: | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | A string containing a prefix that needs to be added before the bananafonana | ||||||||||||||||||||||||||||
| 174 | representation of the ip address. Defaults to empty. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | A markup character to split the string in more readable parts. | ||||||||||||||||||||||||||||
| 177 | Can be C<->, C<_> or C<.>. Defaults to C<->. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | A blocksize that determines the number of consonant-vowel combinations | ||||||||||||||||||||||||||||
| 180 | between each markup character. Defaults to 5. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 Returns | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | A string containing either the bananafonana representation of the ip address | ||||||||||||||||||||||||||||
| 185 | (presented in 1.2.3.4.in-addr.arpa or a.b...e.f.ip6.arpa notation) | ||||||||||||||||||||||||||||
| 186 | or a string representing the ip address determined from the bonananfonana | ||||||||||||||||||||||||||||
| 187 | encoded hostname (for all domains not ending in in-addr|ip6.arpa). | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub bananafonana | ||||||||||||||||||||||||||||
| 192 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
bananafonana: called CC=17 SCAR=31.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 5 | my $name = shift; | |||||||||||||||||||||||||||
| 194 | 5 | ▶ | my $domain = shift || ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 195 | 5 | ▶ | my $prefix = shift || ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 196 | 5 | ▶ | my $markup = shift || BAFO_MARKUP; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || '-'
MC/DC0%0/1
shift() || '-'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 197 | 5 | ▶ | my $blocksize = (shift || BAFO_BLOCKSIZE) + 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {sign => '+', value => ['5']}
MC/DC0%0/1
shift() || {sign => '+', value => ['5']}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 198 | 5 | my $ip; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # Input validation | ||||||||||||||||||||||||||||
| 201 | 5 | ▶ | if (! $markup =~ /[-_\.]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | croak __PACKAGE__ . | |||||||||||||||||||||||||||
| 203 | "::bananafonana -- invalid markup character($markup)"; | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 5 | ▶ | if ($blocksize < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | croak __PACKAGE__ . | |||||||||||||||||||||||||||
| 207 | "::bananafonana -- invalid blocksize ($blocksize)"; | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 5 | ▶ | if ($domain eq "" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | croak __PACKAGE__ . "::bananafonana -- empty domain is not allowed"; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | # Strip leading and trailing dots from domain | ||||||||||||||||||||||||||||
| 213 | 5 | $domain =~ s/^\.|\.$//g; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # IPv4 PTR record | ||||||||||||||||||||||||||||
| 216 | 5 | ▶ | if ($name =~ /^(.*)\.in-addr\.arpa[.]{0,1}$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 217 | 1 | $ip = eval { $prefix.to_bananafonana( | |||||||||||||||||||||||||||
| 218 | new Math::BigInt("0x".sprintf("%02x%02x%02x%02x", | ||||||||||||||||||||||||||||
| 219 | reverse split(/\./, $1))) | ||||||||||||||||||||||||||||
| 220 | ).".".$domain; }; | ||||||||||||||||||||||||||||
| 221 | 1 | ▶ | if (not defined($ip)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | croak __PACKAGE__ . "::bananafonana -- cannot encode $1"; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 1 | return($ip); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # IPv6 PTR record | ||||||||||||||||||||||||||||
| 227 | } elsif ($name =~ /^(.*)\.ip6\.arpa[.]{0,1}$/) { | ||||||||||||||||||||||||||||
| 228 | 2 | $ip = eval { $prefix.to_bananafonana( | |||||||||||||||||||||||||||
| 229 | new Math::BigInt("0x".join('', reverse split(/\./, $1))) | ||||||||||||||||||||||||||||
| 230 | ).".".$domain; }; | ||||||||||||||||||||||||||||
| 231 | 2 | ▶ | if (not defined($ip)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 1 | croak __PACKAGE__ . "::bananafonana -- cannot encode $1"; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 1 | return($ip); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | } elsif ($name =~ /^$prefix(.*)\.$domain[.]{0,1}$/) { | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # A or AAAA record | ||||||||||||||||||||||||||||
| 239 | 2 | $name = $1; | |||||||||||||||||||||||||||
| 240 | 2 | $name =~ s/[-_\.]//g; | |||||||||||||||||||||||||||
| 241 | 2 | ▶ | if (length($name) == 10) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # A record (Note: this also incorrectly matches ::abcd IPv6 addresses!) | ||||||||||||||||||||||||||||
| 244 | 1 | $ip = eval { sprintf("%08x",from_bananafonana($name)); }; | |||||||||||||||||||||||||||
| 245 | 1 | ▶ | if (not defined($ip)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 0 | croak __PACKAGE__ . "::bananafonana -- cannot decode $name"; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | 1 | return(sprintf("%d.%d.%d.%d", | |||||||||||||||||||||||||||
| 249 | hex(substr($ip,0,2)), hex(substr($ip,2,2)), | ||||||||||||||||||||||||||||
| 250 | hex(substr($ip,4,2)), hex(substr($ip,6,2)))); | ||||||||||||||||||||||||||||
| 251 | } else { | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # AAAA record | ||||||||||||||||||||||||||||
| 254 | 1 | $ip = eval { from_bananafonana($name)->as_hex(); }; | |||||||||||||||||||||||||||
| 255 | 1 | ▶ | if (not defined($ip)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 0 | croak __PACKAGE__ . "::bananafonana -- cannot decode $name"; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 1 | ▶ | if (length($ip) < 34) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | $ip = "0x".substr('00000000000000000000000000000000',0, | |||||||||||||||||||||||||||
| 260 | 34-length($ip)).substr($ip,2); | ||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 1 | $name = sprintf("%x:%x:%x:%x:%x:%x:%x:%x", | |||||||||||||||||||||||||||
| 263 | hex(substr($ip,2,4)), hex(substr($ip,6,4)), | ||||||||||||||||||||||||||||
| 264 | hex(substr($ip,10,4)), hex(substr($ip,14,4)), | ||||||||||||||||||||||||||||
| 265 | hex(substr($ip,18,4)), hex(substr($ip,22,4)), | ||||||||||||||||||||||||||||
| 266 | hex(substr($ip,26,4)), hex(substr($ip,30,4))); | ||||||||||||||||||||||||||||
| 267 | 1 | $name =~ s/^(0:)+|(:0)+$|(:0)+:/::/; | |||||||||||||||||||||||||||
| 268 | 1 | return($name); | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | } else { | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | #Invalid question | ||||||||||||||||||||||||||||
| 274 | 0 | croak __PACKAGE__ . "::bananafonana -- invalid input ($name)"; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head1 BUGS | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | The bananafonana function is currently decoding all hostnames with 10 | ||||||||||||||||||||||||||||
| 282 | character bonanafonana encoded addresses as IPv4 addresses. This prevends the | ||||||||||||||||||||||||||||
| 283 | correct encoding and decoding for IPv6 addresses in the range ::/96, which | ||||||||||||||||||||||||||||
| 284 | should not be a big limitation in practice. | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | Michiel Fokke <michiel@fokke.org> | ||||||||||||||||||||||||||||
| 289 | Based upon work from Tony Monroe <tmonroe+perl@nog.net> | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | perl(1). | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =cut | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 1 | 1; | |||||||||||||||||||||||||||
| 298 | __END__ | ||||||||||||||||||||||||||||