diff --git a/include/net.h b/include/net.h index 2a37a43..13e335a 100644 --- a/include/net.h +++ b/include/net.h @@ -315,9 +315,9 @@ void ethaddr_to_string(const u8 enetaddr[6], char *str); #ifdef CONFIG_NET_RESOLV -IPaddr_t resolv(char *host); +IPaddr_t resolv(const char *host); #else -static inline IPaddr_t resolv(char *host) +static inline IPaddr_t resolv(const char *host) { IPaddr_t ip = 0; string_to_ip(host, &ip); diff --git a/net/dhcp.c b/net/dhcp.c index fb8a713..4433b44 100644 --- a/net/dhcp.c +++ b/net/dhcp.c @@ -115,6 +115,7 @@ const char *barebox_var_name; const char *barebox_dhcp_global; void (*handle)(struct dhcp_opt *opt, unsigned char *data, int tlen); + int (*handle_param)(struct dhcp_opt *dhcp_opt, u8 *e); void *data; struct bootp *bp; @@ -200,73 +201,16 @@ env_str_handle(opt, popt, optlen); } -struct dhcp_opt dhcp_options[] = { - { - .option = 1, - .handle = netmask_handle, - }, { - .option = 3, - .handle = gateway_handle, - }, { - .option = 6, - .handle = env_ip_handle, - .barebox_var_name = "net.nameserver", - }, { - .option = 12, - .copy_only_if_valid = 1, - .handle = env_str_handle, - .barebox_var_name = "global.hostname", - }, { - .option = 15, - .handle = env_str_handle, - .barebox_var_name = "net.domainname", - }, { - .option = 17, - .handle = env_str_handle, - .barebox_dhcp_global = "rootpath", - }, { - .option = 51, - .handle = copy_uint32_handle, - .data = &dhcp_leasetime, - }, { - .option = 54, - .handle = copy_ip_handle, - .data = &net_dhcp_server_ip, - .optional = true, - }, { - .option = 66, - .handle = env_str_handle, - .barebox_dhcp_global = "tftp_server_name", - .data = dhcp_tftpname, - }, { - .option = 67, - .handle = bootfile_vendorex_handle, - .barebox_dhcp_global = "bootfile", - }, { - .option = 224, - .handle = env_str_handle, - .barebox_dhcp_global = "oftree_file", - }, -}; - -struct dhcp_param { - unsigned char option; - const char *barebox_var_name; - const char *barebox_dhcp_global; - int (*handle)(struct dhcp_param *param, u8 *e); - void *data; -}; - -static int dhcp_set_string_options(struct dhcp_param *param, u8 *e) +static int dhcp_set_string_options(struct dhcp_opt *param, u8 *e) { int str_len; - char* str = param->data; + const char *str = param->data; if (!str && param->barebox_var_name && IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES)) - str = (char*)getenv(param->barebox_var_name); + str = getenv(param->barebox_var_name); if (!str && param->barebox_dhcp_global && IS_ENABLED(CONFIG_GLOBALVAR)) - str = (char*)dhcp_get_barebox_global(param->barebox_dhcp_global); + str = dhcp_get_barebox_global(param->barebox_dhcp_global); if (!str) return 0; @@ -288,40 +232,82 @@ #define DHCP_USER_CLASS 77 #define DHCP_CLIENT_UUID 97 -struct dhcp_param dhcp_params[] = { +struct dhcp_opt dhcp_options[] = { { + .option = 1, + .handle = netmask_handle, + }, { + .option = 3, + .handle = gateway_handle, + }, { + .option = 6, + .handle = env_ip_handle, + .barebox_var_name = "net.nameserver", + }, { .option = DHCP_HOSTNAME, - .handle = dhcp_set_string_options, + .copy_only_if_valid = 1, + .handle = env_str_handle, + .handle_param = dhcp_set_string_options, .barebox_var_name = "global.hostname", }, { + .option = 15, + .handle = env_str_handle, + .barebox_var_name = "net.domainname", + }, { + .option = 17, + .handle = env_str_handle, + .barebox_dhcp_global = "rootpath", + }, { + .option = 51, + .handle = copy_uint32_handle, + .data = &dhcp_leasetime, + }, { + .option = 54, + .handle = copy_ip_handle, + .data = &net_dhcp_server_ip, + .optional = true, + }, { .option = DHCP_VENDOR_ID, - .handle = dhcp_set_string_options, + .handle_param = dhcp_set_string_options, .barebox_dhcp_global = "vendor_id", + },{ + .option = 66, + .handle = env_str_handle, + .barebox_dhcp_global = "tftp_server_name", + .data = dhcp_tftpname, + }, { + .option = 67, + .handle = bootfile_vendorex_handle, + .barebox_dhcp_global = "bootfile", }, { .option = DHCP_CLIENT_ID, - .handle = dhcp_set_string_options, + .handle_param = dhcp_set_string_options, .barebox_dhcp_global = "client_id", }, { .option = DHCP_USER_CLASS, - .handle = dhcp_set_string_options, + .handle_param = dhcp_set_string_options, .barebox_dhcp_global = "user_class", }, { .option = DHCP_CLIENT_UUID, - .handle = dhcp_set_string_options, + .handle_param = dhcp_set_string_options, .barebox_dhcp_global = "client_uuid", - } + }, { + .option = 224, + .handle = env_str_handle, + .barebox_dhcp_global = "oftree_file", + }, }; static void dhcp_set_param_data(int option, void* data) { - struct dhcp_param *param; + struct dhcp_opt *opt; int i; - for (i = 0; i < ARRAY_SIZE(dhcp_params); i++) { - param = &dhcp_params[i]; + for (i = 0; i < ARRAY_SIZE(dhcp_options); i++) { + opt = &dhcp_options[i]; - if (param->option == option) { - param->data = data; + if (opt->option == option) { + opt->data = data; return; } } @@ -405,6 +391,7 @@ static int dhcp_extended (u8 *e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) { + struct dhcp_opt *opt; int i; u8 *start = e; u8 *cnt; @@ -427,8 +414,11 @@ e += dhcp_set_ip_options(50, e, RequestedIP); e += dhcp_set_ip_options(54, e, ServerID); - for (i = 0; i < ARRAY_SIZE(dhcp_params); i++) - e += dhcp_params[i].handle(&dhcp_params[i], e); + for (i = 0; i < ARRAY_SIZE(dhcp_options); i++) { + opt = &dhcp_options[i]; + if (opt->handle_param) + e += opt->handle_param(opt, e); + } *e++ = 55; /* Parameter Request List */ cnt = e++; /* Pointer to count of requested items */ @@ -492,7 +482,7 @@ return ret; } -static int dhcp_options_handle(unsigned char option, unsigned char *popt, +static void dhcp_options_handle(unsigned char option, unsigned char *popt, int optlen, struct bootp *bp) { int i; @@ -502,13 +492,13 @@ opt = &dhcp_options[i]; if (opt->option == option) { opt->bp = bp; - opt->handle(opt, popt, optlen); - goto end; + if (opt->handle) + opt->handle(opt, popt, optlen); + return; } } -end: - return i; + debug("*** Unhandled DHCP Option in OFFER/ACK: %d\n", option); } static void dhcp_options_process(unsigned char *popt, struct bootp *bp) @@ -516,15 +506,12 @@ unsigned char *end = popt + sizeof(*bp) + OPT_SIZE; int oplen; unsigned char option; - int i; while (popt < end && *popt != 0xff) { oplen = *(popt + 1); option = *popt; - i = dhcp_options_handle(option, popt + 2, oplen, bp); - if (i == ARRAY_SIZE(dhcp_options)) - debug("*** Unhandled DHCP Option in OFFER/ACK: %d\n", option); + dhcp_options_handle(option, popt + 2, oplen, bp); popt += oplen + 2; /* Process next option */ } @@ -742,7 +729,6 @@ static int dhcp_global_init(void) { struct dhcp_opt *opt; - struct dhcp_param *param; int i; for (i = 0; i < ARRAY_SIZE(dhcp_options); i++) { @@ -754,15 +740,6 @@ dhcp_global_add(opt->barebox_dhcp_global); } - for (i = 0; i < ARRAY_SIZE(dhcp_params); i++) { - param = &dhcp_params[i]; - - if (!param->barebox_dhcp_global) - continue; - - dhcp_global_add(param->barebox_dhcp_global); - } - return 0; } late_initcall(dhcp_global_init); diff --git a/net/dns.c b/net/dns.c index 0e16ea2..5488e9f 100644 --- a/net/dns.c +++ b/net/dns.c @@ -59,7 +59,7 @@ static int dns_state; static IPaddr_t dns_ip; -static int dns_send(char *name) +static int dns_send(const char *name) { int ret; struct header *header; @@ -199,7 +199,7 @@ net_eth_to_udplen(packet)); } -IPaddr_t resolv(char *host) +IPaddr_t resolv(const char *host) { IPaddr_t ip; const char *ns;