ipsec: add shared function to simplify ipsec code #5201

This commit is contained in:
Franco Fichtner 2021-09-20 20:34:28 +02:00
parent e2ad649886
commit 82b2ede99b
2 changed files with 10 additions and 4 deletions

View File

@ -1861,13 +1861,11 @@ function ipsec_get_configured_vtis()
}
$inet = is_ipaddrv6($phase2['tunnel_local']) ? 'inet6' : 'inet';
$mask = $inet == 'inet6' ? find_smallest_cidr6([$phase2['tunnel_local'], $phase2['tunnel_remote']]) :
find_smallest_cidr([$phase2['tunnel_local'], $phase2['tunnel_remote']]);
$configured_intf[$intfnm]['networks'][] = [
'inet' => $inet,
'mask' => find_smallest_cidr([$phase2['tunnel_local'], $phase2['tunnel_remote']], $inet),
'tunnel_local' => $phase2['tunnel_local'],
'mask' => $mask,
'tunnel_remote' => $phase2['tunnel_remote']
];
}

View File

@ -470,8 +470,16 @@ function ip2ulong($ip)
return sprintf("%u", ip2long32($ip));
}
/* Find the smallest possible subnet mask for given IP range */
function find_smallest_cidr($ips, $family = 'inet')
{
return $family == 'inet6' ?
find_smallest_cidr6($ips) :
find_smallest_cidr4($ips);
}
/* Find the smallest possible subnet mask for given IPv4 range */
function find_smallest_cidr($ips)
function find_smallest_cidr4($ips)
{
foreach ($ips as $id => $ip) {
$ips[$id] = ip2long($ip);