1 /* vi: set sw=4 ts=4: */
3 * Russ Dill <Russ.Dill@asu.edu> July 2001
5 * Licensed under GPLv2, see file LICENSE in this tarball for details.
10 /* Find the oldest expired lease, NULL if there are no expired leases */
11 static struct dyn_lease *oldest_expired_lease(void)
13 struct dyn_lease *oldest_lease = NULL;
14 leasetime_t oldest_time = time(NULL);
17 /* Unexpired leases have g_leases[i].expires >= current time
18 * and therefore can't ever match */
19 for (i = 0; i < server_config.max_leases; i++) {
20 if (g_leases[i].expires < oldest_time) {
21 oldest_time = g_leases[i].expires;
22 oldest_lease = &g_leases[i];
28 /* Clear out all leases with matching nonzero chaddr OR yiaddr.
29 * If chaddr == NULL, this is a conflict lease.
31 static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
35 for (i = 0; i < server_config.max_leases; i++) {
36 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
37 || (yiaddr && g_leases[i].lease_nip == yiaddr)
39 memset(&g_leases[i], 0, sizeof(g_leases[i]));
44 /* Add a lease into the table, clearing out any old ones.
45 * If chaddr == NULL, this is a conflict lease.
47 struct dyn_lease* FAST_FUNC add_lease(
48 const uint8_t *chaddr, uint32_t yiaddr,
49 leasetime_t leasetime,
50 const char *hostname, int hostname_len)
52 struct dyn_lease *oldest;
54 /* clean out any old ones */
55 clear_leases(chaddr, yiaddr);
57 oldest = oldest_expired_lease();
60 memset(oldest, 0, sizeof(*oldest));
64 hostname_len++; /* include NUL */
65 if (hostname_len > sizeof(oldest->hostname))
66 hostname_len = sizeof(oldest->hostname);
67 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
68 /* sanitization (s/non-ASCII/^/g) */
70 if (*p < ' ' || *p > 126)
76 memcpy(oldest->lease_mac, chaddr, 6);
77 oldest->lease_nip = yiaddr;
78 oldest->expires = time(NULL) + leasetime;
84 /* True if a lease has expired */
85 int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
87 return (lease->expires < (leasetime_t) time(NULL));
90 /* Find the first lease that matches MAC, NULL if no match */
91 struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
95 for (i = 0; i < server_config.max_leases; i++)
96 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
102 /* Find the first lease that matches IP, NULL is no match */
103 struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
107 for (i = 0; i < server_config.max_leases; i++)
108 if (g_leases[i].lease_nip == nip)
114 /* Check if the IP is taken; if it is, add it to the lease table */
115 static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
120 r = arpping(nip, safe_mac,
121 server_config.server_nip,
122 server_config.server_mac,
123 server_config.interface);
128 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
129 inet_ntoa(temp), (unsigned)server_config.conflict_time);
130 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
134 /* Find a new usable (we think) address */
135 uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
138 struct dyn_lease *oldest_lease = NULL;
140 addr = server_config.start_ip; /* addr is in host order here */
141 for (; addr <= server_config.end_ip; addr++) {
143 struct dyn_lease *lease;
145 /* ie, 192.168.55.0 */
146 if ((addr & 0xff) == 0)
148 /* ie, 192.168.55.255 */
149 if ((addr & 0xff) == 0xff)
152 /* is this a static lease addr? */
153 if (is_nip_reserved(server_config.static_leases, nip))
156 lease = find_lease_by_nip(nip);
158 //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
159 if (nobody_responds_to_arp(nip, safe_mac))
162 if (!oldest_lease || lease->expires < oldest_lease->expires)
163 oldest_lease = lease;
168 && is_expired_lease(oldest_lease)
169 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
171 return oldest_lease->lease_nip;