diff mbox series

[07/17] net: ipv6: Add ip6addr, gatewayip6, serverip6 variables callbacks

Message ID 20220830123104.256224-8-v.v.mitrofanov@yadro.com
State Superseded
Delegated to: Ramon Fried
Headers show
Series IPv6 support | expand

Commit Message

Vyacheslav V. Mitrofanov Aug. 30, 2022, 12:30 p.m. UTC
Implement actions on ip6addr, gatewayip6, serverip6 varaibles.
on_ip6addr - convert IPv6 string addr to struct ip6_addr
on_gatewayip6 - convert IPv6 string addr to struct ip6_addr
on_serverip6 - convert IPv6 string addr to struct ip6_addr

Signed-off-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
---
 net/net6.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

Comments

Simon Glass Sept. 1, 2022, 2:26 a.m. UTC | #1
Hi Viacheslav,

On Tue, 30 Aug 2022 at 07:01, Viacheslav Mitrofanov
<v.v.mitrofanov@yadro.com> wrote:
>
> Implement actions on ip6addr, gatewayip6, serverip6 varaibles.
> on_ip6addr - convert IPv6 string addr to struct ip6_addr
> on_gatewayip6 - convert IPv6 string addr to struct ip6_addr
> on_serverip6 - convert IPv6 string addr to struct ip6_addr
>
> Signed-off-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
> ---
>  net/net6.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 67 insertions(+)
>
> diff --git a/net/net6.c b/net/net6.c
> index c2569e0e68..a0410ea8ba 100644
> --- a/net/net6.c
> +++ b/net/net6.c
> @@ -31,3 +31,70 @@ struct in6_addr net_server_ip6 = ZERO_IPV6_ADDR;
>  u32 net_prefix_length;
>
>  bool use_ip6;
> +
> +static int on_ip6addr(const char *name, const char *value, enum env_op op,
> +                     int flags)
> +{
> +       char *v, *s, *strcopy;
> +       int i;
> +
> +       if (flags & H_PROGRAMMATIC)
> +               return 0;
> +
> +       if (op == env_op_delete) {
> +               net_prefix_length = 0;
> +               net_copy_ip6(&net_ip6, &net_null_addr_ip6);
> +               return 0;
> +       }
> +
> +       strcopy = strdup(value);
> +       if (!strcopy)
> +               return -1;

-ENOMEM

But is it possible to avoid memory allocation here? Perhaps just use
strchr() below instead?

> +
> +       net_prefix_length = 128;
> +       i = 0;
> +       s = strcopy;
> +       while (s) {
> +               v = strsep(&s, "/");
> +               if (!v)
> +                       break;
> +
> +               switch (i++) {
> +               case 0:
> +                       string_to_ip6(v, &net_ip6);
> +                       break;
> +               case 1:
> +                       net_prefix_length = simple_strtoul(v, NULL, 10);
> +                       break;
> +               default:
> +                       break;
> +               }
> +       }

Can the above code go into a function and get a unit test?

> +       free(strcopy);
> +
> +       return 0;
> +}
> +
> +U_BOOT_ENV_CALLBACK(ip6addr, on_ip6addr);
> +
> +static int on_gatewayip6(const char *name, const char *value, enum env_op op,
> +                        int flags)
> +{
> +       if (flags & H_PROGRAMMATIC)
> +               return 0;
> +
> +       return string_to_ip6(value, &net_gateway6);
> +}
> +
> +U_BOOT_ENV_CALLBACK(gatewayip6, on_gatewayip6);
> +
> +static int on_serverip6(const char *name, const char *value, enum env_op op,
> +                       int flags)
> +{
> +       if (flags & H_PROGRAMMATIC)
> +               return 0;
> +
> +       return string_to_ip6(value, &net_server_ip6);
> +}
> +
> +U_BOOT_ENV_CALLBACK(serverip6, on_serverip6);
> --
> 2.25.1
>

Regards,
Simon
diff mbox series

Patch

diff --git a/net/net6.c b/net/net6.c
index c2569e0e68..a0410ea8ba 100644
--- a/net/net6.c
+++ b/net/net6.c
@@ -31,3 +31,70 @@  struct in6_addr net_server_ip6 = ZERO_IPV6_ADDR;
 u32 net_prefix_length;
 
 bool use_ip6;
+
+static int on_ip6addr(const char *name, const char *value, enum env_op op,
+		      int flags)
+{
+	char *v, *s, *strcopy;
+	int i;
+
+	if (flags & H_PROGRAMMATIC)
+		return 0;
+
+	if (op == env_op_delete) {
+		net_prefix_length = 0;
+		net_copy_ip6(&net_ip6, &net_null_addr_ip6);
+		return 0;
+	}
+
+	strcopy = strdup(value);
+	if (!strcopy)
+		return -1;
+
+	net_prefix_length = 128;
+	i = 0;
+	s = strcopy;
+	while (s) {
+		v = strsep(&s, "/");
+		if (!v)
+			break;
+
+		switch (i++) {
+		case 0:
+			string_to_ip6(v, &net_ip6);
+			break;
+		case 1:
+			net_prefix_length = simple_strtoul(v, NULL, 10);
+			break;
+		default:
+			break;
+		}
+	}
+	free(strcopy);
+
+	return 0;
+}
+
+U_BOOT_ENV_CALLBACK(ip6addr, on_ip6addr);
+
+static int on_gatewayip6(const char *name, const char *value, enum env_op op,
+			 int flags)
+{
+	if (flags & H_PROGRAMMATIC)
+		return 0;
+
+	return string_to_ip6(value, &net_gateway6);
+}
+
+U_BOOT_ENV_CALLBACK(gatewayip6, on_gatewayip6);
+
+static int on_serverip6(const char *name, const char *value, enum env_op op,
+			int flags)
+{
+	if (flags & H_PROGRAMMATIC)
+		return 0;
+
+	return string_to_ip6(value, &net_server_ip6);
+}
+
+U_BOOT_ENV_CALLBACK(serverip6, on_serverip6);