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 hostname_length = hostname[-1]; /* look at option size byte */
67 if (hostname_length > sizeof(oldest->hostname))
68 hostname_length = sizeof(oldest->hostname);
69 hostname = (uint8_t*) safe_strncpy((char*)oldest->hostname, (char*)hostname, hostname_length);
70 /* sanitization (s/non-ACSII/^/g) */
72 if (*hostname < ' ' || *hostname > 126)
77 memcpy(oldest->chaddr, chaddr, 16);
78 oldest->yiaddr = yiaddr;
79 oldest->expires = time(NULL) + leasetime;
86 /* True if a lease has expired */
87 int FAST_FUNC lease_expired(struct dhcpOfferedAddr *lease)
89 return (lease->expires < (leasetime_t) time(NULL));
93 /* Find the first lease that matches chaddr, NULL if no match */
94 struct dhcpOfferedAddr* FAST_FUNC find_lease_by_chaddr(const uint8_t *chaddr)
98 for (i = 0; i < server_config.max_leases; i++)
99 if (!memcmp(leases[i].chaddr, chaddr, 16))
106 /* Find the first lease that matches yiaddr, NULL is no match */
107 struct dhcpOfferedAddr* FAST_FUNC find_lease_by_yiaddr(uint32_t yiaddr)
111 for (i = 0; i < server_config.max_leases; i++)
112 if (leases[i].yiaddr == yiaddr)
119 /* check is an IP is taken, if it is, add it to the lease table */
120 static int nobody_responds_to_arp(uint32_t addr)
123 static const uint8_t blank_chaddr[16] = { 0 };
124 /* = { 0 } helps gcc to put it in rodata, not bss */
129 r = arpping(addr, server_config.server, server_config.arp, server_config.interface);
134 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
135 inet_ntoa(temp), (unsigned)server_config.conflict_time);
136 add_lease(blank_chaddr, addr, server_config.conflict_time, NULL);
141 /* Find a new usable (we think) address. */
142 uint32_t FAST_FUNC find_free_or_expired_address(void)
145 struct dhcpOfferedAddr *oldest_lease = NULL;
147 addr = server_config.start_ip; /* addr is in host order here */
148 for (; addr <= server_config.end_ip; addr++) {
150 struct dhcpOfferedAddr *lease;
152 /* ie, 192.168.55.0 */
153 if ((addr & 0xff) == 0)
155 /* ie, 192.168.55.255 */
156 if ((addr & 0xff) == 0xff)
158 net_addr = htonl(addr);
159 /* addr has a static lease? */
160 if (reservedIp(server_config.static_leases, net_addr))
163 lease = find_lease_by_yiaddr(net_addr);
165 if (nobody_responds_to_arp(net_addr))
168 if (!oldest_lease || lease->expires < oldest_lease->expires)
169 oldest_lease = lease;
173 if (oldest_lease && lease_expired(oldest_lease)
174 && nobody_responds_to_arp(oldest_lease->yiaddr)
176 return oldest_lease->yiaddr;