dhcpv4: fix unitialization of mask
[project/odhcpd.git] / src / dhcpv4.c
index 04ef182..f6f4b0a 100644 (file)
@@ -118,19 +118,23 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        return -1;
                }
 
-               // Create a range if not specified
-               struct ifreq ifreq;
-               strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
+               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)
+                       bits = 0;
+
+               mask.s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
+               if (bits < 0)
+                       mask.s_addr = ~mask.s_addr;
 
-               struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr;
-               struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask;
+
+               // Create a range if not specified
                if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) &&
-                               !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) &&
-                               !ioctl(sock, SIOCGIFADDR, &ifreq)) {
-                       struct in_addr addr = saddr->sin_addr;
+                               !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) {
 
-                       ioctl(sock, SIOCGIFNETMASK, &ifreq);
-                       struct in_addr mask = smask->sin_addr;
 
                        uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
                        uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
@@ -203,8 +207,8 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                // Clean invalid assignments
                struct dhcpv4_assignment *a, *n;
                list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
-                       if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
-                                       (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) {
+                       if ((htonl(a->addr) & mask.s_addr) !=
+                                       (iface->dhcpv4_start.s_addr & mask.s_addr)) {
                                list_del(&a->head);
                                free(a);
                        }
@@ -222,7 +226,6 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        struct dhcpv4_assignment *a = list_first_entry(&iface->dhcpv4_assignments,
                                        struct dhcpv4_assignment, head);
                        list_del(&a->head);
-                       free(a->hostname);
                        free(a);
                }
 
@@ -637,7 +640,7 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
        } else if (msg == DHCPV4_MSG_RELEASE) {
                if (a && a->valid_until != LONG_MAX)
                        a->valid_until = 0;
-       } else if (msg == DHCPV4_MSG_DECLINE && a->valid_until != LONG_MAX) {
+       } else if (msg == DHCPV4_MSG_DECLINE && a && a->valid_until != LONG_MAX) {
                memset(a->hwaddr, 0, sizeof(a->hwaddr));
                a->valid_until = now + 3600; // Block address for 1h
        }