rework IPv6 address dump logic
[project/odhcpd.git] / src / dhcpv4.c
index 2381cb1..81e537d 100644 (file)
@@ -40,7 +40,7 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
                uint32_t *leasetime, const char *hostname, const size_t hostname_len);
 
-// Create socket and register events
+/* Create socket and register events */
 int init_dhcpv4(void)
 {
        return 0;
@@ -65,7 +65,7 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        return -1;
                }
 
-               // Basic IPv6 configuration
+               /* Basic IPv4 configuration */
                int val = 1;
                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
                setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
@@ -95,7 +95,7 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        return -1;
                }
 
-               // Create a range if not specified
+               /* Create a range if not specified */
                struct ifreq ifreq;
                strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
 
@@ -142,18 +142,18 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
 
                }
 
-               // Parse static entries
+               /* Parse static entries */
                struct lease *lease;
                list_for_each_entry(lease, &leases, head) {
-                       // Construct entry
+                       /* Construct entry */
                        struct dhcpv4_assignment *a = calloc(1, sizeof(*a));
                        if (!a) {
                                syslog(LOG_ERR, "Calloc failed for static lease on interface %s",
                                        iface->ifname);
                                return -1;
                        }
-                       if (lease->dhcpv4_leasetime >= 60)
-                               a->leasetime = lease->dhcpv4_leasetime;
+
+                       a->leasetime = lease->dhcpv4_leasetime;
 
                        a->addr = ntohl(lease->ipaddr.s_addr);
                        memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
@@ -162,20 +162,19 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        /* Infinite valid */
                        a->valid_until = 0;
 
-                       // Assign to all interfaces
+                       /* Assign to all interfaces */
                        struct dhcpv4_assignment *c;
                        list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
                                if (c->addr > a->addr) {
                                        list_add_tail(&a->head, &c->head);
                                        break;
-                               } else if (c->addr == a->addr) {
-                                       // Already an assignment with that number
+                               } else if (c->addr == a->addr)
+                                       /* Already an assignment with that number */
                                        break;
-                               }
                        }
-                       if (&c->head == &iface->dhcpv4_assignments) {
+
+                       if (&c->head == &iface->dhcpv4_assignments)
                                list_add(&a->head, &iface->dhcpv4_assignments);
-                       }
 
                        if (a->head.next) {
                                if (lease->hostname[0]) {
@@ -194,10 +193,6 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                                free_dhcpv4_assignment(a);
                }
 
-
-               if (iface->dhcpv4_leasetime < 60)
-                       iface->dhcpv4_leasetime = 43200;
-
                iface->dhcpv4_event.uloop.fd = sock;
                iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
                odhcpd_register(&iface->dhcpv4_event);
@@ -329,15 +324,15 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
        uint8_t *end = ((uint8_t*)data) + len;
        struct dhcpv4_option *opt;
        dhcpv4_for_each_option(start, end, opt) {
-               if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1) {
+               if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1)
                        reqmsg = opt->data[0];
-               else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
+               else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
                        hostname_len = opt->len;
                        memcpy(hostname, opt->data, hostname_len);
                        hostname[hostname_len] = 0;
-               } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) {
+               } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4)
                        memcpy(&reqaddr, opt->data, 4);
-               else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
+               else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
                        if (memcmp(opt->data, &ifaddr.sin_addr, 4))
                                return;
                } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
@@ -366,9 +361,9 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                        msg = DHCPV4_MSG_NAK;
                else if (reqmsg == DHCPV4_MSG_DISCOVER)
                        return;
-       } else if (reqmsg == DHCPV4_MSG_DISCOVER) {
+       } else if (reqmsg == DHCPV4_MSG_DISCOVER)
                msg = DHCPV4_MSG_OFFER;
-       else if (reqmsg == DHCPV4_MSG_REQUEST && reqaddr.s_addr &&
+       else if (reqmsg == DHCPV4_MSG_REQUEST && reqaddr.s_addr &&
                        reqaddr.s_addr != htonl(lease->addr)) {
                msg = DHCPV4_MSG_NAK;
                /*
@@ -427,10 +422,10 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
        }
 
-       if (iface->search && iface->search_len <= 255) {
+       if (iface->search && iface->search_len <= 255)
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
                                iface->search_len, iface->search);
-       else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
+       else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
                uint8_t search_buf[256];
                int len = dn_comp(_res.dnsrch[0], search_buf,
                                                sizeof(search_buf), NULL, NULL);
@@ -497,14 +492,12 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                ioctl(sock, SIOCSARP, &arp);
        }
 
-       if (dest.sin_addr.s_addr == INADDR_BROADCAST) {
-               /*
-                * reply goes to IP broadcast -> MAC broadcast
-                */
+       if (dest.sin_addr.s_addr == INADDR_BROADCAST)
+               /* reply goes to IP broadcast -> MAC broadcast */
                syslog(LOG_WARNING, "sending %s to ff:ff:ff:ff:ff:ff - %s",
                                dhcpv4_msg_to_string(msg),
                                inet_ntoa(dest.sin_addr));
-       } else {
+       else
                /*
                 * reply is send directly to IP,
                 * MAC is assumed to be the same as the request
@@ -514,7 +507,6 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                                req->chaddr[0],req->chaddr[1],req->chaddr[2],
                                req->chaddr[3],req->chaddr[4],req->chaddr[5],
                                inet_ntoa(dest.sin_addr));
-       }
 
        sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
                        (struct sockaddr*)&dest, sizeof(dest));
@@ -524,10 +516,11 @@ 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) {
+               if (c->addr == try)
                        return false;
-               }
+
        }
+
        return true;
 }
 
@@ -542,7 +535,7 @@ static bool dhcpv4_assign(struct interface *iface,
        if (start <= raddr && raddr <= end && dhcpv4_test(iface, raddr)) {
                assign->addr = raddr;
                list_add(&assign->head, &iface->dhcpv4_assignments);
-               syslog(LOG_DEBUG, "assigning the IP the client asked for: %u.%u.%u.%u",
+               syslog(LOG_INFO, "assigning the IP the client asked for: %u.%u.%u.%u",
                                (assign->addr & 0xff000000) >> 24,
                                (assign->addr & 0x00ff0000) >> 16,
                                (assign->addr & 0x0000ff00) >> 8,
@@ -557,6 +550,7 @@ static bool dhcpv4_assign(struct interface *iface,
                uint8_t o = assign->hwaddr[i];
                seed += (o*2654435761) % UINT32_MAX;
        }
+
        srand(seed);
 
        uint32_t try = (((uint32_t)rand()) % count) + start;
@@ -564,7 +558,7 @@ static bool dhcpv4_assign(struct interface *iface,
        if (list_empty(&iface->dhcpv4_assignments)) {
                assign->addr = try;
                list_add(&assign->head, &iface->dhcpv4_assignments);
-               syslog(LOG_DEBUG, "assigning mapped IP (empty list): %u.%u.%u.%u",
+               syslog(LOG_INFO, "assigning mapped IP (empty list): %u.%u.%u.%u",
                                (assign->addr & 0xff000000) >> 24,
                                (assign->addr & 0x00ff0000) >> 16,
                                (assign->addr & 0x0000ff00) >> 8,
@@ -587,7 +581,7 @@ static bool dhcpv4_assign(struct interface *iface,
                try = (((try - start) + 1) % count) + start;
        }
 
-       syslog(LOG_DEBUG, "can't assign any IP address -> address space is full");
+       syslog(LOG_WARNING, "can't assign any IP address -> address space is full");
        return false;
 }
 
@@ -614,24 +608,24 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                bool assigned = !!a;
                uint32_t my_leasetime;
 
-               if (!a && !iface->no_dynamic_dhcp) { // Create new binding
+               if (!a && !iface->no_dynamic_dhcp) {
+                       /* Create new binding */
                        a = calloc(1, sizeof(*a));
                        if (!a) {
                                syslog(LOG_ERR, "Failed to calloc binding on interface %s", iface->ifname);
                                return NULL;
                        }
                        memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
-                       // Don't consider new assignment as infinite
+                       /* Don't consider new assignment as infinite */
                        a->valid_until = now;
 
                        assigned = dhcpv4_assign(iface, a, raddr);
                }
 
-               if (a->leasetime >= 60) {
+               if (a->leasetime)
                        my_leasetime = a->leasetime;
-               } else {
+               else
                        my_leasetime = iface->dhcpv4_leasetime;
-               }
 
                if ((*leasetime == 0) || (my_leasetime < *leasetime))
                        *leasetime = my_leasetime;
@@ -656,7 +650,8 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                                if (!(a->flags & OAF_STATIC))
                                        a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
                        }
-               } else if (!assigned && a) { // Cleanup failed assignment
+               } else if (!assigned && a) {
+                       /* Cleanup failed assignment */
                        free_dhcpv4_assignment(a);
                        a = NULL;
                }
@@ -674,7 +669,7 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
 
                if (!(a->flags & OAF_STATIC)) {
                        memset(a->hwaddr, 0, sizeof(a->hwaddr));
-                       a->valid_until = now + 3600; // Block address for 1h
+                       a->valid_until = now + 3600; /* Block address for 1h */
                }
        }
 
@@ -682,4 +677,3 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
 
        return lease;
 }
-