Merge pull request #67 from dedeckeh/bugfixes
[project/odhcpd.git] / src / dhcpv4.c
index 99ad658..a657e13 100644 (file)
@@ -119,13 +119,18 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                }
 
                // Create a range if not specified
+               struct ifreq ifreq;
+               strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
+
+               struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr;
+               struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask;
                if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) &&
-                               !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) {
+                               !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) &&
+                               !ioctl(sock, SIOCGIFADDR, &ifreq)) {
+                       struct in_addr addr = saddr->sin_addr;
 
-                       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 } ;
+                       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);
@@ -197,11 +202,9 @@ 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)) {
+                       if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
+                                       (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) {
                                list_del(&a->head);
                                free(a);
                        }