From eabf23d38491aed648bfa22df18230f2f49be1cd Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Tue, 5 Aug 2014 01:52:02 +0200 Subject: Add a way to validate strict IP address (no mask). --- routers/bird.php | 4 ++-- routers/cisco.php | 4 ++-- routers/juniper.php | 4 ++-- routers/quagga.php | 6 +++--- utils.php | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/routers/bird.php b/routers/bird.php index 9b0923f..3556902 100644 --- a/routers/bird.php +++ b/routers/bird.php @@ -31,9 +31,9 @@ final class Bird extends Router { switch ($command) { case 'bgp': - if (match_ipv4($parameters)) { + if (match_ipv4($parameters), false) { $commands[] = $birdc.' \'show route for '.$parameters.'\''; - } else if (match_ipv6($parameters)) { + } else if (match_ipv6($parameters), false) { $commands[] = $birdc6.' \'show route for '.$parameters.'\''; } else { throw new Exception('The parameter is not an IPv4/IPv6 address.'); diff --git a/routers/cisco.php b/routers/cisco.php index bebec78..44fc7ee 100644 --- a/routers/cisco.php +++ b/routers/cisco.php @@ -28,9 +28,9 @@ final class Cisco extends Router { switch ($command) { case 'bgp': - if (match_ipv4($parameters)) { + if (match_ipv4($parameters), false) { $commands[] = 'show bgp ipv4 unicast '.$parameters; - } else if (match_ipv6($parameters)) { + } else if (match_ipv6($parameters), false) { $commands[] = 'show bgp ipv6 unicast '.$parameters; } else { throw new Exception('The parameter is not an IPv4/IPv6 address.'); diff --git a/routers/juniper.php b/routers/juniper.php index 32330e4..f0810eb 100644 --- a/routers/juniper.php +++ b/routers/juniper.php @@ -28,10 +28,10 @@ final class Juniper extends Router { switch ($command) { case 'bgp': - if (match_ipv4($parameters)) { + if (match_ipv4($parameters), false) { $commands[] = 'show route '.$parameters. ' protocol bgp table inet.0 active-path'; - } else if (match_ipv6($parameters)) { + } else if (match_ipv6($parameters), false) { $commands[] = 'show route '.$parameters. ' protocol bgp table inet6.0 active-path'; } else { diff --git a/routers/quagga.php b/routers/quagga.php index a9fe99b..d31e62b 100644 --- a/routers/quagga.php +++ b/routers/quagga.php @@ -30,9 +30,9 @@ final class Quagga extends Router { switch ($command) { case 'bgp': - if (match_ipv4($parameters)) { + if (match_ipv4($parameters), false) { $commands[] = $vtysh.'show bgp ipv4 unicast '.$parameters.'"'; - } else if (match_ipv6($parameters)) { + } else if (match_ipv6($parameters), false) { $commands[] = $vtysh.'show bgp ipv6 unicast '.$parameters.'"'; } else { throw new Exception('The parameter is not an IPv4/IPv6 address.'); @@ -85,4 +85,4 @@ final class Quagga extends Router { } } -// End of cisco.php +// End of quagga.php diff --git a/utils.php b/utils.php index 99a6a06..b6fc061 100644 --- a/utils.php +++ b/utils.php @@ -21,8 +21,8 @@ require_once 'config.php'; -function match_ipv4($ip) { - if (strrpos($ip, '/')) { +function match_ipv4($ip, $ip_only = true) { + if (strrpos($ip, '/') && !$ip_only) { $ip_and_mask = explode('/', $ip, 2); return filter_var($ip_and_mask[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && @@ -32,8 +32,8 @@ function match_ipv4($ip) { } } -function match_ipv6($ip) { - if (strrpos($ip, '/')) { +function match_ipv6($ip, $ip_only = true) { + if (strrpos($ip, '/') && !$ip_only) { $ip_and_mask = explode('/', $ip, 2); return filter_var($ip_and_mask[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && -- cgit v1.2.3