From 0d3caaf8e9a75e9b6ebc9427c75ed2914c60439d Mon Sep 17 00:00:00 2001 From: Samuel Mendoza-Jonas Date: Mon, 19 Dec 2016 15:56:49 +1100 Subject: [PATCH] discover: Record IP address of network interfaces 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 --- discover/network.c | 4 ++++ discover/sysinfo.c | 30 ++++++++++++++++++++++++++++++ discover/sysinfo.h | 2 ++ discover/user-event.c | 11 +++++++++++ lib/pb-protocol/pb-protocol.c | 8 +++++++- lib/types/types.h | 1 + test/parser/handler.c | 8 ++++++++ 7 files changed, 63 insertions(+), 1 deletion(-) diff --git a/discover/network.c b/discover/network.c index 51a846a..69223b1 100644 --- a/discover/network.c +++ b/discover/network.c @@ -384,6 +384,10 @@ static void configure_interface_static(struct network *network, 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) diff --git a/discover/sysinfo.c b/discover/sysinfo.c index 219369a..619c0c5 100644 --- a/discover/sysinfo.c +++ b/discover/sysinfo.c @@ -3,6 +3,7 @@ #include #include +#include #include "discover-server.h" #include "platform.h" @@ -16,6 +17,35 @@ const struct system_info *system_info_get(void) 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) { diff --git a/discover/sysinfo.h b/discover/sysinfo.h index 7181c99..19ed950 100644 --- a/discover/sysinfo.h +++ b/discover/sysinfo.h @@ -7,6 +7,8 @@ struct discover_server; 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, diff --git a/discover/user-event.c b/discover/user-event.c index 20b2bea..fb3fddb 100644 --- a/discover/user-event.c +++ b/discover/user-event.c @@ -37,6 +37,7 @@ #include "resource.h" #include "event.h" #include "user-event.h" +#include "sysinfo.h" #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; + 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); diff --git a/lib/pb-protocol/pb-protocol.c b/lib/pb-protocol/pb-protocol.c index 65a1e93..18edf57 100644 --- a/lib/pb-protocol/pb-protocol.c +++ b/lib/pb-protocol/pb-protocol.c @@ -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) + - sizeof(if_info->link); + sizeof(if_info->link) + + 4 + optional_strlen(if_info->address); } 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); + + pos += pb_protocol_serialise_string(pos, if_info->address); } *(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 (read_string(if_info, &pos, &len, &if_info->address)) + goto out; + sysinfo->interfaces[i] = if_info; } diff --git a/lib/types/types.h b/lib/types/types.h index 63f1b21..13ff7fc 100644 --- a/lib/types/types.h +++ b/lib/types/types.h @@ -89,6 +89,7 @@ struct interface_info { uint8_t *hwaddr; char *name; bool link; + char *address; }; struct blockdev_info { diff --git a/test/parser/handler.c b/test/parser/handler.c index 43e9d71..e356407 100644 --- a/test/parser/handler.c +++ b/test/parser/handler.c @@ -38,6 +38,14 @@ void discover_server_notify_boot_status(struct discover_server *server, (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) { -- 2.39.2