aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-10-08 08:49:26 +0000
committerEric Andersen <andersen@codepoet.org>2004-10-08 08:49:26 +0000
commitabf58d6ba5df9bbe04c4c7008cbbc8c7dc626392 (patch)
treec64a5328d250449c9a4e3964d59a657543e06440
parent751750e3ee0195eef802a1554e97712285bf8fd7 (diff)
downloadbusybox-w32-abf58d6ba5df9bbe04c4c7008cbbc8c7dc626392.tar.gz
busybox-w32-abf58d6ba5df9bbe04c4c7008cbbc8c7dc626392.tar.bz2
busybox-w32-abf58d6ba5df9bbe04c4c7008cbbc8c7dc626392.zip
Wade Berrier writes:
Hello, Here's a patch for a first attempt at static leases for udhcpd. Included in the tarball are 2 files (static_leases.c, static_leases.h) and a patch against the latest cvs. In the config file you can configure static leases with the following format: static_lease 00:60:08:11:CE:4E 192.168.0.54 static_lease 00:60:08:11:CE:3E 192.168.0.44 Comments/suggestions/improvements are welcome. Wade
-rw-r--r--examples/udhcp/udhcpd.conf7
-rw-r--r--networking/udhcp/Makefile.in2
-rw-r--r--networking/udhcp/dhcpd.c25
-rw-r--r--networking/udhcp/dhcpd.h7
-rw-r--r--networking/udhcp/files.c53
-rw-r--r--networking/udhcp/leases.c7
-rw-r--r--networking/udhcp/serverpacket.c20
-rw-r--r--networking/udhcp/static_leases.c119
-rw-r--r--networking/udhcp/static_leases.h25
9 files changed, 260 insertions, 5 deletions
diff --git a/examples/udhcp/udhcpd.conf b/examples/udhcp/udhcpd.conf
index 00105b3e5..f91fddebf 100644
--- a/examples/udhcp/udhcpd.conf
+++ b/examples/udhcp/udhcpd.conf
@@ -114,3 +114,10 @@ option lease 864000 # 10 days of seconds
114#opt ntpsrv 114#opt ntpsrv
115#opt tftp 115#opt tftp
116#opt bootfile 116#opt bootfile
117
118
119# Static leases map
120#static_lease 00:60:08:11:CE:4E 192.168.0.54
121#static_lease 00:60:08:11:CE:3E 192.168.0.44
122
123
diff --git a/networking/udhcp/Makefile.in b/networking/udhcp/Makefile.in
index 2d7a08816..94750f693 100644
--- a/networking/udhcp/Makefile.in
+++ b/networking/udhcp/Makefile.in
@@ -40,7 +40,7 @@ UDHCP-$(CONFIG_UDHCP_SHARED) += common.c options.c packet.c pidfile.c \
40UDHCP-$(CONFIG_UDHCPC) += dhcpc.c clientpacket.c clientsocket.c \ 40UDHCP-$(CONFIG_UDHCPC) += dhcpc.c clientpacket.c clientsocket.c \
41 script.c 41 script.c
42UDHCP-$(CONFIG_UDHCPD) += dhcpd.c arpping.c files.c leases.c \ 42UDHCP-$(CONFIG_UDHCPD) += dhcpd.c arpping.c files.c leases.c \
43 serverpacket.c 43 serverpacket.c static_leases.c
44UDHCP-$(CONFIG_DUMPLEASES) += dumpleases.c 44UDHCP-$(CONFIG_DUMPLEASES) += dumpleases.c
45UDHCP_OBJS=$(patsubst %.c,$(UDHCP_DIR)%.o, $(UDHCP-y)) 45UDHCP_OBJS=$(patsubst %.c,$(UDHCP_DIR)%.o, $(UDHCP-y))
46 46
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c
index 6f38f07f7..ab3ddfe4f 100644
--- a/networking/udhcp/dhcpd.c
+++ b/networking/udhcp/dhcpd.c
@@ -44,6 +44,7 @@
44#include "serverpacket.h" 44#include "serverpacket.h"
45#include "common.h" 45#include "common.h"
46#include "signalpipe.h" 46#include "signalpipe.h"
47#include "static_leases.h"
47 48
48 49
49/* globals */ 50/* globals */
@@ -68,9 +69,12 @@ int main(int argc, char *argv[])
68 unsigned long timeout_end; 69 unsigned long timeout_end;
69 struct option_set *option; 70 struct option_set *option;
70 struct dhcpOfferedAddr *lease; 71 struct dhcpOfferedAddr *lease;
72 struct dhcpOfferedAddr static_lease;
71 int max_sock; 73 int max_sock;
72 unsigned long num_ips; 74 unsigned long num_ips;
73 75
76 uint32_t static_lease_ip;
77
74 memset(&server_config, 0, sizeof(struct server_config_t)); 78 memset(&server_config, 0, sizeof(struct server_config_t));
75 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]); 79 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
76 80
@@ -162,8 +166,25 @@ int main(int argc, char *argv[])
162 continue; 166 continue;
163 } 167 }
164 168
165 /* ADDME: look for a static lease */ 169 /* Look for a static lease */
170 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
171
172 if(static_lease_ip)
173 {
174 printf("Found static lease: %x\n", static_lease_ip);
175
176 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
177 static_lease.yiaddr = static_lease_ip;
178 static_lease.expires = 0;
179
180 lease = &static_lease;
181
182 }
183 else
184 {
166 lease = find_lease_by_chaddr(packet.chaddr); 185 lease = find_lease_by_chaddr(packet.chaddr);
186 }
187
167 switch (state[0]) { 188 switch (state[0]) {
168 case DHCPDISCOVER: 189 case DHCPDISCOVER:
169 DEBUG(LOG_INFO,"received DISCOVER"); 190 DEBUG(LOG_INFO,"received DISCOVER");
@@ -181,7 +202,7 @@ int main(int argc, char *argv[])
181 if (requested) memcpy(&requested_align, requested, 4); 202 if (requested) memcpy(&requested_align, requested, 4);
182 if (server_id) memcpy(&server_id_align, server_id, 4); 203 if (server_id) memcpy(&server_id_align, server_id, 4);
183 204
184 if (lease) { /*ADDME: or static lease */ 205 if (lease) {
185 if (server_id) { 206 if (server_id) {
186 /* SELECTING State */ 207 /* SELECTING State */
187 DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align)); 208 DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align));
diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h
index 39658a8ff..c47f6aa3f 100644
--- a/networking/udhcp/dhcpd.h
+++ b/networking/udhcp/dhcpd.h
@@ -99,6 +99,12 @@ struct option_set {
99 struct option_set *next; 99 struct option_set *next;
100}; 100};
101 101
102struct static_lease {
103 uint8_t *mac;
104 uint32_t *ip;
105 struct static_lease *next;
106};
107
102struct server_config_t { 108struct server_config_t {
103 uint32_t server; /* Our IP, in network order */ 109 uint32_t server; /* Our IP, in network order */
104 uint32_t start; /* Start address of leases, network order */ 110 uint32_t start; /* Start address of leases, network order */
@@ -124,6 +130,7 @@ struct server_config_t {
124 uint32_t siaddr; /* next server bootp option */ 130 uint32_t siaddr; /* next server bootp option */
125 char *sname; /* bootp server name */ 131 char *sname; /* bootp server name */
126 char *boot_file; /* bootp boot file option */ 132 char *boot_file; /* bootp boot file option */
133 struct static_lease *static_leases; /* List of ip/mac pairs to assign static leases */
127}; 134};
128 135
129extern struct server_config_t server_config; 136extern struct server_config_t server_config;
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index 89287ca2d..73a3bbc6e 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -11,6 +11,9 @@
11#include <ctype.h> 11#include <ctype.h>
12#include <netdb.h> 12#include <netdb.h>
13 13
14#include <netinet/ether.h>
15#include "static_leases.h"
16
14#include "dhcpd.h" 17#include "dhcpd.h"
15#include "files.h" 18#include "files.h"
16#include "options.h" 19#include "options.h"
@@ -38,6 +41,22 @@ static int read_ip(const char *line, void *arg)
38 return retval; 41 return retval;
39} 42}
40 43
44static int read_mac(const char *line, void *arg)
45{
46 uint8_t *mac_bytes = arg;
47 struct ether_addr *temp_ether_addr;
48 int retval = 1;
49
50 temp_ether_addr = ether_aton(line);
51
52 if(temp_ether_addr == NULL)
53 retval = 0;
54 else
55 memcpy(mac_bytes, temp_ether_addr, 6);
56
57 return retval;
58}
59
41 60
42static int read_str(const char *line, void *arg) 61static int read_str(const char *line, void *arg)
43{ 62{
@@ -150,6 +169,39 @@ static int read_opt(const char *const_line, void *arg)
150 return retval; 169 return retval;
151} 170}
152 171
172static int read_staticlease(const char *const_line, void *arg)
173{
174
175 char *line;
176 char *mac_string;
177 char *ip_string;
178 uint8_t *mac_bytes;
179 uint32_t *ip;
180
181
182 /* Allocate memory for addresses */
183 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
184 ip = xmalloc(sizeof(uint32_t));
185
186 /* Read mac */
187 line = (char *) const_line;
188 mac_string = strtok(line, " \t");
189 read_mac(mac_string, mac_bytes);
190
191 /* Read ip */
192 ip_string = strtok(NULL, " \t");
193 read_ip(ip_string, ip);
194
195 addStaticLease(arg, mac_bytes, ip);
196
197#ifdef UDHCP_DEBUG
198 printStaticLeases(arg);
199#endif
200
201 return 1;
202
203}
204
153 205
154static const struct config_keyword keywords[] = { 206static const struct config_keyword keywords[] = {
155 /* keyword handler variable address default */ 207 /* keyword handler variable address default */
@@ -171,6 +223,7 @@ static const struct config_keyword keywords[] = {
171 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, 223 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
172 {"sname", read_str, &(server_config.sname), ""}, 224 {"sname", read_str, &(server_config.sname), ""},
173 {"boot_file", read_str, &(server_config.boot_file), ""}, 225 {"boot_file", read_str, &(server_config.boot_file), ""},
226 {"static_lease",read_staticlease, &(server_config.static_leases), ""},
174 /*ADDME: static lease */ 227 /*ADDME: static lease */
175 {"", NULL, NULL, ""} 228 {"", NULL, NULL, ""}
176}; 229};
diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c
index d478880a3..4da21a23b 100644
--- a/networking/udhcp/leases.c
+++ b/networking/udhcp/leases.c
@@ -16,6 +16,8 @@
16#include "arpping.h" 16#include "arpping.h"
17#include "common.h" 17#include "common.h"
18 18
19#include "static_leases.h"
20
19 21
20uint8_t blank_chaddr[] = {[0 ... 15] = 0}; 22uint8_t blank_chaddr[] = {[0 ... 15] = 0};
21 23
@@ -134,6 +136,10 @@ uint32_t find_address(int check_expired)
134 /* ie, 192.168.55.255 */ 136 /* ie, 192.168.55.255 */
135 if ((addr & 0xFF) == 0xFF) continue; 137 if ((addr & 0xFF) == 0xFF) continue;
136 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
137 /* lease is not taken */ 143 /* lease is not taken */
138 ret = htonl(addr); 144 ret = htonl(addr);
139 if ((!(lease = find_lease_by_yiaddr(ret)) || 145 if ((!(lease = find_lease_by_yiaddr(ret)) ||
@@ -147,5 +153,6 @@ uint32_t find_address(int check_expired)
147 break; 153 break;
148 } 154 }
149 } 155 }
156 }
150 return 0; 157 return 0;
151} 158}
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c
index bc9d822a9..75d55bd92 100644
--- a/networking/udhcp/serverpacket.c
+++ b/networking/udhcp/serverpacket.c
@@ -29,6 +29,7 @@
29#include "dhcpd.h" 29#include "dhcpd.h"
30#include "options.h" 30#include "options.h"
31#include "common.h" 31#include "common.h"
32#include "static_leases.h"
32 33
33/* send a packet to giaddr using the kernel ip stack */ 34/* send a packet to giaddr using the kernel ip stack */
34static int send_packet_to_relay(struct dhcpMessage *payload) 35static int send_packet_to_relay(struct dhcpMessage *payload)
@@ -113,9 +114,15 @@ int sendOffer(struct dhcpMessage *oldpacket)
113 struct option_set *curr; 114 struct option_set *curr;
114 struct in_addr addr; 115 struct in_addr addr;
115 116
117 uint32_t static_lease_ip;
118
116 init_packet(&packet, oldpacket, DHCPOFFER); 119 init_packet(&packet, oldpacket, DHCPOFFER);
117 120
121 static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
122
118 /* ADDME: if static, short circuit */ 123 /* ADDME: if static, short circuit */
124 if(!static_lease_ip)
125 {
119 /* the client is in our lease/offered table */ 126 /* the client is in our lease/offered table */
120 if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) { 127 if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) {
121 if (!lease_expired(lease)) 128 if (!lease_expired(lease))
@@ -132,15 +139,17 @@ int sendOffer(struct dhcpMessage *oldpacket)
132 ntohl(req_align) >= ntohl(server_config.start) && 139 ntohl(req_align) >= ntohl(server_config.start) &&
133 ntohl(req_align) <= ntohl(server_config.end) && 140 ntohl(req_align) <= ntohl(server_config.end) &&
134 141
135 /* and its not already taken/offered */ /* ADDME: check that its not a static lease */ 142 !static_lease_ip && /* Check that its not a static lease */
143 /* and is not already taken/offered */
136 ((!(lease = find_lease_by_yiaddr(req_align)) || 144 ((!(lease = find_lease_by_yiaddr(req_align)) ||
137 145
138 /* or its taken, but expired */ /* ADDME: or maybe in here */ 146 /* or its taken, but expired */ /* ADDME: or maybe in here */
139 lease_expired(lease)))) { 147 lease_expired(lease)))) {
140 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */ 148 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
141 149
142 /* otherwise, find a free IP */ /*ADDME: is it a static lease? */ 150 /* otherwise, find a free IP */
143 } else { 151 } else {
152 /* Is it a static lease? (No, because find_address skips static lease) */
144 packet.yiaddr = find_address(0); 153 packet.yiaddr = find_address(0);
145 154
146 /* try for an expired lease */ 155 /* try for an expired lease */
@@ -167,7 +176,14 @@ int sendOffer(struct dhcpMessage *oldpacket)
167 /* Make sure we aren't just using the lease time from the previous offer */ 176 /* Make sure we aren't just using the lease time from the previous offer */
168 if (lease_time_align < server_config.min_lease) 177 if (lease_time_align < server_config.min_lease)
169 lease_time_align = server_config.lease; 178 lease_time_align = server_config.lease;
179 }
170 /* ADDME: end of short circuit */ 180 /* ADDME: end of short circuit */
181 else
182 {
183 /* It is a static lease... use it */
184 packet.yiaddr = static_lease_ip;
185 }
186
171 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align)); 187 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
172 188
173 curr = server_config.options; 189 curr = server_config.options;
diff --git a/networking/udhcp/static_leases.c b/networking/udhcp/static_leases.c
new file mode 100644
index 000000000..1124d39de
--- /dev/null
+++ b/networking/udhcp/static_leases.c
@@ -0,0 +1,119 @@
1/*
2 * static_leases.c -- Couple of functions to assist with storing and
3 * retrieving data for static leases
4 *
5 * Wade Berrier <wberrier@myrealbox.com> September 2004
6 *
7 */
8
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13
14#include "static_leases.h"
15#include "dhcpd.h"
16
17/* Takes the address of the pointer to the static_leases linked list,
18 * Address to a 6 byte mac address
19 * Address to a 4 byte ip address */
20int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip)
21{
22
23 struct static_lease *cur;
24 struct static_lease *new_static_lease;
25
26 /* Build new node */
27 new_static_lease = xmalloc(sizeof(struct static_lease));
28 new_static_lease->mac = mac;
29 new_static_lease->ip = ip;
30 new_static_lease->next = NULL;
31
32 /* If it's the first node to be added... */
33 if(*lease_struct == NULL)
34 {
35 *lease_struct = new_static_lease;
36 }
37 else
38 {
39 cur = *lease_struct;
40 while(cur->next != NULL)
41 {
42 cur = cur->next;
43 }
44
45 cur->next = new_static_lease;
46 }
47
48 return 1;
49
50}
51
52/* Check to see if a mac has an associated static lease */
53uint32_t getIpByMac(struct static_lease *lease_struct, void *arg)
54{
55 uint32_t return_ip;
56 struct static_lease *cur = lease_struct;
57 uint8_t *mac = arg;
58
59 return_ip = 0;
60
61 while(cur != NULL)
62 {
63 /* If the client has the correct mac */
64 if(memcmp(cur->mac, mac, 6) == 0)
65 {
66 return_ip = *(cur->ip);
67 }
68
69 cur = cur->next;
70 }
71
72 return return_ip;
73
74}
75
76/* Check to see if an ip is reserved as a static ip */
77uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip)
78{
79 struct static_lease *cur = lease_struct;
80
81 uint32_t return_val = 0;
82
83 while(cur != NULL)
84 {
85 /* If the client has the correct ip */
86 if(*cur->ip == ip)
87 return_val = 1;
88
89 cur = cur->next;
90 }
91
92 return return_val;
93
94}
95
96#ifdef UDHCP_DEBUG
97/* Print out static leases just to check what's going on */
98/* Takes the address of the pointer to the static_leases linked list */
99void printStaticLeases(struct static_lease **arg)
100{
101 /* Get a pointer to the linked list */
102 struct static_lease *cur = *arg;
103
104 while(cur != NULL)
105 {
106 /* printf("PrintStaticLeases: Lease mac Address: %x\n", cur->mac); */
107 printf("PrintStaticLeases: Lease mac Value: %x\n", *(cur->mac));
108 /* printf("PrintStaticLeases: Lease ip Address: %x\n", cur->ip); */
109 printf("PrintStaticLeases: Lease ip Value: %x\n", *(cur->ip));
110
111 cur = cur->next;
112 }
113
114
115}
116#endif
117
118
119
diff --git a/networking/udhcp/static_leases.h b/networking/udhcp/static_leases.h
new file mode 100644
index 000000000..d06520b23
--- /dev/null
+++ b/networking/udhcp/static_leases.h
@@ -0,0 +1,25 @@
1/* static_leases.h */
2#ifndef _STATIC_LEASES_H
3#define _STATIC_LEASES_H
4
5#include "dhcpd.h"
6
7/* Config file will pass static lease info to this function which will add it
8 * to a data structure that can be searched later */
9int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip);
10
11/* Check to see if a mac has an associated static lease */
12uint32_t getIpByMac(struct static_lease *lease_struct, void *arg);
13
14/* Check to see if an ip is reserved as a static ip */
15uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip);
16
17#ifdef UDHCP_DEBUG
18/* Print out static leases just to check what's going on */
19void printStaticLeases(struct static_lease **lease_struct);
20#endif
21
22#endif
23
24
25