Commit ccdbeba4 authored by sunnavy's avatar sunnavy
Browse files

ipv6 support for IPAddress

parent 32afb860
......@@ -52,6 +52,7 @@ use strict;
use warnings;
use RT::Interface::Web;
use Regexp::Common qw(RE_net_IPv4);
use Regexp::IPv6 qw($IPv6_re);
use Regexp::Common::net::CIDR;
require Net::CIDR;
......@@ -77,17 +78,16 @@ sub Create {
$cf_as_sys->Load($args{'CustomField'});
if($cf_as_sys->Type eq 'IPAddress') {
if ( $args{'Content'} ) {
$args{'Content'} = $self->ParseIP( $args{'Content'} );
}
unless ( defined $args{'Content'} ) {
return
wantarray
? ( 0, $self->loc("Content can't be empty for IPAddress") )
: 0;
}
if ( $args{'Content'} =~ /^\s*$RE{net}{IPv4}\s*$/o ) {
$args{'Content'} = sprintf "%03d.%03d.%03d.%03d", split /\./,
$args{'Content'};
}
}
if($cf_as_sys->Type eq 'IPAddressRange') {
......@@ -423,4 +423,52 @@ sub ParseIPRange {
return $sIP, $eIP;
}
sub ParseIP {
my $self = shift;
my $value = shift or return;
$value = lc $value;
$value =~ s!^\s+!!;
$value =~ s!\s+$!!;
if ( $value =~ /^($RE{net}{IPv4})$/o ) {
return sprintf "%03d.%03d.%03d.%03d", split /\./, $1;
}
elsif ( $value =~ /^$IPv6_re$/ ) {
# up_fields are before '::'
# low_fields are after '::' but without v4
# v4_fields are the v4
my ( @up_fields, @low_fields, @v4_fields );
my $v6;
if ( $value =~ /(.*:)(\d+\..*)/ ) {
( $v6, my $v4 ) = ( $1, $2 );
chop $v6 unless $v6 =~ /::$/;
while ( $v4 =~ /(\d+)\.(\d+)/g ) {
push @v4_fields, sprintf '%.2x%.2x', $1, $2;
}
}
else {
$v6 = $value;
}
my ( $up, $low );
if ( $v6 =~ /::/ ) {
( $up, $low ) = split /::/, $v6;
}
else {
$up = $v6;
}
@up_fields = split /:/, $up;
@low_fields = split /:/, $low if $low;
my @zero_fields =
('0000') x ( 8 - @v4_fields - @up_fields - @low_fields );
my @fields = ( @up_fields, @zero_fields, @low_fields, @v4_fields );
return join ':', map { sprintf "%.4x", hex "0x$_" } @fields;
}
return;
}
1;
......@@ -1380,11 +1380,14 @@ sub _CustomFieldLimit {
return $op;
};
if ( $cf
&& $cf->Type eq 'IPAddress'
&& $value =~ /^\s*$RE{net}{IPv4}\s*$/o )
{
$value = sprintf "%03d.%03d.%03d.%03d", split /\./, $value;
if ( $cf && $cf->Type eq 'IPAddress' ) {
my $parsed = RT::ObjectCustomFieldValue->ParseIP($value);
if ($parsed) {
$value = $parsed;
}
else {
$RT::Logger->warn("$value is not a valid IPAddress");
}
}
if ( $cf
......
......@@ -207,6 +207,7 @@ Devel::StackTrace 1.19
Text::Password::Pronounceable
Net::CIDR
Regexp::Common::net::CIDR
Regexp::IPv6
.
$deps{'MASON'} = [ text_to_hash( << '.') ];
......
......@@ -85,7 +85,8 @@ while ( my $CF = $CustomFields->Next ) {
if ($value) {
if ( $CF->Type eq 'IPAddress' ) {
use Regexp::Common qw(RE_net_IPv4);
unless ( $value =~ /^$RE{net}{IPv4}$/ ) {
my $ip = RT::ObjectCustomFieldValue->ParseIP( $value );
unless ( $ip ) {
my $msg =
loc( "Input can not be parsed to " . loc( $CF->Type ) );
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
......
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