]> git.ipfire.org Git - people/stevee/network.git/blobdiff - functions.ip
Bump version to 006.
[people/stevee/network.git] / functions.ip
index 1258ac18ec2139987a3846d0a405ed96de6a282a..7e47d07630346ff1ca581e2aef61a4017f2e8808 100644 (file)
@@ -24,7 +24,6 @@ IP_SUPPORTED_PROTOCOLS=""
 
 function ip_split_prefix() {
        local address=${1}
-
        assert isset address
 
        echo "${address%%/*}"
@@ -32,7 +31,6 @@ function ip_split_prefix() {
 
 function ip_get_prefix() {
        local address=${1}
-
        assert isset address
 
        # Break if no prefix is provided
@@ -49,14 +47,11 @@ function ip_detect_protocol() {
        local protocol
        for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
                if ${protocol}_is_valid ${address}; then
-                       log DEBUG "Address '${address}' was detected to be protocol '${protocol}'."
                        echo "${protocol}"
                        return ${EXIT_OK}
                fi
        done
 
-       log DEBUG "Protocol version of address '${address}' could not be detected."
-
        return ${EXIT_ERROR}
 }
 
@@ -68,6 +63,56 @@ function ip_protocol_is_supported() {
        listmatch ${proto} ${IP_SUPPORTED_PROTOCOLS}
 }
 
+function ip_is_valid() {
+       local address=${1}
+       assert isset address
+
+       local proto=$(ip_detect_protocol ${address})
+       isset proto && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+}
+
+function ip_is_network() {
+       local network=${1}
+       assert isset network
+
+       # Get the address part.
+       local address=$(ip_split_prefix ${network})
+       isset address || return ${EXIT_FALSE}
+
+       # Get the prefix.
+       local prefix=$(ip_get_prefix ${network})
+       isset prefix || return ${EXIT_FALSE}
+
+       # Detect the protocol.
+       local proto=$(ip_detect_protocol ${address})
+       assert isset proto
+
+       # Check if the prefix is correct.
+       ip_prefix_is_valid ${proto} ${prefix} || return ${EXIT_FALSE}
+
+       return ${EXIT_TRUE}
+}
+
+function ip_prefix_is_valid() {
+       local proto=${1}
+       assert isset proto
+
+       local prefix=${2}
+
+       case "${proto}" in
+               ipv4)
+                       ipv4_prefix_is_valid ${prefix}
+                       return $?
+                       ;;
+               ipv6)
+                       ipv6_prefix_is_valid ${prefix}
+                       return $?
+                       ;;
+       esac
+
+       assert ip_protocol_is_supported ${proto}
+}
+
 function ip_address_add() {
        local device=${1}
        local address=${2}