device_is_virtual ${device} && \
return ${EXIT_ERROR}
+ [ "$(__device_get_file ${device} type)" != "1" ] && \
+ return ${EXIT_ERROR}
+
return ${EXIT_OK}
}
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 $@
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}
# 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
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() {