Merge branch 'netmask4' into next

This commit is contained in:
Christian Hesse 2025-10-19 19:26:51 +02:00
commit c62f236251
3 changed files with 21 additions and 3 deletions

View file

@ -22,10 +22,12 @@
:global EitherOr; :global EitherOr;
:global FetchHuge; :global FetchHuge;
:global HumanReadableNum; :global HumanReadableNum;
:global IfThenElse;
:global LogPrint; :global LogPrint;
:global LogPrintOnce; :global LogPrintOnce;
:global LogPrintVerbose; :global LogPrintVerbose;
:global MIN; :global MIN;
:global NetMask4;
:global ScriptLock; :global ScriptLock;
:global WaitFullyConnected; :global WaitFullyConnected;
@ -114,8 +116,13 @@
:do { :do {
:local Branch; :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 ~ "^[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={ :local Net $Address;
:set Address [ :pick $Address 0 ([ :len $Address ] - 3) ]; :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 Branch [ $GetBranch $Address ];
:set ($IPv4Addresses->$Branch->$Address) $TimeOut; :set ($IPv4Addresses->$Branch->$Address) $TimeOut;

View file

@ -61,6 +61,7 @@
:global MAX; :global MAX;
:global MIN; :global MIN;
:global MkDir; :global MkDir;
:global NetMask4;
:global NotificationFunctions; :global NotificationFunctions;
:global ParseDate; :global ParseDate;
:global ParseKeyValueStore; :global ParseKeyValueStore;
@ -990,6 +991,13 @@
:return true; :return true;
} }
# return an IPv4 netmask for CIDR
:set NetMask4 do={
:local CIDR [ :tonum $1 ];
:return ((255.255.255.255 << (32 - $CIDR)) & 255.255.255.255);
}
# prepare NotificationFunctions array # prepare NotificationFunctions array
:if ([ :typeof $NotificationFunctions ] != "array") do={ :if ([ :typeof $NotificationFunctions ] != "array") do={
:set NotificationFunctions ({}); :set NotificationFunctions ({});

View file

@ -34,9 +34,12 @@
# calculate and return netmask, network, min host, max host and broadcast # calculate and return netmask, network, min host, max host and broadcast
:set IPCalcReturn do={ :set IPCalcReturn do={
:local Input [ :tostr $1 ]; :local Input [ :tostr $1 ];
:global NetMask4;
:local Address [ :toip [ :pick $Input 0 [ :find $Input "/" ] ] ]; :local Address [ :toip [ :pick $Input 0 [ :find $Input "/" ] ] ];
:local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $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 { :local Return {
"address"=$Address; "address"=$Address;