]> git.proxmox.com Git - mirror_lxc.git/commitdiff
confile_utils: add lxc_config_net_hwaddr
author0x0916 <w@laoqinren.net>
Wed, 28 Jun 2017 02:56:43 +0000 (10:56 +0800)
committer0x0916 <w@laoqinren.net>
Thu, 29 Jun 2017 05:18:41 +0000 (13:18 +0800)
`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>
src/lxc/confile_utils.c
src/lxc/confile_utils.h

index 2d8492c15dba4ed5038c3efb3b94737324d9f9c3..3329a819a5cf362e7dfb446745c3249cea3e75ba 100644 (file)
@@ -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
index 5831df5c20e292e8b3a2e5e7b204b70463511748..bee53dd264495b26a04622e54b2f00806019b335 100644 (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 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);