Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Regexp: Private IP Addresses

by ikegami (Pope)
on Aug 25, 2009 at 19:25 UTC ( #791164=note: print w/replies, xml ) Need Help??

in reply to Regexp: Private IP Addresses

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 # | \xAC[\x10-\x1F] # | \xC0\xA8 # ) }x; }


use strict; use warnings; use Socket qw( inet_aton ); sub is_private { my ($packed_ip) = @_; return $packed_ip =~ m{ ^ (?: \x0A # | \xAC[\x10-\x1F] # | \xC0\xA8 # ) }x; } my $result = ''; for (qw( )) { 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 # || ($nummy_ip & 0xFFF00000) == 0xAC100000 # || ($nummy_ip & 0xFFFF0000) == 0xC0A80000; # }
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__

Replies are listed 'Best First'.
Re^2: Regexp: Private IP Addresses
by markkawika (Monk) on Aug 26, 2009 at 21:20 UTC

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://791164]
[PriNet]: i think i found the problem, the "source" array that sets the individual key values isn't being cleared, it's keeping the previous values...
[atcroft]: PriNet: You might consider posting a small, self-contained snippet of that segment of your code, and see if someone else has a better idea.....
[atcroft]: PriNet: Ahhh... interesting.
[PriNet]: small would be a problem *heh*... the program reads all the lines from a pdf and converts the individual colum data to a seperate key/value pair... that's where the "previous" array from the pdf is not being cleared properly before reading the next pdf
[atcroft]: PriNet: It was a thought....
[PriNet]: thanx... actually just having someone to talk to was making me think... *heh*
[PriNet]: a regular array would just be "delete @array" correct? no need for the post "()" ?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-06-28 03:18 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (620 votes). Check out past polls.