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(const uint8_t *chaddr, uint32_t yiaddr, leasetime_t leasetime)
53 struct dhcpOfferedAddr *oldest;
55 /* clean out any old ones */
56 clear_lease(chaddr, yiaddr);
58 oldest = oldest_expired_lease();
61 memcpy(oldest->chaddr, chaddr, 16);
62 oldest->yiaddr = yiaddr;
63 oldest->expires = time(NULL) + leasetime;
70 /* True if a lease has expired */
71 int FAST_FUNC lease_expired(struct dhcpOfferedAddr *lease)
73 return (lease->expires < (leasetime_t) time(NULL));
77 /* Find the first lease that matches chaddr, NULL if no match */
78 struct dhcpOfferedAddr* FAST_FUNC find_lease_by_chaddr(const uint8_t *chaddr)
82 for (i = 0; i < server_config.max_leases; i++)
83 if (!memcmp(leases[i].chaddr, chaddr, 16))
90 /* Find the first lease that matches yiaddr, NULL is no match */
91 struct dhcpOfferedAddr* FAST_FUNC find_lease_by_yiaddr(uint32_t yiaddr)
95 for (i = 0; i < server_config.max_leases; i++)
96 if (leases[i].yiaddr == yiaddr)
103 /* check is an IP is taken, if it is, add it to the lease table */
104 static int nobody_responds_to_arp(uint32_t addr)
107 static const uint8_t blank_chaddr[16] = { 0 };
108 /* = { 0 } helps gcc to put it in rodata, not bss */
113 r = arpping(addr, server_config.server, server_config.arp, server_config.interface);
118 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
119 inet_ntoa(temp), (unsigned)server_config.conflict_time);
120 add_lease(blank_chaddr, addr, server_config.conflict_time);
125 /* Find a new usable (we think) address. */
126 uint32_t FAST_FUNC find_free_or_expired_address(void)
129 struct dhcpOfferedAddr *oldest_lease = NULL;
131 addr = server_config.start_ip; /* addr is in host order here */
132 for (; addr <= server_config.end_ip; addr++) {
134 struct dhcpOfferedAddr *lease;
136 /* ie, 192.168.55.0 */
137 if ((addr & 0xff) == 0)
139 /* ie, 192.168.55.255 */
140 if ((addr & 0xff) == 0xff)
142 net_addr = htonl(addr);
143 /* addr has a static lease? */
144 if (reservedIp(server_config.static_leases, net_addr))
147 lease = find_lease_by_yiaddr(net_addr);
149 if (nobody_responds_to_arp(net_addr))
152 if (!oldest_lease || lease->expires < oldest_lease->expires)
153 oldest_lease = lease;
157 if (oldest_lease && lease_expired(oldest_lease)
158 && nobody_responds_to_arp(oldest_lease->yiaddr)
160 return oldest_lease->yiaddr;