router: fix dns search list option
[project/odhcpd.git] / src / dhcpv4.c
index ff2d8c2ed51342fdc0de134b95718dec6d715ae2..7058b834f04e3318f624ef9b801d08e0194f096b 100644 (file)
@@ -73,7 +73,7 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable)
 {
        int ret = 0;
 
-       if (iface->dhcpv4_event.uloop.fd > 0) {
+       if (iface->dhcpv4_event.uloop.fd >= 0) {
                uloop_fd_delete(&iface->dhcpv4_event.uloop);
                close(iface->dhcpv4_event.uloop.fd);
                iface->dhcpv4_event.uloop.fd = -1;
@@ -163,7 +163,7 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable)
        }
 
 out:
-       if (ret < 0 && iface->dhcpv4_event.uloop.fd > 0) {
+       if (ret < 0 && iface->dhcpv4_event.uloop.fd >= 0) {
                close(iface->dhcpv4_event.uloop.fd);
                iface->dhcpv4_event.uloop.fd = -1;
        }
@@ -219,7 +219,7 @@ static int setup_dhcpv4_addresses(struct interface *iface)
        }
 
        if (!iface->addr4_len) {
-               syslog(LOG_WARNING, "No network(s) available on %s", iface->name);
+               syslog(LOG_ERR, "No network(s) available on %s", iface->name);
                return -1;
        }
 
@@ -249,7 +249,7 @@ static int setup_dhcpv4_addresses(struct interface *iface)
 
        /* Don't allocate IP range for subnets bigger than 28 */
        if (iface->addr4[0].prefix > 28) {
-               syslog(LOG_WARNING, "Auto allocation of DHCP range fails on %s", iface->name);
+               syslog(LOG_ERR, "Auto allocation of DHCP range fails on %s", iface->name);
                return -1;
        }
 
@@ -523,7 +523,7 @@ static void dhcpv4_fr_send(struct dhcp_assignment *a)
                syslog(LOG_ERR, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg),
                        odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
        else
-               syslog(LOG_WARNING, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg),
+               syslog(LOG_NOTICE, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg),
                        odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
 }
 
@@ -594,7 +594,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
        syslog(LOG_NOTICE, "Got DHCPv4 request on %s", iface->name);
 
        if (!iface->dhcpv4_start_ip.s_addr && !iface->dhcpv4_end_ip.s_addr) {
-               syslog(LOG_WARNING, "No DHCP range available on %s", iface->name);
+               syslog(LOG_ERR, "No DHCP range available on %s", iface->name);
                return;
        }
 
@@ -717,7 +717,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                        req->ciaddr.s_addr = INADDR_ANY;
        }
 
-       syslog(LOG_WARNING, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg),
+       syslog(LOG_NOTICE, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg),
                        odhcpd_print_mac(req->chaddr, req->hlen), iface->name);
 
 #ifdef WITH_UBUS
@@ -869,7 +869,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                        "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
                        inet_ntoa(dest.sin_addr));
        else
-               syslog(LOG_ERR, "Sent %s to %s - %s",
+               syslog(LOG_NOTICE, "Sent %s to %s - %s",
                        dhcpv4_msg_to_string(msg),
                        dest.sin_addr.s_addr == INADDR_BROADCAST ?
                        "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
@@ -917,8 +917,12 @@ static bool dhcpv4_assign(struct interface *iface, struct dhcp_assignment *a,
                }
 
                list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
-                       if (ntohl(c->addr) > ntohl(raddr)) {
-                               list_add_tail(&a->head, &c->head);
+                       if (ntohl(raddr) == ntohl(c->addr))
+                               break;
+
+                       if (ntohl(c->addr) > ntohl(raddr) || list_is_last(&c->head, &iface->dhcpv4_assignments)) {
+                               list_add_tail(&a->head,
+                                             ntohl(c->addr) > ntohl(raddr) ? &c->head : &iface->dhcpv4_assignments);
 raddr_out:
                                a->addr = raddr;
 
@@ -927,8 +931,7 @@ raddr_out:
                                       ((uint8_t *)&a->addr)[2], ((uint8_t *)&a->addr)[3]);
 
                                return true;
-                       } else if (ntohl(raddr) == ntohl(c->addr))
-                               break;
+                       }
                }
        }
 
@@ -959,18 +962,21 @@ raddr_out:
                }
 
                list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
-                       if (ntohl(c->addr) > try) {
-                               list_add_tail(&a->head, &c->head);
+                       if (try == ntohl(c->addr))
+                               break;
+
+                       if (ntohl(c->addr) > try || list_is_last(&c->head, &iface->dhcpv4_assignments)) {
+                               list_add_tail(&a->head,
+                                             ntohl(c->addr) > try ? &c->head : &iface->dhcpv4_assignments);
                                a->addr = htonl(try);
 
-                               syslog(LOG_DEBUG, "Assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
+                               syslog(LOG_INFO, "Assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
                                        ((uint8_t *)&a->addr)[0], ((uint8_t *)&a->addr)[1],
                                        ((uint8_t *)&a->addr)[2], ((uint8_t *)&a->addr)[3],
                                        i, count);
 
                                return true;
-                       } else if (try == ntohl(c->addr))
-                               break;
+                       }
                }
        }