From patchwork Tue Dec 24 10:15:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim FitzGeorge X-Patchwork-Id: 2658 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 47hsXv4LVcz3xXq for ; Tue, 24 Dec 2019 10:15:11 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail02.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 47hsXt1FKSz2ZN; Tue, 24 Dec 2019 10:15:10 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 47hsXs6LxLz2yHt; Tue, 24 Dec 2019 10:15:09 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 47hsXs0fxzz2xm4 for ; Tue, 24 Dec 2019 10:15:09 +0000 (UTC) Received: from smtp.hosts.co.uk (smtp.hosts.co.uk [85.233.160.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPS id 47hsXr2GFbz2ZN for ; Tue, 24 Dec 2019 10:15:08 +0000 (UTC) Received: from [95.149.142.227] (helo=aragorn.hosts.co.uk.tfitzgeorge.me.uk) by smtp.hosts.co.uk with esmtpa (Exim) (envelope-from ) id 1ijhDX-0005Kz-AF; Tue, 24 Dec 2019 10:15:07 +0000 From: Tim FitzGeorge To: development@lists.ipfire.org Subject: [PATCH v2] Fix network-functions net membership check Date: Tue, 24 Dec 2019 10:15:03 +0000 Message-Id: <20191224101503.7483-1-ipfr@tfitzgeorge.me.uk> X-Mailer: git-send-email 2.16.4 Authentication-Results: mail01.ipfire.org; dkim=none; dmarc=none; spf=pass (mail01.ipfire.org: domain of ipfr@tfitzgeorge.me.uk designates 85.233.160.19 as permitted sender) smtp.mailfrom=ipfr@tfitzgeorge.me.uk X-Rspamd-Queue-Id: 47hsXr2GFbz2ZN X-Spamd-Result: default: False [-3.23 / 11.00]; RCVD_TLS_LAST(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RWL_MAILSPIKE_GOOD(0.00)[85.233.160.19:from]; R_SPF_ALLOW(-0.20)[+ip4:85.233.160.0/27]; MIME_GOOD(-0.10)[text/plain]; SENDER_REP_HAM(0.00)[asn: 8622(-0.30), country: GB(-0.00), ip: 85.233.160.19(-0.76)]; DMARC_NA(0.00)[tfitzgeorge.me.uk]; RECEIVED_SPAMHAUS_PBL(0.00)[95.149.142.227:received]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MX_GOOD(-0.01)[cached: mx1.ukservers.net]; RCPT_COUNT_TWO(0.00)[2]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM(-0.82)[-0.822]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:8622, ipnet:85.233.160.0/19, country:GB]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-2.99)[99.97%]; RCVD_IN_DNSWL_LOW(-0.10)[85.233.160.19:from] X-Rspamd-Server: mail01.haj.ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Replace textual comparison of two number with arithmetic in ip_address_in_Network(). Changes since V1: - Correctly copied change from production system - Fixed test of non-existent variables in network2bin - Enhanced testsuite to report which test failed Fixes: 12263 Signed-off-by: Tim FitzGeorge --- config/cfgroot/network-functions.pl | 38 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl index 8649d0502..b911e214f 100644 --- a/config/cfgroot/network-functions.pl +++ b/config/cfgroot/network-functions.pl @@ -111,11 +111,11 @@ sub network_equal { my @bin1 = &network2bin($network1); my @bin2 = &network2bin($network2); - if (!defined $bin1 || !defined $bin2) { + if (!defined $bin1[0] || !defined $bin2[0]) { return undef; } - if ($bin1[0] eq $bin2[0] && $bin1[1] eq $bin2[1]) { + if ($bin1[0] == $bin2[0] && $bin1[1] == $bin2[1]) { return 1; } @@ -295,7 +295,7 @@ sub ip_address_in_network($$) { # Find end address my $broadcast_bin = $network_bin ^ (~$netmask_bin % 2 ** 32); - return (($address_bin ge $network_bin) && ($address_bin le $broadcast_bin)); + return (($address_bin >= $network_bin) && ($address_bin <= $broadcast_bin)); } sub setup_upstream_proxy() { @@ -449,14 +449,15 @@ sub get_mac_by_name($) { # Remove the next line to enable the testsuite __END__ -sub assert($) { +sub assert($$) { + my $tst = shift; my $ret = shift; if ($ret) { return; } - print "ASSERTION ERROR"; + print "ASSERTION ERROR - $tst\n"; exit(1); } @@ -464,10 +465,10 @@ sub testsuite() { my $result; my $address1 = &ip2bin("8.8.8.8"); - assert($address1 == 134744072); + assert('ip2bin("8.8.8.8")', $address1 == 134744072); my $address2 = &bin2ip($address1); - assert($address2 eq "8.8.8.8"); + assert("bin2ip($address1)", $address2 eq "8.8.8.8"); # Check if valid IP addresses are correctly recognised. foreach my $address ("1.2.3.4", "192.168.180.1", "127.0.0.1") { @@ -486,34 +487,37 @@ sub testsuite() { } $result = &check_ip_address_and_netmask("192.168.180.0/255.255.255.0"); - assert($result); + assert('check_ip_address_and_netmask("192.168.180.0/255.255.255.0")', $result); $result = &convert_netmask2prefix("255.255.254.0"); - assert($result == 23); + assert('convert_netmask2prefix("255.255.254.0")', $result == 23); $result = &convert_prefix2netmask(8); - assert($result eq "255.0.0.0"); + assert('convert_prefix2netmask(8)', $result eq "255.0.0.0"); $result = &find_next_ip_address("1.2.3.4", 2); - assert($result eq "1.2.3.6"); + assert('find_next_ip_address("1.2.3.4", 2)', $result eq "1.2.3.6"); $result = &network_equal("192.168.0.0/24", "192.168.0.0/255.255.255.0"); - assert($result); + assert('network_equal("192.168.0.0/24", "192.168.0.0/255.255.255.0")', $result); $result = &network_equal("192.168.0.0/24", "192.168.0.0/25"); - assert(!$result); + assert('network_equal("192.168.0.0/24", "192.168.0.0/25")', !$result); $result = &network_equal("192.168.0.0/24", "192.168.0.128/25"); - assert(!$result); + assert('network_equal("192.168.0.0/24", "192.168.0.128/25")', !$result); $result = &network_equal("192.168.0.1/24", "192.168.0.XXX/24"); - assert(!$result); + assert('network_equal("192.168.0.1/24", "192.168.0.XXX/24")', !$result); $result = &ip_address_in_network("10.0.1.4", "10.0.0.0/8"); - assert($result); + assert('ip_address_in_network("10.0.1.4", "10.0.0.0/8"', $result); $result = &ip_address_in_network("192.168.30.11", "192.168.30.0/255.255.255.0"); - assert($result); + assert('ip_address_in_network("192.168.30.11", "192.168.30.0/255.255.255.0")', $result); + + $result = &ip_address_in_network("192.168.30.11", "0.0.0.0/8"); + assert('ip_address_in_network("192.168.30.11", "0.0.0.0/8")', !$result); print "Testsuite completed successfully!\n";