dhcpv4: fix char ubus_get_address4 int ubus_get_mask4 function
authorPatrick Grimm <patrick@lunatiki.de>
Thu, 18 Jun 2015 02:27:24 +0000 (04:27 +0200)
committerPatrick Grimm <patrick@lunatiki.de>
Thu, 18 Jun 2015 02:27:24 +0000 (04:27 +0200)
Signed-off-by: Patrick Grimm <patrick@lunatiki.de>
src/dhcpv4.c
src/odhcpd.h
src/ubus.c

index 99ad658fe5e0547f085fe98f285e263162e83fe9..07ad3ffd78c352e940cb6ab1a4552772154e54cd 100644 (file)
@@ -118,14 +118,22 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        return -1;
                }
 
+               const char* saddr = ubus_get_address4(iface->name);
+               struct in_addr addr;
+               inet_pton(AF_INET,saddr, &addr);
+               int bits = ubus_get_mask4(iface->name);
+               struct in_addr mask;
+               if (!(bits < -32 || bits > 32)) {
+                       mask.s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
+                       if (bits < 0)
+                               mask.s_addr = ~mask.s_addr;
+               }
+
+
                // Create a range if not specified
                if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) &&
                                !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) {
 
-                       struct in_addr *saddr = ubus_get_address4(iface->name);
-                       struct in_addr addr = { .s_addr = saddr->s_addr } ;
-                       struct in_addr *smask = ubus_get_mask4(iface->name);
-                       struct in_addr mask = { .s_addr = smask->s_addr } ;
 
                        uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
                        uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
@@ -197,8 +205,6 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
 
                // Clean invalid assignments
                struct dhcpv4_assignment *a, *n;
-               struct in_addr *smask = ubus_get_mask4(iface->name);
-               struct in_addr mask = { .s_addr = smask->s_addr } ;
                list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
                        if ((htonl(a->addr) & mask.s_addr) !=
                                        (iface->dhcpv4_start.s_addr & mask.s_addr)) {
index 82ba46ba8b493d484ac7de8bac4361d5abb5db35..b8cbeb2679ed0b47bca69db89d7f83522faa35ef 100644 (file)
@@ -209,8 +209,8 @@ int init_ubus(void);
 const char* ubus_get_ifname(const char *name);
 void ubus_apply_network(void);
 bool ubus_has_prefix(const char *name, const char *ifname);
-struct in_addr* ubus_get_address4(const char *name);
-struct in_addr* ubus_get_mask4(const char *name);
+const char* ubus_get_address4(const char *name);
+int ubus_get_mask4(const char *name);
 #endif
 
 
index 2b6b1c7e56911719003d91bccb7fb224b9158cf9..e8563bd50d8b1eca4753e25d681bf8fb83c93df3 100644 (file)
@@ -372,7 +372,7 @@ bool ubus_has_prefix(const char *name, const char *ifname)
        return false;
 }
 
-struct in_addr* ubus_get_address4(const char *name)
+const char* ubus_get_address4(const char *name)
 {
        struct blob_attr *c, *cur;
        unsigned rem;
@@ -395,11 +395,13 @@ struct in_addr* ubus_get_address4(const char *name)
                        struct blob_attr *d;
                        unsigned drem;
                        blobmsg_for_each_attr(d, cur, drem) {
-                               struct blob_attr *addr[ADDR_ATTR_MAX];
-                               blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d));
-                               struct in_addr *addr4;
-                               if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_ADDRESS]), &addr4) == 1)
-                                       return addr4;
+                               struct blob_attr *ccur;
+                               unsigned ddrem;
+                               struct blob_attr *dict = blobmsg_data(cur);
+                               blobmsg_for_each_attr(ccur, dict, ddrem) {
+                                       if (!strcmp(blobmsg_name(ccur), "address"))
+                                               return blobmsg_get_string(ccur);
+                               }
                        }
                }
        }
@@ -407,13 +409,12 @@ struct in_addr* ubus_get_address4(const char *name)
        return NULL;
 }
 
-struct in_addr* ubus_get_mask4(const char *name)
+int ubus_get_mask4(const char *name)
 {
        struct blob_attr *c, *cur;
        unsigned rem;
-
        if (!dump)
-               return NULL;
+               return 0;
 
        blobmsg_for_each_attr(c, dump, rem) {
                struct blob_attr *tb[IFACE_ATTR_MAX];
@@ -430,16 +431,18 @@ struct in_addr* ubus_get_mask4(const char *name)
                        struct blob_attr *d;
                        unsigned drem;
                        blobmsg_for_each_attr(d, cur, drem) {
-                               struct blob_attr *addr[ADDR_ATTR_MAX];
-                               blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d));
-                               struct in_addr *addr4;
-                               if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_MASK]), &addr4) == 1)
-                                       return addr4;
+                               struct blob_attr *ccur;
+                               unsigned ddrem;
+                               struct blob_attr *dict = blobmsg_data(cur);
+                               blobmsg_for_each_attr(ccur, dict, ddrem) {
+                                       if (!strcmp(blobmsg_name(ccur), "mask"))
+                                               return blobmsg_get_u32(ccur);
+                               }
                        }
                }
        }
 
-       return NULL;
+       return 0;
 }
 
 int init_ubus(void)