blob: f5113408b73ff87f9eaeebeebff6c1508b68e4df [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * leases.c -- tools to manage DHCP leases
4 * Russ Dill <Russ.Dill@asu.edu> July 2001
5 */
6
7#include <time.h>
8#include <string.h>
9#include <sys/socket.h>
10#include <netinet/in.h>
11#include <arpa/inet.h>
12
13#include "dhcpd.h"
14#include "files.h"
15#include "options.h"
16#include "leases.h"
17#include "arpping.h"
18#include "common.h"
19
20#include "static_leases.h"
21
22
23uint8_t blank_chaddr[] = {[0 ... 15] = 0};
24
25/* clear every lease out that chaddr OR yiaddr matches and is nonzero */
26void clear_lease(uint8_t *chaddr, uint32_t yiaddr)
27{
28 unsigned int i, j;
29
30 for (j = 0; j < 16 && !chaddr[j]; j++);
31
32 for (i = 0; i < server_config.max_leases; i++)
33 if ((j != 16 && !memcmp(leases[i].chaddr, chaddr, 16)) ||
34 (yiaddr && leases[i].yiaddr == yiaddr)) {
35 memset(&(leases[i]), 0, sizeof(struct dhcpOfferedAddr));
36 }
37}
38
39
40/* add a lease into the table, clearing out any old ones */
41struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
42{
43 struct dhcpOfferedAddr *oldest;
44
45 /* clean out any old ones */
46 clear_lease(chaddr, yiaddr);
47
48 oldest = oldest_expired_lease();
49
50 if (oldest) {
51 memcpy(oldest->chaddr, chaddr, 16);
52 oldest->yiaddr = yiaddr;
53 oldest->expires = time(0) + lease;
54 }
55
56 return oldest;
57}
58
59
60/* true if a lease has expired */
61int lease_expired(struct dhcpOfferedAddr *lease)
62{
63 return (lease->expires < (unsigned long) time(0));
64}
65
66
67/* Find the oldest expired lease, NULL if there are no expired leases */
68struct dhcpOfferedAddr *oldest_expired_lease(void)
69{
70 struct dhcpOfferedAddr *oldest = NULL;
71 unsigned long oldest_lease = time(0);
72 unsigned int i;
73
74
75 for (i = 0; i < server_config.max_leases; i++)
76 if (oldest_lease > leases[i].expires) {
77 oldest_lease = leases[i].expires;
78 oldest = &(leases[i]);
79 }
80 return oldest;
81
82}
83
84
85/* Find the first lease that matches chaddr, NULL if no match */
86struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr)
87{
88 unsigned int i;
89
90 for (i = 0; i < server_config.max_leases; i++)
91 if (!memcmp(leases[i].chaddr, chaddr, 16)) return &(leases[i]);
92
93 return NULL;
94}
95
96
97/* Find the first lease that matches yiaddr, NULL is no match */
98struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
99{
100 unsigned int i;
101
102 for (i = 0; i < server_config.max_leases; i++)
103 if (leases[i].yiaddr == yiaddr) return &(leases[i]);
104
105 return NULL;
106}
107
108
109/* check is an IP is taken, if it is, add it to the lease table */
110static int check_ip(uint32_t addr)
111{
112 struct in_addr temp;
113
114 if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
115 temp.s_addr = addr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000116 bb_info_msg("%s belongs to someone, reserving it for %ld seconds",
Mike Frysinger7031f622006-05-08 03:20:50 +0000117 inet_ntoa(temp), server_config.conflict_time);
118 add_lease(blank_chaddr, addr, server_config.conflict_time);
119 return 1;
120 } else return 0;
121}
122
123
124/* find an assignable address, it check_expired is true, we check all the expired leases as well.
125 * Maybe this should try expired leases by age... */
126uint32_t find_address(int check_expired)
127{
128 uint32_t addr, ret;
129 struct dhcpOfferedAddr *lease = NULL;
130
131 addr = ntohl(server_config.start); /* addr is in host order here */
132 for (;addr <= ntohl(server_config.end); addr++) {
133
134 /* ie, 192.168.55.0 */
135 if (!(addr & 0xFF)) continue;
136
137 /* ie, 192.168.55.255 */
138 if ((addr & 0xFF) == 0xFF) continue;
139
140 /* Only do if it isn't an assigned as a static lease */
141 if(!reservedIp(server_config.static_leases, htonl(addr)))
142 {
143
144 /* lease is not taken */
145 ret = htonl(addr);
146 if ((!(lease = find_lease_by_yiaddr(ret)) ||
147
148 /* or it expired and we are checking for expired leases */
149 (check_expired && lease_expired(lease))) &&
150
151 /* and it isn't on the network */
152 !check_ip(ret)) {
153 return ret;
154 break;
155 }
156 }
157 }
158 return 0;
159}