mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] net: dhcp: unify options and params
@ 2015-12-14 10:41 Sascha Hauer
  2015-12-14 10:41 ` [PATCH 2/4] net: dhcp: simplify dhcp_options_process Sascha Hauer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sascha Hauer @ 2015-12-14 10:41 UTC (permalink / raw)
  To: Barebox List

The dhcp code distinguishes between options and params. If sent to the
server they are params, if received from the server they are options.
Unify them all to be options. If it has a handle_param callback it's
a param, if it has a handle callback it's an option.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/dhcp.c | 134 ++++++++++++++++++++++++++-----------------------------------
 1 file changed, 57 insertions(+), 77 deletions(-)

diff --git a/net/dhcp.c b/net/dhcp.c
index fb8a713..fa4c4d1 100644
--- a/net/dhcp.c
+++ b/net/dhcp.c
@@ -115,6 +115,7 @@ struct dhcp_opt {
 	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,6 +201,37 @@ static void bootfile_vendorex_handle(struct dhcp_opt *opt, unsigned char *popt,
 	env_str_handle(opt, popt, optlen);
 }
 
+static int dhcp_set_string_options(struct dhcp_opt *param, u8 *e)
+{
+	int str_len;
+	char* str = param->data;
+
+	if (!str && param->barebox_var_name && IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES))
+		str = (char*)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);
+
+	if (!str)
+		return 0;
+
+	str_len = strlen(str);
+	if (!str_len)
+		return 0;
+
+	*e++ = param->option;
+	*e++ = str_len;
+	memcpy(e, str, str_len);
+
+	return str_len + 2;
+}
+
+#define DHCP_HOSTNAME		12
+#define DHCP_VENDOR_ID		60
+#define DHCP_CLIENT_ID		61
+#define DHCP_USER_CLASS		77
+#define DHCP_CLIENT_UUID	97
+
 struct dhcp_opt dhcp_options[] = {
 	{
 		.option = 1,
@@ -212,9 +244,10 @@ struct dhcp_opt dhcp_options[] = {
 		.handle = env_ip_handle,
 		.barebox_var_name = "net.nameserver",
 	}, {
-		.option = 12,
+		.option = DHCP_HOSTNAME,
 		.copy_only_if_valid = 1,
 		.handle = env_str_handle,
+		.handle_param = dhcp_set_string_options,
 		.barebox_var_name = "global.hostname",
 	}, {
 		.option = 15,
@@ -234,6 +267,10 @@ struct dhcp_opt dhcp_options[] = {
 		.data = &net_dhcp_server_ip,
 		.optional = true,
 	}, {
+		.option = DHCP_VENDOR_ID,
+		.handle_param = dhcp_set_string_options,
+		.barebox_dhcp_global = "vendor_id",
+	},{
 		.option = 66,
 		.handle = env_str_handle,
 		.barebox_dhcp_global = "tftp_server_name",
@@ -243,85 +280,34 @@ struct dhcp_opt dhcp_options[] = {
 		.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)
-{
-	int str_len;
-	char* str = param->data;
-
-	if (!str && param->barebox_var_name && IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES))
-		str = (char*)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);
-
-	if (!str)
-		return 0;
-
-	str_len = strlen(str);
-	if (!str_len)
-		return 0;
-
-	*e++ = param->option;
-	*e++ = str_len;
-	memcpy(e, str, str_len);
-
-	return str_len + 2;
-}
-
-#define DHCP_HOSTNAME		12
-#define DHCP_VENDOR_ID		60
-#define DHCP_CLIENT_ID		61
-#define DHCP_USER_CLASS		77
-#define DHCP_CLIENT_UUID	97
-
-struct dhcp_param dhcp_params[] = {
-	{
-		.option = DHCP_HOSTNAME,
-		.handle = dhcp_set_string_options,
-		.barebox_var_name = "global.hostname",
-	}, {
-		.option = DHCP_VENDOR_ID,
-		.handle = dhcp_set_string_options,
-		.barebox_dhcp_global = "vendor_id",
-	}, {
 		.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 void bootp_copy_net_params(struct bootp *bp)
 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 @@ static int dhcp_extended (u8 *e, int message_type, IPaddr_t ServerID,
 	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 */
@@ -742,7 +732,6 @@ static void dhcp_global_add(const char *var)
 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 +743,6 @@ static int dhcp_global_init(void)
 		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);
-- 
2.6.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/4] net: dhcp: simplify dhcp_options_process
  2015-12-14 10:41 [PATCH 1/4] net: dhcp: unify options and params Sascha Hauer
@ 2015-12-14 10:41 ` Sascha Hauer
  2015-12-14 10:41 ` [PATCH 3/4] net: dhcp: make unmodified variable const Sascha Hauer
  2015-12-14 10:41 ` [PATCH 4/4] net: resolv: Make argument const Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2015-12-14 10:41 UTC (permalink / raw)
  To: Barebox List

dhcp_options_handle returns the index into the dhcp_options array.
This is only to be able in the caller to print a debug message when
the index returned from dhcp_options_handle is out of bounds.
Simplify this by printing the debug message in dhcp_options_handle
and not in the caller.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/dhcp.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/net/dhcp.c b/net/dhcp.c
index fa4c4d1..78e5fe0 100644
--- a/net/dhcp.c
+++ b/net/dhcp.c
@@ -482,7 +482,7 @@ static int bootp_request(void)
 	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;
@@ -492,13 +492,13 @@ static int dhcp_options_handle(unsigned char option, unsigned char *popt,
 		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)
@@ -506,15 +506,12 @@ static void dhcp_options_process(unsigned char *popt, struct bootp *bp)
 	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 */
 	}
-- 
2.6.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/4] net: dhcp: make unmodified variable const
  2015-12-14 10:41 [PATCH 1/4] net: dhcp: unify options and params Sascha Hauer
  2015-12-14 10:41 ` [PATCH 2/4] net: dhcp: simplify dhcp_options_process Sascha Hauer
@ 2015-12-14 10:41 ` Sascha Hauer
  2015-12-14 10:41 ` [PATCH 4/4] net: resolv: Make argument const Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2015-12-14 10:41 UTC (permalink / raw)
  To: Barebox List

'str' in dhcp_set_string_options is never modified, so make it const.
With this we no longer have to cast away the const returned from getenv.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/dhcp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/net/dhcp.c b/net/dhcp.c
index 78e5fe0..4433b44 100644
--- a/net/dhcp.c
+++ b/net/dhcp.c
@@ -204,13 +204,13 @@ static void bootfile_vendorex_handle(struct dhcp_opt *opt, unsigned char *popt,
 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;
-- 
2.6.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 4/4] net: resolv: Make argument const
  2015-12-14 10:41 [PATCH 1/4] net: dhcp: unify options and params Sascha Hauer
  2015-12-14 10:41 ` [PATCH 2/4] net: dhcp: simplify dhcp_options_process Sascha Hauer
  2015-12-14 10:41 ` [PATCH 3/4] net: dhcp: make unmodified variable const Sascha Hauer
@ 2015-12-14 10:41 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2015-12-14 10:41 UTC (permalink / raw)
  To: Barebox List

resolv() is not allowed to change the hostname argument, make it const.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/net.h | 4 ++--
 net/dns.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

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 @@ int string_to_ethaddr(const char *str, u8 enetaddr[6]);
 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/dns.c b/net/dns.c
index 0e16ea2..5488e9f 100644
--- a/net/dns.c
+++ b/net/dns.c
@@ -59,7 +59,7 @@ static uint64_t dns_timer_start;
 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 @@ static void dns_handler(void *ctx, char *packet, unsigned len)
 		net_eth_to_udplen(packet));
 }
 
-IPaddr_t resolv(char *host)
+IPaddr_t resolv(const char *host)
 {
 	IPaddr_t ip;
 	const char *ns;
-- 
2.6.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-12-14 10:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-14 10:41 [PATCH 1/4] net: dhcp: unify options and params Sascha Hauer
2015-12-14 10:41 ` [PATCH 2/4] net: dhcp: simplify dhcp_options_process Sascha Hauer
2015-12-14 10:41 ` [PATCH 3/4] net: dhcp: make unmodified variable const Sascha Hauer
2015-12-14 10:41 ` [PATCH 4/4] net: resolv: Make argument const Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox