From 2328dcc5f0fd5cd875ec15d47260d3626615bb0d Mon Sep 17 00:00:00 2001 From: Seth Mos Date: Mon, 21 Dec 2009 15:27:20 +0100 Subject: Partial rewrite of gateway code, now partially facilitates dynamic interfaces - Upgrade code takes different naming into account - Add gateway entries for dynamic interfaces - Rewrite status array to also support dynamic interfaces. - Always use unique localhost monitor IP addresses for down dynamic interfaces - Accept the gateway IP address of "dynamic" on the gateway edit page --- etc/inc/gwlb.inc | 124 +++++++++++++++++++++------------ etc/inc/upgrade_config.inc | 54 +++++++------- usr/local/www/system_gateways_edit.php | 31 +++++---- 3 files changed, 126 insertions(+), 83 deletions(-) diff --git a/etc/inc/gwlb.inc b/etc/inc/gwlb.inc index 3f0e151..fbc3145 100644 --- a/etc/inc/gwlb.inc +++ b/etc/inc/gwlb.inc @@ -143,7 +143,13 @@ EOD; /* add static routes for each gateway with their monitor IP */ if(is_array($gateways_arr)) { + $i = 2; foreach($gateways_arr as $name => $gateway) { + /* for dynamic gateways without an IP address we subtitute a local one */ + if($gateway['gateway'] == "dynamic") { + $gateway['monitor'] = "127.0.0.{$i}"; + $i++; + } if($gateway['monitor'] == "") { $gateway['monitor'] = $gateway['gateway']; } @@ -187,109 +193,135 @@ function return_gateways_status() { $gateways_arr = return_gateways_array(); $apingerstatus = array(); - if(is_readable("{$g['tmp_path']}/apinger.status")) + if(is_readable("{$g['tmp_path']}/apinger.status")) { $apingerstatus = file("{$g['tmp_path']}/apinger.status"); + } $status = array(); + $i = 2; + /* assign a dummy IP address for dynamic interfaces in case we need it */ + $monitor = array(); + foreach($gateways_arr as $name => $gateway) { + if($gateway['monitor'] == "dynamic") { + $gateway['monitor'] = "127.0.0.{$i}"; + $i++; + } + $status[$gateway['monitor']]['monitor'] = $gateway['monitor']; + $status[$gateway['monitor']]['interface'] = $gateway['interface']; + $status[$gateway['monitor']]['gateway'] = $gateway['gateway']; + $status[$gateway['monitor']]['name'] = $gateway['name']; + $status[$gateway['monitor']]['status'] = "down"; + + } foreach($apingerstatus as $line) { $fields = explode(":", $line); switch($fields[0]) { case "Target": $target = trim($fields[1]); - $status[$target] = array(); - $status[$target]['monitor'] = $target; - foreach($gateways_arr as $name => $gateway) { - if($gateway['monitor'] == "$target") { - $status[$target]['gateway'] = $gateway['gateway']; - $status[$target]['interface'] = $gateway['interface']; - } - } break; case "Description": - $status[$target]['name'] = trim($fields[1]); + if($target) + $status[$target]['name'] = trim($fields[1]); break; case "Last reply received": - $status[$target]['lastcheck'] = trim($fields[1]) .":". trim($fields[2]) .":". trim($fields[3]); + if($target) + $status[$target]['lastcheck'] = trim($fields[1]) .":". trim($fields[2]) .":". trim($fields[3]); break; case "Average delay": - $status[$target]['delay'] = trim($fields[1]); + if($target) + $status[$target]['delay'] = trim($fields[1]); break; case "Average packet loss": - $status[$target]['loss'] = trim($fields[1]); + if($target) + $status[$target]['loss'] = trim($fields[1]); break; case "Active alarms": - $status[$target]['status'] = trim($fields[1]); + if($target) + $status[$target]['status'] = trim($fields[1]); break; } } return($status); } +/* Return all configured gateways on the system */ function return_gateways_array($disabled = false) { global $config; $gateways_arr = array(); /* Loop through all interfaces with a gateway and add it to a array */ - if ($disabled == false) + if ($disabled == false) { $iflist = get_configured_interface_with_descr(); - else + } else { $iflist = get_configured_interface_with_descr(false, true); + } - foreach($iflist as $ifname => $friendly ) { - if(interface_has_gateway($ifname)) { + /* tack on all the hard defined gateways as well */ + if(is_array($config['gateways']['gateway_item'])) { + $i = 0; + foreach($config['gateways']['gateway_item'] as $gateway) { + if($gateway['gateway'] == "dynamic") { + $gateway['gateway'] = get_interface_gateway($gateway['interface']); + /* no IP address found, set to dynamic */ + if(! is_ipaddr($gateway['gateway'])) { + $gateway['gateway'] = "dynamic"; + } + } + if($gateway['monitor'] == "") { + $gateway['monitor'] = $gateway['gateway']; + } + /* include the gateway index as the attribute */ + $gateway['interface'] = convert_friendly_interface_to_real_interface_name($gateway['interface']); + $gateway['attribute'] = "$i"; + + $gateways_arr[$gateway['name']] = $gateway; + $i++; + } + } else { + foreach($iflist as $ifname => $friendly ) { + if(! interface_has_gateway($ifname)) { + continue; + } $gateway = array(); $gateway['gateway'] = get_interface_gateway($ifname); + $gateway['interface'] = get_real_interface($ifname); + $gateway['name'] = "{$ifname}"; + $gateway['attribute'] = "system"; + /* Loopback dummy for dynamic interfaces without a IP */ if(!is_ipaddr(trim($gateway['gateway']))) { - $gateway['gateway'] = "127.0.0.2"; + $gateway['gateway'] = "dynamic"; } - /* - * do not add dynamic gateways if it is also found - * in the gateways array. - * XXX: NB: Can this ever happen?! - * smos@ get_interface_gateway() also succeeds for - * static gateways, thus they need to be excluded - */ + + /* automatically skip known static and dynamic gateways we have a array entry for */ if(is_array($config['gateways']['gateway_item'])) { foreach($config['gateways']['gateway_item'] as $gateway_item) { if($gateway_item['gateway'] == $gateway['gateway']) { continue 2; } + if(($gateway_item['gateway'] == "dynamic") && ($gateway_item['interface'] == $gateway['interface'])) { + continue 2; + } } } /* retrieve a proper monitor IP? */ - if(is_ipaddr($config['interfaces'][$ifname]['monitorip'])) { - $gateway['monitor'] = $config['interfaces'][$ifname]['monitorip']; + if(is_ipaddr($interface['monitorip'])) { + $gateway['monitor'] = $interface['monitorip']; } else { $gateway['monitor'] = $gateway['gateway']; } - $gateway['interface'] = get_real_interface($ifname); - $gateway['name'] = "{$ifname}"; - $gateway['descr'] = "Interface {$friendly} Auto added Gateway"; - $gateway['attribute'] = "system"; - $gateways_arr[$ifname] = $gateway; - } - } + $gateway['descr'] = "Interface $ifname Dynamic Gateway"; - /* tack on all the hard defined gateways as well */ - if(is_array($config['gateways']['gateway_item'])) { - $i = 0; - foreach($config['gateways']['gateway_item'] as $gateway) { - if($gateway['monitor'] == "") { - $gateway['monitor'] = $gateway['gateway']; - } - /* include the gateway index as the attribute */ - $gateway['interface'] = convert_friendly_interface_to_real_interface_name($gateway['interface']); - $gateway['attribute'] = "$i"; - $gateways_arr[$gateway['name']] = $gateway; + $gateways_arr[$ifname] = $gateway; $i++; } } return($gateways_arr); } + /* return a array with all gateway groups with name as key * All gateway groups will be processed before returning the array. */ diff --git a/etc/inc/upgrade_config.inc b/etc/inc/upgrade_config.inc index 24a104f..0a8cf45 100644 --- a/etc/inc/upgrade_config.inc +++ b/etc/inc/upgrade_config.inc @@ -763,32 +763,34 @@ function upgrade_042_to_043() { if(! interface_has_gateway($ifname)) { continue; } - if(is_ipaddr($config['interfaces'][$ifname]['gateway'])) { + if(is_ipaddr($interface['gateway'])) { $config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway']; - $config['gateways']['gateway_item'][$i]['interface'] = $ifname; - $config['gateways']['gateway_item'][$i]['name'] = strtoupper($ifname) ."_GW"; - /* add default gateway bit for wan on upgrade */ - if($ifname == "wan") { - $config['gateways']['gateway_item'][$i]['defaultgw'] = true; - } - if(is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) { - $config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway']; - unset($config['interfaces'][$ifname]['use_rrd_gateway']); - } - $config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name']; + $config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Static Gateway"; + } else { + $config['gateways']['gateway_item'][$i]['gateway'] = "dynamic"; + $config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Dynamic Gateway"; + } + $config['gateways']['gateway_item'][$i]['interface'] = $ifname; + $config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname); + /* add default gateway bit for wan on upgrade */ + if($ifname == "wan") { + $config['gateways']['gateway_item'][$i]['defaultgw'] = true; + } + if(is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) { + $config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway']; + unset($config['interfaces'][$ifname]['use_rrd_gateway']); + } + $config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name']; - /* Update all filter rules which might reference this gateway */ - $j = 0; - foreach($config['filter']['rule'] as $rule) { - if(is_ipaddr($rule['gateway']) && ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway'])) { - $config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name']; - } - $j++; + /* Update all filter rules which might reference this gateway */ + $j = 0; + foreach($config['filter']['rule'] as $rule) { + if(is_ipaddr($rule['gateway']) && ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway'])) { + $config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name']; } - - /* any other gateway upgrades here */ - $i++; + $j++; } + $i++; } } @@ -1548,8 +1550,8 @@ function upgrade_053_to_054() { foreach($lbpool['servers'] as $member) { $split = split("\|", $member); $interface = $split[0]; - /* on static upgraded configuration we automatically append _GW */ - $static_name = strtoupper($ifname) . "_GW"; + /* on static upgraded configuration we automatically prepend GW_ */ + $static_name = "GW_" . strtoupper($interface); if(is_array($gateways[$static_name])) { $interface = $static_name; } @@ -1561,7 +1563,9 @@ function upgrade_053_to_054() { $i = 1; } $gateway_group['item'][] = "$interface|$i"; - $config['interfaces'][$interface]['monitorip'] = "$monitor"; + if(is_ipaddr($monitor)) { + $config['interfaces'][$interface]['monitorip'] = "$monitor"; + } } $gateway_group_arr[] = $gateway_group; } else { diff --git a/usr/local/www/system_gateways_edit.php b/usr/local/www/system_gateways_edit.php index 170f071..20f303d 100755 --- a/usr/local/www/system_gateways_edit.php +++ b/usr/local/www/system_gateways_edit.php @@ -90,7 +90,7 @@ if ($_POST) { $input_errors[] = "The gateway name must not contain invalid characters."; } /* skip system gateways which have been automatically added */ - if ($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system")) { + if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system")) && (! $_POST['gateway'] == "dynamic")) { $input_errors[] = "A valid gateway IP address must be specified."; } if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system")) { @@ -107,20 +107,26 @@ if ($_POST) { if (isset($_POST['name'])) { /* check for overlaps */ foreach ($a_gateways as $gateway) { - if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) + if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) { continue; - - if (($gateway['name'] <> "") && (in_array($_POST['name'], $gateway)) && ($gateway['attribute'] != "system")) { - $input_errors[] = "The gateway name \"{$_POST['name']}\" already exists."; - break; } - if (($gateway['gateway'] <> "") && (in_array($_POST['gateway'], $gateway)) && ($gateway['attribute'] != "system")) { - $input_errors[] = "The gateway IP address \"{$_POST['gateway']}\" already exists."; - break; + if($_POST['name'] <> "") { + if (($gateway['name'] <> "") && (in_array($_POST['name'], $gateway)) && ($gateway['attribute'] != "system")) { + $input_errors[] = "The gateway name \"{$_POST['name']}\" already exists."; + break; + } + } + if(is_ipaddr($_POST['gateway'])) { + if (($gateway['gateway'] <> "") && (in_array($_POST['gateway'], $gateway)) && ($gateway['attribute'] != "system")) { + $input_errors[] = "The gateway IP address \"{$_POST['gateway']}\" already exists."; + break; + } } - if (($gateway['monitorip'] <> "") && (in_array($_POST['monitor'], $gateway)) && ($gateway['attribute'] != "system")) { - $input_errors[] = "The monitor IP address \"{$_POST['monitor']}\" is already in use. You must choose a different monitor IP."; - break; + if(is_ipaddr($_POST['monitor'])) { + if (($gateway['monitor'] <> "") && (in_array($_POST['monitor'], $gateway)) && ($gateway['attribute'] != "system")) { + $input_errors[] = "The monitor IP address \"{$_POST['monitor']}\" is already in use. You must choose a different monitor IP."; + break; + } } } } @@ -180,6 +186,7 @@ if ($_POST) { } } + $pgtitle = array("System","Gateways","Edit gateway"); include("head.inc"); -- cgit v1.1