public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
From: Antonio Quartulli <antonio@mandelbit.com>
To: development@lists.ipfire.org
Cc: Antonio Quartulli <antonio@mandelbit.com>
Subject: [PATCH 1/4] network-functions.pl: Add ip_address_is_global()
Date: Tue,  1 Dec 2026 15:31:50 +0100	[thread overview]
Message-ID: <20261201143155.1798388-2-antonio@mandelbit.com> (raw)
In-Reply-To: <20261201143155.1798388-1-antonio@mandelbit.com>

Returns whether an IPv4 address can be reached from the Internet, based
on the IANA special-purpose address registry.

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 config/cfgroot/network-functions.pl | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl
index a90655305..9f37c490f 100644
--- a/config/cfgroot/network-functions.pl
+++ b/config/cfgroot/network-functions.pl
@@ -425,6 +425,23 @@ sub ip_address_in_range($$) {
 	return (($ipaddress_bin >= $ipstart_bin) && ($ipaddress_bin <= $ipend_bin));
 }
 
+# Returns true if the IPv4 address can be reached from the Internet, i.e. it
+# isn't in any of the special-purpose ranges that aren't globally reachable
+# (based on the IANA registry).
+sub ip_address_is_global($) {
+	my $address = shift;
+
+	return 0 unless (&check_ip_address($address));
+
+	foreach my $network ("0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8",
+			"169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16",
+			"198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4") {
+		return 0 if (&ip_address_in_network($address, $network));
+	}
+
+	return 1;
+}
+
 sub check_port
 {
 	$_ = $_[0];
@@ -1022,6 +1039,18 @@ sub testsuite() {
 	$result = &ip_address_in_range("192.168.30.21", "192.168.30.10", "192.168.30.20");
 	assert('ip_address_in_range("192.168.30.21", "192.168.30.10", "192.168.30.20")', !$result);
 
+	# Check &ip_address_is_global()
+	foreach my $address ("81.3.27.37", "8.8.8.8", "1.0.0.0", "100.63.255.255", "100.128.0.0",
+			"169.253.255.255", "172.15.255.255", "172.32.0.0", "224.0.0.1", "239.255.255.255") {
+		assert("ip_address_is_global(\"$address\")", &ip_address_is_global($address));
+	}
+
+	foreach my $address ("10.1.2.3", "172.16.0.0", "172.31.255.255", "192.168.1.1", "100.64.0.0",
+			"100.127.255.255", "127.0.0.1", "169.254.0.0", "169.254.255.255", "0.255.255.255",
+			"192.0.2.1", "198.18.0.1", "240.0.0.0", "255.255.255.255", "a.b.c.d", "") {
+		assert("!ip_address_is_global(\"$address\")", !&ip_address_is_global($address));
+	}
+
 	# Check &get_prefix()
 	$result = &get_prefix("192.168.0.0/24");
 	assert('get_prefix("192.168.0.0/24")', $result == 24);
-- 
2.56.1



  reply	other threads:[~2026-12-01 14:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:31 [PATCH 0/4] Use the address ddns publishes in the DDNS sync check (#14766) Antonio Quartulli
2026-12-01 14:31 ` Antonio Quartulli [this message]
2026-12-01 14:31 ` [PATCH 2/4] wio.cgi: Get the address of the DDNS hosts only once Antonio Quartulli
2026-12-01 14:31 ` [PATCH 3/4] ddns.cgi: Get the address of the DDNS hosts only when needed Antonio Quartulli
2026-12-01 14:31 ` [PATCH 4/4] http-client-functions.pl: Use the address ddns publishes for the sync check Antonio Quartulli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201143155.1798388-2-antonio@mandelbit.com \
    --to=antonio@mandelbit.com \
    --cc=development@lists.ipfire.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox