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

I'd like to be able to perform some boolean logic on IP addresses; just basic stuff like ANDing and maybe ORing

Thing is, today I'm having a mental block and am consistantly failing to find a way to convert (say) 255.255.240.0 into a 32bit int... (which I can then do it all with)...

Replies are listed 'Best First'.
Re: Boolean logic with IP addresses
by busunsl (Vicar) on May 17, 2001 at 17:47 UTC
    This will give you a 32bit int:
    unpack "I", pack "C4", split /\./, '255.255.240.0';
      That's the one I'd seen before, and was trying to remember :o)

      As a caveat, the byte order on some processors means that this doesn't generate platform independent ints, but they're good enough for what I needed -- thanks...

Re: Boolean logic with IP addresses
by chipmunk (Parson) on May 17, 2001 at 18:10 UTC
    Another option would be to convert the IP addresses to 4 byte strings, and use the string behavior, rather than the numeric, of the bitwise operators. Thanks to pack, the conversion is very easy.
    #!/usr/local/bin/perl my $ip1 = '255.255.240.0'; my $ip2 = '206.170.14.76'; my $packed_ip1 = pack 'C*', split /\./, $ip1; my $packed_ip2 = pack 'C*', split /\./, $ip2; my $and = $packed_ip1 & $packed_ip2; my $or = $packed_ip1 | $packed_ip2; my $xor = $packed_ip1 ^ $packed_ip2; for ($and, $or, $xor) { print join('.', unpack 'C*', $_), "\n"; }
Re: Boolean logic with IP addresses
by Brovnik (Hermit) on May 17, 2001 at 17:56 UTC
    The Net::IP module has various functions for manipulating IP addresses, including checking ranges, etc.
Re: Boolean logic with IP addresses
by alfie (Pilgrim) on May 17, 2001 at 17:38 UTC
    Given the fact that $ip holds a valid IP-Address:
    my @parts=split('\.',$ip); my $ipint=$parts[0]*256**3+$parts[1]*256**2+$parts[2]*256+$parts[3]; undef @parts;
    There might be trickier ways but that should do for the start :)

    Update: Of course the MSB is in the first digit, it came to my mind when traveling home yesterday, sorry.... Thanks, jink. Well thought :)
    --
    use signature; signature(" So long\nAlfie");

      Isn't that supposed to be:
      my $ipint=$parts[0]*256**3+$parts[1]*256**2+$parts[2]*256+$parts[3];
      See azatoths reply.

      All Camels are equal, but some Camels are more equal than others.
Re: Boolean logic with IP addresses
by azatoth (Curate) on May 17, 2001 at 17:43 UTC
Re: Boolean logic with IP addresses
by Beatnik (Parson) on May 17, 2001 at 17:43 UTC
    should do the trick... bitwise :)
    (@segments) = split(/\./,$ip); foreach(@segments) { print unpack("B*",chr($_)); }
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.

      FYI, the "B" spec for unpack is "binary" as in a string of ASCII "1"s and "0"s. Take for example "192.168.10.1". Your code outputs:

      001100010011100100110010 001100010011011000111000 0011000100110000 00110001
      which isn't of much use for doing bit-wise stuff on IP addresses. It is the ASCII values of the characters from the strings "192", "168", "10", and "1" printed in binary notation.

              - tye (but my friends call me "Tye")