confile_utils: add lxc_config_net_hwaddr

`lxc_config_net_hwaddr` return true if the config entry
is `lxc.network.hwaddr` or `lxc.net.[i].hwaddr`, `lxc.network.[i].hwaddr`

Signed-off-by: 0x0916 <w@laoqinren.net>
This commit is contained in:
0x0916 2017-06-28 10:56:43 +08:00
parent 82c3a0dc57
commit ce4be612ce
2 changed files with 61 additions and 0 deletions

View File

@ -532,6 +532,66 @@ int rand_complete_hwaddr(char *hwaddr)
return 0; 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 * 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 * the unexpanded_config, so that after a save_config we store the hwaddr for

View File

@ -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 config_ip_prefix(struct in_addr *addr);
extern int network_ifname(char **valuep, const char *value); extern int network_ifname(char **valuep, const char *value);
extern int rand_complete_hwaddr(char *hwaddr); extern int rand_complete_hwaddr(char *hwaddr);
extern bool lxc_config_net_hwaddr(const char *line);
extern void update_hwaddr(const char *line); extern void update_hwaddr(const char *line);
extern bool new_hwaddr(char *hwaddr); extern bool new_hwaddr(char *hwaddr);
extern int lxc_get_conf_str(char *retv, int inlen, const char *value); extern int lxc_get_conf_str(char *retv, int inlen, const char *value);