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