Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regex host portion of IPv6 address

by quester (Vicar)
on Mar 28, 2008 at 09:44 UTC ( [id://676937]=note: print w/replies, xml ) Need Help??


in reply to Regex host portion of IPv6 address

I think all the corner cases in IPv6 addresses are likely to make you crazy if you try to do the whole problem in an RE. You could try the is_ipv6 function here:
my $quad = "[0-9a-fA-F]{0,4}"; my $ipv6addr = "(?:$quad:){2,7}$quad"; sub is_ipv6 { local $_ = $_[0]; return 0 unless m{^$ipv6addr(?:/(\d+))?$}; my $mask = defined($1) ? $1 : 128; # in 5.10: my $mask = $1 // 128; return 0 if /:::/ or /::.*::/ or not /::/ and 7 != tr/:/:/; return ( 0 <= $mask and $mask <= 128 ); }
But you might be better off to just go ahead and try to convert the address with NetAddr::IP -> new6, and see if the result is true. A quick and dirty test comparing the two approaches...
use warnings; use strict; use NetAddr::IP; my $quad = "[0-9a-fA-F]{0,4}"; my $ipv6addr = "(?:$quad:){2,7}$quad"; sub is_ipv6 { local $_ = $_[0]; return 0 unless m{^$ipv6addr(?:/(\d+))?$}; my $mask = defined($1) ? $1 : 128; # in 5.10: my $mask = $1 // 128; return 0 if /:::/ or /::.*::/ or not /::/ and 7 != tr/:/:/; return ( 0 <= $mask and $mask <= 128 ); } while (<DATA>) { chomp; printf "%-25s ", $_; my $addr = NetAddr::IP -> new6 ($_); $addr ||= ""; printf "%-25s ", $addr; print is_ipv6($_) ? "yes\n" : "no\n"; } __DATA__ :: ::/0 ::1/128 1::/64 : :/96 2001:1::/64 2001:0:6:4003::/64 2001::12/128 2001::15/0 2001::15/-1 2001::15/129 20012::15/96 2001:1:::2/96 2001::1::2/96 2001::1:2::3/96 2001:2:3:4:5:6:7/48 2001:2:3:4::6:7/48 2001:2:3:4:5:6:7:8/64 2001:2:3:4:5:6:7:8:9/96 2001:2:3:4::6:7:8:9/96
prints the following:
:: 0:0:0:0:0:0:0:0/128 yes ::/0 0:0:0:0:0:0:0:0/0 yes ::1/128 0:0:0:0:0:0:0:1/128 yes 1::/64 1:0:0:0:0:0:0:0/64 yes : no :/96 no 2001:1::/64 2001:1:0:0:0:0:0:0/64 yes 2001:0:6:4003::/64 2001:0:6:4003:0:0:0:0/64 yes 2001::12/128 2001:0:0:0:0:0:0:12/128 yes 2001::15/0 2001:0:0:0:0:0:0:15/0 yes 2001::15/-1 no 2001::15/129 no 20012::15/96 no 2001:1:::2/96 no 2001::1::2/96 no 2001::1:2::3/96 no 2001:2:3:4:5:6:7/48 no 2001:2:3:4::6:7/48 2001:2:3:4:0:0:6:7/48 yes 2001:2:3:4:5:6:7:8/64 2001:2:3:4:5:6:7:8/64 yes 2001:2:3:4:5:6:7:8:9/96 no 2001:2:3:4::6:7:8:9/96 no

Replies are listed 'Best First'.
Re^2: Regex host portion of IPv6 address
by ewhitt (Scribe) on Mar 28, 2008 at 10:07 UTC
    Thanks for the response. Your code goes beyond my understanding. I thought I was close with my code. ;-)
    if ($line =~ m{\s+Prefix\s+((.*)::(\w{1,4})/(\d{1,3}))} ) { $ipv6Address = "$1::$2/$3"; }
    I am able to match

    Prefix 2001::1/128
    Prefix 2001::2/128

    but not

    Prefix 2001:1::/64

      The part that's hard to see is that there must be exactly one double colon :: in the address ... unless there are exactly seven colons, in which case there must be no double colon. For example,

      0001:0002:0003:0004:0005:0006:0007:0008
      

      The other and more important thing - which I forgot altogether - is that addresses that are mapped from IPv4 to IPv6 can optionally be written with the last two groups of hex digits replaced by a dotted-decimal IPv4 address, as in these two:

      ::ffff:10.32.12.1
      ::10.32.12.1
      

      which are equivalent to

      ::ffff:0a20:0c01
      ::0a20:0c01
      

      I think the suggestion from an Anonymous Monk of "Work less, use Net::IPv6Addr" is really a much better idea.

      Adding the code to parse mapped address would turn the code I suggested from a mess into an abomination... a useful one to be sure, but still very very abominable. As Otto von Bismark might say, "Some CPAN modules are like laws and sausages, it is better not to see them being made."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://676937]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found