Contents of /trunk/mkinitrd-magellan/busybox/networking/udhcp/leases.c
Parent Directory | Revision Log
Revision 987 -
(show annotations)
(download)
Sun May 30 11:40:40 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 4394 byte(s)
Sun May 30 11:40:40 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 4394 byte(s)
-added upstream dhcpd patch
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * leases.c -- tools to manage DHCP leases |
4 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
5 | * |
6 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
7 | */ |
8 | |
9 | #include "common.h" |
10 | #include "dhcpd.h" |
11 | |
12 | |
13 | /* Find the oldest expired lease, NULL if there are no expired leases */ |
14 | static struct dyn_lease *oldest_expired_lease(void) |
15 | { |
16 | struct dyn_lease *oldest_lease = NULL; |
17 | leasetime_t oldest_time = time(NULL); |
18 | unsigned i; |
19 | |
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]; |
26 | } |
27 | } |
28 | return oldest_lease; |
29 | } |
30 | |
31 | |
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) |
34 | { |
35 | unsigned i, j; |
36 | |
37 | for (j = 0; j < 16 && !chaddr[j]; j++) |
38 | continue; |
39 | |
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) |
43 | ) { |
44 | memset(&g_leases[i], 0, sizeof(g_leases[i])); |
45 | } |
46 | } |
47 | } |
48 | |
49 | |
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, |
54 | const char *hostname, int hostname_len) |
55 | { |
56 | struct dyn_lease *oldest; |
57 | |
58 | /* clean out any old ones */ |
59 | clear_lease(chaddr, yiaddr); |
60 | |
61 | oldest = oldest_expired_lease(); |
62 | |
63 | if (oldest) { |
64 | oldest->hostname[0] = '\0'; |
65 | if (hostname) { |
66 | char *p; |
67 | |
68 | hostname_len++; /* include NUL */ |
69 | if (hostname_len > sizeof(oldest->hostname)) |
70 | hostname_len = sizeof(oldest->hostname); |
71 | p = safe_strncpy(oldest->hostname, hostname, hostname_len); |
72 | /* sanitization (s/non-ASCII/^/g) */ |
73 | while (*p) { |
74 | if (*p < ' ' || *p > 126) |
75 | *p = '^'; |
76 | p++; |
77 | } |
78 | } |
79 | memcpy(oldest->lease_mac, chaddr, 6); |
80 | oldest->lease_nip = yiaddr; |
81 | oldest->expires = time(NULL) + leasetime; |
82 | } |
83 | |
84 | return oldest; |
85 | } |
86 | |
87 | |
88 | /* True if a lease has expired */ |
89 | int FAST_FUNC is_expired_lease(struct dyn_lease *lease) |
90 | { |
91 | return (lease->expires < (leasetime_t) time(NULL)); |
92 | } |
93 | |
94 | |
95 | /* Find the first lease that matches MAC, NULL if no match */ |
96 | struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac) |
97 | { |
98 | unsigned i; |
99 | |
100 | for (i = 0; i < server_config.max_leases; i++) |
101 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) |
102 | return &g_leases[i]; |
103 | |
104 | return NULL; |
105 | } |
106 | |
107 | |
108 | /* Find the first lease that matches IP, NULL is no match */ |
109 | struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip) |
110 | { |
111 | unsigned i; |
112 | |
113 | for (i = 0; i < server_config.max_leases; i++) |
114 | if (g_leases[i].lease_nip == nip) |
115 | return &g_leases[i]; |
116 | |
117 | return NULL; |
118 | } |
119 | |
120 | |
121 | /* Check if the IP is taken; if it is, add it to the lease table */ |
122 | static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac) |
123 | { |
124 | /* 16 zero bytes */ |
125 | static const uint8_t blank_chaddr[16] = { 0 }; |
126 | /* = { 0 } helps gcc to put it in rodata, not bss */ |
127 | |
128 | struct in_addr temp; |
129 | int r; |
130 | |
131 | r = arpping(nip, safe_mac, |
132 | server_config.server_nip, |
133 | server_config.server_mac, |
134 | server_config.interface); |
135 | if (r) |
136 | return r; |
137 | |
138 | temp.s_addr = nip; |
139 | bb_info_msg("%s belongs to someone, reserving it for %u seconds", |
140 | inet_ntoa(temp), (unsigned)server_config.conflict_time); |
141 | add_lease(blank_chaddr, nip, server_config.conflict_time, NULL, 0); |
142 | return 0; |
143 | } |
144 | |
145 | |
146 | /* Find a new usable (we think) address */ |
147 | uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac) |
148 | { |
149 | uint32_t addr; |
150 | struct dyn_lease *oldest_lease = NULL; |
151 | |
152 | addr = server_config.start_ip; /* addr is in host order here */ |
153 | for (; addr <= server_config.end_ip; addr++) { |
154 | uint32_t nip; |
155 | struct dyn_lease *lease; |
156 | |
157 | /* ie, 192.168.55.0 */ |
158 | if ((addr & 0xff) == 0) |
159 | continue; |
160 | /* ie, 192.168.55.255 */ |
161 | if ((addr & 0xff) == 0xff) |
162 | continue; |
163 | nip = htonl(addr); |
164 | /* is this a static lease addr? */ |
165 | if (is_nip_reserved(server_config.static_leases, nip)) |
166 | continue; |
167 | |
168 | lease = find_lease_by_nip(nip); |
169 | if (!lease) { |
170 | if (nobody_responds_to_arp(nip, safe_mac)) |
171 | return nip; |
172 | } else { |
173 | if (!oldest_lease || lease->expires < oldest_lease->expires) |
174 | oldest_lease = lease; |
175 | } |
176 | } |
177 | |
178 | if (oldest_lease && is_expired_lease(oldest_lease) |
179 | && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac) |
180 | ) { |
181 | return oldest_lease->lease_nip; |
182 | } |
183 | |
184 | return 0; |
185 | } |