udhcp: move options.c to common.c; disable unused bool and s16 option code

function                                             old     new   delta
udhcp_add_binary_option                                -      94     +94
udhcp_str2nip                                          -      42     +42
len_of_option_as_string                               12      10      -2
dhcp_option_lengths                                   12      10      -2
udhcpc_main                                         2859    2851      -8
read_nip                                              42       -     -42
xmalloc_optname_optval                               590     536     -54
udhcp_str2optset                                     443     366     -77
udhcp_add_option_string                               86       -     -86
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 0/5 up/down: 136/-271)         Total: -135 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2010-03-26 08:35:24 +01:00
parent 7e6add1dfc
commit a8f6b99987
8 changed files with 485 additions and 497 deletions

View File

@@ -22,18 +22,6 @@ static inline uint64_t hton64(uint64_t v)
#define ntoh64(v) hton64(v)
/* on these functions, make sure your datatype matches */
static int FAST_FUNC read_nip(const char *line, void *arg)
{
len_and_sockaddr *lsa;
lsa = host_and_af2sockaddr(line, 0, AF_INET);
if (!lsa)
return 0;
*(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
free(lsa);
return 1;
}
static int FAST_FUNC read_str(const char *line, void *arg)
{
char **dest = arg;
@@ -49,198 +37,6 @@ static int FAST_FUNC read_u32(const char *line, void *arg)
return errno == 0;
}
static int read_yn(const char *line, void *arg)
{
char *dest = arg;
if (strcasecmp("yes", line) == 0) {
*dest = 1;
return 1;
}
if (strcasecmp("no", line) == 0) {
*dest = 0;
return 1;
}
return 0;
}
/* find option 'code' in opt_list */
struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
{
while (opt_list && opt_list->data[OPT_CODE] < code)
opt_list = opt_list->next;
if (opt_list && opt_list->data[OPT_CODE] == code)
return opt_list;
return NULL;
}
/* add an option to the opt_list */
static NOINLINE void attach_option(
struct option_set **opt_list,
const struct dhcp_option *option,
char *buffer,
int length)
{
struct option_set *existing, *new, **curr;
#if ENABLE_FEATURE_UDHCP_RFC3397
char *allocated = NULL;
#endif
existing = find_option(*opt_list, option->code);
if (!existing) {
log2("Attaching option %02x to list", option->code);
#if ENABLE_FEATURE_UDHCP_RFC3397
if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) {
/* reuse buffer and length for RFC1035-formatted string */
allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length);
}
#endif
/* make a new option */
new = xmalloc(sizeof(*new));
new->data = xmalloc(length + OPT_DATA);
new->data[OPT_CODE] = option->code;
new->data[OPT_LEN] = length;
memcpy(new->data + OPT_DATA, buffer, length);
curr = opt_list;
while (*curr && (*curr)->data[OPT_CODE] < option->code)
curr = &(*curr)->next;
new->next = *curr;
*curr = new;
goto ret;
}
if (option->flags & OPTION_LIST) {
unsigned old_len;
/* add it to an existing option */
log1("Attaching option %02x to existing member of list", option->code);
old_len = existing->data[OPT_LEN];
#if ENABLE_FEATURE_UDHCP_RFC3397
if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) {
/* reuse buffer and length for RFC1035-formatted string */
allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length);
}
#endif
if (old_len + length < 255) {
/* actually 255 is ok too, but adding a space can overlow it */
existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length);
if ((option->flags & OPTION_TYPE_MASK) == OPTION_STRING) {
/* add space separator between STRING options in a list */
existing->data[OPT_DATA + old_len] = ' ';
old_len++;
}
memcpy(existing->data + OPT_DATA + old_len, buffer, length);
existing->data[OPT_LEN] = old_len + length;
} /* else, ignore the data, we could put this in a second option in the future */
} /* else, ignore the new data */
ret: ;
#if ENABLE_FEATURE_UDHCP_RFC3397
free(allocated);
#endif
}
/* read a dhcp option and add it to opt_list */
int FAST_FUNC udhcp_str2optset(const char *const_line, void *arg)
{
struct option_set **opt_list = arg;
char *opt, *val, *endptr;
char *line;
const struct dhcp_option *option;
int retval, length, idx;
char buffer[8] ALIGNED(4);
uint16_t *result_u16 = (uint16_t *) buffer;
uint32_t *result_u32 = (uint32_t *) buffer;
/* Cheat, the only *const* line possible is "" */
line = (char *) const_line;
opt = strtok(line, " \t=");
if (!opt)
return 0;
idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
if (idx < 0)
return 0;
option = &dhcp_options[idx];
retval = 0;
do {
val = strtok(NULL, ", \t");
if (!val)
break;
length = dhcp_option_lengths[option->flags & OPTION_TYPE_MASK];
retval = 0;
opt = buffer; /* new meaning for variable opt */
switch (option->flags & OPTION_TYPE_MASK) {
case OPTION_IP:
retval = read_nip(val, buffer);
break;
case OPTION_IP_PAIR:
retval = read_nip(val, buffer);
val = strtok(NULL, ", \t/-");
if (!val)
retval = 0;
if (retval)
retval = read_nip(val, buffer + 4);
break;
case OPTION_STRING:
#if ENABLE_FEATURE_UDHCP_RFC3397
case OPTION_STR1035:
#endif
length = strnlen(val, 254);
if (length > 0) {
opt = val;
retval = 1;
}
break;
case OPTION_BOOLEAN:
retval = read_yn(val, buffer);
break;
case OPTION_U8:
buffer[0] = strtoul(val, &endptr, 0);
retval = (endptr[0] == '\0');
break;
/* htonX are macros in older libc's, using temp var
* in code below for safety */
/* TODO: use bb_strtoX? */
case OPTION_U16: {
unsigned long tmp = strtoul(val, &endptr, 0);
*result_u16 = htons(tmp);
retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
break;
}
case OPTION_S16: {
long tmp = strtol(val, &endptr, 0);
*result_u16 = htons(tmp);
retval = (endptr[0] == '\0');
break;
}
case OPTION_U32: {
unsigned long tmp = strtoul(val, &endptr, 0);
*result_u32 = htonl(tmp);
retval = (endptr[0] == '\0');
break;
}
case OPTION_S32: {
long tmp = strtol(val, &endptr, 0);
*result_u32 = htonl(tmp);
retval = (endptr[0] == '\0');
break;
}
default:
break;
}
if (retval)
attach_option(opt_list, option, opt, length);
} while (retval && option->flags & OPTION_LIST);
return retval;
}
static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
{
char *line;
@@ -257,7 +53,7 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
/* Read ip */
ip_string = strtok_r(NULL, " \t", &line);
if (!ip_string || !read_nip(ip_string, &nip))
if (!ip_string || !udhcp_str2nip(ip_string, &nip))
return 0;
add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
@@ -277,8 +73,8 @@ struct config_keyword {
static const struct config_keyword keywords[] = {
/* keyword handler variable address default */
{"start", read_nip, &(server_config.start_ip), "192.168.0.20"},
{"end", read_nip, &(server_config.end_ip), "192.168.0.254"},
{"start", udhcp_str2nip, &(server_config.start_ip), "192.168.0.20"},
{"end", udhcp_str2nip, &(server_config.end_ip), "192.168.0.254"},
{"interface", read_str, &(server_config.interface), "eth0"},
/* Avoid "max_leases value not sane" warning by setting default
* to default_end_ip - default_start_ip + 1: */
@@ -290,7 +86,7 @@ static const struct config_keyword keywords[] = {
{"min_lease", read_u32, &(server_config.min_lease_sec),"60"},
{"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
{"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
{"siaddr", read_nip, &(server_config.siaddr_nip), "0.0.0.0"},
{"siaddr", udhcp_str2nip, &(server_config.siaddr_nip), "0.0.0.0"},
/* keywords with no defaults must be last! */
{"option", udhcp_str2optset, &(server_config.options), ""},
{"opt", udhcp_str2optset, &(server_config.options), ""},