source: MondoRescue/branches/3.3/mindi-busybox/networking/udhcp/leases.c@ 3621

Last change on this file since 3621 was 3621, checked in by Bruno Cornec, 7 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

File size: 5.3 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]2/*
3 * Russ Dill <Russ.Dill@asu.edu> July 2001
[2725]4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
[821]6 */
[1765]7#include "common.h"
[821]8#include "dhcpd.h"
9
[1765]10/* Find the oldest expired lease, NULL if there are no expired leases */
[2725]11static struct dyn_lease *oldest_expired_lease(void)
[1765]12{
[2725]13 struct dyn_lease *oldest_lease = NULL;
14 leasetime_t oldest_time = time(NULL);
[1765]15 unsigned i;
[821]16
[2725]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++) {
[3621]20 if (g_leases[i].expires == 0 /* empty entry */
21 || g_leases[i].expires < oldest_time
22 ) {
[2725]23 oldest_time = g_leases[i].expires;
24 oldest_lease = &g_leases[i];
[1765]25 }
[2725]26 }
27 return oldest_lease;
[1765]28}
[821]29
[2725]30/* Clear out all leases with matching nonzero chaddr OR yiaddr.
31 * If chaddr == NULL, this is a conflict lease.
32 */
33static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
[821]34{
[2725]35 unsigned i;
[821]36
[2725]37 for (i = 0; i < server_config.max_leases; i++) {
38 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
39 || (yiaddr && g_leases[i].lease_nip == yiaddr)
[1765]40 ) {
[2725]41 memset(&g_leases[i], 0, sizeof(g_leases[i]));
[821]42 }
[2725]43 }
[821]44}
45
[2725]46/* Add a lease into the table, clearing out any old ones.
47 * If chaddr == NULL, this is a conflict lease.
48 */
49struct dyn_lease* FAST_FUNC add_lease(
50 const uint8_t *chaddr, uint32_t yiaddr,
51 leasetime_t leasetime,
52 const char *hostname, int hostname_len)
[821]53{
[2725]54 struct dyn_lease *oldest;
[821]55
56 /* clean out any old ones */
[2725]57 clear_leases(chaddr, yiaddr);
[821]58
59 oldest = oldest_expired_lease();
60
61 if (oldest) {
[2725]62 memset(oldest, 0, sizeof(*oldest));
63 if (hostname) {
64 char *p;
65
66 hostname_len++; /* include NUL */
67 if (hostname_len > sizeof(oldest->hostname))
68 hostname_len = sizeof(oldest->hostname);
69 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
[3621]70 /*
71 * Sanitization (s/bad_char/./g).
72 * The intent is not to allow only "DNS-valid" hostnames,
73 * but merely make dumpleases output safe for shells to use.
74 * We accept "0-9A-Za-z._-", all other chars turn to dots.
75 */
[2725]76 while (*p) {
[3621]77 if (!isalnum(*p) && *p != '-' && *p != '_')
78 *p = '.';
[2725]79 p++;
80 }
81 }
82 if (chaddr)
83 memcpy(oldest->lease_mac, chaddr, 6);
84 oldest->lease_nip = yiaddr;
85 oldest->expires = time(NULL) + leasetime;
[821]86 }
87
88 return oldest;
89}
90
[2725]91/* True if a lease has expired */
92int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
[821]93{
[2725]94 return (lease->expires < (leasetime_t) time(NULL));
[821]95}
96
[2725]97/* Find the first lease that matches MAC, NULL if no match */
98struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
[821]99{
[1765]100 unsigned i;
[821]101
102 for (i = 0; i < server_config.max_leases; i++)
[2725]103 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
104 return &g_leases[i];
[821]105
106 return NULL;
107}
108
[2725]109/* Find the first lease that matches IP, NULL is no match */
110struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
[821]111{
[1765]112 unsigned i;
[821]113
114 for (i = 0; i < server_config.max_leases; i++)
[2725]115 if (g_leases[i].lease_nip == nip)
116 return &g_leases[i];
[821]117
118 return NULL;
119}
120
[2725]121/* Check if the IP is taken; if it is, add it to the lease table */
[3621]122static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms)
[821]123{
124 struct in_addr temp;
[1765]125 int r;
[821]126
[2725]127 r = arpping(nip, safe_mac,
128 server_config.server_nip,
129 server_config.server_mac,
[3621]130 server_config.interface,
131 arpping_ms);
[1765]132 if (r)
133 return r;
134
[2725]135 temp.s_addr = nip;
[3621]136 bb_error_msg("%s belongs to someone, reserving it for %u seconds",
[1765]137 inet_ntoa(temp), (unsigned)server_config.conflict_time);
[2725]138 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
[1765]139 return 0;
[821]140}
141
[2725]142/* Find a new usable (we think) address */
[3621]143uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms)
[821]144{
[2725]145 uint32_t addr;
146 struct dyn_lease *oldest_lease = NULL;
[821]147
[3232]148#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
149 uint32_t stop;
150 unsigned i, hash;
151
152 /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
153 * dispersal even with similarly-valued "strings".
154 */
155 hash = 0;
156 for (i = 0; i < 6; i++)
157 hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
158
159 /* pick a seed based on hwaddr then iterate until we find a free address. */
160 addr = server_config.start_ip
161 + (hash % (1 + server_config.end_ip - server_config.start_ip));
162 stop = addr;
163#else
164 addr = server_config.start_ip;
165#define stop (server_config.end_ip + 1)
166#endif
167 do {
[2725]168 uint32_t nip;
169 struct dyn_lease *lease;
170
[821]171 /* ie, 192.168.55.0 */
[2725]172 if ((addr & 0xff) == 0)
[3232]173 goto next_addr;
[821]174 /* ie, 192.168.55.255 */
[2725]175 if ((addr & 0xff) == 0xff)
[3232]176 goto next_addr;
[2725]177 nip = htonl(addr);
[3232]178 /* skip our own address */
179 if (nip == server_config.server_nip)
180 goto next_addr;
[2725]181 /* is this a static lease addr? */
182 if (is_nip_reserved(server_config.static_leases, nip))
[3232]183 goto next_addr;
[2725]184
185 lease = find_lease_by_nip(nip);
186 if (!lease) {
187//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
[3621]188 if (nobody_responds_to_arp(nip, safe_mac, arpping_ms))
[2725]189 return nip;
190 } else {
191 if (!oldest_lease || lease->expires < oldest_lease->expires)
192 oldest_lease = lease;
[821]193 }
[2725]194
[3232]195 next_addr:
196 addr++;
197#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
198 if (addr > server_config.end_ip)
199 addr = server_config.start_ip;
200#endif
201 } while (addr != stop);
202
[2725]203 if (oldest_lease
204 && is_expired_lease(oldest_lease)
[3621]205 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms)
[2725]206 ) {
207 return oldest_lease->lease_nip;
208 }
209
[821]210 return 0;
211}
Note: See TracBrowser for help on using the repository browser.