Compare commits

...

3 commits

Author SHA1 Message Date
Christian Hesse
f58980d701 mod/ipcalc: use $NetMask4 2025-10-16 10:48:59 +02:00
Christian Hesse
d587281513 fw-addr-lists: normalize the addresses 2025-10-16 10:48:53 +02:00
Christian Hesse
434bf82ddf global-functions: introduce $NetMask6 2025-10-16 10:47:19 +02:00
3 changed files with 46 additions and 7 deletions

View file

@ -22,10 +22,12 @@
:global EitherOr;
:global FetchHuge;
:global HumanReadableNum;
:global IfThenElse;
:global LogPrint;
:global LogPrintOnce;
:global LogPrintVerbose;
:global MIN;
:global NetMask4;
:global NetMask6;
: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;
@ -123,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;

View file

@ -62,6 +62,7 @@
:global MIN;
:global MkDir;
:global NetMask4;
:global NetMask6;
:global NotificationFunctions;
:global ParseDate;
:global ParseKeyValueStore;
@ -998,6 +999,34 @@
: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 NetMask6Cache;
:global GetRandom20CharAlNum;
:if ([ :typeof $NetMask6Cache ] = "array") do={
:return ($NetMask6Cache->$CIDR);
}
:set NetMask6Cache { "128"=(~::) };
:local GenList ($FuncName . "-" . [ $GetRandom20CharAlNum ]);
:for I from=0 to=127 do={
/ipv6/firewall/address-list/add dynamic=yes timeout=1m list=$GenList address=((~::) . "/" . $I) comment=$I;
}
:foreach FwAddrList in=[ /ipv6/firewall/address-list/find where list=$GenList ] do={
:local Address [ /ipv6/firewall/address-list/get $FwAddrList ];
:set ($NetMask6Cache->($Address->"comment")) \
[ :toip6 [ :pick ($Address->"address") 0 [ :find ($Address->"address") "/" ] ] ];
}
:return ($NetMask6Cache->$CIDR);
}
# prepare NotificationFunctions array
:if ([ :typeof $NotificationFunctions ] != "array") do={
:set NotificationFunctions ({});

View file

@ -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;