jetpack_protect_ip_is_private

Checks an IP to see if it is within a private range.

Description

jetpack_protect_ip_is_private( (int) $ip ); 

Parameters (1)

0. $ip (int)
The ip.

Usage

  1. if ( !function_exists( 'jetpack_protect_ip_is_private' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/protect/shared-functions.php'; 
  3.  
  4. // The ip. 
  5. $ip = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = jetpack_protect_ip_is_private($ip); 
  9.  

Defined (1)

The function is defined in the following location(s).

/modules/protect/shared-functions.php  
  1. function jetpack_protect_ip_is_private( $ip ) { 
  2.  
  3. // we are dealing with ipv6, so we can simply rely on filter_var 
  4. if ( false === strpos( $ip, '.' ) ) { 
  5. return !filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ); 
  6.  
  7. // we are dealing with ipv4 
  8. $private_ip4_addresses = array( 
  9. '10.0.0.0|10.255.255.255', // single class A network 
  10. '172.16.0.0|172.31.255.255', // 16 contiguous class B network 
  11. '192.168.0.0|192.168.255.255', // 256 contiguous class C network 
  12. '169.254.0.0|169.254.255.255', // Link-local address also referred to as Automatic Private IP Addressing 
  13. '127.0.0.0|127.255.255.255' // localhost 
  14. ); 
  15. $long_ip = ip2long( $ip ); 
  16. if ( -1 != $long_ip ) { 
  17. foreach ( $private_ip4_addresses as $pri_addr ) { 
  18. list ( $start, $end ) = explode( '|', $pri_addr ); 
  19. if ( $long_ip >= ip2long( $start ) && $long_ip <= ip2long( $end ) ) { 
  20. return true; 
  21. return false;