udhcp: fix wrong order of args here: index_in_strings(opt, dhcp_option_strings);
code shrink while at it: function old new delta read_config 406 323 -83
This commit is contained in:
		@@ -108,7 +108,7 @@ int send_inform(struct dhcpMessage *oldpacket);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/*** files.h ***/
 | 
					/*** files.h ***/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int read_config(const char *file);
 | 
					void read_config(const char *file);
 | 
				
			||||||
void write_leases(void);
 | 
					void write_leases(void);
 | 
				
			||||||
void read_leases(const char *file);
 | 
					void read_leases(const char *file);
 | 
				
			||||||
struct option_set *find_option(struct option_set *opt_list, uint8_t code);
 | 
					struct option_set *find_option(struct option_set *opt_list, uint8_t code);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -167,7 +167,7 @@ static int read_opt(const char *const_line, void *arg)
 | 
				
			|||||||
	if (!opt)
 | 
						if (!opt)
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	idx = index_in_strings(opt, dhcp_option_strings); /* NB: was strcasecmp! */
 | 
						idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
 | 
				
			||||||
	if (idx < 0)
 | 
						if (idx < 0)
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
	option = &dhcp_options[idx];
 | 
						option = &dhcp_options[idx];
 | 
				
			||||||
@@ -286,8 +286,6 @@ static const struct config_keyword keywords[] = {
 | 
				
			|||||||
	{"start",        read_ip,  &(server_config.start_ip),     "192.168.0.20"},
 | 
						{"start",        read_ip,  &(server_config.start_ip),     "192.168.0.20"},
 | 
				
			||||||
	{"end",          read_ip,  &(server_config.end_ip),       "192.168.0.254"},
 | 
						{"end",          read_ip,  &(server_config.end_ip),       "192.168.0.254"},
 | 
				
			||||||
	{"interface",    read_str, &(server_config.interface),    "eth0"},
 | 
						{"interface",    read_str, &(server_config.interface),    "eth0"},
 | 
				
			||||||
	{"option",       read_opt, &(server_config.options),      ""},
 | 
					 | 
				
			||||||
	{"opt",          read_opt, &(server_config.options),      ""},
 | 
					 | 
				
			||||||
	/* Avoid "max_leases value not sane" warning by setting default
 | 
						/* Avoid "max_leases value not sane" warning by setting default
 | 
				
			||||||
	 * to default_end_ip - default_start_ip + 1: */
 | 
						 * to default_end_ip - default_start_ip + 1: */
 | 
				
			||||||
	{"max_leases",   read_u32, &(server_config.max_leases),   "235"},
 | 
						{"max_leases",   read_u32, &(server_config.max_leases),   "235"},
 | 
				
			||||||
@@ -299,13 +297,17 @@ static const struct config_keyword keywords[] = {
 | 
				
			|||||||
	{"min_lease",    read_u32, &(server_config.min_lease),    "60"},
 | 
						{"min_lease",    read_u32, &(server_config.min_lease),    "60"},
 | 
				
			||||||
	{"lease_file",   read_str, &(server_config.lease_file),   LEASES_FILE},
 | 
						{"lease_file",   read_str, &(server_config.lease_file),   LEASES_FILE},
 | 
				
			||||||
	{"pidfile",      read_str, &(server_config.pidfile),      "/var/run/udhcpd.pid"},
 | 
						{"pidfile",      read_str, &(server_config.pidfile),      "/var/run/udhcpd.pid"},
 | 
				
			||||||
	{"notify_file",  read_str, &(server_config.notify_file),  ""},
 | 
					 | 
				
			||||||
	{"siaddr",       read_ip,  &(server_config.siaddr),       "0.0.0.0"},
 | 
						{"siaddr",       read_ip,  &(server_config.siaddr),       "0.0.0.0"},
 | 
				
			||||||
 | 
						/* keywords with no defaults must be last! */
 | 
				
			||||||
 | 
						{"option",       read_opt, &(server_config.options),      ""},
 | 
				
			||||||
 | 
						{"opt",          read_opt, &(server_config.options),      ""},
 | 
				
			||||||
 | 
						{"notify_file",  read_str, &(server_config.notify_file),  ""},
 | 
				
			||||||
	{"sname",        read_str, &(server_config.sname),        ""},
 | 
						{"sname",        read_str, &(server_config.sname),        ""},
 | 
				
			||||||
	{"boot_file",    read_str, &(server_config.boot_file),    ""},
 | 
						{"boot_file",    read_str, &(server_config.boot_file),    ""},
 | 
				
			||||||
	{"static_lease", read_staticlease, &(server_config.static_leases), ""},
 | 
						{"static_lease", read_staticlease, &(server_config.static_leases), ""},
 | 
				
			||||||
	/* ADDME: static lease */
 | 
						/* ADDME: static lease */
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@@ -315,28 +317,23 @@ static const struct config_keyword keywords[] = {
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
#define READ_CONFIG_BUF_SIZE 80
 | 
					#define READ_CONFIG_BUF_SIZE 80
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int read_config(const char *file)
 | 
					void read_config(const char *file)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	FILE *in;
 | 
						FILE *in;
 | 
				
			||||||
	char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
 | 
						char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
 | 
				
			||||||
	int i, lm = 0;
 | 
						int i, lineno;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (i = 0; i < ARRAY_SIZE(keywords); i++)
 | 
						for (i = 0; i < KWS_WITH_DEFAULTS; i++)
 | 
				
			||||||
		if (keywords[i].def[0])
 | 
							keywords[i].handler(keywords[i].def, keywords[i].var);
 | 
				
			||||||
			keywords[i].handler(keywords[i].def, keywords[i].var);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	in = fopen_or_warn(file, "r");
 | 
						in = fopen_or_warn(file, "r");
 | 
				
			||||||
	if (!in) {
 | 
						if (!in)
 | 
				
			||||||
		return 0;
 | 
							return;
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lineno = 0;
 | 
				
			||||||
	while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
 | 
						while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
 | 
				
			||||||
		char debug_orig[READ_CONFIG_BUF_SIZE];
 | 
							lineno++;
 | 
				
			||||||
 | 
							/* *strchrnul(buffer, '\n') = '\0'; - trim() will do it */
 | 
				
			||||||
		lm++;
 | 
					 | 
				
			||||||
		*strchrnul(buffer, '\n') = '\0';
 | 
					 | 
				
			||||||
		if (ENABLE_FEATURE_UDHCP_DEBUG)
 | 
					 | 
				
			||||||
			strcpy(debug_orig, buffer);
 | 
					 | 
				
			||||||
		*strchrnul(buffer, '#') = '\0';
 | 
							*strchrnul(buffer, '#') = '\0';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		token = strtok(buffer, " \t");
 | 
							token = strtok(buffer, " \t");
 | 
				
			||||||
@@ -344,29 +341,24 @@ int read_config(const char *file)
 | 
				
			|||||||
		line = strtok(NULL, "");
 | 
							line = strtok(NULL, "");
 | 
				
			||||||
		if (!line) continue;
 | 
							if (!line) continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		/* eat leading whitespace */
 | 
							trim(line); /* remove leading/trailing whitespace */
 | 
				
			||||||
		line = skip_whitespace(line);
 | 
					 | 
				
			||||||
		/* eat trailing whitespace */
 | 
					 | 
				
			||||||
		i = strlen(line) - 1;
 | 
					 | 
				
			||||||
		while (i >= 0 && isspace(line[i]))
 | 
					 | 
				
			||||||
			line[i--] = '\0';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for (i = 0; i < ARRAY_SIZE(keywords); i++)
 | 
							for (i = 0; i < ARRAY_SIZE(keywords); i++) {
 | 
				
			||||||
			if (!strcasecmp(token, keywords[i].keyword))
 | 
								if (!strcasecmp(token, keywords[i].keyword)) {
 | 
				
			||||||
				if (!keywords[i].handler(line, keywords[i].var)) {
 | 
									if (!keywords[i].handler(line, keywords[i].var)) {
 | 
				
			||||||
					bb_error_msg("cannot parse line %d of %s", lm, file);
 | 
										bb_error_msg("can't parse line %d in %s at '%s'",
 | 
				
			||||||
					if (ENABLE_FEATURE_UDHCP_DEBUG)
 | 
												lineno, file, line);
 | 
				
			||||||
						bb_error_msg("cannot parse '%s'", debug_orig);
 | 
					 | 
				
			||||||
					/* reset back to the default value */
 | 
										/* reset back to the default value */
 | 
				
			||||||
					keywords[i].handler(keywords[i].def, keywords[i].var);
 | 
										keywords[i].handler(keywords[i].def, keywords[i].var);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	fclose(in);
 | 
						fclose(in);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	server_config.start_ip = ntohl(server_config.start_ip);
 | 
						server_config.start_ip = ntohl(server_config.start_ip);
 | 
				
			||||||
	server_config.end_ip = ntohl(server_config.end_ip);
 | 
						server_config.end_ip = ntohl(server_config.end_ip);
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return 1;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user