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 dhcpOfferedAddr *oldest_expired_lease(void)
16 struct dhcpOfferedAddr *oldest_lease = NULL;
17 leasetime_t oldest_time = time(NULL);
20 /* Unexpired leases have leases[i].expires >= current time
21 * and therefore can't ever match */
22 for (i = 0; i < server_config.max_leases; i++) {
23 if (leases[i].expires < oldest_time) {
24 oldest_time = leases[i].expires;
25 oldest_lease = &(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(leases[i].chaddr, chaddr, 16) == 0)
42 || (yiaddr && leases[i].yiaddr == yiaddr)
44 memset(&(leases[i]), 0, sizeof(leases[i]));
50 /* Add a lease into the table, clearing out any old ones */
51 struct dhcpOfferedAddr* FAST_FUNC add_lease(
52 const uint8_t *chaddr, uint32_t yiaddr,
53 leasetime_t leasetime, uint8_t *hostname)
55 struct dhcpOfferedAddr *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->chaddr, chaddr, 16);
79 oldest->yiaddr = yiaddr;
80 oldest->expires = time(NULL) + leasetime;
87 /* True if a lease has expired */
88 int FAST_FUNC lease_expired(struct dhcpOfferedAddr *lease)
90 return (lease->expires < (leasetime_t) time(NULL));
94 /* Find the first lease that matches chaddr, NULL if no match */
95 struct dhcpOfferedAddr* FAST_FUNC find_lease_by_chaddr(const uint8_t *chaddr)
99 for (i = 0; i < server_config.max_leases; i++)
100 if (!memcmp(leases[i].chaddr, chaddr, 16))
107 /* Find the first lease that matches yiaddr, NULL is no match */
108 struct dhcpOfferedAddr* FAST_FUNC find_lease_by_yiaddr(uint32_t yiaddr)
112 for (i = 0; i < server_config.max_leases; i++)
113 if (leases[i].yiaddr == yiaddr)
120 /* check is an IP is taken, if it is, add it to the lease table */
121 static int nobody_responds_to_arp(uint32_t addr)
124 static const uint8_t blank_chaddr[16] = { 0 };
125 /* = { 0 } helps gcc to put it in rodata, not bss */
130 r = arpping(addr, server_config.server, server_config.arp, server_config.interface);
135 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
136 inet_ntoa(temp), (unsigned)server_config.conflict_time);
137 add_lease(blank_chaddr, addr, server_config.conflict_time, NULL);
142 /* Find a new usable (we think) address. */
143 uint32_t FAST_FUNC find_free_or_expired_address(void)
146 struct dhcpOfferedAddr *oldest_lease = NULL;
148 addr = server_config.start_ip; /* addr is in host order here */
149 for (; addr <= server_config.end_ip; addr++) {
151 struct dhcpOfferedAddr *lease;
153 /* ie, 192.168.55.0 */
154 if ((addr & 0xff) == 0)
156 /* ie, 192.168.55.255 */
157 if ((addr & 0xff) == 0xff)
159 net_addr = htonl(addr);
160 /* addr has a static lease? */
161 if (reservedIp(server_config.static_leases, net_addr))
164 lease = find_lease_by_yiaddr(net_addr);
166 if (nobody_responds_to_arp(net_addr))
169 if (!oldest_lease || lease->expires < oldest_lease->expires)
170 oldest_lease = lease;
174 if (oldest_lease && lease_expired(oldest_lease)
175 && nobody_responds_to_arp(oldest_lease->yiaddr)
177 return oldest_lease->yiaddr;