added isLocalAddress method to IPAddr.class.php to check for local addresses (RFC1918...
[PHP-P10.git] / Uplink / IPAddr.class.php
index 1eddcbed0aa7f872ff688d122121426430ffd534..2370cf66e48fab506177595eec9a74ad9edd3611 100644 (file)
@@ -96,6 +96,34 @@ class IPAddr {
        public function isIPv6() {
                return $this->addr_is_ipv6;
        }
        public function isIPv6() {
                return $this->addr_is_ipv6;
        }
+       
+       public function isLocalAddress() {
+               /* checks if address is out of:
+               * 127.0.0.1/32
+               * 10.0.0.0/8
+               * 192.168.0.0/16
+               * 172.16.0.0/12
+               * ::1/128
+               * fc00::/7
+               */
+               if($this->addr_is_ipv6) {
+                       if(
+                               (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
+                               ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 && 
+                                $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
+                         )
+                               return true;
+               } else {
+                       if(
+                               (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
+                               (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
+                               (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
+                               (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
+                         )
+                               return true;
+               }
+               return false;
+       }
 
        public function getAddress() {
                if($this->isIPv6()) {
 
        public function getAddress() {
                if($this->isIPv6()) {