Compare commits

..

6 commits

Author SHA1 Message Date
Christian Hesse
fb84ebd6e3 mod/ipcalc: support IPv6
Well, some of these values do not make a lot of sense for IPv6...
Something to be cleaned up later.
2025-10-16 17:31:15 +02:00
Christian Hesse
bdb449cbda fw-addr-lists: use $NetMask6 2025-10-16 15:43:43 +02:00
Christian Hesse
7bfdd5a994 global-functions: $NetMask6: implement simple caching 2025-10-16 15:43:43 +02:00
Christian Hesse
8f328b4804 global-functions: introduce $NetMask6 2025-10-16 15:43:43 +02:00
Christian Hesse
47309e5c03 fw-addr-lists: normalize IPv4 addresses 2025-10-16 15:43:43 +02:00
Christian Hesse
9fa11cb79a mod/ipcalc: use $NetMask4 2025-10-16 13:03:54 +02:00
3 changed files with 33 additions and 22 deletions

View file

@ -117,7 +117,7 @@
: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={
:local Net $Address;
:local CIDR "32";
:local CIDR 32;
:local Slash [ :find $Address "/" ];
:if ([ :typeof $Slash ] = "num") do={
:set Net [ :toip [ :pick $Address 0 $Slash ] ]
@ -130,7 +130,7 @@
}
:if ($Address ~ "^[0-9a-zA-Z]*:[0-9a-zA-Z:\\.]+(/[0-9]{1,3})?\$") do={
:local Net $Address;
:local CIDR "128";
:local CIDR 128;
:local Slash [ :find $Address "/" ];
:if ([ :typeof $Slash ] = "num") do={
:set Net [ :toip6 [ :pick $Address 0 $Slash ] ]

View file

@ -1004,27 +1004,27 @@
:local FuncName $0;
:local CIDR [ :tostr $1 ];
:global IfThenElse;
:global MAX;
:global MIN;
:global NetMask6Cache;
:global GetRandom20CharAlNum;
:if ([ :typeof $NetMask6Cache ] = "array") do={
:if ([ :typeof ($NetMask6Cache->$CIDR) ] = "ip6") 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") "/" ] ] ];
:if ([ :typeof $NetMask6Cache ] = "nothing") do={
:set NetMask6Cache ({});
}
:return ($NetMask6Cache->$CIDR);
: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) ":" ]);
}
:set Mask [ :toip6 $Mask ];
:set ($NetMask6Cache->$CIDR) $Mask;
:return $Mask;
}
# prepare NotificationFunctions array

View file

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