Example #1
0
 *  
 * @return integer Number of mask bits 
 * @throws Exception 
 */
/*********************** TESTING *******************************/
print netmask2bitmask('255.255.255.255');
print netmask2bitmask('255.255.255.0');
print netmask2bitmask('255.255.0.0');
print netmask2bitmask('255.0.0.0');
print netmask2bitmask('0.0.0.0');
print netmask2bitmask('255.255.255.192');
print netmask2bitmask('255.255.255.128');
print netmask2bitmask('255.255.255.gh');
print netmask2bitmask('ffff:ffff:ffff:ffc0:0:0:0:0');
print netmask2bitmask('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
print netmask2bitmask('ffff:ffff:ffff:ffc0:0:0:0:h');
/*****************************************************************/
function netmask2bitmask($netmask)
{
    try {
        echo "Net mask: " . $netmask . "<br/>";
        //validate $netmask
        if (filter_var($netmask, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
            return calculateIpv4($netmask);
        }
        if (filter_var($netmask, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
            throw new Exception("Invalid Net Mask!!!");
        }
        calculateIpv6($netmask);
    } catch (Exception $e) {
        print $e->getMessage() . "<br/><br/>";
Example #2
0
function netmask2bitmask($netmask)
{
    try {
        // Validate if $netmask is string
        if (gettype($netmask) !== "string") {
            throw new Exception("Netmask is not a string");
        }
        // Validate if $netmask is IPV4 or IPV6 address
        if (filter_var($netmask, FILTER_VALIDATE_IP) === false) {
            throw new Exception("Netmask is not a IPV4 or IPV6 valid address");
        }
        // Split string into octets or hextets
        $array = preg_split("/\\.|\\:/", $netmask);
        // Convert to binary, delete zeros and get length of resultant string
        if (filter_var($netmask, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === $netmask) {
            $func = function ($number) {
                return strlen(str_replace("0", "", decbin($number)));
            };
        } else {
            $func = function ($hexa) {
                return strlen(str_replace("0", "", decbin(base_convert($hexa, 16, 10))));
            };
        }
        return array_sum(array_map($func, $array));
    } catch (Exception $ex) {
        print $ex->getMessage();
    }
}
print netmask2bitmask('255.255.0.0') . "\n";
print netmask2bitmask('ffff:ffff:ffff:ffc0:0:0:0:0') . "\n";
Example #3
0
            $isIpV4 = true;
        }
    }
    if (!$isIpV4 && !$isIpV6) {
        throw new Exception("Please type a valid <b>Netmask</b>.");
    }
    if ($isIpV6) {
        $array = explode(":", $netmask);
        $array = array_filter($array);
        foreach ($array as $value) {
            $number += strlen(rtrim(base_convert($value, 16, 2), '0'));
        }
        echo $number;
    }
    if ($isIpV4) {
        if ($netmask === "0.0.0.0") {
            echo "0";
            return;
        }
        $long = ip2long($netmask);
        $base = ip2long('255.255.255.255');
        echo 32 - log(($long ^ $base) + 1, 2);
    }
}
if (isset($_POST['netmask'])) {
    try {
        netmask2bitmask($_POST['netmask']);
    } catch (Exception $e) {
        echo '<h2><b>Message:</b></h2> ' . $e->getMessage();
    }
}
Example #4
0
        }
    }
}
function validateipv6($ip)
{
    if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
        return true;
    }
    return false;
}
echo "Resultado 1 255.255.0.0 : ";
print netmask2bitmask('255.255.0.0') . "<br>";
//16
echo "Resultado 2 255.254.0.0 : ";
print netmask2bitmask('255.254.0.0') . "<br>";
//15
echo "Resultado 3 255.252.0.0 : ";
print netmask2bitmask('255.252.0.0') . "<br>";
//14
echo "Resultado 4 255.248.0.0 : ";
print netmask2bitmask('255.248.0.0') . "<br>";
//14
echo "Resultado 5 255.240.0.0 : ";
print netmask2bitmask('255.240.0.0') . "<br>";
//14
echo "Resultado 2 : ";
print netmask2bitmask('ffff:ffff:ffff:ffc0:0:0:0:0') . "<br>";
//58 is a valid ipv6
echo "Resultado 2 : ";
print netmask2bitmask(':ffff:ffff:ffc0:0:0:0:0') . "<br>";
//this is not a valid ipv6