Commit 09cedc1a authored by sunnavy's avatar sunnavy
Browse files

ipv6 support of iprange

parent a86b2ff0
......@@ -250,7 +250,7 @@ sub Content {
return $content if $self->CustomFieldObj->Type eq 'IPAddress';
my $large_content = $self->__Value('LargeContent');
my $large_content = $self->LargeContent;
if ( $large_content =~ /^\s*($re_ip_serialized)\s*$/o ) {
my $eIP = sprintf "%d.%d.%d.%d", split /\./, $1;
if ( $content eq $eIP ) {
......@@ -260,6 +260,15 @@ sub Content {
return $content . "-" . $eIP;
}
}
elsif ( $large_content =~ /^\s*($IPv6_re)\s*$/o ) {
my $eIP = $1;
if ( $content eq $eIP ) {
return $content;
}
else {
return $content . "-" . $eIP;
}
}
else {
return $content;
}
......@@ -400,24 +409,40 @@ sub IncludeContentForValue {
sub ParseIPRange {
my $self = shift;
my $arg = shift or return ();
my $value = shift or return;
$value = lc $value;
$value =~ s!^\s+!!;
$value =~ s!\s+$!!;
if ( $arg =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/go ) {
if ( $value =~ /^$RE{net}{CIDR}{IPv4}{-keep}$/go ) {
my $cidr = join( '.', map $_||0, (split /\./, $1)[0..3] ) ."/$2";
$arg = (Net::CIDR::cidr2range( $cidr ))[0] || $arg;
$value = (Net::CIDR::cidr2range( $cidr ))[0] || $value;
}
elsif ( $value =~ /^$IPv6_re(?:\/\d+)?$/o ) {
$value = (Net::CIDR::cidr2range( $value ))[0] || $value;
}
my ($sIP, $eIP);
if ( $arg =~ /^\s*($RE{net}{IPv4})\s*$/o ) {
if ( $value =~ /^($RE{net}{IPv4})$/o ) {
$sIP = $eIP = sprintf "%03d.%03d.%03d.%03d", split /\./, $1;
}
elsif ( $arg =~ /^\s*($RE{net}{IPv4})-($RE{net}{IPv4})\s*$/o ) {
elsif ( $value =~ /^($RE{net}{IPv4})-($RE{net}{IPv4})$/o ) {
$sIP = sprintf "%03d.%03d.%03d.%03d", split /\./, $1;
$eIP = sprintf "%03d.%03d.%03d.%03d", split /\./, $2;
}
elsif ( $value =~ /^($IPv6_re)$/o ) {
$sIP = $self->ParseIP( $1 );
$eIP = $sIP;
}
elsif ( $value =~ /^($IPv6_re)-($IPv6_re)$/o ) {
($sIP, $eIP) = ( $1, $2 );
$sIP = $self->ParseIP( $sIP );
$eIP = $self->ParseIP( $eIP );
}
else {
return ();
return;
}
($sIP, $eIP) = ($eIP, $sIP) if $sIP gt $eIP;
return $sIP, $eIP;
......
......@@ -1390,17 +1390,35 @@ sub _CustomFieldLimit {
}
}
if ( $cf
&& $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o )
{
if ( $cf && $cf->Type eq 'IPAddressRange' ) {
# convert incomplete 192.168/24 to 192.168.0.0/24 format
my $cidr =
join( '.', map $_ || 0, ( split /\./, $1 )[ 0 .. 3 ] ) . "/$2";
if ( $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o ) {
# convert to range and continue, it will be catched by next wrapper
$value = ( Net::CIDR::cidr2range($cidr) )[0] || $value;
# convert incomplete 192.168/24 to 192.168.0.0/24 format
$value =
join( '.', map $_ || 0, ( split /\./, $1 )[ 0 .. 3 ] ) . "/$2"
|| $value;
}
my ( $start_ip, $end_ip ) =
RT::ObjectCustomFieldValue->ParseIPRange($value);
if ( $start_ip && $end_ip ) {
if ( $op =~ /^([<>])=?$/ ) {
my $is_less = $1 eq '<' ? 1 : 0;
if ( $is_less ) {
$value = $start_ip;
}
else {
$value = $end_ip;
}
}
else {
$value = join '-', $start_ip, $end_ip;
}
}
else {
$RT::Logger->warn("$value is not a valid IPAddressRange");
}
}
my $single_value = !$cf || !$cfid || $cf->SingleValue;
......@@ -1432,11 +1450,7 @@ sub _CustomFieldLimit {
}
elsif ( $op !~ /^[<>]=?$/ && ( $cf && $cf->Type eq 'IPAddressRange')) {
$value =~ /^\s*($RE{net}{IPv4})\s*(?:-\s*($RE{net}{IPv4})\s*)?$/o;
my ($start_ip, $end_ip) = ($1, ($2 || $1));
$_ = sprintf "%03d.%03d.%03d.%03d", split /\./, $_
for $start_ip, $end_ip;
($start_ip, $end_ip) = ($end_ip, $start_ip) if $start_ip gt $end_ip;
my ($start_ip, $end_ip) = split /-/, $value;
$self->_OpenParen;
if ( $op !~ /NOT|!=|<>/i ) { # positive equation
......@@ -1451,16 +1465,17 @@ sub _CustomFieldLimit {
);
# as well limit borders so DB optimizers can use better
# estimations and scan less rows
$self->_CustomFieldLimit(
$field, '>=', '000.000.000.000', %rest,
SUBKEY => $rest{'SUBKEY'}. '.Content',
ENTRYAGGREGATOR => 'AND',
);
$self->_CustomFieldLimit(
$field, '<=', '255.255.255.255', %rest,
SUBKEY => $rest{'SUBKEY'}. '.LargeContent',
ENTRYAGGREGATOR => 'AND',
);
# have to disable this tweak because of ipv6
# $self->_CustomFieldLimit(
# $field, '>=', '000.000.000.000', %rest,
# SUBKEY => $rest{'SUBKEY'}. '.Content',
# ENTRYAGGREGATOR => 'AND',
# );
# $self->_CustomFieldLimit(
# $field, '<=', '255.255.255.255', %rest,
# SUBKEY => $rest{'SUBKEY'}. '.LargeContent',
# ENTRYAGGREGATOR => 'AND',
# );
}
else { # negative equation
$self->_CustomFieldLimit($field, '>', $end_ip, %rest);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment