config: rework code to get rid of IFNAMSIZ usage
authorHans Dedecker <dedeckeh@gmail.com>
Tue, 27 Jun 2017 09:33:08 +0000 (11:33 +0200)
committerHans Dedecker <dedeckeh@gmail.com>
Tue, 27 Jun 2017 11:17:52 +0000 (13:17 +0200)
Get rid of IFNAMSIZE usage for interface name and ifname variables

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
src/config.c
src/dhcpv4.c
src/odhcpd.h

index 7940095b409f1f2bf5bc7b58649a56dcfba210a7..9ce20efd930857ba9e774721caa653808e663aa0 100644 (file)
@@ -4,12 +4,14 @@
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <libgen.h>
+#include <net/if.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <syslog.h>
 
 #include <uci.h>
 #include <uci_blob.h>
+#include <libubox/utils.h>
 
 #include "odhcpd.h"
 
@@ -235,6 +237,7 @@ static void close_interface(struct interface *iface)
        setup_dhcpv4_interface(iface, false);
 
        clean_interface(iface);
+       free(iface->ifname);
        free(iface);
 }
 
@@ -394,11 +397,13 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 
        struct interface *iface = get_interface(name);
        if (!iface) {
-               iface = calloc(1, sizeof(*iface));
+               char *iface_name;
+
+               iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
                if (!iface)
                        return -1;
 
-               strncpy(iface->name, name, sizeof(iface->name) - 1);
+               iface->name = strcpy(iface_name, name);
 
                set_interface_defaults(iface);
 
@@ -415,15 +420,20 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        }
 
 #ifdef WITH_UBUS
-       if (overwrite || !iface->ifname[0])
+       if (overwrite || !iface->ifname)
                ifname = ubus_get_ifname(name);
 #endif
 
-       if (!iface->ifname[0] && !ifname)
+       if (!iface->ifname && !ifname)
                goto err;
 
-       if (ifname)
-               strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
+       if (ifname) {
+               free(iface->ifname);
+               iface->ifname = strdup(ifname);
+
+               if (!iface->ifname)
+                       goto err;
+       }
 
        if ((iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
                goto err;
index 21d94f2290330ed291db37f79f76c2792a1f5f14..e1f0df6f488eeae745844f9d9e347e6cf21f6e76 100644 (file)
@@ -138,8 +138,6 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                                        iface->dhcpv4_end.s_addr = end | htonl(12);
                                }
                        }
-
-
                }
 
                /* Parse static entries */
index 1803a413326247e15ed22f81d72c6ea6ccf85cfa..93adca62cb33a26b189643e16fc0e4419614a11a 100644 (file)
@@ -16,7 +16,6 @@
 #include <netinet/in.h>
 #include <netinet/icmp6.h>
 #include <netinet/ether.h>
-#include <net/if.h>
 #include <stdbool.h>
 #include <syslog.h>
 
@@ -118,8 +117,8 @@ struct interface {
        struct list_head head;
 
        int ifindex;
-       char ifname[IF_NAMESIZE];
-       char name[IF_NAMESIZE];
+       char *ifname;
+       const char *name;
 
        // Runtime data
        struct uloop_timeout timer_rs;