treewide: align function naming
[project/odhcpd.git] / src / dhcpv4.c
index 12e6336..bfe70cb 100644 (file)
 
 static int setup_dhcpv4_addresses(struct interface *iface);
 static void update_static_assignments(struct interface *iface);
+static void valid_until_cb(struct uloop_timeout *event);
 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a);
 static void dhcpv4_fr_start(struct dhcpv4_assignment *a);
 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a);
 static void handle_dhcpv4(void *addr, void *data, size_t len,
                struct interface *iface, void *dest_addr);
 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
-               enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
+               enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
                uint32_t *leasetime, const char *hostname, const size_t hostname_len,
                const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid);
+
+static struct uloop_timeout valid_until_timeout = {.cb = valid_until_cb};
 static uint32_t serial = 0;
 
 struct odhcpd_ref_ip {
@@ -55,12 +58,13 @@ struct odhcpd_ref_ip {
 };
 
 /* Create socket and register events */
-int init_dhcpv4(void)
+int dhcpv4_init(void)
 {
+       uloop_timeout_set(&valid_until_timeout, 1000);
        return 0;
 }
 
-int setup_dhcpv4_interface(struct interface *iface, bool enable)
+int dhcpv4_setup_interface(struct interface *iface, bool enable)
 {
        if (iface->dhcpv4_event.uloop.fd > 0) {
                uloop_fd_delete(&iface->dhcpv4_event.uloop);
@@ -323,6 +327,23 @@ static bool leases_require_fr(struct interface *iface, struct odhcpd_ipaddr *add
        return fr_ip ? true : false;
 }
 
+static void valid_until_cb(struct uloop_timeout *event)
+{
+       time_t now = odhcpd_time();
+       struct interface *iface;
+       list_for_each_entry(iface, &interfaces, head) {
+               if (iface->dhcpv4 != MODE_SERVER || iface->dhcpv4_assignments.next == NULL)
+                       continue;
+
+               struct dhcpv4_assignment *a, *n;
+               list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
+                       if (!INFINITE_VALID(a->valid_until) && a->valid_until < now)
+                               free_dhcpv4_assignment(a);
+               }
+       }
+       uloop_timeout_set(event, 1000);
+}
+
 void dhcpv4_addr_update(struct interface *iface)
 {
        if (iface->dhcpv4 == MODE_DISABLED)
@@ -333,7 +354,7 @@ void dhcpv4_addr_update(struct interface *iface)
        struct dhcpv4_assignment *c;
        uint32_t mask = iface->dhcpv4_mask.s_addr;
 
-       memset(&ip, sizeof(ip), 0);
+       memset(&ip, 0, sizeof(ip));
        ip.addr.in = iface->dhcpv4_local;
        ip.prefix = odhcpd_netmask2bitlen(false, &iface->dhcpv4_mask);
        ip.broadcast = iface->dhcpv4_bcast;
@@ -581,7 +602,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
        uint8_t reqmsg = DHCPV4_MSG_REQUEST;
        uint8_t msg = DHCPV4_MSG_ACK;
 
-       struct in_addr reqaddr = {INADDR_ANY};
+       uint32_t reqaddr = INADDR_ANY;
        uint32_t leasetime = 0;
        size_t hostname_len = 0;
        char hostname[256];
@@ -645,7 +666,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
        } else if (reqmsg == DHCPV4_MSG_DISCOVER)
                msg = DHCPV4_MSG_OFFER;
        else if (reqmsg == DHCPV4_MSG_REQUEST &&
-                       ((reqaddr.s_addr && reqaddr.s_addr != lease->addr) ||
+                       ((reqaddr && reqaddr != lease->addr) ||
                         (req->ciaddr.s_addr && req->ciaddr.s_addr != lease->addr))) {
                msg = DHCPV4_MSG_NAK;
                /*
@@ -831,17 +852,6 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                        (struct sockaddr*)&dest, sizeof(dest));
 }
 
-static bool dhcpv4_test(struct interface *iface, uint32_t try)
-{
-       struct dhcpv4_assignment *c;
-       list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
-               if (c->addr == try)
-                       return false;
-       }
-
-       return true;
-}
-
 static bool dhcpv4_assign(struct interface *iface,
                struct dhcpv4_assignment *assign, uint32_t raddr)
 {
@@ -850,7 +860,8 @@ static bool dhcpv4_assign(struct interface *iface,
        uint32_t count = end - start + 1;
 
        // try to assign the IP the client asked for
-       if (start <= ntohl(raddr) && ntohl(raddr) <= end && dhcpv4_test(iface, raddr)) {
+       if (start <= ntohl(raddr) && ntohl(raddr) <= end &&
+                       !find_assignment_by_addr(iface, raddr)) {
                assign->addr = raddr;
                syslog(LOG_INFO, "assigning the IP the client asked for: %u.%u.%u.%u",
                                ((uint8_t *)&assign->addr)[0],
@@ -883,7 +894,7 @@ static bool dhcpv4_assign(struct interface *iface,
        }
 
        for (uint32_t i = 0; i < count; ++i) {
-               if (dhcpv4_test(iface, htonl(try))) {
+               if (!find_assignment_by_addr(iface, htonl(try))) {
                        /* test was successful: IP address is not assigned, assign it */
                        assign->addr = htonl(try);
                        syslog(LOG_DEBUG, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
@@ -903,23 +914,14 @@ static bool dhcpv4_assign(struct interface *iface,
 
 
 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
-               enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
+               enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
                uint32_t *leasetime, const char *hostname, const size_t hostname_len,
                const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid)
 {
+       struct dhcpv4_assignment *a = find_assignment_by_hwaddr(iface, mac);
        struct dhcpv4_assignment *lease = NULL;
        time_t now = odhcpd_time();
 
-       struct dhcpv4_assignment *c, *n, *a = NULL;
-       list_for_each_entry_safe(c, n, &iface->dhcpv4_assignments, head) {
-               if (!memcmp(c->hwaddr, mac, 6)) {
-                       a = c;
-                       if (c->addr == reqaddr.s_addr)
-                               break;
-               } else if (!INFINITE_VALID(c->valid_until) && c->valid_until < now)
-                       free_dhcpv4_assignment(c);
-       }
-
        if (a && (a->flags & OAF_BOUND) && a->fr_ip) {
                *fr_serverid = a->fr_ip->addr.addr.in.s_addr;
                dhcpv4_fr_stop(a);
@@ -941,7 +943,7 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                                /* Don't consider new assignment as infinite */
                                a->valid_until = now;
 
-                               assigned = dhcpv4_assign(iface, a, reqaddr.s_addr);
+                               assigned = dhcpv4_assign(iface, a, reqaddr);
                                if (assigned) {
                                        a->iface = iface;
                                        list_add(&a->head, &iface->dhcpv4_assignments);
@@ -951,7 +953,7 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                                (iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr)) {
                        list_del(&a->head);
 
-                       assigned = dhcpv4_assign(iface, a, reqaddr.s_addr);
+                       assigned = dhcpv4_assign(iface, a, reqaddr);
                        if (assigned)
                                list_add(&a->head, &iface->dhcpv4_assignments);
                }