From 9fa11cb79a2d0e23ff73b1317fdff123636fca10 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 10:42:23 +0200 Subject: [PATCH 1/6] mod/ipcalc: use $NetMask4 --- mod/ipcalc.rsc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mod/ipcalc.rsc b/mod/ipcalc.rsc index eacff6d9..fecf6f26 100644 --- a/mod/ipcalc.rsc +++ b/mod/ipcalc.rsc @@ -34,9 +34,12 @@ # calculate and return netmask, network, min host, max host and broadcast :set IPCalcReturn do={ :local Input [ :tostr $1 ]; + + :global NetMask4; + :local Address [ :toip [ :pick $Input 0 [ :find $Input "/" ] ] ]; :local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $Input ] ] ]; - :local Mask ((255.255.255.255 << (32 - $Bits)) & 255.255.255.255); + :local Mask [ $NetMask4 $Bits ]; :local Return { "address"=$Address; From 47309e5c03e32e05ed9435ed05f9549dfddd338f Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 13:02:28 +0200 Subject: [PATCH 2/6] fw-addr-lists: normalize IPv4 addresses --- fw-addr-lists.rsc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fw-addr-lists.rsc b/fw-addr-lists.rsc index cd136f95..26e041aa 100644 --- a/fw-addr-lists.rsc +++ b/fw-addr-lists.rsc @@ -22,10 +22,12 @@ :global EitherOr; :global FetchHuge; :global HumanReadableNum; + :global IfThenElse; :global LogPrint; :global LogPrintOnce; :global LogPrintVerbose; :global MIN; + :global NetMask4; :global ScriptLock; :global WaitFullyConnected; @@ -114,8 +116,13 @@ :do { :local Branch; :if ($Address ~ "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}(/[0-9]{1,2})?\$") do={ - :if ($Address ~ "/32\$") do={ - :set Address [ :pick $Address 0 ([ :len $Address ] - 3) ]; + :local Net $Address; + :local CIDR 32; + :local Slash [ :find $Address "/" ]; + :if ([ :typeof $Slash ] = "num") do={ + :set Net [ :toip [ :pick $Address 0 $Slash ] ] + :set CIDR [ :pick $Address ($Slash + 1) [ :len $Address ] ]; + :set Address [ :tostr (([ :toip $Net ] & [ $NetMask4 $CIDR ]) . [ $IfThenElse ($CIDR < 32) ("/" . $CIDR) ]) ]; } :set Branch [ $GetBranch $Address ]; :set ($IPv4Addresses->$Branch->$Address) $TimeOut; From 8f328b48047de084987e694264b249f55f89faf0 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 10:30:20 +0200 Subject: [PATCH 3/6] global-functions: introduce $NetMask6 --- global-functions.rsc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/global-functions.rsc b/global-functions.rsc index 5c98a206..1ad12653 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -62,6 +62,7 @@ :global MIN; :global MkDir; :global NetMask4; +:global NetMask6; :global NotificationFunctions; :global ParseDate; :global ParseKeyValueStore; @@ -998,6 +999,22 @@ :return ((255.255.255.255 << (32 - $CIDR)) & 255.255.255.255); } +# return an IPv6 netmask for CIDR +:set NetMask6 do={ + :local FuncName $0; + :local CIDR [ :tostr $1 ]; + + :global IfThenElse; + :global MAX; + :global MIN; + + :local Mask ""; + :for I from=0 to=7 do={ + :set Mask ($Mask . [ :convert from=num to=hex (0xffff - (0xffff >> [ :tonum [ $MIN [ $MAX ($CIDR - ((16 * $I))) 0 ] 16 ] ])) ] . [ $IfThenElse ($I < 7) ":" ]); + } + :return [ :toip6 $Mask ]; +} + # prepare NotificationFunctions array :if ([ :typeof $NotificationFunctions ] != "array") do={ :set NotificationFunctions ({}); From 7bfdd5a994fd5aff07f3cd7f07b5cfe3af0f7ed8 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 15:13:14 +0200 Subject: [PATCH 4/6] global-functions: $NetMask6: implement simple caching --- global-functions.rsc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 1ad12653..b7cff0fe 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -1008,11 +1008,23 @@ :global MAX; :global MIN; + :global NetMask6Cache; + + :if ([ :typeof ($NetMask6Cache->$CIDR) ] = "ip6") do={ + :return ($NetMask6Cache->$CIDR); + } + + :if ([ :typeof $NetMask6Cache ] = "nothing") do={ + :set NetMask6Cache ({}); + } + :local Mask ""; :for I from=0 to=7 do={ :set Mask ($Mask . [ :convert from=num to=hex (0xffff - (0xffff >> [ :tonum [ $MIN [ $MAX ($CIDR - ((16 * $I))) 0 ] 16 ] ])) ] . [ $IfThenElse ($I < 7) ":" ]); } - :return [ :toip6 $Mask ]; + :set Mask [ :toip6 $Mask ]; + :set ($NetMask6Cache->$CIDR) $Mask; + :return $Mask; } # prepare NotificationFunctions array From bdb449cbdae14b351a9fa2876a9a96b6e3501b04 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 13:02:14 +0200 Subject: [PATCH 5/6] fw-addr-lists: use $NetMask6 --- fw-addr-lists.rsc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fw-addr-lists.rsc b/fw-addr-lists.rsc index 26e041aa..c85cc8bf 100644 --- a/fw-addr-lists.rsc +++ b/fw-addr-lists.rsc @@ -26,8 +26,8 @@ :global LogPrint; :global LogPrintOnce; :global LogPrintVerbose; - :global MIN; :global NetMask4; + :global NetMask6; :global ScriptLock; :global WaitFullyConnected; @@ -130,13 +130,13 @@ } :if ($Address ~ "^[0-9a-zA-Z]*:[0-9a-zA-Z:\\.]+(/[0-9]{1,3})?\$") do={ :local Net $Address; - :local Cidr 64; + :local CIDR 128; :local Slash [ :find $Address "/" ]; :if ([ :typeof $Slash ] = "num") do={ :set Net [ :toip6 [ :pick $Address 0 $Slash ] ] - :set Cidr [ $MIN [ :pick $Address ($Slash + 1) [ :len $Address ] ] 64 ]; + :set CIDR [ :pick $Address ($Slash + 1) [ :len $Address ] ]; } - :set Address (([ :toip6 $Net ] & ffff:ffff:ffff:ffff::) . "/" . $Cidr); + :set Address (([ :toip6 $Net ] & [ $NetMask6 $CIDR ]) . "/" . $CIDR); :set Branch [ $GetBranch $Address ]; :set ($IPv6Addresses->$Branch->$Address) $TimeOut; :error true; From fb84ebd6e36b8ede37fee36a1ea2791f35a0471e Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 17:31:15 +0200 Subject: [PATCH 6/6] mod/ipcalc: support IPv6 Well, some of these values do not make a lot of sense for IPv6... Something to be cleaned up later. --- mod/ipcalc.rsc | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/mod/ipcalc.rsc b/mod/ipcalc.rsc index fecf6f26..d65d4724 100644 --- a/mod/ipcalc.rsc +++ b/mod/ipcalc.rsc @@ -36,21 +36,32 @@ :local Input [ :tostr $1 ]; :global NetMask4; + :global NetMask6; - :local Address [ :toip [ :pick $Input 0 [ :find $Input "/" ] ] ]; + :local Address [ :pick $Input 0 [ :find $Input "/" ] ]; :local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $Input ] ] ]; - :local Mask [ $NetMask4 $Bits ]; + :local Mask; + :local One; + :if ([ :typeof [ :toip $Address ] ] = "ip") do={ + :set Address [ :toip $Address ]; + :set Mask [ $NetMask4 $Bits ]; + :set One 0.0.0.1; + } else={ + :set Address [ :toip6 $Address ]; + :set Mask [ $NetMask6 $Bits ]; + :set One ::1; + } - :local Return { + :local Return ({ "address"=$Address; "netmask"=$Mask; "networkaddress"=($Address & $Mask); "networkbits"=$Bits; "network"=(($Address & $Mask) . "/" . $Bits); - "hostmin"=(($Address & $Mask) | 0.0.0.1); - "hostmax"=(($Address | ~$Mask) ^ 0.0.0.1); + "hostmin"=(($Address & $Mask) | $One); + "hostmax"=(($Address | ~$Mask) ^ $One); "broadcast"=($Address | ~$Mask); - } + }); :return $Return; }