#!/bin/sh # ===================================================================== # Projet : DARKWALL # Fichier : lib/ip6NetworkMaskTools # Auteur : Nicolas "DarkHack" Chapuis (c) 2026 # ================== DARKHACK ================== # [ darkweb.fr - system online ] # Powered by Perplexity, your AI assistant # https://www.perplexity.ai # ============================================== # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ===================================================================== if ! type "shInclude" > /dev/null 2>&1; then racineSh="${racineSh:-/etc/darksh}"; . "$racineSh/lib/shTools"; shDebugEnable="1";fi rootDarkWall="${rootDarkWall:-/etc/darkwall}" [ ! -d "$rootDarkWall" ] && shDebug 0 "exit cause rootDarWall is not dir:$rootDarkWall" && exit 0 requiresCmd="" requiresCmd="printf timeout sleep kill cat echo sh rm mkdir grep md5sum" requiresCmd="${requiresCmd} ip ip6tables wc sed awk fold" shWhichCmd $requiresCmd >/dev/null shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) requiresCmd="" requiresCmd="brctl ifup ifdown dhclient" shWhichCmd $requiresCmd >/dev/null shWarnOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) ip6GetLocal() { local log="ip6GetLocal:" for ip6 in $(ip6toolsGetIps);do shDebugHidden 2 ""$log"test:$ip6" if ip6IsSite $ip6 ;then echo $ip6 fi done } ip6IsSite() { local log="ip6IsSite:" local ip6="$1" shDebugHidden 2 ""$log" ip6IsSite?$ip6" if ip6IsValid $ip6;then # ip is global if ip is outside if ip6IsInNetwork $ip6 "fec0::/10" ;then # is an site ip6 return 0; else # is not site ip6 return 1; fi else # ip invalide return 1; fi # should not be here, all case is invalid return 1; } ip6GetGlobal() { local log="ip6GetGlobal:" # try get ip by http local ip6="$(wget --no-check-certificate -O - -q -t 1 --timeout=10 --dns-timeout=10 --connect-timeout=10 --read-timeout=10 myip6.darkweb.fr| tr -d '\t' | tr -d ' ' | tr -d '\n')" # echo "is global - 1 :$ip6" if ip6IsGlobal $ip6;then echo "$ip6";else # try get by ip cmd line local ip6="$(ip -6 addr show | grep global | grep dynamic | cut -d' ' -f6 | cut -d'/' -f1)" if ip6IsGlobal $ip6;then echo "$ip6";else local ip6="$(ip -6 addr show | grep global | cut -d'/' -f1 | rev | cut -d' ' -f1 | rev)" if ip6IsGlobal $ip6;then echo "$ip6";else shDebugHidden 1 ""$log" no global ip6 :$ip6" fi fi fi } ip6InDecimal() { local log="ip6InDecimal:" local nbHexDef="0" for testHex in $( $cmdEcho "${1}" | $cmdTr -s '\:' ' ' | $cmdTr '[:lower:]' '[:upper:]' );do if [ -n $testHex ];then local nbHexDef=$(( nbHexDef + 1 ));fi done local strCor="" for x in $( seq "$nbHexDef" 8 );do local strCor="$strCor:0";done [ -z "$1" ] && return ; local ip6="$( $cmdEcho $1 | $cmdSed "s/::/$strCor/g" )" local ip6Decimal="" local cpt=0 for partIp6 in $( $cmdEcho "${ip6}" | $cmdTr '\:' ' ' | $cmdTr '[:lower:]' '[:upper:]' );do local partDecIp6="$( $cmdEcho "ibase=16; $partIp6" | bc )" if [ -z "${ip6Decimal}" ];then local ip6Decimal="${partDecIp6}" else local ip6Decimal="${ip6Decimal}:${partDecIp6}" fi local cpt=$(( cpt + 1 )) done if [ "$cpt" -eq 7 ];then local ip6Decimal="${ip6Decimal}:0";fi echo ${ip6Decimal} } ip6InHex() { local log="ip6InHex:" local nbHexDef="0" for testHex in $(echo "$1" | tr '\:' ' ' | tr '[:lower:]' '[:upper:]');do if [ -n $testHex ];then local nbHexDef=`expr $nbHexDef + 1`;fi;done local strCor="" for x in $(seq "$nbHexDef" 7);do local strCor="$strCor:0";done [ -z "$1" ] && return ; local ip6="$(echo $1 | sed "s/::/$strCor:/g" )" local ip6Hex="" local cpt=0 for partIp6 in $(echo "$ip6" | tr '\:' ' ' | tr '[:lower:]' '[:upper:]');do if [ -z "$(echo $partIp6 | grep -E '^[0-9A-Fa-f]{1,4}$')" ];then continue;fi if [ -n "$ip6Hex" ];then local ip6Hex="$ip6Hex:";fi local ip6Hex="$ip6Hex"$(echo "obase=16;ibase=16; $partIp6" | bc 2>&1)"" local cpt=`expr $cpt + 1` done if [ "$cpt" -eq 7 ];then local ip6Hex="0:$ip6Hex";fi echo $ip6Hex } ip6InArpa() { local log="ip6InArpa:" local ip6Arpa="" [ -z "$1" ] && return ; for partIp6 in $(ip6InHex "$1" | tr '\:' ' ' | tr '[:upper:]' '[:lower:]');do local cpt=4 for char in $(echo "$partIp6" | sed -e 's/\(.\)/\1 /g');do local cpt=`expr $cpt - 1` done while [ $cpt -ne 0 ];do if [ -z "$${ip6Arpa}" ];then local ip6Arpa="0" else local ip6Arpa="0.${ip6Arpa}";fi local cpt=`expr $cpt - 1` done for char in $(echo "$partIp6" | sed -e 's/\(.\)/\1 /g');do if [ -z "$${ip6Arpa}" ];then local ip6Arpa="${char}" else local ip6Arpa="${char}.${ip6Arpa}";fi done done echo ${ip6Arpa}ip6.arpa } ip6IsValid() { local log="ip6IsValid:" local ip6="$(ip6InHex $1)" if [ -z "$ip6" ] || [ -z "$(echo $ip6 | grep -E '^[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}\:[0-9A-Fa-f]{1,4}$')" ];then # empty or invalid ip6 return 1; else # valid ip6 return 0; fi } ip6IsGlobal() { local log="ip6IsGlobal:" local ip6="$1" if ip6IsValid $ip6;then # RFC 4291 : Not specified local networks="::/128" # RFC 4291 : Addresse bouclage local networks="$networks ::1/128" # RFC 4291 : ip6 to ip4 local networks="$networks ::ffff:0:0/96" # RFC 6666 : solicitation du trou noir local networks="$networks 0100::/64" # RFC 4380 : Teredo local networks="$networks 2001::/32" # RFC 5180 : Test de perf local networks="$networks 2001:2::/48" # RFC 4843 : Orchid local networks="$networks 2001:10::/28" # RFC 3849 : Documentation local networks="$networks 2001:db8::/32" # RFC 3056 : 6to4 local networks="$networks 2002::/16" # RFC 4193 : Adr Local unique local networks="$networks fc00::/7" # RFC 4291 : Adr Local Link local networks="$networks fe80::/10" # RFC 4291 : Adr Multi Cast local networks="$networks ff00::/8" # for global, ip is not inside for network in $(echo $networks);do # ip is global if ip is outside if ip6IsInNetwork $ip6 $network ;then # inside return 1; else # outside continue; fi done local networks="" # RFC 3587 : Adr Unicast routable internet local networks="$networks 2000::/3" # for global, ip is inside for network in $(echo $networks);do # ip is global if ip is outside if ip6IsInNetwork $ip6 $network ;then # inside continue; else # outside return 1; fi done else # ip invalide return 1; fi # valid ip and not inside private or other not global network return 0; } #ip6CidrToNetwork "$ip6/$mask" ip6CidrToNetwork() { local ipCidr="${1}" local binIpNetwork="" for partIpNetwork in $( $cmdEcho ${ipCidr} | $cmdCut -d'/' -f 1 | $cmdTr '.' ' ');do local binIpNetwork="${binIpNetwork}$(printf '%0.8d\n' $(echo "obase=2;$partIpNetwork" | bc))" done local mask="$( $cmdEcho ${ipCidr} | $cmdCut -d'/' -f 2 )" local binMask="$( $cmdPrintf "%.*d\n" $mask 0 | $cmdTr '0' '1' )$( $cmdPrintf "%.*d\n" $(( 128 - $mask )) 0)" #$cmdEcho "$binIpNetwork => $binMask" local binNetwork="" local cpt=0 for testMask in $( $cmdEcho "$binMask" | $cmdFold -w1 );do local cpt=$(( $cpt + 1 )) if [ 1 -eq $testMask ] && [ 1 -eq $( $cmdExpr substr $binIpNetwork $cpt 1 ) ];then local binNetwork="${binNetwork}1" else local binNetwork="${binNetwork}0" fi done local network="" local cpt=0 local precPart="" local lockEmpty=1 for partBinNetwork in $( $cmdEcho $binNetwork | $cmdRev | $cmdFold -w16 );do local cpt=$(( $cpt + 1 )) local binValue=0 if [ 1 -eq $( $cmdExpr substr $partBinNetwork 16 1) ];then local binValue=$(( $binValue + 1 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 15 1) ];then local binValue=$(( $binValue + 2 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 14 1) ];then local binValue=$(( $binValue + 4 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 13 1) ];then local binValue=$(( $binValue + 8 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 12 1) ];then local binValue=$(( $binValue + 16 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 11 1) ];then local binValue=$(( $binValue + 32 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 10 1) ];then local binValue=$(( $binValue + 64 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 9 1) ];then local binValue=$(( $binValue + 128 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 8 1) ];then local binValue=$(( $binValue + 256 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 7 1) ];then local binValue=$(( $binValue + 512 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 6 1) ];then local binValue=$(( $binValue + 1024 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 5 1) ];then local binValue=$(( $binValue + 2048 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 4 1) ];then local binValue=$(( $binValue + 4096 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 3 1) ];then local binValue=$(( $binValue + 8184 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 2 1) ];then local binValue=$(( $binValue + 16368 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 1 1) ];then local binValue=$(( $binValue + 32736 ));fi local hexValue="$( printf '%.x' $binValue )" if [ -z "${hexValue}" ];then if [ 1 -eq $cpt ];then local network=":" else if [ 0 -eq ${lockEmpty} ];then local network="0:${network}" fi fi else if [ -z "${network}" ];then local network="${hexValue}"; else local lockEmpty=0 local network="${hexValue}:${network}" fi fi done $cmdEcho "${network}/${mask}" } # ip6IsInNetwork "$ip" "$net/$mask" # ip6IsInNetwork "$ip" "$net" "$mask" ip6IsInNetwork() { local ip6="$( ip6InDecimal ${1} )" # local ip6="$1" local network="$( ip6InDecimal $( $cmdEcho ${2} | $cmdCut -d'/' -f 1 ) )" # local network="$( $cmdEcho ${2} | $cmdCut -d'/' -f 1 )" if [ -z "$3" ];then local mask="$( $cmdEcho ${2} | $cmdGrep / | $cmdCut -d'/' -f 2 )" if [ -z "$mask" ];then local mask="128";fi local mask="$( printf "%.*d" ${mask} 0 | $cmdTr '0' '1' )$( printf "%.*d" $(( 128 - ${mask} )) 0)" else local mask="" for partMask in $( $cmdEcho "${3}" | $cmdTr ':' ' ');do local mask="${mask}$( printf '%0.16d' $( $cmdEcho "obase=2;$partMask" | bc ) )" done fi #$cmdEcho "$network $mask $ip6" >&2 local cpt=1 for partNetwork in $(echo "$network" | tr ':' ' ');do local partMask="$( $cmdEcho $mask | $cmdFold -w16 | $cmdTr '\n' ' ' | $cmdCut -d' ' -f $cpt )" local partIp6="$( $cmdEcho $ip6 | $cmdCut -f $cpt -d':' )" local cpt=$(( $cpt + 1 )) #echo "$partNetwork:$partMask:$partIp6" if ip6IsInNetworkPart "$partIp6" "$partNetwork" "$partMask" ;then continue;else return 1;fi done return 0; } ip6IsInNetworkPart() { local partIp6="${1}" local partNetwork="${2}" local partMask="${3}" local binPartIp6="$(printf '%0.16d' $(echo "obase=2;$partIp6" | bc))" local binPartNetwork="$(printf '%0.16d' $(echo "obase=2;$partNetwork" | bc))" local binPartMask="$partMask" local cpt=0 for testMask in $( echo "$binPartMask" | $cmdFold -w1);do local cpt=$(( $cpt + 1 )) if [ 0 -eq $testMask ];then continue;fi binNetwork="$(expr substr $binPartNetwork $cpt 1)" binIp6="$(expr substr $binPartIp6 $cpt 1)" if [ "$binNetwork" -ne "$binIp6" ];then return 1;fi done return 0 } #for ip in "192.168.0.1" "192.168.1.1" "192.167.0.1" "193.2.3.1";do # echo "ip:$ip network:192.168.0.0" # if ipIsInNetwork $ip 192.168.0.0 255.255.255.0 ;then echo "24:inside";else echo "24:outside";fi # if ipIsInNetwork $ip 192.168.0.0/16 ;then echo "16:inside";else echo "16:outside";fi # if ipIsInNetwork $ip 192.168.0.0/8 ;then echo "08:inside";else echo "08:outside";fi #done