Project

General

Profile

« Previous | Next » 

Revision b17ac4f7

Added by Stilez y over 10 years ago

"Like with like" - move a few functions to better places in the code (they are placed strangely)

A few functions such as ipcmp(), subnet_expand(), and check_subnets_overlap() are in illogical places - away from all the other ip comparison and subnet basic functions and in the middle of alias handling and interface enumeration.

No change to functional code, just moving to earlier in the file (next to other IP compare and subnet functions) for ease of future contributors.

View differences:

etc/inc/util.inc
440 440
	return ip2ulong($ip1) > ip2ulong($ip2);
441 441
}
442 442

  
443
/* compare two IP addresses */
444
function ipcmp($a, $b) {
445
	if (ip_less_than($a, $b))
446
		return -1;
447
	else if (ip_greater_than($a, $b))
448
		return 1;
449
	else
450
		return 0;
451
}
452

  
443 453
/* Convert a range of IPv4 addresses to an array of individual addresses. */
444 454
/* Note: IPv6 ranges are not yet supported here. */
445 455
function ip_range_to_address_array($startip, $endip, $max_size = 5000) {
......
682 692
		return is_subnet($subnet);
683 693
}
684 694

  
695
function subnet_size($subnet) {
696
	if (is_subnetv4($subnet)) {
697
		list ($ip, $bits) = explode("/", $subnet);
698
		return round(exp(log(2) * (32 - $bits)));
699
	}
700
	else if (is_subnetv6($subnet)) {
701
		list ($ip, $bits) = explode("/", $subnet);
702
		return round(exp(log(2) * (128 - $bits)));
703
	}
704
	else {
705
		return 0;
706
	}
707
}
708

  
709

  
710
function subnet_expand($subnet) {
711
	if (is_subnetv4($subnet)) {
712
		return subnetv4_expand($subnet);
713
	} else if (is_subnetv6($subnet)) {
714
		return subnetv6_expand($subnet);
715
	} else {
716
		return $subnet;
717
	}
718
}
719

  
720
function subnetv4_expand($subnet) {
721
	$result = array();
722
	list ($ip, $bits) = explode("/", $subnet);
723
	$net  = ip2long($ip);
724
	$mask = (0xffffffff << (32 - $bits));
725
	$net &= $mask;
726
	$size = round(exp(log(2) * (32 - $bits)));
727
	for ($i = 0; $i < $size; $i += 1) {
728
		$result[] = long2ip($net | $i);
729
	}
730
	return $result;
731
}
732

  
733
/* find out whether two subnets overlap */
734
function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
735

  
736
	if (!is_numeric($bits1))
737
		$bits1 = 32;
738
	if (!is_numeric($bits2))
739
		$bits2 = 32;
740

  
741
	if ($bits1 < $bits2)
742
		$relbits = $bits1;
743
	else
744
		$relbits = $bits2;
745

  
746
	$sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1);
747
	$sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2);
748

  
749
	return ($sn1 == $sn2);
750
}
751

  
752
/* find out whether two IPv6 subnets overlap */
753
function check_subnetsv6_overlap($subnet1, $bits1, $subnet2, $bits2) {
754
	$sub1_min = gen_subnetv6($subnet1, $bits1);
755
	$sub1_max = gen_subnetv6_max($subnet1, $bits1);
756
	$sub2_min = gen_subnetv6($subnet2, $bits2);
757
	$sub2_max = gen_subnetv6_max($subnet2, $bits2);
758

  
759
	return (is_inrange_v6($sub1_min, $sub2_min, $sub2_max) || is_inrange_v6($sub1_max, $sub2_min, $sub2_max) || is_inrange_v6($sub2_min, $sub1_min, $sub1_max));
760
}
761

  
762
/* return true if $addr is in $subnet, false if not */
763
function ip_in_subnet($addr,$subnet) {
764
	if(is_ipaddrv6($addr)) {
765
		return (Net_IPv6::isInNetmask($addr, $subnet));
766
	} else { /* XXX: Maybe check for IPv4 */
767
		list($ip, $mask) = explode('/', $subnet);
768
		$mask = (0xffffffff << (32 - $mask)) & 0xffffffff;
769
		return ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
770
	}
771
}
772

  
685 773
/* returns true if $hostname is just a valid hostname (top part without any of the domain part) */
686 774
function is_unqualified_hostname($hostname) {
687 775
	if (!is_string($hostname))
......
1369 1457
	return null;
1370 1458
}
1371 1459

  
1372
function subnet_size($subnet) {
1373
	if (is_subnetv4($subnet)) {
1374
		list ($ip, $bits) = explode("/", $subnet);
1375
		return round(exp(log(2) * (32 - $bits)));
1376
	}
1377
	else if (is_subnetv6($subnet)) {
1378
		list ($ip, $bits) = explode("/", $subnet);
1379
		return round(exp(log(2) * (128 - $bits)));
1380
	}
1381
	else {
1382
		return 0;
1383
	}
1384
}
1385

  
1386
function subnet_expand($subnet) {
1387
	if (is_subnetv4($subnet)) {
1388
		return subnetv4_expand($subnet);
1389
	} else if (is_subnetv6($subnet)) {
1390
		return subnetv6_expand($subnet);
1391
	} else {
1392
		return $subnet;
1393
	}
1394
}
1395

  
1396
function subnetv4_expand($subnet) {
1397
	$result = array();
1398
	list ($ip, $bits) = explode("/", $subnet);
1399
	$net  = ip2long($ip);
1400
	$mask = (0xffffffff << (32 - $bits));
1401
	$net &= $mask;
1402
	$size = round(exp(log(2) * (32 - $bits)));
1403
	for ($i = 0; $i < $size; $i += 1) {
1404
		$result[] = long2ip($net | $i);
1405
	}
1406
	return $result;
1407
}
1408

  
1409
/* find out whether two subnets overlap */
1410
function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
1411

  
1412
	if (!is_numeric($bits1))
1413
		$bits1 = 32;
1414
	if (!is_numeric($bits2))
1415
		$bits2 = 32;
1416

  
1417
	if ($bits1 < $bits2)
1418
		$relbits = $bits1;
1419
	else
1420
		$relbits = $bits2;
1421

  
1422
	$sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1);
1423
	$sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2);
1424

  
1425
	return ($sn1 == $sn2);
1426
}
1427

  
1428
/* find out whether two IPv6 subnets overlap */
1429
function check_subnetsv6_overlap($subnet1, $bits1, $subnet2, $bits2) {
1430
	$sub1_min = gen_subnetv6($subnet1, $bits1);
1431
	$sub1_max = gen_subnetv6_max($subnet1, $bits1);
1432
	$sub2_min = gen_subnetv6($subnet2, $bits2);
1433
	$sub2_max = gen_subnetv6_max($subnet2, $bits2);
1434

  
1435
	return (is_inrange_v6($sub1_min, $sub2_min, $sub2_max) || is_inrange_v6($sub1_max, $sub2_min, $sub2_max) || is_inrange_v6($sub2_min, $sub1_min, $sub1_max));
1436
}
1437

  
1438
/* compare two IP addresses */
1439
function ipcmp($a, $b) {
1440
	if (ip_less_than($a, $b))
1441
		return -1;
1442
	else if (ip_greater_than($a, $b))
1443
		return 1;
1444
	else
1445
		return 0;
1446
}
1447

  
1448
/* return true if $addr is in $subnet, false if not */
1449
function ip_in_subnet($addr,$subnet) {
1450
	if(is_ipaddrv6($addr)) {
1451
		return (Net_IPv6::isInNetmask($addr, $subnet));
1452
	} else { /* XXX: Maybe check for IPv4 */
1453
		list($ip, $mask) = explode('/', $subnet);
1454
		$mask = (0xffffffff << (32 - $mask)) & 0xffffffff;
1455
		return ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
1456
	}
1457
}
1458

  
1459 1460
/* verify (and remove) the digital signature on a file - returns 0 if OK */
1460 1461
function verify_digital_signature($fname) {
1461 1462
	global $g;

Also available in: Unified diff