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