[PATCH 1/8] net: dhcp: factorise option recption handling

Sascha Hauer s.hauer at pengutronix.de
Mon Apr 2 15:02:46 EDT 2012


On Mon, Apr 02, 2012 at 04:19:03PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> Instead of using a static switch case the handle the received option
> use an array of supported option.
> 
> This will allow to unset the env var without duplicating the code.
> 
> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>

Much better, thanks.

> +struct dhcp_opt {
> +	unsigned char option;
> +	const char *barebox_var_name;
> +	void (*handle)(struct dhcp_opt *opt, unsigned char *data, int tlen);
> +	void *data;
> +
> +	/* request automatically the option when creating the DHCP request */
> +	bool optinal;

s/optinal/optional/?

You can optimize the size of this struct a bit by putting this bool
after the unsigned char above.

> +
> +	struct bootp *bp;
> +};
> +
> +static void netmask_handle(struct dhcp_opt *opt, unsigned char *popt, int optlen)
> +{
> +	IPaddr_t ip;
> +
> +	ip = net_read_ip(popt);
> +	net_set_netmask(ip);
> +}
> +
> +static void gateway_handle(struct dhcp_opt *opt, unsigned char *popt, int optlen)
> +{
> +	IPaddr_t ip;
> +
> +	ip = net_read_ip(popt);
> +	net_set_gateway(ip);
> +}
> +
> +static void env_ip_handle(struct dhcp_opt *opt, unsigned char *popt, int optlen)
> +{
> +	IPaddr_t ip;
> +
> +	if (!opt->barebox_var_name) {
> +		pr_err("dhcp: %s: option %d no barebox_var_name set\n",
> +		       __func__, opt->option);
> +		return;
> +	}

I think we don't need this check. It is easy enough to review the code
to not have this bug.

> +
> +	ip = net_read_ip(popt);
> +	setenv_ip(opt->barebox_var_name, ip);
> +}
> +
> +static void env_str_handle(struct dhcp_opt *opt, unsigned char *popt, int optlen)
> +{
> +	char str[256];
> +
> +	if (!opt->barebox_var_name) {
> +		pr_err("dhcp: %s: option %d no barebox_var_name set\n",
> +		       __func__, opt->option);
> +		return;
> +	}

ditto

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list