discover: Record IP address of network interfaces
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Mon, 19 Dec 2016 04:56:49 +0000 (15:56 +1100)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 20 Dec 2016 05:40:22 +0000 (16:40 +1100)
If an interface has a valid IP address (either via DHCP or static
config) store it in the interface_info struct so it can be referenced
later.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
discover/network.c
discover/sysinfo.c
discover/sysinfo.h
discover/user-event.c
lib/pb-protocol/pb-protocol.c
lib/types/types.h
test/parser/handler.c

index 51a846af8a0c6ae8e75582f1eea619a75e8c1eb7..69223b149adacd0c133beb06de88d06300e324a4 100644 (file)
@@ -384,6 +384,10 @@ static void configure_interface_static(struct network *network,
                return;
        }
 
                return;
        }
 
+       system_info_set_interface_address(sizeof(interface->hwaddr),
+                               interface->hwaddr,
+                               config->static_config.address);
+
        /* we need the interface up before we can route through it */
        rc = interface_up(interface);
        if (rc)
        /* we need the interface up before we can route through it */
        rc = interface_up(interface);
        if (rc)
index 219369a0d5456f5bda27fed2ba7a534a4db7b64b..619c0c588ff61a42326dddfcc5885e113870a80e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <talloc/talloc.h>
 #include <process/process.h>
 
 #include <talloc/talloc.h>
 #include <process/process.h>
+#include <log/log.h>
 
 #include "discover-server.h"
 #include "platform.h"
 
 #include "discover-server.h"
 #include "platform.h"
@@ -16,6 +17,35 @@ const struct system_info *system_info_get(void)
        return sysinfo;
 }
 
        return sysinfo;
 }
 
+
+void system_info_set_interface_address(unsigned int hwaddr_size,
+               uint8_t *hwaddr, const char *address)
+{
+       struct interface_info *if_info;
+       unsigned int i;
+
+       for (i = 0; i < sysinfo->n_interfaces; i++) {
+               if_info = sysinfo->interfaces[i];
+
+               if (if_info->hwaddr_size != hwaddr_size)
+                       continue;
+
+               if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
+                       continue;
+
+               /* Found an existing interface. Notify clients if a new address
+                * is set */
+               if (!if_info->address || strcmp(if_info->address, address)) {
+                       talloc_free(if_info->address);
+                       if_info->address = talloc_strdup(if_info, address);
+                       discover_server_notify_system_info(server, sysinfo);
+                       return;
+               }
+       }
+
+       pb_log("Couldn't find interface matching %s\n", "foo");
+}
+
 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
                const char *name, bool link)
 {
 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
                const char *name, bool link)
 {
index 7181c99cd42080cf7edd1a6e327b048e617bae48..19ed95077d247917eceff5875e4d1b751361e74f 100644 (file)
@@ -7,6 +7,8 @@ struct discover_server;
 
 const struct system_info *system_info_get(void);
 
 
 const struct system_info *system_info_get(void);
 
+void system_info_set_interface_address(unsigned int hwaddr_size,
+               uint8_t *hwaddr, const char *address);
 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
                const char *name, bool link);
 void system_info_register_blockdev(const char *name, const char *uuid,
 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
                const char *name, bool link);
 void system_info_register_blockdev(const char *name, const char *uuid,
index 20b2bea928ea75b221deac7ed675fd9f44345f81..fb3fddb5c4bc5f7390f114ec7820f33d7ec45e77 100644 (file)
@@ -37,6 +37,7 @@
 #include "resource.h"
 #include "event.h"
 #include "user-event.h"
 #include "resource.h"
 #include "event.h"
 #include "user-event.h"
+#include "sysinfo.h"
 
 
 #define MAC_ADDR_SIZE  6
 
 
 #define MAC_ADDR_SIZE  6
@@ -385,6 +386,16 @@ static int user_event_dhcp(struct user_event *uev, struct event *event)
        struct device_handler *handler = uev->handler;
        struct discover_device *dev;
 
        struct device_handler *handler = uev->handler;
        struct discover_device *dev;
 
+       uint8_t hwaddr[MAC_ADDR_SIZE];
+
+       sscanf(event_get_param(event, "mac"),
+              "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
+              hwaddr, hwaddr + 1, hwaddr + 2,
+              hwaddr + 3, hwaddr + 4, hwaddr + 5);
+
+       system_info_set_interface_address(sizeof(hwaddr), hwaddr,
+                                         event_get_param(event, "ip"));
+
        dev = discover_device_create(handler, event_get_param(event, "mac"),
                                        event->device);
 
        dev = discover_device_create(handler, event_get_param(event, "mac"),
                                        event->device);
 
index 65a1e93408d834d0449d458d0806e3ef50be66f6..18edf57e24ca341e88a21fa2e06891685c297c0a 100644 (file)
@@ -249,7 +249,8 @@ int pb_protocol_system_info_len(const struct system_info *sysinfo)
                struct interface_info *if_info = sysinfo->interfaces[i];
                len +=  4 + if_info->hwaddr_size +
                        4 + optional_strlen(if_info->name) +
                struct interface_info *if_info = sysinfo->interfaces[i];
                len +=  4 + if_info->hwaddr_size +
                        4 + optional_strlen(if_info->name) +
-                       sizeof(if_info->link);
+                       sizeof(if_info->link) +
+                       4 + optional_strlen(if_info->address);
        }
 
        for (i = 0; i < sysinfo->n_blockdevs; i++) {
        }
 
        for (i = 0; i < sysinfo->n_blockdevs; i++) {
@@ -465,6 +466,8 @@ int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
 
                *(bool *)pos = if_info->link;
                pos += sizeof(bool);
 
                *(bool *)pos = if_info->link;
                pos += sizeof(bool);
+
+               pos += pb_protocol_serialise_string(pos, if_info->address);
        }
 
        *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
        }
 
        *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
@@ -949,6 +952,9 @@ int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
                if_info->link = *(bool *)pos;
                pos += sizeof(if_info->link);
 
                if_info->link = *(bool *)pos;
                pos += sizeof(if_info->link);
 
+               if (read_string(if_info, &pos, &len, &if_info->address))
+                       goto out;
+
                sysinfo->interfaces[i] = if_info;
        }
 
                sysinfo->interfaces[i] = if_info;
        }
 
index 63f1b213c19261687e9ebd9c85272c0327882c59..13ff7fc79f83bf3f4787a08198db019be9f9749b 100644 (file)
@@ -89,6 +89,7 @@ struct interface_info {
        uint8_t         *hwaddr;
        char            *name;
        bool            link;
        uint8_t         *hwaddr;
        char            *name;
        bool            link;
+       char            *address;
 };
 
 struct blockdev_info {
 };
 
 struct blockdev_info {
index 43e9d71309139706a406c028bc8a08c764aebf06..e35640712770c9a0c13692691f6a660e1d60868e 100644 (file)
@@ -38,6 +38,14 @@ void discover_server_notify_boot_status(struct discover_server *server,
        (void)status;
 }
 
        (void)status;
 }
 
+void system_info_set_interface_address(unsigned int hwaddr_size,
+               uint8_t *hwaddr, const char *address)
+{
+       (void)hwaddr_size;
+       (void)hwaddr;
+       (void)address;
+}
+
 void discover_server_notify_config(struct discover_server *server,
                struct config *config)
 {
 void discover_server_notify_config(struct discover_server *server,
                struct config *config)
 {