udhcp: abort if we see unknown option, and show valid options if so

function                                             old     new   delta
udhcp_option_idx                                       -      77     +77
udhcp_str2optset                                     366     351     -15
udhcpc_main                                         2845    2801     -44
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 77/-59)             Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-03-27 23:23:41 +01:00
parent c2fdd41f5f
commit 9107b63a7f
3 changed files with 26 additions and 12 deletions

View File

@ -146,6 +146,25 @@ static void log_option(const char *pfx, const uint8_t *opt)
# define log_option(pfx, opt) ((void)0) # define log_option(pfx, opt) ((void)0)
#endif #endif
unsigned FAST_FUNC udhcp_option_idx(const char *name)
{
int n = index_in_strings(dhcp_option_strings, name);
if (n >= 0)
return n;
{
char buf[sizeof(dhcp_option_strings)];
char *d = buf;
const char *s = dhcp_option_strings;
while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) {
*d++ = (*s == '\0' ? ' ' : *s);
s++;
}
*d = '\0';
bb_error_msg_and_die("unknown option '%s', known options: %s", name, buf);
}
}
/* get an option with bounds checking (warning, result is not aligned). */ /* get an option with bounds checking (warning, result is not aligned). */
uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code) uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code)
{ {
@ -372,7 +391,7 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg)
char *opt, *val, *endptr; char *opt, *val, *endptr;
char *str; char *str;
const struct dhcp_option *option; const struct dhcp_option *option;
int retval, length, idx; int retval, length;
char buffer[8] ALIGNED(4); char buffer[8] ALIGNED(4);
uint16_t *result_u16 = (uint16_t *) buffer; uint16_t *result_u16 = (uint16_t *) buffer;
uint32_t *result_u32 = (uint32_t *) buffer; uint32_t *result_u32 = (uint32_t *) buffer;
@ -383,10 +402,7 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg)
if (!opt) if (!opt)
return 0; return 0;
idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */ option = &dhcp_options[udhcp_option_idx(opt)];
if (idx < 0)
return 0;
option = &dhcp_options[idx];
retval = 0; retval = 0;
do { do {

View File

@ -169,6 +169,8 @@ extern const struct dhcp_option dhcp_options[];
extern const char dhcp_option_strings[]; extern const char dhcp_option_strings[];
extern const uint8_t dhcp_option_lengths[]; extern const uint8_t dhcp_option_lengths[];
unsigned FAST_FUNC udhcp_option_idx(const char *name);
uint8_t *udhcp_get_option(struct dhcp_packet *packet, int code) FAST_FUNC; uint8_t *udhcp_get_option(struct dhcp_packet *packet, int code) FAST_FUNC;
int udhcp_end_option(uint8_t *optionptr) FAST_FUNC; int udhcp_end_option(uint8_t *optionptr) FAST_FUNC;
void udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) FAST_FUNC; void udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) FAST_FUNC;

View File

@ -879,21 +879,17 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
client_config.no_default_options = 1; client_config.no_default_options = 1;
while (list_O) { while (list_O) {
char *optstr = llist_pop(&list_O); char *optstr = llist_pop(&list_O);
int n = index_in_strings(dhcp_option_strings, optstr); unsigned n = udhcp_option_idx(optstr);
if (n < 0)
bb_error_msg_and_die("unknown option '%s'", optstr);
n = dhcp_options[n].code; n = dhcp_options[n].code;
client_config.opt_mask[n >> 3] |= 1 << (n & 7); client_config.opt_mask[n >> 3] |= 1 << (n & 7);
} }
while (list_x) { while (list_x) {
int n; unsigned n;
char *optstr = llist_pop(&list_x); char *optstr = llist_pop(&list_x);
char *colon = strchr(optstr, ':'); char *colon = strchr(optstr, ':');
if (colon) if (colon)
*colon = '\0'; *colon = '\0';
n = index_in_strings(dhcp_option_strings, optstr); n = udhcp_option_idx(optstr);
if (n < 0)
bb_error_msg_and_die("unknown option '%s'", optstr);
if (colon) if (colon)
*colon = ' '; *colon = ' ';
udhcp_str2optset(optstr, &client_config.options); udhcp_str2optset(optstr, &client_config.options);