# IpNetwork.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007 by Vladimir A. Altuchov                            *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00015     final class IpNetwork implements SingleRange
    00016     {
    00017         const MASK_MAX_SIZE = 31;
    00018
    00019         private $ip         = null;
    00020         private $end        = null;
    00021         private $mask       = null;
    00022         private $longMask   = null;
    00023
    00027         public static function create(IpAddress $ip, $mask)
    00028         {
    00029             return new self($ip, $mask);
    00030         }
    00031
    00032         public function __construct(IpAddress $ip, $mask)
    00033         {
    00034             Assert::isInteger($mask);
    00035
    00036             if ($mask == 0 || self::MASK_MAX_SIZE < $mask)
    00037                 throw new WrongArgumentException('wrong mask given');
    00038
    00039             $this->longMask =
    00040                 (int) (pow(2, (32 - $mask)) * (pow(2, $mask) - 1));
    00041
    00042             if (($ip->getLongIp() & $this->longMask) != $ip->getLongIp())
    00043                 throw new WrongArgumentException('wrong ip network given');
    00044
    00045             $this->ip = $ip;
    00046             $this->mask = $mask;
    00047         }
    00048
    00049         public function getMask()
    00050         {
    00051             return $this->mask;
    00052         }
    00053
    00057         public function getStart()
    00058         {
    00059             return $this->ip;
    00060         }
    00061
    00065         public function getEnd()
    00066         {
    00067             if (!$this->end) {
    00068                 $this->end =
    00069                     IpAddress::create(
    00070                         long2ip($this->ip->getLongIp() | ~$this->longMask)
    00071                     );
    00072             }
    00073
    00074             return $this->end;
    00075         }
    00076
    00077         public function contains(/* IpAddress */ $probe)
    00078         {
    00079             Assert::isInstance($probe, 'IpAddress');
    00080
    00081             return
    00082                 ($probe->getLongIp() & $this->longMask)
    00083                 == $this->ip->getLongIp();
    00084         }
    00085     }
    00086 ?>