- Add ip library,

- Move ip low level functions to ip library,
- Add libs RPM package.
This commit is contained in:
2026-04-21 18:44:38 +02:00
parent ad3862a5e0
commit 4ed3d26dad
5 changed files with 186 additions and 104 deletions

View File

@@ -36,7 +36,7 @@ fi
#-----------------------------------------------------------------------------------------------------------------------------------
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/base.bash"
. "${RX3_LIB_DIR}/ip.bash"
@@ -47,13 +47,13 @@ fi
#-----------------------------------------------------------------------------------------------------------------------------------
declare -Ag DNS_A_TAB
declare -g DNS_A_ID_LIST
declare -g DNS_A_ID_LIST=""
declare -Ag DNS_PTR_TAB
declare -g DNS_PTR_ID_LIST
declare -g DNS_PTR_ID_LIST=""
declare -g DNS_CACHE_FILE
declare -g DNS_CACHE_LOCK
declare -g DNS_CACHE_FILE=""
declare -g DNS_CACHE_LOCK=""
#declare -g DNS_CACHE_LOCK="${DNS_CACHE_FILE}"
declare -g DNS_CACHE_UPDATED=0
@@ -224,19 +224,19 @@ dns_lookup()
if [[ "${dl_flag}" != "NOCACHE" ]]
then
dns_tab_get ${dl_type} ${dl_key}
{ dns_tab_get ${dl_type} ${dl_key}; rc=$?; } || true
else
false
rc=1
fi
if [[ "$?" != "0" ]]
if [[ "${rc}" != "0" ]]
then
log_trace "DNS" "Out of Cache: Type: [${dl_type}] Key: [${dl_key}] Flag: [${dl_flag}]"
#log_trace "DNS" "Out of Cache: Type: [${dl_type}] Key: [${dl_key}] Flag: [${dl_flag}]"
case ${dl_type}
in
"A")
dns_value="$( dig +short ${dl_key} 2>/dev/null)"
dns_value="$( dig +short ${dl_key} 2>/dev/null | ip_ip_filter)"
;;
"PTR")
@@ -333,8 +333,8 @@ dns_host_update()
dns_init()
{
file_dir_init ${DNS_CACHE_FILE} root apache
file_dir_init ${DNS_CACHE_LOCK} root apache
file_dir_init "${DNS_CACHE_FILE}" root apache
file_dir_init "${DNS_CACHE_LOCK}" root apache
}

145
lib/rx3/ip.bash Normal file
View File

@@ -0,0 +1,145 @@
#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Rx3 IP Library
#
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; If not, see
# <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------------------------------------------------------------
if [[ "${RX3_IP_LIB}" != "" ]]
then
return
else
declare -g RX3_IP_LIB=1
fi
#-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/base.bash"
#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variable
#-----------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------
# Is Valid Ip
#--------------------------------------------------------------------------------------------------------------------------
ip_is_valid_ip()
{
local ip=$1
local regex='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
if [[ $ip =~ $regex ]]
then
IFS='.' read -r o1 o2 o3 o4 <<< "$ip"
for octet in $o1 $o2 $o3 $o4
do
if (( octet < 0 || octet > 255 ))
then
return 1
fi
done
return 0
else
return 1
fi
}
#--------------------------------------------------------------------------------------------------------------------------
# Ip To Num
#--------------------------------------------------------------------------------------------------------------------------
ip_ip_to_num()
{
local ip="$1"
local a
local b
local c
local d
IFS=. read -r a b c d <<< "${ip}"
echo $(( (a << 24) + (b << 16) + (c << 8) + d ))
}
#--------------------------------------------------------------------------------------------------------------------------
# Num To Ip
#--------------------------------------------------------------------------------------------------------------------------
ip_num_to_ip()
{
local num="$1"
# Optional safety check
if (( num < 0 || num > 4294967295 ))
then
echo_error "num_to_ip: value out of range (04294967295)"
return 1
fi
# Extract each byte by shifting and masking
local a=$(( (num >> 24) & 255 ))
local b=$(( (num >> 16) & 255 ))
local c=$(( (num >> 8) & 255 ))
local d=$(( num & 255 ))
printf '%d.%d.%d.%d\n' "$a" "$b" "$c" "$d"
}
#-----------------------------------------------------------------------------------------------------------------------------------
# IP Filter
#-----------------------------------------------------------------------------------------------------------------------------------
ip_ip_filter()
{
grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
}

View File

@@ -79,88 +79,6 @@ declare -g NETWORK_NC_TIMEOUT
#--------------------------------------------------------------------------------------------------------------------------
# is_valid_ip
#--------------------------------------------------------------------------------------------------------------------------
is_valid_ip()
{
local ip=$1
local regex='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
if [[ $ip =~ $regex ]]
then
IFS='.' read -r o1 o2 o3 o4 <<< "$ip"
for octet in $o1 $o2 $o3 $o4
do
if (( octet < 0 || octet > 255 ))
then
return 1
fi
done
return 0
else
return 1
fi
}
#--------------------------------------------------------------------------------------------------------------------------
# ip_to_num
#--------------------------------------------------------------------------------------------------------------------------
ip_to_num()
{
local ip="$1"
local a
local b
local c
local d
IFS=. read -r a b c d <<< "${ip}"
echo $(( (a << 24) + (b << 16) + (c << 8) + d ))
}
#--------------------------------------------------------------------------------------------------------------------------
# num_to_ip
#--------------------------------------------------------------------------------------------------------------------------
num_to_ip()
{
local num="$1"
# Optional safety check
if (( num < 0 || num > 4294967295 ))
then
echo_error "num_to_ip: value out of range (04294967295)"
return 1
fi
# Extract each byte by shifting and masking
local a=$(( (num >> 24) & 255 ))
local b=$(( (num >> 16) & 255 ))
local c=$(( (num >> 8) & 255 ))
local d=$(( num & 255 ))
printf '%d.%d.%d.%d\n' "$a" "$b" "$c" "$d"
}
#--------------------------------------------------------------------------------------------------------------------------
# network_common_load
#--------------------------------------------------------------------------------------------------------------------------
@@ -658,7 +576,7 @@ network_src_tab_load()
log_trace "Network" "Loading Src tab..."
ip_base=$( ip_to_num ${NETWORK_SRC_LOCAL_ADDRESS})
ip_base=$( ip_ip_to_num ${NETWORK_SRC_LOCAL_ADDRESS})
while IFS= read -r line
do
@@ -691,7 +609,7 @@ network_src_tab_load()
# [[ $nstl_host_name =~ ([0-9]+) ]]
# nstl_device="${NETWORK_SRC_LOCAL_DEVICE}-${BASH_REMATCH[1]}"
ip_num=$(ip_to_num ${nstl_ip})
ip_num=$(ip_ip_to_num ${nstl_ip})
device_num="$(( ( ${ip_num} - ${ip_base}) / 4))"
nstl_device="${NETWORK_SRC_LOCAL_DEVICE}-${device_num}"
@@ -1214,7 +1132,7 @@ network_dst_address_refresh()
dst_ip_new="$( nc -w "${NETWORK_NC_TIMEOUT}" "${proxy_host}" "${proxy_port}" 2>/dev/null)"
fi
if is_valid_ip "${dst_ip_new}"
if ip_is_valid_ip "${dst_ip_new}"
then
if [[ "${dst_ip}" != "${dst_ip_new}" ]]
then