diff mbox series

[PATCHv4,5/5] net/lwip: apps/http: add dns support

Message ID 20230714142000.5534-6-maxim.uvarov@linaro.org
State New
Headers show
Series net/lwip: add lwip library for the network stack | expand

Commit Message

Maxim Uvarov July 14, 2023, 2:20 p.m. UTC
provide hostname to client and allow lwip to resolve it.

Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
 lib/lwip/apps/http/lwip-wget.c | 59 ++++++++++++++++++++++++++++++----
 1 file changed, 53 insertions(+), 6 deletions(-)

Comments

Ilias Apalodimas July 27, 2023, 12:59 p.m. UTC | #1
On Fri, Jul 14, 2023 at 08:20:00PM +0600, Maxim Uvarov wrote:
> provide hostname to client and allow lwip to resolve it.
> 
> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> ---
>  lib/lwip/apps/http/lwip-wget.c | 59 ++++++++++++++++++++++++++++++----
>  1 file changed, 53 insertions(+), 6 deletions(-)
> 
> diff --git a/lib/lwip/apps/http/lwip-wget.c b/lib/lwip/apps/http/lwip-wget.c
> index 0308b0b04a..db553a0f14 100644
> --- a/lib/lwip/apps/http/lwip-wget.c
> +++ b/lib/lwip/apps/http/lwip-wget.c
> @@ -45,18 +45,65 @@ static void httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_conten
>  	}
>  }
>  
> +/* http://hostname:port/url */
> +static int parse_url(char *url, char *host, int *port)
> +{
> +	int ret;
> +	char *p, *pp;
> +
> +	p = strstr(url, "http://");
> +	if (!p) {
> +		printf("err: no http://!\n");
> +		return -1;
> +	}
> +
> +	p += strlen("http://");
> +
> +	/* parse hostname */
> +	pp = strchr(p, ':');
> +	if (pp) {
> +		char portstr[5];
> +
> +		memcpy(host, p, pp - p);

You are copying bytes over without taking into account the length of the
array. Also, I think we should keep things as simple as possible for the
first iteration, so can we ignore the port config for now and just stick to
80?

Thanks
/Ilias
> +		host[pp - p + 1] = '\0';
> +
> +		p = pp + 1;
> +		pp = strchr(p, '/');
> +		if (!pp) {
> +			printf("wrong url\n");
> +			return -2;
> +		}
> +
> +		memcpy(portstr, p, pp - p);
> +		portstr[pp - p] = '\0';
> +		*port = atoi(portstr);
> +	} else {
> +		pp = strchr(p, '/');
> +		if (!pp) {
> +			printf("wrong url\n");
> +			return -3;
> +		}
> +		memcpy(host, p, pp - p);
> +		host[pp - p + 1] = '\0';
> +		*port = 80; /* default */
> +	}
> +
> +	return 0;
> +}
> +
>  int lwip_wget(ulong addr, char *url)
>  {
>  	err_t err;
> -	int port = 80;
> -	char *server_name;
> +	int port;
> +	char server_name[80];
>  	httpc_state_t *connection;
>  
>  	daddr = addr;
> -	server_name = env_get("serverip");
> -	if (!server_name) {
> -		printf("error: serverip variable has to be set\n");
> -		return CMD_RET_FAILURE;
> +
> +	err = parse_url(url, server_name, &port);
> +	if (err) {
> +		printf("error parse_url\n");
> +		return -1;
>  	}
>  
>  	printf("downloading %s to addr 0x%lx\n", url, addr);
> -- 
> 2.30.2
>
diff mbox series

Patch

diff --git a/lib/lwip/apps/http/lwip-wget.c b/lib/lwip/apps/http/lwip-wget.c
index 0308b0b04a..db553a0f14 100644
--- a/lib/lwip/apps/http/lwip-wget.c
+++ b/lib/lwip/apps/http/lwip-wget.c
@@ -45,18 +45,65 @@  static void httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_conten
 	}
 }
 
+/* http://hostname:port/url */
+static int parse_url(char *url, char *host, int *port)
+{
+	int ret;
+	char *p, *pp;
+
+	p = strstr(url, "http://");
+	if (!p) {
+		printf("err: no http://!\n");
+		return -1;
+	}
+
+	p += strlen("http://");
+
+	/* parse hostname */
+	pp = strchr(p, ':');
+	if (pp) {
+		char portstr[5];
+
+		memcpy(host, p, pp - p);
+		host[pp - p + 1] = '\0';
+
+		p = pp + 1;
+		pp = strchr(p, '/');
+		if (!pp) {
+			printf("wrong url\n");
+			return -2;
+		}
+
+		memcpy(portstr, p, pp - p);
+		portstr[pp - p] = '\0';
+		*port = atoi(portstr);
+	} else {
+		pp = strchr(p, '/');
+		if (!pp) {
+			printf("wrong url\n");
+			return -3;
+		}
+		memcpy(host, p, pp - p);
+		host[pp - p + 1] = '\0';
+		*port = 80; /* default */
+	}
+
+	return 0;
+}
+
 int lwip_wget(ulong addr, char *url)
 {
 	err_t err;
-	int port = 80;
-	char *server_name;
+	int port;
+	char server_name[80];
 	httpc_state_t *connection;
 
 	daddr = addr;
-	server_name = env_get("serverip");
-	if (!server_name) {
-		printf("error: serverip variable has to be set\n");
-		return CMD_RET_FAILURE;
+
+	err = parse_url(url, server_name, &port);
+	if (err) {
+		printf("error parse_url\n");
+		return -1;
 	}
 
 	printf("downloading %s to addr 0x%lx\n", url, addr);