Skip to content

Commit

Permalink
Minor refactoring of IP validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Sep 18, 2024
1 parent 534acd6 commit 492f8a0
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions projects/packages/ip/src/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ public static function validate_netmask( $netmask, $ip_version ) {
* @return bool True if IP is within the range, false otherwise.
*/
public static function ip_in_ipv4_cidr( $ip, $range, $netmask ) {
// Validate netmask: must be between 0 and 32 for IPv4.
if ( $netmask < 0 || $netmask > 32 ) {
return false; // Invalid netmask for IPv4.
// Validate arguments.
if ( ! self::validate_ip_addresses( array( $ip, $range ) ) && self::validate_netmask( $netmask, 'ipv4' ) ) {
return false; // Invalid IP address or netmask.
}

// Convert IP addresses from their dotted representation to 32-bit unsigned integers.
Expand Down Expand Up @@ -398,14 +398,9 @@ public static function ip_in_ipv4_cidr( $ip, $range, $netmask ) {
* @return bool True if IP is within the range, false otherwise.
*/
public static function ip_in_ipv6_cidr( $ip, $range, $netmask ) {
// Validate netmask: must be between 0 and 128 for IPv6.
if ( $netmask < 0 || $netmask > 128 ) {
return false; // Invalid netmask for IPv6.
}

// Validate the provided IP addresses.
if ( ! self::validate_ip_addresses( array( $ip, $range ) ) ) {
return false;
// Validate arguments.
if ( ! self::validate_ip_addresses( array( $ip, $range ) ) || ! self::validate_netmask( $netmask, 'ipv6' ) ) {
return false; // Invalid IP address or netmask.
}

// Convert IP addresses from their textual representation to binary strings.
Expand Down

0 comments on commit 492f8a0

Please sign in to comment.