]> git.ipfire.org Git - people/jschlag/network.git/commitdiff
Add new function: device_get_by_assigned_ip_address()
authorJonatan Schlag via network <network@lists.ipfire.org>
Fri, 23 Feb 2018 11:05:33 +0000 (11:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Mar 2018 14:51:27 +0000 (14:51 +0000)
This function is used to get a device from an IP address
which is assigned to the device.
This function needs to be introduced
to set the routes for IPsec correctly.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.device

index cb4911f9ae7a6ec04888fe675a57c707c940d9c5..2de1ad9491f8825511abb782d033696402470cbc 100644 (file)
@@ -1058,3 +1058,30 @@ device_queue_set_smp_affinity() {
 
        __processor_id_to_bitmap ${processor} > ${path}
 }
+
+# Tries to find a device which has the given IP address assigned
+device_get_by_assigned_ip_address() {
+       local ip=${1}
+
+       assert isset ip
+
+       local device
+
+       # Read the first line of ip addr show to
+       read -r device <<< $(ip addr show to "${ip}")
+
+       # If we did not found a device we return with ${EXIT_ERROR}
+       if ! isset device; then
+               return ${EXIT_ERROR}
+       fi
+
+       # We get something like:
+       # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
+       # and we want upl0 so we take the second word and removing the :
+       device=(${device})
+       device=${device[1]}
+       device=${device%:}
+
+       print "${device}"
+       return ${EXIT_OK}
+}