aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/files.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/files.c')
-rw-r--r--networking/udhcp/files.c123
1 files changed, 63 insertions, 60 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index 842e0f2db..3d0344b09 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -3,23 +3,20 @@
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4 */ 4 */
5 5
6#include <stdio.h>
7#include <sys/socket.h> 6#include <sys/socket.h>
8#include <arpa/inet.h> 7#include <arpa/inet.h>
9#include <string.h> 8#include <string.h>
10#include <stdlib.h> 9#include <stdlib.h>
11#include <time.h> 10#include <time.h>
12#include <ctype.h> 11#include <ctype.h>
13#include <netdb.h>
14 12
15#include "debug.h"
16#include "dhcpd.h" 13#include "dhcpd.h"
17#include "files.h" 14#include "files.h"
18#include "options.h" 15#include "options.h"
19#include "leases.h" 16#include "common.h"
20 17
21/* on these functions, make sure you datatype matches */ 18/* on these functions, make sure you datatype matches */
22static int read_ip(char *line, void *arg) 19static int read_ip(const char *line, void *arg)
23{ 20{
24 struct in_addr *addr = arg; 21 struct in_addr *addr = arg;
25 struct hostent *host; 22 struct hostent *host;
@@ -34,7 +31,7 @@ static int read_ip(char *line, void *arg)
34} 31}
35 32
36 33
37static int read_str(char *line, void *arg) 34static int read_str(const char *line, void *arg)
38{ 35{
39 char **dest = arg; 36 char **dest = arg;
40 37
@@ -45,7 +42,7 @@ static int read_str(char *line, void *arg)
45} 42}
46 43
47 44
48static int read_u32(char *line, void *arg) 45static int read_u32(const char *line, void *arg)
49{ 46{
50 u_int32_t *dest = arg; 47 u_int32_t *dest = arg;
51 char *endptr; 48 char *endptr;
@@ -54,7 +51,7 @@ static int read_u32(char *line, void *arg)
54} 51}
55 52
56 53
57static int read_yn(char *line, void *arg) 54static int read_yn(const char *line, void *arg)
58{ 55{
59 char *dest = arg; 56 char *dest = arg;
60 int retval = 1; 57 int retval = 1;
@@ -68,32 +65,33 @@ static int read_yn(char *line, void *arg)
68 return retval; 65 return retval;
69} 66}
70 67
68#define READ_CONFIG_BUF_SIZE 512 /* domainname may have 254 chars */
71 69
72/* read a dhcp option and add it to opt_list */ 70/* read a dhcp option and add it to opt_list */
73static int read_opt(char *line, void *arg) 71static int read_opt(const char *const_line, void *arg)
74{ 72{
73 char line[READ_CONFIG_BUF_SIZE];
75 struct option_set **opt_list = arg; 74 struct option_set **opt_list = arg;
76 char *opt, *val, *endptr; 75 char *opt, *val, *endptr;
77 struct dhcp_option *option = NULL; 76 struct dhcp_option *option;
78 int retval = 0, length = 0; 77 int retval = 0, length;
79 char buffer[255]; 78 char buffer[256]; /* max opt length */
80 u_int16_t result_u16; 79 u_int16_t result_u16;
81 u_int32_t result_u32; 80 u_int32_t result_u32;
82 int i; 81 void *valptr;
83
84 if (!(opt = strtok(line, " \t="))) return 0;
85 82
86 for (i = 0; options[i].code; i++) 83 if ((opt = strtok(strcpy(line, const_line), " \t="))) {
87 if (!strcmp(options[i].name, opt))
88 option = &(options[i]);
89 84
90 if (!option) return 0; 85 for (option = options; option->code; option++)
86 if (!strcasecmp(option->name, opt))
87 break;
91 88
92 do { 89 if (option->code) do {
93 val = strtok(NULL, ", \t"); 90 val = strtok(NULL, ", \t");
94 if (val) { 91 if(!val)
92 break;
95 length = option_lengths[option->flags & TYPE_MASK]; 93 length = option_lengths[option->flags & TYPE_MASK];
96 retval = 0; 94 valptr = NULL;
97 switch (option->flags & TYPE_MASK) { 95 switch (option->flags & TYPE_MASK) {
98 case OPTION_IP: 96 case OPTION_IP:
99 retval = read_ip(val, buffer); 97 retval = read_ip(val, buffer);
@@ -107,8 +105,9 @@ static int read_opt(char *line, void *arg)
107 length = strlen(val); 105 length = strlen(val);
108 if (length > 0) { 106 if (length > 0) {
109 if (length > 254) length = 254; 107 if (length > 254) length = 254;
110 memcpy(buffer, val, length); 108 endptr = buffer + length;
111 retval = 1; 109 endptr[0] = 0;
110 valptr = val;
112 } 111 }
113 break; 112 break;
114 case OPTION_BOOLEAN: 113 case OPTION_BOOLEAN:
@@ -116,41 +115,44 @@ static int read_opt(char *line, void *arg)
116 break; 115 break;
117 case OPTION_U8: 116 case OPTION_U8:
118 buffer[0] = strtoul(val, &endptr, 0); 117 buffer[0] = strtoul(val, &endptr, 0);
119 retval = (endptr[0] == '\0'); 118 valptr = buffer;
120 break; 119 break;
121 case OPTION_U16: 120 case OPTION_U16:
122 result_u16 = htons(strtoul(val, &endptr, 0)); 121 result_u16 = htons(strtoul(val, &endptr, 0));
123 memcpy(buffer, &result_u16, 2); 122 valptr = &result_u16;
124 retval = (endptr[0] == '\0');
125 break; 123 break;
126 case OPTION_S16: 124 case OPTION_S16:
127 result_u16 = htons(strtol(val, &endptr, 0)); 125 result_u16 = htons(strtol(val, &endptr, 0));
128 memcpy(buffer, &result_u16, 2); 126 valptr = &result_u16;
129 retval = (endptr[0] == '\0');
130 break; 127 break;
131 case OPTION_U32: 128 case OPTION_U32:
132 result_u32 = htonl(strtoul(val, &endptr, 0)); 129 result_u32 = htonl(strtoul(val, &endptr, 0));
133 memcpy(buffer, &result_u32, 4); 130 valptr = &result_u32;
134 retval = (endptr[0] == '\0');
135 break; 131 break;
136 case OPTION_S32: 132 case OPTION_S32:
137 result_u32 = htonl(strtol(val, &endptr, 0)); 133 result_u32 = htonl(strtol(val, &endptr, 0));
138 memcpy(buffer, &result_u32, 4); 134 valptr = &result_u32;
139 retval = (endptr[0] == '\0');
140 break; 135 break;
141 default: 136 default:
137 retval = 0;
142 break; 138 break;
143 } 139 }
140 if(valptr) {
141 memcpy(buffer, valptr, length);
142 retval = (endptr[0] == '\0');
143 }
144 if (retval) 144 if (retval)
145 attach_option(opt_list, option, buffer, length); 145 attach_option(opt_list, option, buffer, length);
146 }; 146 else
147 } while (val && retval && option->flags & OPTION_LIST); 147 break;
148 } while (option->flags & OPTION_LIST);
149 }
148 return retval; 150 return retval;
149} 151}
150 152
151 153
152static struct config_keyword keywords[] = { 154static const struct config_keyword keywords[] = {
153 /* keyword[14] handler variable address default[20] */ 155 /* keyword handler variable address default */
154 {"start", read_ip, &(server_config.start), "192.168.0.20"}, 156 {"start", read_ip, &(server_config.start), "192.168.0.20"},
155 {"end", read_ip, &(server_config.end), "192.168.0.254"}, 157 {"end", read_ip, &(server_config.end), "192.168.0.254"},
156 {"interface", read_str, &(server_config.interface), "eth0"}, 158 {"interface", read_str, &(server_config.interface), "eth0"},
@@ -163,7 +165,7 @@ static struct config_keyword keywords[] = {
163 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"}, 165 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
164 {"offer_time", read_u32, &(server_config.offer_time), "60"}, 166 {"offer_time", read_u32, &(server_config.offer_time), "60"},
165 {"min_lease", read_u32, &(server_config.min_lease), "60"}, 167 {"min_lease", read_u32, &(server_config.min_lease), "60"},
166 {"lease_file", read_str, &(server_config.lease_file), "/var/lib/misc/udhcpd.leases"}, 168 {"lease_file", read_str, &(server_config.lease_file), leases_file},
167 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, 169 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
168 {"notify_file", read_str, &(server_config.notify_file), ""}, 170 {"notify_file", read_str, &(server_config.notify_file), ""},
169 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, 171 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
@@ -174,14 +176,15 @@ static struct config_keyword keywords[] = {
174}; 176};
175 177
176 178
177int read_config(char *file) 179int read_config(const char *file)
178{ 180{
179 FILE *in; 181 FILE *in;
180 char buffer[80], orig[80], *token, *line; 182 char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE];
183 char *token, *line;
181 int i; 184 int i;
182 185
183 for (i = 0; strlen(keywords[i].keyword); i++) 186 for (i = 0; keywords[i].keyword[0]; i++)
184 if (strlen(keywords[i].def)) 187 if (keywords[i].def[0])
185 keywords[i].handler(keywords[i].def, keywords[i].var); 188 keywords[i].handler(keywords[i].def, keywords[i].var);
186 189
187 if (!(in = fopen(file, "r"))) { 190 if (!(in = fopen(file, "r"))) {
@@ -189,24 +192,25 @@ int read_config(char *file)
189 return 0; 192 return 0;
190 } 193 }
191 194
192 while (fgets(buffer, 80, in)) { 195 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
193 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; 196 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
194 strncpy(orig, buffer, 80); 197 strcpy(orig, buffer);
195 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; 198 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
196 token = buffer + strspn(buffer, " \t"); 199 token = strtok(buffer, " \t");
197 if (*token == '\0') continue; 200 if(!token)
198 line = token + strcspn(token, " \t="); 201 continue;
199 if (*line == '\0') continue; 202 line = strtok(NULL, "");
200 *line = '\0'; 203 if(!line)
204 continue;
205 while(*line == '=' || isspace(*line))
201 line++; 206 line++;
202
203 /* eat leading whitespace */
204 line = line + strspn(line, " \t=");
205 /* eat trailing whitespace */ 207 /* eat trailing whitespace */
206 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); 208 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
207 line[i] = '\0'; 209 line[i] = '\0';
210 if (*line == '\0')
211 continue;
208 212
209 for (i = 0; strlen(keywords[i].keyword); i++) 213 for (i = 0; keywords[i].keyword[0]; i++)
210 if (!strcasecmp(token, keywords[i].keyword)) 214 if (!strcasecmp(token, keywords[i].keyword))
211 if (!keywords[i].handler(line, keywords[i].var)) { 215 if (!keywords[i].handler(line, keywords[i].var)) {
212 LOG(LOG_ERR, "unable to parse '%s'", orig); 216 LOG(LOG_ERR, "unable to parse '%s'", orig);
@@ -233,16 +237,17 @@ void write_leases(void)
233 } 237 }
234 238
235 for (i = 0; i < server_config.max_leases; i++) { 239 for (i = 0; i < server_config.max_leases; i++) {
240 struct dhcpOfferedAddr lease;
236 if (leases[i].yiaddr != 0) { 241 if (leases[i].yiaddr != 0) {
237 if (server_config.remaining) { 242 if (server_config.remaining) {
238 if (lease_expired(&(leases[i]))) 243 if (lease_expired(&(leases[i])))
239 lease_time = 0; 244 lease_time = 0;
240 else lease_time = leases[i].expires - curr; 245 else lease_time = leases[i].expires - curr;
241 } else lease_time = leases[i].expires; 246 } else lease_time = leases[i].expires;
242 lease_time = htonl(lease_time); 247 lease.expires = htonl(lease_time);
243 fwrite(leases[i].chaddr, 16, 1, fp); 248 memcpy(lease.chaddr, leases[i].chaddr, 16);
244 fwrite(&(leases[i].yiaddr), 4, 1, fp); 249 lease.yiaddr = leases[i].yiaddr;
245 fwrite(&lease_time, 4, 1, fp); 250 fwrite(leases, sizeof(lease), 1, fp);
246 } 251 }
247 } 252 }
248 fclose(fp); 253 fclose(fp);
@@ -254,7 +259,7 @@ void write_leases(void)
254} 259}
255 260
256 261
257void read_leases(char *file) 262void read_leases(const char *file)
258{ 263{
259 FILE *fp; 264 FILE *fp;
260 unsigned int i = 0; 265 unsigned int i = 0;
@@ -280,5 +285,3 @@ void read_leases(char *file)
280 DEBUG(LOG_INFO, "Read %d leases", i); 285 DEBUG(LOG_INFO, "Read %d leases", i);
281 fclose(fp); 286 fclose(fp);
282} 287}
283
284