00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class IpNetwork implements SingleRange
00017 {
00018 const MASK_MAX_SIZE = 31;
00019
00020 private $ip = null;
00021 private $end = null;
00022 private $mask = null;
00023 private $longMask = null;
00024
00028 public static function create(IpAddress $ip, $mask)
00029 {
00030 return new self($ip, $mask);
00031 }
00032
00033 public function __construct(IpAddress $ip, $mask)
00034 {
00035 Assert::isInteger($mask);
00036
00037 if ($mask == 0 || self::MASK_MAX_SIZE < $mask)
00038 throw new WrongArgumentException('wrong mask given');
00039
00040 $this->longMask =
00041 (int) (pow(2, (32 - $mask)) * (pow(2, $mask) - 1));
00042
00043 if (($ip->getLongIp() & $this->longMask) != $ip->getLongIp())
00044 throw new WrongArgumentException('wrong ip network given');
00045
00046 $this->ip = $ip;
00047 $this->mask = $mask;
00048 }
00049
00050 public function getMask()
00051 {
00052 return $this->mask;
00053 }
00054
00058 public function getStart()
00059 {
00060 return $this->ip;
00061 }
00062
00066 public function getEnd()
00067 {
00068 if (!$this->end) {
00069 $this->end =
00070 IpAddress::create(
00071 long2ip($this->ip->getLongIp() | ~$this->longMask)
00072 );
00073 }
00074
00075 return $this->end;
00076 }
00077
00078 public function contains( $probe)
00079 {
00080 return
00081 ($probe->getLongIp() & $this->longMask)
00082 == $this->ip->getLongIp();
00083 }
00084 }
00085 ?>