diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c index 2d8492c15..3329a819a 100644 --- a/src/lxc/confile_utils.c +++ b/src/lxc/confile_utils.c @@ -532,6 +532,66 @@ int rand_complete_hwaddr(char *hwaddr) return 0; } +bool lxc_config_net_hwaddr(const char *line) +{ + char *copy, *p; + + if (strncmp(line, "lxc.net", 7) != 0) + return false; + if (strncmp(line, "lxc.network.hwaddr", 18) == 0) + return true; + + /* We have to dup the line, if line is something like + * "lxc.net.[i].xxx = xxxxx ", we need to remove + * '[i]' and compare its key with 'lxc.net.hwaddr'*/ + copy = strdup(line); + if (!copy) { + SYSERROR("failed to allocate memory"); + return false; + } + if (*(copy + 8) >= '0' && *(copy + 8) <= '9') { + p = strchr(copy + 8, '.'); + if (!p) { + free(copy); + return false; + } + /* strlen("hwaddr") = 6 */ + strncpy(copy + 8, p + 1, 6); + copy[8 + 6] = '\0'; + } + if (strncmp(copy, "lxc.net.hwaddr", 14) == 0) { + free(copy); + return true; + } + free(copy); + + /* We have to dup the line second time, if line is something like + * "lxc.network.[i].xxx = xxxxx ", we need to remove + * '[i]' and compare its key with 'lxc.network.hwaddr'*/ + copy = strdup(line); + if (!copy) { + SYSERROR("failed to allocate memory"); + return false; + } + if (*(copy + 12) >= '0' && *(copy + 12) <= '9') { + p = strchr(copy + 12, '.'); + if (!p) { + free(copy); + return false; + } + /* strlen("hwaddr") = 6 */ + strncpy(copy + 12, p + 1, 6); + copy[12 + 6] = '\0'; + } + if (strncmp(copy, "lxc.network.hwaddr", 18) == 0) { + free(copy); + return true; + } + + free(copy); + return false; +} + /* * If we find a lxc.net.hwaddr in the original config file, we expand it in * the unexpanded_config, so that after a save_config we store the hwaddr for diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h index 5831df5c2..bee53dd26 100644 --- a/src/lxc/confile_utils.h +++ b/src/lxc/confile_utils.h @@ -79,6 +79,7 @@ extern int set_config_path_item(char **conf_item, const char *value); extern int config_ip_prefix(struct in_addr *addr); extern int network_ifname(char **valuep, const char *value); extern int rand_complete_hwaddr(char *hwaddr); +extern bool lxc_config_net_hwaddr(const char *line); extern void update_hwaddr(const char *line); extern bool new_hwaddr(char *hwaddr); extern int lxc_get_conf_str(char *retv, int inlen, const char *value);