]> git.ipfire.org Git - people/arne_f/network.git/blobdiff - functions.device
Fix weird device CLI command.
[people/arne_f/network.git] / functions.device
index d0ab0ce36085c60146637ee40c92a678c03edeb4..1ddc7d2c9d9b34349dbb29fd1e3d87fdd93ec69a 100644 (file)
@@ -153,6 +153,9 @@ function device_is_real() {
        device_is_virtual ${device} && \
                return ${EXIT_ERROR}
 
+       [ "$(__device_get_file ${device} type)" != "1" ] && \
+               return ${EXIT_ERROR}
+
        return ${EXIT_OK}
 }
 
@@ -273,6 +276,17 @@ function device_is_promisc() {
        ip link show ${device} | grep -qE "<.*PROMISC.*>"
 }
 
+function device_set_promisc() {
+       local device=${1}
+       local state=${2}
+
+       assert device_exists ${device}
+       assert isset state
+       assert isoneof state on off
+
+       ip link set ${device} promisc ${state}
+}
+
 # Check if the device is free
 function device_is_free() {
        ! device_is_used $@
@@ -292,25 +306,6 @@ function device_is_used() {
        return ${EXIT_ERROR}
 }
 
-# XXX to be removed I think
-function device_get_free() {
-       local destination=${1}
-
-       # Replace + by a valid number
-       if grep -q "+$" <<<${destination}; then
-               local number=0
-               destination=$(sed -e "s/+//" <<<$destination)
-               while [ "${number}" -le "100" ]; do
-                       if ! device_exists "${destination}${number}"; then
-                               destination="${destination}${number}"
-                               break
-                       fi
-                       number=$(($number + 1))
-               done
-       fi
-       echo "${destination}"
-}
-
 function device_hash() {
        local device=${1}
 
@@ -324,7 +319,7 @@ function device_hash() {
 # Give the device a new name
 function device_set_name() {
        local source=$1
-       local destination=$(device_get_free ${2})
+       local destination=${2}
 
        # Check if devices exists
        if ! device_exists ${source} || device_exists ${destination}; then
@@ -477,35 +472,36 @@ function device_discover() {
        done
 }
 
-function device_has_ipv4() {
+function device_has_ip() {
        local device=${1}
        local addr=${2}
 
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
+       assert isset addr
+       assert device_exists ${device}
+
+       # IPv6 addresses must be fully imploded
+       local protocol=$(ip_detect_protocol ${addr})
+       case "${protocol}" in
+               ipv6)
+                       addr=$(ipv6_implode ${addr})
+                       ;;
+       esac
 
-       ip addr show ${device} | grep -q -e "inet " -e "${addr}"
+       listmatch ${addr} $(device_get_addresses ${device})
 }
 
-function device_has_ipv6() {
+function device_get_addresses() {
        local device=${1}
-       local addr=${2}
-
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
 
-       local prefix=${addr##*/}
-       addr=$(ipv6_implode ${addr%%/*})
+       assert device_exists ${device}
 
-       if [ -n "${prefix}" ]; then
-               addr="${addr}/${prefix}"
-       fi
-
-       ip addr show ${device} | grep -q "inet6 ${addr}"
+       local prot
+       local addr
+       local line
+       ip addr show ${device} | \
+               while read prot addr line; do
+                       [ "${prot:0:4}" = "inet" ] && echo "${addr}"
+               done
 }
 
 function __device_get_file() {