[gnome-boxes] libvirt-machine: Add get_ip_address()



commit 77694c5afba473bc216dca17f6d17f8a84e4a0ab
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Fri Jul 3 15:56:43 2015 +0100

    libvirt-machine: Add get_ip_address()
    
    Add a method to fetch IP address of the machine. This is the IP of the
    running guest and you only get the IP if machine is using 'virbr0'
    bridge network, managed by system libvirt.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=744004

 src/libvirt-machine.vala |   54 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)
---
diff --git a/src/libvirt-machine.vala b/src/libvirt-machine.vala
index f0752ed..c8ce457 100644
--- a/src/libvirt-machine.vala
+++ b/src/libvirt-machine.vala
@@ -108,6 +108,8 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
     private bool force_stopped;
     private bool saving; // Machine is being saved currently..
 
+    private GVir.Connection system_virt_connection;
+
     construct {
         stats = new MachineStat[STATS_SIZE];
         stats_cancellable = new Cancellable ();
@@ -211,6 +213,12 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
             load_screenshot ();
         set_screenshot_enable (true);
 
+        try {
+            system_virt_connection = yield get_system_virt_connection ();
+        } catch (GLib.Error error) {
+            warning ("Failed to connection to system libvirt: %s", error.message);
+        }
+
         update_info ();
     }
 
@@ -636,6 +644,33 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         try_shutdown ();
     }
 
+    public string? get_ip_address () {
+        if (system_virt_connection == null || !is_on ())
+            return null;
+
+        var mac = get_mac_address ();
+        if (mac == null)
+            return null;
+
+        foreach (var network in system_virt_connection.get_networks ()) {
+            try {
+                var leases = network.get_dhcp_leases (mac, 0);
+
+                if (leases.length () == 0 || leases.data.get_iface () != "virbr0")
+                    continue;
+
+                // Get first IP in the list
+                return leases.data.get_ip ();
+            } catch (GLib.Error error) {
+                warning ("Failed to get DHCP leases from network '%s': %s",
+                         network.get_name (),
+                         error.message);
+            }
+        }
+
+        return null;
+    }
+
     private async void wait_for_save () {
         if (!saving)
             return;
@@ -677,4 +712,23 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         else
             info = null;
     }
+
+    private string? get_mac_address () {
+        GVirConfig.DomainInterface? iface_config = null;
+
+        foreach (var device_config in domain_config.get_devices ()) {
+            // Only entertain bridge network. With user networking, the IP address isn't even reachable from 
host so
+            // it's pretty much useless to show that to user.
+            if (device_config is GVirConfig.DomainInterfaceBridge) {
+                iface_config = device_config as GVirConfig.DomainInterface;
+
+                break;
+            }
+        }
+
+        if (iface_config == null)
+            return null;
+
+        return iface_config.get_mac ().dup ();
+    }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]