summaryrefslogtreecommitdiff
path: root/networking/udhcp/files.c
diff options
context:
space:
mode:
authorRuss Dill <Russ.Dill@asu.edu>2003-12-18 22:25:38 +0000
committerRuss Dill <Russ.Dill@asu.edu>2003-12-18 22:25:38 +0000
commit4e864a36b611f56c6b347be1dace4e5e805a3eb8 (patch)
tree6582084290de4bead010f5fb8ff0a0a6d2b573fd /networking/udhcp/files.c
parente30495654d8bb38f7ea234d9d0ab0929525501e3 (diff)
downloadbusybox-w32-4e864a36b611f56c6b347be1dace4e5e805a3eb8.tar.gz
busybox-w32-4e864a36b611f56c6b347be1dace4e5e805a3eb8.tar.bz2
busybox-w32-4e864a36b611f56c6b347be1dace4e5e805a3eb8.zip
Finish remerging busybox udhcp and udhcp. Some cleanups as well.
Diffstat (limited to 'networking/udhcp/files.c')
-rw-r--r--networking/udhcp/files.c98
1 files changed, 49 insertions, 49 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index f24f01cb6..6d72863ee 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -16,6 +16,13 @@
16#include "options.h" 16#include "options.h"
17#include "common.h" 17#include "common.h"
18 18
19/*
20 * Domain names may have 254 chars, and string options can be 254
21 * chars long. However, 80 bytes will be enough for most, and won't
22 * hog up memory. If you have a special application, change it
23 */
24#define READ_CONFIG_BUF_SIZE 80
25
19/* on these functions, make sure you datatype matches */ 26/* on these functions, make sure you datatype matches */
20static int read_ip(const char *line, void *arg) 27static int read_ip(const char *line, void *arg)
21{ 28{
@@ -66,23 +73,23 @@ static int read_yn(const char *line, void *arg)
66 return retval; 73 return retval;
67} 74}
68 75
69#define READ_CONFIG_BUF_SIZE 512 /* domainname may have 254 chars */
70 76
71/* read a dhcp option and add it to opt_list */ 77/* read a dhcp option and add it to opt_list */
72static int read_opt(const char *const_line, void *arg) 78static int read_opt(const char *const_line, void *arg)
73{ 79{
74 char line[READ_CONFIG_BUF_SIZE];
75 struct option_set **opt_list = arg; 80 struct option_set **opt_list = arg;
76 char *opt, *val, *endptr; 81 char *opt, *val, *endptr;
77 struct dhcp_option *option; 82 struct dhcp_option *option;
78 int retval = 0, length; 83 int retval = 0, length;
79 char buffer[256]; /* max opt length */ 84 char buffer[8];
80 u_int16_t result_u16; 85 char *line;
81 u_int32_t result_u32; 86 u_int16_t *result_u16 = (u_int16_t *) buffer;
82 void *valptr; 87 u_int32_t *result_u32 = (u_int32_t *) buffer;
88
89 /* Cheat, the only const line we'll actually get is "" */
90 line = (char *) const_line;
91 if (!(opt = strtok(line, " \t="))) return 0;
83 92
84 if (!(opt = strtok(strcpy(line, const_line), " \t="))) return 0;
85
86 for (option = dhcp_options; option->code; option++) 93 for (option = dhcp_options; option->code; option++)
87 if (!strcasecmp(option->name, opt)) 94 if (!strcasecmp(option->name, opt))
88 break; 95 break;
@@ -90,11 +97,10 @@ static int read_opt(const char *const_line, void *arg)
90 if (!option->code) return 0; 97 if (!option->code) return 0;
91 98
92 do { 99 do {
93 val = strtok(NULL, ", \t"); 100 if (!(val = strtok(NULL, ", \t"))) break;
94 if(!val)
95 break;
96 length = option_lengths[option->flags & TYPE_MASK]; 101 length = option_lengths[option->flags & TYPE_MASK];
97 valptr = NULL; 102 retval = 0;
103 opt = buffer; /* new meaning for variable opt */
98 switch (option->flags & TYPE_MASK) { 104 switch (option->flags & TYPE_MASK) {
99 case OPTION_IP: 105 case OPTION_IP:
100 retval = read_ip(val, buffer); 106 retval = read_ip(val, buffer);
@@ -108,9 +114,8 @@ static int read_opt(const char *const_line, void *arg)
108 length = strlen(val); 114 length = strlen(val);
109 if (length > 0) { 115 if (length > 0) {
110 if (length > 254) length = 254; 116 if (length > 254) length = 254;
111 endptr = buffer + length; 117 opt = val;
112 endptr[0] = 0; 118 retval = 1;
113 valptr = val;
114 } 119 }
115 break; 120 break;
116 case OPTION_BOOLEAN: 121 case OPTION_BOOLEAN:
@@ -118,43 +123,36 @@ static int read_opt(const char *const_line, void *arg)
118 break; 123 break;
119 case OPTION_U8: 124 case OPTION_U8:
120 buffer[0] = strtoul(val, &endptr, 0); 125 buffer[0] = strtoul(val, &endptr, 0);
121 valptr = buffer; 126 retval = (endptr[0] == '\0');
122 break; 127 break;
123 case OPTION_U16: 128 case OPTION_U16:
124 result_u16 = htons(strtoul(val, &endptr, 0)); 129 *result_u16 = htons(strtoul(val, &endptr, 0));
125 valptr = &result_u16; 130 retval = (endptr[0] == '\0');
126 break; 131 break;
127 case OPTION_S16: 132 case OPTION_S16:
128 result_u16 = htons(strtol(val, &endptr, 0)); 133 *result_u16 = htons(strtol(val, &endptr, 0));
129 valptr = &result_u16; 134 retval = (endptr[0] == '\0');
130 break; 135 break;
131 case OPTION_U32: 136 case OPTION_U32:
132 result_u32 = htonl(strtoul(val, &endptr, 0)); 137 *result_u32 = htonl(strtoul(val, &endptr, 0));
133 valptr = &result_u32; 138 retval = (endptr[0] == '\0');
134 break; 139 break;
135 case OPTION_S32: 140 case OPTION_S32:
136 result_u32 = htonl(strtol(val, &endptr, 0)); 141 *result_u32 = htonl(strtol(val, &endptr, 0));
137 valptr = &result_u32; 142 retval = (endptr[0] == '\0');
138 break; 143 break;
139 default: 144 default:
140 retval = 0;
141 break; 145 break;
142 } 146 }
143 if (valptr) {
144 memcpy(buffer, valptr, length);
145 retval = (endptr[0] == '\0');
146 }
147 if (retval) 147 if (retval)
148 attach_option(opt_list, option, buffer, length); 148 attach_option(opt_list, option, opt, length);
149 else 149 } while (retval && option->flags & OPTION_LIST);
150 break;
151 } while (option->flags & OPTION_LIST);
152 return retval; 150 return retval;
153} 151}
154 152
155 153
156static const struct config_keyword keywords[] = { 154static const struct config_keyword keywords[] = {
157 /* keyword handler variable address default */ 155 /* keyword handler variable address default */
158 {"start", read_ip, &(server_config.start), "192.168.0.20"}, 156 {"start", read_ip, &(server_config.start), "192.168.0.20"},
159 {"end", read_ip, &(server_config.end), "192.168.0.254"}, 157 {"end", read_ip, &(server_config.end), "192.168.0.254"},
160 {"interface", read_str, &(server_config.interface), "eth0"}, 158 {"interface", read_str, &(server_config.interface), "eth0"},
@@ -167,7 +165,7 @@ static const struct config_keyword keywords[] = {
167 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"}, 165 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
168 {"offer_time", read_u32, &(server_config.offer_time), "60"}, 166 {"offer_time", read_u32, &(server_config.offer_time), "60"},
169 {"min_lease", read_u32, &(server_config.min_lease), "60"}, 167 {"min_lease", read_u32, &(server_config.min_lease), "60"},
170 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, 168 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
171 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, 169 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
172 {"notify_file", read_str, &(server_config.notify_file), ""}, 170 {"notify_file", read_str, &(server_config.notify_file), ""},
173 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, 171 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
@@ -181,9 +179,11 @@ static const struct config_keyword keywords[] = {
181int read_config(const char *file) 179int read_config(const char *file)
182{ 180{
183 FILE *in; 181 FILE *in;
184 char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE]; 182 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
185 char *token, *line; 183#ifdef UDHCP_DEBUG
186 int i; 184 char orig[READ_CONFIG_BUF_SIZE];
185#endif
186 int i, lm = 0;
187 187
188 for (i = 0; keywords[i].keyword[0]; i++) 188 for (i = 0; keywords[i].keyword[0]; i++)
189 if (keywords[i].def[0]) 189 if (keywords[i].def[0])
@@ -195,27 +195,27 @@ int read_config(const char *file)
195 } 195 }
196 196
197 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { 197 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
198 lm++;
198 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; 199 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
200#ifdef UDHCP_DEBUG
199 strcpy(orig, buffer); 201 strcpy(orig, buffer);
202#endif
200 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; 203 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
201 token = strtok(buffer, " \t"); 204
202 if(!token) 205 if (!(token = strtok(buffer, " \t"))) continue;
203 continue; 206 if (!(line = strtok(NULL, ""))) continue;
204 line = strtok(NULL, ""); 207
205 if(!line) 208 /* eat leading whitespace */
206 continue; 209 line = line + strspn(line, " \t=");
207 while(*line == '=' || isspace(*line))
208 line++;
209 /* eat trailing whitespace */ 210 /* eat trailing whitespace */
210 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); 211 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
211 line[i] = '\0'; 212 line[i] = '\0';
212 if (*line == '\0')
213 continue;
214 213
215 for (i = 0; keywords[i].keyword[0]; i++) 214 for (i = 0; keywords[i].keyword[0]; i++)
216 if (!strcasecmp(token, keywords[i].keyword)) 215 if (!strcasecmp(token, keywords[i].keyword))
217 if (!keywords[i].handler(line, keywords[i].var)) { 216 if (!keywords[i].handler(line, keywords[i].var)) {
218 LOG(LOG_ERR, "unable to parse '%s'", orig); 217 LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
218 DEBUG(LOG_ERR, "unable to parse '%s'", orig);
219 /* reset back to the default value */ 219 /* reset back to the default value */
220 keywords[i].handler(keywords[i].def, keywords[i].var); 220 keywords[i].handler(keywords[i].def, keywords[i].var);
221 } 221 }