global-functions: introduce $NetMask6

RouterOS does not support bit shifting on IPv6 data types, so we have to
split the problem:

 * each 16 bit block is calculated separately, as number
 * the complete netmask is assembled in a loop, as string
 * the final string is casted to correct data type
This commit is contained in:
Christian Hesse 2025-10-16 10:30:20 +02:00
parent c62f236251
commit 6ad6f9aa08

View file

@ -62,6 +62,7 @@
:global MIN;
:global MkDir;
:global NetMask4;
:global NetMask6;
:global NotificationFunctions;
:global ParseDate;
:global ParseKeyValueStore;
@ -998,6 +999,24 @@
: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 ({});