Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Regexp: Private IP Addresses

by Anonymous Monk
on Aug 25, 2009 at 18:08 UTC ( [id://791145]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there any regexp to recognize private IP Addresses? I didn't find in Regexp-Common.

Replies are listed 'Best First'.
Re: Regexp: Private IP Addresses
by ikegami (Patriarch) on Aug 25, 2009 at 19:25 UTC

    One often works with IP addresses in packed form which allows for a very efficient regex match:

    sub is_private { my ($packed_ip) = @_; return $packed_ip =~ m{ ^ (?: \x0A # 10.0.0.0/8 | \xAC[\x10-\x1F] # 172.16.0.0/12 | \xC0\xA8 # 192.168.0.0/16 ) }x; }

    Test:

    use strict; use warnings; use Socket qw( inet_aton ); sub is_private { my ($packed_ip) = @_; return $packed_ip =~ m{ ^ (?: \x0A # 10.0.0.0/8 | \xAC[\x10-\x1F] # 172.16.0.0/12 | \xC0\xA8 # 192.168.0.0/16 ) }x; } my $result = ''; for (qw( 9.255.255.255 10.0.0.0 10.255.255.255 11.0.0.0 172.15.255.255 172.16.0.0 172.31.255.255 172.32.0.0 192.167.255.255 192.168.0.0 192.168.255.255 192.169.0.0 )) { my $packed_ip = inet_aton($_); $result .= is_private($packed_ip) ? 1 : 0; } print("got: $result\n"); print("expect: ", "0110"x3, "\n");
    got: 011001100110 expect: 011001100110

    Update: Alternatives:

    sub is_private { my ($packed_ip) = @_; return ($packed_ip & "\xFF\x00\x00\x00") eq "\x0A\x00\x00\x00" || ($packed_ip & "\xFF\xF0\x00\x00") eq "\xAC\x10\x00\x00" || ($packed_ip & "\xFF\xFF\x00\x00") eq "\xC0\xA8\x00\x00"; }
    sub is_private { my $nummy_ip = unpack('N', shift); return ($nummy_ip & 0xFF000000) == 0x0A000000 # 10.0.0.0/8 || ($nummy_ip & 0xFFF00000) == 0xAC100000 # 172.16.0.0/12 || ($nummy_ip & 0xFFFF0000) == 0xC0A80000; # 192.168.0.0/16 }
    use Inline CPP => <<'__EOI__'; // Assumes the arg had UTF8=0 IV is_private(const char* packed_ip) { // XXX Alignment issues const U16& hi = *(const U16*)packed_ip; // The irrelevant branch will be optimised away. if (*(const U16*)("\x12\x34") == 0x1234) { // Little endian if ( hi == 0xC0A8 || (hi & 0xFFF0) == 0xAC10 || (hi & 0xFF00) == 0x0A00 ) return 1; } else { // Little endian if ( hi == 0xA8C0 || (hi & 0xF0FF) == 0x10AC || (hi & 0x00FF) == 0x000A ) return 1; } return 0; } __EOI__

        No. There are similarities between all the solutions provided in this discussion (including between mine and that module's), but the module's solution is far from being exactly the same as any of mine.

        According to the docs, it uses the binary representation of the address (a long string of zeros and ones), whereas all of my solutions work with the address as a native number (C long). That makes some of them byte-oriented and some of them mask-oriented, but none are bit-oriented like that module.

        Mind you, any of the solutions in this discussion could be given the same interface as that module.

Re: Regexp: Private IP Addresses
by markkawika (Monk) on Aug 25, 2009 at 18:22 UTC

    You can use Net::IP::Match::Regexp to do this:

    use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip ); my $regexp = create_iprange_regexp( qw( 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 ) ); if (match_ip($my_ip, $regexp)) { ... }
      If you know you have an IP-address (got it from a network socket, for example), it would be much more efficient to match it with a one-line regexp:
      /^(10\.\d+|172\.(1[6-9]|2\d|3[0-1])|192\.168)(\.\d+){2}$/;
      If you got it from a non-network input (like a user form), don't trust in it, it may be 10.9999.1234.666.
        Yes, using a regular expression is "much more efficient", but yours is in some cases, as you point out, incorrect. And using Net::IP::Match::Regexp you can still do more than sixty seven thousand comparisons per second (on my old and rusty laptop, anyway) if the regexp is initialized once only. So what do you want:
        • Superfast and possibly incorrect, or
        • Fast and correct?
        $ cat 791149.pl use strict; use warnings; use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip ); use Benchmark qw / cmpthese /; my $regexp_init_once = create_iprange_regexp( qw( 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 ) + ); sub netip { my $regexp = create_iprange_regexp( qw( 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 + ) ); return ( match_ip( $_[0], $regexp ) ); } sub netip_init_once { return match_ip( $_[0], $regexp_init_once ); } sub regexp { return $_[0] =~ /^(10\.\d+|172\.(1[6-9]|2\d|3[0-1])|192\.168)(\.\d+) +{2}$/; } cmpthese( -1, { 'netip' => sub { netip('1.2.3.4') }, 'netip_init_once' => sub { netip_init_once('1.2.3.4') }, 'regexp' => sub { regexp('1.2.3.4') }, } ); __END__ $ perl 791149.pl Rate netip netip_init_once reg +exp netip 2438/s -- -96% -1 +00% netip_init_once 67622/s 2674% -- - +95% regexp 1390157/s 56918% 1956% + --
        --
        No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found