From 464ae23f92b5056cc1dc1bf7bf701a58187447c3 Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Fri, 12 Dec 2014 13:01:43 +0100 Subject: Improve 'source-interface-id' option. On software router, an IPv4 and IPv6 addresses need to be specified. Not specifying one of them or both will result in the router trying to use the best address to contact the destination. This fix the bug where software routers could not ping or traceroute IPv6 destination with only a IPv4 source address (obviously). --- routers/quagga.php | 152 ++++++++++++++++++++++++++--------------------------- 1 file changed, 74 insertions(+), 78 deletions(-) (limited to 'routers/quagga.php') diff --git a/routers/quagga.php b/routers/quagga.php index d12c9e3..3dea33b 100644 --- a/routers/quagga.php +++ b/routers/quagga.php @@ -23,6 +23,72 @@ require_once('router.php'); require_once('includes/utils.php'); final class Quagga extends Router { + protected function build_ping($destination) { + $ping = null; + + if (match_fqdn($destination)) { + $fqdn = $destination; + $destination = fqdn_to_ip_address($fqdn); + + if (!$destination) { + throw new Exception('No A or AAAA record found for '.$fqdn); + } + } + + if (match_ipv4($destination)) { + $ping = 'ping -A -c 10 '.$destination; + } else if (match_ipv6($destination)) { + $ping = 'ping6 -A -c 10 '.$destination; + } else { + throw new Exception('The parameter is not an IPv4/IPv6 address or a FQDN.'); + } + + if (($ping != null) && $this->has_source_interface_id()) { + if (match_ipv4($destination) && + ($this->get_source_interface_id('ipv4') != null)) { + $ping .= ' -I '.$this->get_source_interface_id('ipv4'); + } else if (match_ipv6($destination) && + ($this->get_source_interface_id('ipv6') != null)) { + $ping .= ' -I '.$this->get_source_interface_id('ipv6'); + } + } + + return $ping; + } + + protected function build_traceroute($destination) { + $traceroute = null; + + if (match_fqdn($destination)) { + $fqdn = $destination; + $destination = fqdn_to_ip_address($fqdn); + + if (!$destination) { + throw new Exception('No A or AAAA record found for '.$fqdn); + } + } + + if (match_ipv4($destination)) { + $traceroute = 'traceroute -4 -A -q1 -N32 -w1 -m15 '.$destination; + } else if (match_ipv6($destination)) { + $traceroute = 'traceroute -6 -A -q1 -N32 -w1 -m15 '.$destination; + } else { + throw new Exception('The parameter is not an IPv4/IPv6 address or a FQDN.'); + } + + if (($traceroute != null) && $this->has_source_interface_id()) { + if (match_ipv4($destination) && + ($this->get_source_interface_id('ipv4') != null)) { + $traceroute .= ' -s '.$this->get_source_interface_id('ipv4'); + } else if (match_ipv6($destination) && + ($this->get_source_interface_id('ipv6') != null)) { + $traceroute .= ' -s '.$this->get_source_interface_id('ipv6'); + } + } + + return $traceroute; + } + protected function build_commands($command, $parameters) { $commands = array(); @@ -58,88 +124,18 @@ final class Quagga extends Router { break; case 'ping': - $append = null; - if (isset($this->config['source-interface-id'])) { - $append = ' -I '.$this->config['source-interface-id']; - } - - if (match_ipv4($parameters)) { - $ping = 'ping -A -c 10 '.$parameters; - if ($append != null) { - $ping .= $append; - } - $commands[] = $ping; - } else if (match_ipv6($parameters)) { - $ping = 'ping6 -A -c 10 '.$parameters; - if ($append != null) { - $ping .= $append; - } - $commands[] = $ping; - } else if (match_fqdn($parameters)) { - $ip_address = fqdn_to_ip_address($parameters); - - if (!$ip_address) { - throw new Exception('No A or AAAA record found for '.$parameters); - } - - if (match_ipv4($ip_address)) { - $ping = 'ping -A -c 10 '.$parameters; - if ($append != null) { - $ping .= $append; - } - $commands[] = $ping; - } else if (match_ipv6($ip_address)) { - $ping = 'ping6 -A -c 10 '.$parameters; - if ($append != null) { - $ping .= $append; - } - $commands[] = $ping; - } - } else { - throw new Exception('The parameter is not an IPv4/IPv6 address.'); + try { + $commands[] = $this->build_ping($parameters); + } catch (Exception $e) { + throw $e; } break; case 'traceroute': - $append = null; - if (isset($this->config['source-interface-id'])) { - $append = ' -s '.$this->config['source-interface-id']; - } - - if (match_ipv4($parameters)) { - $traceroute = 'traceroute -4 -A -q1 -N32 -w1 -m15 '.$parameters; - if ($append != null) { - $traceroute .= $append; - } - $commands[] .= $traceroute; - } else if (match_ipv6($parameters)) { - $traceroute = 'traceroute -6 -A -q1 -N32 -w1 -m15 '.$parameters; - if ($append != null) { - $traceroute .= $append; - } - $commands[] = $traceroute; - } else if (match_fqdn($parameters)) { - $ip_address = fqdn_to_ip_address($parameters); - - if (!$ip_address) { - throw new Exception('No A or AAAA record found for '.$parameters); - } - - if (match_ipv4($ip_address)) { - $traceroute = 'traceroute -4 -A -q1 -N32 -w1 -m15 '.$parameters; - if ($append != null) { - $traceroute .= $append; - } - $commands[] .= $traceroute; - } else if (match_ipv6($ip_address)) { - $traceroute = 'traceroute -6 -A -q1 -N32 -w1 -m15 '.$parameters; - if ($append != null) { - $traceroute .= $append; - } - $commands[] = $traceroute; - } - } else { - throw new Exception('The parameter is not an IPv4/IPv6 address.'); + try { + $commands[] = $this->build_traceroute($parameters); + } catch (Exception $e) { + throw $e; } break; -- cgit v1.2.3