1 /* vi: set sw=4 ts=4: */
3 * leases.c -- tools to manage DHCP leases
4 * Russ Dill <Russ.Dill@asu.edu> July 2001
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
13 /* Find the oldest expired lease, NULL if there are no expired leases */
14 static struct dyn_lease *oldest_expired_lease(void)
16 struct dyn_lease *oldest_lease = NULL;
17 leasetime_t oldest_time = time(NULL);
20 /* Unexpired leases have g_leases[i].expires >= current time
21 * and therefore can't ever match */
22 for (i = 0; i < server_config.max_leases; i++) {
23 if (g_leases[i].expires < oldest_time) {
24 oldest_time = g_leases[i].expires;
25 oldest_lease = &g_leases[i];
32 /* Clear every lease out that chaddr OR yiaddr matches and is nonzero */
33 static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
37 for (j = 0; j < 16 && !chaddr[j]; j++)
40 for (i = 0; i < server_config.max_leases; i++) {
41 if ((j != 16 && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
42 || (yiaddr && g_leases[i].lease_nip == yiaddr)
44 memset(&g_leases[i], 0, sizeof(g_leases[i]));
50 /* Add a lease into the table, clearing out any old ones */
51 struct dyn_lease* FAST_FUNC add_lease(
52 const uint8_t *chaddr, uint32_t yiaddr,
53 leasetime_t leasetime, uint8_t *hostname)
55 struct dyn_lease *oldest;
56 uint8_t hostname_length;
58 /* clean out any old ones */
59 clear_lease(chaddr, yiaddr);
61 oldest = oldest_expired_lease();
64 oldest->hostname[0] = '\0';
66 /* option size byte, + 1 for NUL */
67 hostname_length = hostname[-1] + 1;
68 if (hostname_length > sizeof(oldest->hostname))
69 hostname_length = sizeof(oldest->hostname);
70 hostname = (uint8_t*) safe_strncpy((char*)oldest->hostname, (char*)hostname, hostname_length);
71 /* sanitization (s/non-ASCII/^/g) */
73 if (*hostname < ' ' || *hostname > 126)
78 memcpy(oldest->lease_mac, chaddr, 6);
79 oldest->lease_nip = yiaddr;
80 oldest->expires = time(NULL) + leasetime;
87 /* True if a lease has expired */
88 int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
90 return (lease->expires < (leasetime_t) time(NULL));
94 /* Find the first lease that matches MAC, NULL if no match */
95 struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
99 for (i = 0; i < server_config.max_leases; i++)
100 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
107 /* Find the first lease that matches IP, NULL is no match */
108 struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
112 for (i = 0; i < server_config.max_leases; i++)
113 if (g_leases[i].lease_nip == nip)
120 /* Check if the IP is taken; if it is, add it to the lease table */
121 static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
124 static const uint8_t blank_chaddr[16] = { 0 };
125 /* = { 0 } helps gcc to put it in rodata, not bss */
130 r = arpping(nip, safe_mac,
131 server_config.server_nip,
132 server_config.server_mac,
133 server_config.interface);
138 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
139 inet_ntoa(temp), (unsigned)server_config.conflict_time);
140 add_lease(blank_chaddr, nip, server_config.conflict_time, NULL);
145 /* Find a new usable (we think) address */
146 uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
149 struct dyn_lease *oldest_lease = NULL;
151 addr = server_config.start_ip; /* addr is in host order here */
152 for (; addr <= server_config.end_ip; addr++) {
154 struct dyn_lease *lease;
156 /* ie, 192.168.55.0 */
157 if ((addr & 0xff) == 0)
159 /* ie, 192.168.55.255 */
160 if ((addr & 0xff) == 0xff)
163 /* is this a static lease addr? */
164 if (is_nip_reserved(server_config.static_leases, nip))
167 lease = find_lease_by_nip(nip);
169 if (nobody_responds_to_arp(nip, safe_mac))
172 if (!oldest_lease || lease->expires < oldest_lease->expires)
173 oldest_lease = lease;
177 if (oldest_lease && is_expired_lease(oldest_lease)
178 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
180 return oldest_lease->lease_nip;