lib/efi: Cleanup read/write routines
[petitboot] / discover / sysinfo.c
index 0ac29c1ead35adddd7f9a52acbe6c8fbd8f811c5..a9d7ff547b11ffb22a40bbdf4e738448de4f6a16 100644 (file)
@@ -4,6 +4,7 @@
 #include <talloc/talloc.h>
 #include <process/process.h>
 #include <log/log.h>
 #include <talloc/talloc.h>
 #include <process/process.h>
 #include <log/log.h>
+#include <url/url.h>
 
 #include "discover-server.h"
 #include "platform.h"
 
 #include "discover-server.h"
 #include "platform.h"
@@ -17,13 +18,12 @@ 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;
 void system_info_set_interface_address(unsigned int hwaddr_size,
                uint8_t *hwaddr, const char *address)
 {
        struct interface_info *if_info;
        unsigned int i;
-       char mac[20];
+       char mac[20], **if_addr, *new_addr, *subnet;
 
        for (i = 0; i < sysinfo->n_interfaces; i++) {
                if_info = sysinfo->interfaces[i];
 
        for (i = 0; i < sysinfo->n_interfaces; i++) {
                if_info = sysinfo->interfaces[i];
@@ -34,11 +34,24 @@ void system_info_set_interface_address(unsigned int hwaddr_size,
                if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
                        continue;
 
                if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
                        continue;
 
+               /*
+                * Don't include the subnet from a static config, and check if
+                * we're updating the IPv4 or IPv6 address.
+                * */
+               if ((subnet = strchr(address, '/')))
+                       new_addr = talloc_strndup(if_info, address, subnet - address);
+               else
+                       new_addr = talloc_strdup(if_info, address);
+               if (addr_scheme(new_addr) == AF_INET)
+                       if_addr = &if_info->address;
+               else
+                       if_addr = &if_info->address_v6;
+
                /* Found an existing interface. Notify clients if a new address
                 * is set */
                /* 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);
+               if (!*if_addr || strcmp(*if_addr, address)) {
+                       talloc_free(*if_addr);
+                       *if_addr = new_addr;
                        discover_server_notify_system_info(server, sysinfo);
                        return;
                }
                        discover_server_notify_system_info(server, sysinfo);
                        return;
                }
@@ -139,3 +152,21 @@ void system_info_init(struct discover_server *s)
        sysinfo = talloc_zero(server, struct system_info);
        platform_get_sysinfo(sysinfo);
 }
        sysinfo = talloc_zero(server, struct system_info);
        platform_get_sysinfo(sysinfo);
 }
+
+/* Only reset device information. Platform information is static */
+void system_info_reinit(void)
+{
+       unsigned int i;
+
+       for (i = 0; i < sysinfo->n_blockdevs; i++)
+               talloc_free(sysinfo->blockdevs[i]);
+       talloc_free(sysinfo->blockdevs);
+       sysinfo->blockdevs = NULL;
+       sysinfo->n_blockdevs = 0;
+
+       for (i = 0; i < sysinfo->n_interfaces; i++)
+               talloc_free(sysinfo->interfaces[i]);
+       talloc_free(sysinfo->interfaces);
+       sysinfo->interfaces = NULL;
+       sysinfo->n_interfaces = 0;
+}