The version of vichan running on lainchan.org
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

707 行
23KB

  1. <?php
  2. /**
  3. * This file is part of the Lifo\IP PHP Library.
  4. *
  5. * (c) Jason Morriss <lifo2013@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Lifo\IP;
  11. /**
  12. * CIDR Block helper class.
  13. *
  14. * Most routines can be used statically or by instantiating an object and
  15. * calling its methods.
  16. *
  17. * Provides routines to do various calculations on IP addresses and ranges.
  18. * Convert to/from CIDR to ranges, etc.
  19. */
  20. class CIDR
  21. {
  22. const INTERSECT_NO = 0;
  23. const INTERSECT_YES = 1;
  24. const INTERSECT_LOW = 2;
  25. const INTERSECT_HIGH = 3;
  26. protected $start;
  27. protected $end;
  28. protected $prefix;
  29. protected $version;
  30. protected $istart;
  31. protected $iend;
  32. private $cache;
  33. /**
  34. * Create a new CIDR object.
  35. *
  36. * The IP range can be arbitrary and does not have to fall on a valid CIDR
  37. * range. Some methods will return different values depending if you ignore
  38. * the prefix or not. By default all prefix sensitive methods will assume
  39. * the prefix is used.
  40. *
  41. * @param string $cidr An IP address (1.2.3.4), CIDR block (1.2.3.4/24),
  42. * or range "1.2.3.4-1.2.3.10"
  43. * @param string $end Ending IP in range if no cidr/prefix is given
  44. */
  45. public function __construct($cidr, $end = null)
  46. {
  47. if ($end !== null) {
  48. $this->setRange($cidr, $end);
  49. } else {
  50. $this->setCidr($cidr);
  51. }
  52. }
  53. /**
  54. * Returns the string representation of the CIDR block.
  55. */
  56. public function __toString()
  57. {
  58. // do not include the prefix if its a single IP
  59. try {
  60. if ($this->isTrueCidr() && (
  61. ($this->version == 4 and $this->prefix != 32) ||
  62. ($this->version == 6 and $this->prefix != 128)
  63. )
  64. ) {
  65. return $this->start . '/' . $this->prefix;
  66. }
  67. } catch (\Exception $e) {
  68. // isTrueCidr() calls getRange which can throw an exception
  69. }
  70. if (strcmp($this->start, $this->end) == 0) {
  71. return $this->start;
  72. }
  73. return $this->start . ' - ' . $this->end;
  74. }
  75. public function __clone()
  76. {
  77. // do not clone the cache. No real reason why. I just want to keep the
  78. // memory foot print as low as possible, even though this is trivial.
  79. $this->cache = array();
  80. }
  81. /**
  82. * Set an arbitrary IP range.
  83. * The closest matching prefix will be calculated but the actual range
  84. * stored in the object can be arbitrary.
  85. * @param string $start Starting IP or combination "start-end" string.
  86. * @param string $end Ending IP or null.
  87. */
  88. public function setRange($ip, $end = null)
  89. {
  90. if (strpos($ip, '-') !== false) {
  91. list($ip, $end) = array_map('trim', explode('-', $ip, 2));
  92. }
  93. if (false === filter_var($ip, FILTER_VALIDATE_IP) ||
  94. false === filter_var($end, FILTER_VALIDATE_IP)) {
  95. throw new \InvalidArgumentException("Invalid IP range \"$ip-$end\"");
  96. }
  97. // determine version (4 or 6)
  98. $this->version = (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? 6 : 4;
  99. $this->istart = IP::inet_ptod($ip);
  100. $this->iend = IP::inet_ptod($end);
  101. // fix order
  102. if (bccomp($this->istart, $this->iend) == 1) {
  103. list($this->istart, $this->iend) = array($this->iend, $this->istart);
  104. list($ip, $end) = array($end, $ip);
  105. }
  106. $this->start = $ip;
  107. $this->end = $end;
  108. // calculate real prefix
  109. $len = $this->version == 4 ? 32 : 128;
  110. $this->prefix = $len - strlen(BC::bcdecbin(BC::bcxor($this->istart, $this->iend)));
  111. }
  112. /**
  113. * Returns true if the current IP is a true cidr block
  114. */
  115. public function isTrueCidr()
  116. {
  117. return $this->start == $this->getNetwork() && $this->end == $this->getBroadcast();
  118. }
  119. /**
  120. * Set the CIDR block.
  121. *
  122. * The prefix length is optional and will default to 32 ot 128 depending on
  123. * the version detected.
  124. *
  125. * @param string $cidr CIDR block string, eg: "192.168.0.0/24" or "2001::1/64"
  126. * @throws \InvalidArgumentException If the CIDR block is invalid
  127. */
  128. public function setCidr($cidr)
  129. {
  130. if (strpos($cidr, '-') !== false) {
  131. return $this->setRange($cidr);
  132. }
  133. list($ip, $bits) = array_pad(array_map('trim', explode('/', $cidr, 2)), 2, null);
  134. if (false === filter_var($ip, FILTER_VALIDATE_IP)) {
  135. throw new \InvalidArgumentException("Invalid IP address \"$cidr\"");
  136. }
  137. // determine version (4 or 6)
  138. $this->version = (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? 6 : 4;
  139. $this->start = $ip;
  140. $this->istart = IP::inet_ptod($ip);
  141. if ($bits !== null and $bits !== '') {
  142. $this->prefix = $bits;
  143. } else {
  144. $this->prefix = $this->version == 4 ? 32 : 128;
  145. }
  146. if (($this->prefix < 0)
  147. || ($this->prefix > 32 and $this->version == 4)
  148. || ($this->prefix > 128 and $this->version == 6)) {
  149. throw new \InvalidArgumentException("Invalid IP address \"$cidr\"");
  150. }
  151. $this->end = $this->getBroadcast();
  152. $this->iend = IP::inet_ptod($this->end);
  153. $this->cache = array();
  154. }
  155. /**
  156. * Get the IP version. 4 or 6.
  157. *
  158. * @return integer
  159. */
  160. public function getVersion()
  161. {
  162. return $this->version;
  163. }
  164. /**
  165. * Get the prefix.
  166. *
  167. * Always returns the "proper" prefix, even if the IP range is arbitrary.
  168. *
  169. * @return integer
  170. */
  171. public function getPrefix()
  172. {
  173. return $this->prefix;
  174. }
  175. /**
  176. * Return the starting presentational IP or Decimal value.
  177. *
  178. * Ignores prefix
  179. */
  180. public function getStart($decimal = false)
  181. {
  182. return $decimal ? $this->istart : $this->start;
  183. }
  184. /**
  185. * Return the ending presentational IP or Decimal value.
  186. *
  187. * Ignores prefix
  188. */
  189. public function getEnd($decimal = false)
  190. {
  191. return $decimal ? $this->iend : $this->end;
  192. }
  193. /**
  194. * Return the next presentational IP or Decimal value (following the
  195. * broadcast address of the current CIDR block).
  196. */
  197. public function getNext($decimal = false)
  198. {
  199. $next = bcadd($this->getEnd(true), '1');
  200. return $decimal ? $next : new self(IP::inet_dtop($next));
  201. }
  202. /**
  203. * Returns true if the IP is an IPv4
  204. *
  205. * @return boolean
  206. */
  207. public function isIPv4()
  208. {
  209. return $this->version == 4;
  210. }
  211. /**
  212. * Returns true if the IP is an IPv6
  213. *
  214. * @return boolean
  215. */
  216. public function isIPv6()
  217. {
  218. return $this->version == 6;
  219. }
  220. /**
  221. * Get the cidr notation for the subnet block.
  222. *
  223. * This is useful for when you want a string representation of the IP/prefix
  224. * and the starting IP is not on a valid network boundrary (eg: Displaying
  225. * an IP from an interface).
  226. *
  227. * @return string IP in CIDR notation "ipaddr/prefix"
  228. */
  229. public function getCidr()
  230. {
  231. return $this->start . '/' . $this->prefix;
  232. }
  233. /**
  234. * Get the [low,high] range of the CIDR block
  235. *
  236. * Prefix sensitive.
  237. *
  238. * @param boolean $ignorePrefix If true the arbitrary start-end range is
  239. * returned. default=false.
  240. */
  241. public function getRange($ignorePrefix = false)
  242. {
  243. $range = $ignorePrefix
  244. ? array($this->start, $this->end)
  245. : self::cidr_to_range($this->start, $this->prefix);
  246. // watch out for IP '0' being converted to IPv6 '::'
  247. if ($range[0] == '::' and strpos($range[1], ':') == false) {
  248. $range[0] = '0.0.0.0';
  249. }
  250. return $range;
  251. }
  252. /**
  253. * Return the IP in its fully expanded form.
  254. *
  255. * For example: 2001::1 == 2007:0000:0000:0000:0000:0000:0000:0001
  256. *
  257. * @see IP::inet_expand
  258. */
  259. public function getExpanded()
  260. {
  261. return IP::inet_expand($this->start);
  262. }
  263. /**
  264. * Get network IP of the CIDR block
  265. *
  266. * Prefix sensitive.
  267. *
  268. * @param boolean $ignorePrefix If true the arbitrary start-end range is
  269. * returned. default=false.
  270. */
  271. public function getNetwork($ignorePrefix = false)
  272. {
  273. // micro-optimization to prevent calling getRange repeatedly
  274. $k = $ignorePrefix ? 1 : 0;
  275. if (!isset($this->cache['range'][$k])) {
  276. $this->cache['range'][$k] = $this->getRange($ignorePrefix);
  277. }
  278. return $this->cache['range'][$k][0];
  279. }
  280. /**
  281. * Get broadcast IP of the CIDR block
  282. *
  283. * Prefix sensitive.
  284. *
  285. * @param boolean $ignorePrefix If true the arbitrary start-end range is
  286. * returned. default=false.
  287. */
  288. public function getBroadcast($ignorePrefix = false)
  289. {
  290. // micro-optimization to prevent calling getRange repeatedly
  291. $k = $ignorePrefix ? 1 : 0;
  292. if (!isset($this->cache['range'][$k])) {
  293. $this->cache['range'][$k] = $this->getRange($ignorePrefix);
  294. }
  295. return $this->cache['range'][$k][1];
  296. }
  297. /**
  298. * Get the network mask based on the prefix.
  299. *
  300. */
  301. public function getMask()
  302. {
  303. return self::prefix_to_mask($this->prefix, $this->version);
  304. }
  305. /**
  306. * Get total hosts within CIDR range
  307. *
  308. * Prefix sensitive.
  309. *
  310. * @param boolean $ignorePrefix If true the arbitrary start-end range is
  311. * returned. default=false.
  312. */
  313. public function getTotal($ignorePrefix = false)
  314. {
  315. // micro-optimization to prevent calling getRange repeatedly
  316. $k = $ignorePrefix ? 1 : 0;
  317. if (!isset($this->cache['range'][$k])) {
  318. $this->cache['range'][$k] = $this->getRange($ignorePrefix);
  319. }
  320. return bcadd(bcsub(IP::inet_ptod($this->cache['range'][$k][1]),
  321. IP::inet_ptod($this->cache['range'][$k][0])), '1');
  322. }
  323. public function intersects($cidr)
  324. {
  325. return self::cidr_intersect((string)$this, $cidr);
  326. }
  327. /**
  328. * Determines the intersection between an IP (with optional prefix) and a
  329. * CIDR block.
  330. *
  331. * The IP will be checked against the CIDR block given and will either be
  332. * inside or outside the CIDR completely, or partially.
  333. *
  334. * NOTE: The caller should explicitly check against the INTERSECT_*
  335. * constants because this method will return a value > 1 even for partial
  336. * matches.
  337. *
  338. * @param mixed $ip The IP/cidr to match
  339. * @param mixed $cidr The CIDR block to match within
  340. * @return integer Returns an INTERSECT_* constant
  341. * @throws \InvalidArgumentException if either $ip or $cidr is invalid
  342. */
  343. public static function cidr_intersect($ip, $cidr)
  344. {
  345. // use fixed length HEX strings so we can easily do STRING comparisons
  346. // instead of using slower bccomp() math.
  347. list($lo,$hi) = array_map(function($v){ return sprintf("%032s", IP::inet_ptoh($v)); }, CIDR::cidr_to_range($ip));
  348. list($min,$max) = array_map(function($v){ return sprintf("%032s", IP::inet_ptoh($v)); }, CIDR::cidr_to_range($cidr));
  349. /** visualization of logic used below
  350. lo-hi = $ip to check
  351. min-max = $cidr block being checked against
  352. --- --- --- lo --- --- hi --- --- --- --- --- IP/prefix to check
  353. --- min --- --- max --- --- --- --- --- --- --- Partial "LOW" match
  354. --- --- --- --- --- min --- --- max --- --- --- Partial "HIGH" match
  355. --- --- --- --- min max --- --- --- --- --- --- No match "NO"
  356. --- --- --- --- --- --- --- --- min --- max --- No match "NO"
  357. min --- max --- --- --- --- --- --- --- --- --- No match "NO"
  358. --- --- min --- --- --- --- max --- --- --- --- Full match "YES"
  359. */
  360. // IP is exact match or completely inside the CIDR block
  361. if ($lo >= $min and $hi <= $max) {
  362. return self::INTERSECT_YES;
  363. }
  364. // IP is completely outside the CIDR block
  365. if ($max < $lo or $min > $hi) {
  366. return self::INTERSECT_NO;
  367. }
  368. // @todo is it useful to return LOW/HIGH partial matches?
  369. // IP matches the lower end
  370. if ($max <= $hi and $min <= $lo) {
  371. return self::INTERSECT_LOW;
  372. }
  373. // IP matches the higher end
  374. if ($min >= $lo and $max >= $hi) {
  375. return self::INTERSECT_HIGH;
  376. }
  377. return self::INTERSECT_NO;
  378. }
  379. /**
  380. * Converts an IPv4 or IPv6 CIDR block into its range.
  381. *
  382. * @todo May not be the fastest way to do this.
  383. *
  384. * @static
  385. * @param string $cidr CIDR block or IP address string.
  386. * @param integer|null $bits If /bits is not specified on string they can be
  387. * passed via this parameter instead.
  388. * @return array A 2 element array with the low, high range
  389. */
  390. public static function cidr_to_range($cidr, $bits = null)
  391. {
  392. if (strpos($cidr, '/') !== false) {
  393. list($ip, $_bits) = array_pad(explode('/', $cidr, 2), 2, null);
  394. } else {
  395. $ip = $cidr;
  396. $_bits = $bits;
  397. }
  398. if (false === filter_var($ip, FILTER_VALIDATE_IP)) {
  399. throw new \InvalidArgumentException("IP address \"$cidr\" is invalid");
  400. }
  401. // force bit length to 32 or 128 depending on type of IP
  402. $bitlen = (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? 128 : 32;
  403. if ($bits === null) {
  404. // if no prefix is given use the length of the binary string which
  405. // will give us 32 or 128 and result in a single IP being returned.
  406. $bits = $_bits !== null ? $_bits : $bitlen;
  407. }
  408. if ($bits > $bitlen) {
  409. throw new \InvalidArgumentException("IP address \"$cidr\" is invalid");
  410. }
  411. $ipdec = IP::inet_ptod($ip);
  412. $ipbin = BC::bcdecbin($ipdec, $bitlen);
  413. // calculate network
  414. $netmask = BC::bcbindec(str_pad(str_repeat('1',$bits), $bitlen, '0'));
  415. $ip1 = BC::bcand($ipdec, $netmask);
  416. // calculate "broadcast" (not technically a broadcast in IPv6)
  417. $ip2 = BC::bcor($ip1, BC::bcnot($netmask));
  418. return array(IP::inet_dtop($ip1), IP::inet_dtop($ip2));
  419. }
  420. /**
  421. * Return the CIDR string from the range given
  422. */
  423. public static function range_to_cidr($start, $end)
  424. {
  425. $cidr = new CIDR($start, $end);
  426. return (string)$cidr;
  427. }
  428. /**
  429. * Return the maximum prefix length that would fit the IP address given.
  430. *
  431. * This is useful to determine how my bit would be needed to store the IP
  432. * address when you don't already have a prefix for the IP.
  433. *
  434. * @example 216.240.32.0 would return 27
  435. *
  436. * @param string $ip IP address without prefix
  437. * @param integer $bits Maximum bits to check; defaults to 32 for IPv4 and 128 for IPv6
  438. */
  439. public static function max_prefix($ip, $bits = null)
  440. {
  441. static $mask = array();
  442. $ver = (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? 6 : 4;
  443. $max = $ver == 6 ? 128 : 32;
  444. if ($bits === null) {
  445. $bits = $max;
  446. }
  447. $int = IP::inet_ptod($ip);
  448. while ($bits > 0) {
  449. // micro-optimization; calculate mask once ...
  450. if (!isset($mask[$ver][$bits-1])) {
  451. // 2^$max - 2^($max - $bits);
  452. if ($ver == 4) {
  453. $mask[$ver][$bits-1] = pow(2, $max) - pow(2, $max - ($bits-1));
  454. } else {
  455. $mask[$ver][$bits-1] = bcsub(bcpow(2, $max), bcpow(2, $max - ($bits-1)));
  456. }
  457. }
  458. $m = $mask[$ver][$bits-1];
  459. //printf("%s/%d: %s & %s == %s\n", $ip, $bits-1, BC::bcdecbin($m, 32), BC::bcdecbin($int, 32), BC::bcdecbin(BC::bcand($int, $m)));
  460. //echo "$ip/", $bits-1, ": ", IP::inet_dtop($m), " ($m) & $int == ", BC::bcand($int, $m), "\n";
  461. if (bccomp(BC::bcand($int, $m), $int) != 0) {
  462. return $bits;
  463. }
  464. $bits--;
  465. }
  466. return $bits;
  467. }
  468. /**
  469. * Return a contiguous list of true CIDR blocks that span the range given.
  470. *
  471. * Note: It's not a good idea to call this with IPv6 addresses. While it may
  472. * work for certain ranges this can be very slow. Also an IPv6 list won't be
  473. * as accurate as an IPv4 list.
  474. *
  475. * @example
  476. * range_to_cidrlist(192.168.0.0, 192.168.0.15) ==
  477. * 192.168.0.0/28
  478. * range_to_cidrlist(192.168.0.0, 192.168.0.20) ==
  479. * 192.168.0.0/28
  480. * 192.168.0.16/30
  481. * 192.168.0.20/32
  482. */
  483. public static function range_to_cidrlist($start, $end)
  484. {
  485. $ver = (false === filter_var($start, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? 6 : 4;
  486. $start = IP::inet_ptod($start);
  487. $end = IP::inet_ptod($end);
  488. $len = $ver == 4 ? 32 : 128;
  489. $log2 = $ver == 4 ? log(2) : BC::bclog(2);
  490. $list = array();
  491. while (BC::cmp($end, $start) >= 0) { // $end >= $start
  492. $prefix = self::max_prefix(IP::inet_dtop($start), $len);
  493. if ($ver == 4) {
  494. $diff = $len - floor( log($end - $start + 1) / $log2 );
  495. } else {
  496. // this is not as accurate due to the bclog function
  497. $diff = bcsub($len, BC::bcfloor(bcdiv(BC::bclog(bcadd(bcsub($end, $start), '1')), $log2)));
  498. }
  499. if ($prefix < $diff) {
  500. $prefix = $diff;
  501. }
  502. $list[] = IP::inet_dtop($start) . "/" . $prefix;
  503. if ($ver == 4) {
  504. $start += pow(2, $len - $prefix);
  505. } else {
  506. $start = bcadd($start, bcpow(2, $len - $prefix));
  507. }
  508. }
  509. return $list;
  510. }
  511. /**
  512. * Return an list of optimized CIDR blocks by collapsing adjacent CIDR
  513. * blocks into larger blocks.
  514. *
  515. * @param array $cidrs List of CIDR block strings or objects
  516. * @param integer $maxPrefix Maximum prefix to allow
  517. * @return array Optimized list of CIDR objects
  518. */
  519. public static function optimize_cidrlist($cidrs, $maxPrefix = 32)
  520. {
  521. // all indexes must be a CIDR object
  522. $cidrs = array_map(function($o){ return $o instanceof CIDR ? $o : new CIDR($o); }, $cidrs);
  523. // sort CIDR blocks in proper order so we can easily loop over them
  524. $cidrs = self::cidr_sort($cidrs);
  525. $list = array();
  526. while ($cidrs) {
  527. $c = array_shift($cidrs);
  528. $start = $c->getStart();
  529. $max = bcadd($c->getStart(true), $c->getTotal());
  530. // loop through each cidr block until its ending range is more than
  531. // the current maximum.
  532. while (!empty($cidrs) and $cidrs[0]->getStart(true) <= $max) {
  533. $b = array_shift($cidrs);
  534. $newmax = bcadd($b->getStart(true), $b->getTotal());
  535. if ($newmax > $max) {
  536. $max = $newmax;
  537. }
  538. }
  539. // add the new cidr range to the optimized list
  540. $list = array_merge($list, self::range_to_cidrlist($start, IP::inet_dtop(bcsub($max, '1'))));
  541. }
  542. return $list;
  543. }
  544. /**
  545. * Sort the list of CIDR blocks, optionally with a custom callback function.
  546. *
  547. * @param array $cidrs A list of CIDR blocks (strings or objects)
  548. * @param Closure $callback Optional callback to perform the sorting.
  549. * See PHP usort documentation for more details.
  550. */
  551. public static function cidr_sort($cidrs, $callback = null)
  552. {
  553. // all indexes must be a CIDR object
  554. $cidrs = array_map(function($o){ return $o instanceof CIDR ? $o : new CIDR($o); }, $cidrs);
  555. if ($callback === null) {
  556. $callback = function($a, $b) {
  557. if (0 != ($o = BC::cmp($a->getStart(true), $b->getStart(true)))) {
  558. return $o; // < or >
  559. }
  560. if ($a->getPrefix() == $b->getPrefix()) {
  561. return 0;
  562. }
  563. return $a->getPrefix() < $b->getPrefix() ? -1 : 1;
  564. };
  565. } elseif (!($callback instanceof \Closure) or !is_callable($callback)) {
  566. throw new \InvalidArgumentException("Invalid callback in CIDR::cidr_sort, expected Closure, got " . gettype($callback));
  567. }
  568. usort($cidrs, $callback);
  569. return $cidrs;
  570. }
  571. /**
  572. * Return the Prefix bits from the IPv4 mask given.
  573. *
  574. * This is only valid for IPv4 addresses since IPv6 addressing does not
  575. * have a concept of network masks.
  576. *
  577. * Example: 255.255.255.0 == 24
  578. *
  579. * @param string $mask IPv4 network mask.
  580. */
  581. public static function mask_to_prefix($mask)
  582. {
  583. if (false === filter_var($mask, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  584. throw new \InvalidArgumentException("Invalid IP netmask \"$mask\"");
  585. }
  586. return strrpos(IP::inet_ptob($mask, 32), '1') + 1;
  587. }
  588. /**
  589. * Return the network mask for the prefix given.
  590. *
  591. * Normally this is only useful for IPv4 addresses but you can generate a
  592. * mask for IPv6 addresses as well, only because its mathematically
  593. * possible.
  594. *
  595. * @param integer $prefix CIDR prefix bits (0-128)
  596. * @param integer $version IP version. If null the version will be detected
  597. * based on the prefix length given.
  598. */
  599. public static function prefix_to_mask($prefix, $version = null)
  600. {
  601. if ($version === null) {
  602. $version = $prefix > 32 ? 6 : 4;
  603. }
  604. if ($prefix < 0 or $prefix > 128) {
  605. throw new \InvalidArgumentException("Invalid prefix length \"$prefix\"");
  606. }
  607. if ($version != 4 and $version != 6) {
  608. throw new \InvalidArgumentException("Invalid version \"$version\". Must be 4 or 6");
  609. }
  610. if ($version == 4) {
  611. return long2ip($prefix == 0 ? 0 : (0xFFFFFFFF >> (32 - $prefix)) << (32 - $prefix));
  612. } else {
  613. return IP::inet_dtop($prefix == 0 ? 0 : BC::bcleft(BC::bcright(BC::MAX_UINT_128, 128-$prefix), 128-$prefix));
  614. }
  615. }
  616. /**
  617. * Return true if the $ip given is a true CIDR block.
  618. *
  619. * A true CIDR block is one where the $ip given is the actual Network
  620. * address and broadcast matches the prefix appropriately.
  621. */
  622. public static function cidr_is_true($ip)
  623. {
  624. $ip = new CIDR($ip);
  625. return $ip->isTrueCidr();
  626. }
  627. }