Jigs wrote:
> Hi All,
>
> I was using the Net::IP perl module from CPAN and the function to
> validate an IP address allows single numbers to be a valid address.
> Here is the snippet from the function ip_is_ipv4:
>
> # Single Numbers are considered to be IPv4
> if ($ip =~ m/^(\d+)$/ and $1 < 256) { return 1 }
>
> # Count quads
> my $n = ($ip =~ tr/\./\./);
>
> # IPv4 must have from 1 to 4 quads
> unless ($n >= 0 and $n < 4) {
> $ERROR = "Invalid IP address $ip";
> $ERRNO = 105;
> return 0;
> }
>
> Can someone please explain why single numbers are valis IP addresses?
>
> Thank in advance ...
>
Because an IP address is a 32-bit number that is commonly broken out as
four 8-bit numbers for our convenience and poor memory.
oneal@caffeine ~ $ ping -c 1
www.google.com
PING
www.l.google.com (64.233.161.147) 56(84) bytes of data.
64 bytes from 64.233.161.147: icmp_seq=1 ttl=243 time=8.69 ms
oneal@caffeine ~ $ ping -c 1 64.233.161.147
PING 64.233.161.147 (64.233.161.147) 56(84) bytes of data.
64 bytes from 64.233.161.147: icmp_seq=1 ttl=243 time=8.49 ms
oneal@caffeine ~ $ echo '161 * 256 + 147' | bc
41363
oneal@caffeine ~ $ ping -c 1 64.233.41363
PING 64.233.41363 (64.233.161.147) 56(84) bytes of data.
64 bytes from 64.233.161.147: icmp_seq=1 ttl=243 time=8.62 ms
oneal@caffeine ~ $ echo '(((64*256+233)*256)+161)*256+147' | bc
1089053075
oneal@caffeine ~ $ ping -c 1 1089053075
PING 1089053075 (64.233.161.147) 56(84) bytes of data.
64 bytes from 64.233.161.147: icmp_seq=1 ttl=243 time=8.64 ms
At least for people familiar with the google address space,
64.233.161.147 is a lot easier to remember than 1089053075.
Doug