blob: 0b6b409cd5a9b37b6df7f16c106ad9ae09847bb5 [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
Russ Dill61fb4892002-10-14 21:41:28 +000012#include "dhcpd.h"
13#include "files.h"
14#include "options.h"
Russ Dill61fb4892002-10-14 21:41:28 +000015#include "arpping.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000016#include "common.h"
17
Russ Dill61fb4892002-10-14 21:41:28 +000018
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
Glenn L McGrath24833432003-06-10 17:22:49 +0000105/* check is an IP is taken, if it is, add it to the lease table */
106static int check_ip(u_int32_t addr)
107{
108 struct in_addr temp;
109
110 if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
111 temp.s_addr = addr;
112 LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
113 inet_ntoa(temp), server_config.conflict_time);
114 add_lease(blank_chaddr, addr, server_config.conflict_time);
115 return 1;
116 } else return 0;
117}
118
Russ Dill61fb4892002-10-14 21:41:28 +0000119/* find an assignable address, it check_expired is true, we check all the expired leases as well.
120 * Maybe this should try expired leases by age... */
121u_int32_t find_address(int check_expired)
122{
123 u_int32_t addr, ret;
124 struct dhcpOfferedAddr *lease = NULL;
125
126 addr = ntohl(server_config.start); /* addr is in host order here */
127 for (;addr <= ntohl(server_config.end); addr++) {
128
129 /* ie, 192.168.55.0 */
130 if (!(addr & 0xFF)) continue;
131
132 /* ie, 192.168.55.255 */
133 if ((addr & 0xFF) == 0xFF) continue;
134
135 /* lease is not taken */
136 ret = htonl(addr);
137 if ((!(lease = find_lease_by_yiaddr(ret)) ||
138
139 /* or it expired and we are checking for expired leases */
140 (check_expired && lease_expired(lease))) &&
141
142 /* and it isn't on the network */
143 !check_ip(ret)) {
144 return ret;
145 break;
146 }
147 }
148 return 0;
149}