discover: Set UUID for network interface devices
authorJeremy Kerr <jk@ozlabs.org>
Thu, 17 Jul 2014 01:13:58 +0000 (09:13 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 17 Jul 2014 03:09:06 +0000 (11:09 +0800)
We want to lookup network (as well as block) devices by UUID, so set one
based on the MAC address.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/network.c

index 1801710773b975106e43d6592de4a85d8ad55322..ad17a417e4f48d7fed992d48291807b71f1a1221 100644 (file)
@@ -150,6 +150,25 @@ static int network_send_link_query(struct network *network)
        return 0;
 }
 
+static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
+{
+       const int l = strlen("xx:");
+       char *buf;
+       int i;
+
+       if (len <= 0)
+               return talloc_strdup(ctx, "");
+
+       buf = talloc_array(ctx, char, (len * l) + 1);
+
+       for (i = 0; i < len; i++)
+               sprintf(buf + (l * i), "%02x:", addr[i]);
+
+       *(buf + (l * len) - 1) = '\0';
+
+       return buf;
+}
+
 static void add_interface(struct network *network,
                struct interface *interface)
 {
@@ -157,6 +176,8 @@ static void add_interface(struct network *network,
        interface->dev = discover_device_create(network->handler,
                                        interface->name);
        interface->dev->device->type = DEVICE_TYPE_NETWORK;
+       interface->dev->uuid = mac_bytes_to_string(interface->dev,
+                       interface->hwaddr, sizeof(interface->hwaddr));
        device_handler_add_device(network->handler, interface->dev);
 }