]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
network: add lxc_network_info struct
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 26 Feb 2021 12:32:11 +0000 (13:32 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 26 Feb 2021 13:20:07 +0000 (14:20 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/network.c

index 06366be93a41a7f9732295d02898ddbd5f2d1db7..3559091f8bff1c8799fe9c7d0a839f6b78643fd1 100644 (file)
@@ -50,6 +50,69 @@ typedef int (*netdev_configure_server_cb)(struct lxc_handler *, struct lxc_netde
 typedef int (*netdev_configure_container_cb)(struct lxc_netdev *);
 typedef int (*netdev_shutdown_server_cb)(struct lxc_handler *, struct lxc_netdev *);
 
+const struct lxc_network_info {
+       const char *name;
+       const char *template;
+} lxc_network_info[LXC_NET_MAXCONFTYPE + 1] = {
+       [LXC_NET_EMPTY]         = { "empty",            "emptXXXXXX"    },
+       [LXC_NET_VETH]          = { "veth",             "vethXXXXXX"    },
+       [LXC_NET_MACVLAN]       = { "macvlan",          "macvXXXXXX"    },
+       [LXC_NET_IPVLAN]        = { "ipvlan",           "ipvlXXXXXX"    },
+       [LXC_NET_PHYS]          = { "phys",             "physXXXXXX"    },
+       [LXC_NET_VLAN]          = { "vlan",             "vlanXXXXXX"    },
+       [LXC_NET_NONE]          = { "none",             "noneXXXXXX"    },
+       [LXC_NET_MAXCONFTYPE]   = { NULL,               NULL            }
+};
+
+const char *lxc_net_type_to_str(int type)
+{
+       if (type < 0 || type > LXC_NET_MAXCONFTYPE)
+               return NULL;
+
+       return lxc_network_info[type].name;
+}
+
+static const char padchar[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+char *lxc_ifname_alnum_case_sensitive(char *template)
+{
+       char name[IFNAMSIZ];
+       size_t i = 0;
+#ifdef HAVE_RAND_R
+       unsigned int seed;
+
+       seed = randseed(false);
+#else
+
+       (void)randseed(true);
+#endif
+
+       if (strlen(template) >= IFNAMSIZ)
+               return NULL;
+
+       /* Generate random names until we find one that doesn't exist. */
+       for (;;) {
+               name[0] = '\0';
+               (void)strlcpy(name, template, IFNAMSIZ);
+
+               for (i = 0; i < strlen(name); i++) {
+                       if (name[i] == 'X') {
+#ifdef HAVE_RAND_R
+                               name[i] = padchar[rand_r(&seed) % strlen(padchar)];
+#else
+                               name[i] = padchar[rand() % strlen(padchar)];
+#endif
+                       }
+               }
+
+               if (if_nametoindex(name) == 0)
+                       break;
+       }
+
+       (void)strlcpy(template, name, strlen(template) + 1);
+
+       return template;
+}
 static const char loop_device[] = "lo";
 
 static int lxc_ip_route_dest(__u16 nlmsg_type, int family, int ifindex, void *dest, unsigned int netmask)
@@ -2736,66 +2799,6 @@ int lxc_bridge_attach(const char *bridge, const char *ifname)
        return err;
 }
 
-static const char *const lxc_network_types[LXC_NET_MAXCONFTYPE + 1] = {
-       [LXC_NET_EMPTY]   = "empty",
-       [LXC_NET_VETH]    = "veth",
-       [LXC_NET_MACVLAN] = "macvlan",
-       [LXC_NET_IPVLAN]  = "ipvlan",
-       [LXC_NET_PHYS]    = "phys",
-       [LXC_NET_VLAN]    = "vlan",
-       [LXC_NET_NONE]    = "none",
-};
-
-const char *lxc_net_type_to_str(int type)
-{
-       if (type < 0 || type > LXC_NET_MAXCONFTYPE)
-               return NULL;
-
-       return lxc_network_types[type];
-}
-
-static const char padchar[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
-char *lxc_ifname_alnum_case_sensitive(char *template)
-{
-       char name[IFNAMSIZ];
-       size_t i = 0;
-#ifdef HAVE_RAND_R
-       unsigned int seed;
-
-       seed = randseed(false);
-#else
-
-       (void)randseed(true);
-#endif
-
-       if (strlen(template) >= IFNAMSIZ)
-               return NULL;
-
-       /* Generate random names until we find one that doesn't exist. */
-       for (;;) {
-               name[0] = '\0';
-               (void)strlcpy(name, template, IFNAMSIZ);
-
-               for (i = 0; i < strlen(name); i++) {
-                       if (name[i] == 'X') {
-#ifdef HAVE_RAND_R
-                               name[i] = padchar[rand_r(&seed) % strlen(padchar)];
-#else
-                               name[i] = padchar[rand() % strlen(padchar)];
-#endif
-                       }
-               }
-
-               if (if_nametoindex(name) == 0)
-                       break;
-       }
-
-       (void)strlcpy(template, name, strlen(template) + 1);
-
-       return template;
-}
-
 int setup_private_host_hw_addr(char *veth1)
 {
        __do_close int sockfd = -EBADF;