Networking Forums

Networking Forums > Computer Networking > Linux Networking > IPv4 address validation in Net::IP

Reply
Thread Tools Display Modes

IPv4 address validation in Net::IP

 
 
Jigs
Guest
Posts: n/a

 
      11-07-2006, 10:34 PM
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 ...

 
Reply With Quote
 
 
 
 
Trygve Selmer
Guest
Posts: n/a

 
      11-09-2006, 12:53 PM
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.
>
> Can someone please explain why single numbers are valis IP addresses?


Because it is expanded with zeros to a valid address:

10 => 10.0.0.0
10.1 => 10.1.0.0
192 => 192.0.0.0

 
Reply With Quote
 
Douglas O'Neal
Guest
Posts: n/a

 
      11-09-2006, 01:32 PM
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IPv4 Autoconfiguration Address Dan Walowski Windows Networking 1 07-20-2009 08:46 PM
How to listen on loopback address on both IPv4 and IPv6? Emmanuel Stapf [ES] Linux Networking 5 05-13-2009 04:26 PM
Fake address for NAT connection support (IPv4) Mark T.B. Carroll Linux Networking 4 03-28-2007 02:49 PM
Using IPv4 TCPMSS target with IPv6-in-IPv4 Mark T.B. Carroll Linux Networking 1 03-18-2007 10:30 AM
Find IPv4 address(es) for an interface Spoon Linux Networking 4 06-07-2006 01:04 AM



1 2 3 4 5 6 7 8 9 10 11