diff --git a/Changes b/Changes index 8ac9897..219d074 100644 --- a/Changes +++ b/Changes @@ -39,4 +39,8 @@ 2.00 Wed Jun 28 2000 - Released under the new *official* name of NetAddr::IP +2.10 Thu Oct 12 2000 + - Added support for ->new($min, $max, $bits) form + - Added ->to_numeric. This helps serializing objects + diff --git a/IP.pm b/IP.pm index 21e5d54..0ffc82c 100644 --- a/IP.pm +++ b/IP.pm @@ -28,7 +28,7 @@ ); -$VERSION = '2.00'; +$VERSION = '2.10'; # Preloaded methods go here. @@ -98,6 +98,37 @@ $result; } +sub _addr_to_number { + my $addr = shift; + my @o = split(/\./, $addr, 4); + + $o[0] = new Math::BigInt $o[0]; + $o[1] = new Math::BigInt $o[1]; + $o[2] = new Math::BigInt $o[2]; + $o[3] = new Math::BigInt $o[3]; + + return ($o[0] * 2 ** 24 + + $o[1] * 2 ** 16 + + $o[2] * 2 ** 8 + + $o[3]); +} + +sub _number_to_addr { + my $number = new Math::BigInt shift; + my @o; + + $o[0] = new Math::BigInt($number->bdiv(2**24)); + $o[1] = new Math::BigInt($number->bdiv(2**16))-$o[0]*2**8; + $o[2] = new Math::BigInt($number->bdiv(2**8))-$o[0]*2**16-$o[1]*2**8; + $o[3] = new Math::BigInt($number)-$o[0]*2**24-$o[1]*2**16-$o[2]*2**8; + + foreach (@o) { s/[-+]//g; } + + print "_number_to_addr $number is ", join('.', @o), "\n"; + return join('.', @o); + +} + sub _mask_to_bits { my $mask = shift; my $i; @@ -129,7 +160,16 @@ sub new { my $type = shift; my $class = ref($type) || $type || "NetAddr::IP"; - my ($ip, $mask) = @_; + my $ip = shift; + my $mask = shift; + my $bits = shift; + + if (length $bits) { + my $min = $ip; + $ip = _number_to_addr($min); + $mask = $bits; + } + $ip = "0.0.0.0" unless defined $ip; if ($ip =~ /\/([\d\.]+)$/) { # croak "inconsistent mask. Use only one form of netmask" @@ -153,9 +193,22 @@ my $self = { 'addr' => _pack_address($ip), 'mask' => $mask }; + bless $self, $class; } +sub to_numeric { + my $self = shift; + if (wantarray) { + return (_addr_to_number(_unpack_address($self->{'addr'})), + _addr_to_number(_unpack_address($self->broadcast->{'addr'})), + _mask_to_bits $self->{'mask'}); + } + else { + return _addr_to_number _unpack_address $self->network->{'addr'}; + } +} + sub new_subnet { my $ip = new @_; return undef unless $ip; @@ -512,11 +565,16 @@ my $subnet = new NetAddr::IP("10.0.0.0", "255.255.255.0"); my $othersubnet = new NetAddr::IP("10.0.0.0", "24"); my $yetanothersubnet = new NetAddr::IP "10.0.0.0/24"; + my $serialsubnet = new NetAddr::IP($min, $max, $bits); # A proper subnet (or undef if any host but is set) my $subnet_ok = new_subnet NetAddr::IP("10.0.0.0", "24"); my $subnet_undef = new_subnet NetAddr::IP("10.0.0.1", "24"); + # A numeric representation of a subnet/host address + my $address = $ip->to_numeric(); + my ($min, $max, $bits) = $ip->to_numeric(); + # A string representation of an address or subnet print "My ip address is ", $ip->to_string, "\n"; diff --git a/MANIFEST b/MANIFEST index 5bcaa77..cb31ecf 100644 --- a/MANIFEST +++ b/MANIFEST @@ -6,4 +6,5 @@ examples/example.pl examples/expand.pl examples/simple.pl +examples/serial.pl t/1.t diff --git a/examples/serial.pl b/examples/serial.pl new file mode 100644 index 0000000..08d4c38 --- /dev/null +++ b/examples/serial.pl @@ -0,0 +1,28 @@ + +use NetAddr::IP; + +print <to_numeric; + my $nip = new NetAddr::IP (@num); + print ( "$i: ", $ip->to_string, " == ", $nip->to_string, " == ", + join('/', ($ip->to_numeric)), "\n" ); +} +