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

Maxim Uvarov maxim.uvarov at linaro.org
Fri Jul 14 16:20:00 CEST 2023


provide hostname to client and allow lwip to resolve it.

Signed-off-by: Maxim Uvarov <maxim.uvarov at 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);
+		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



More information about the U-Boot mailing list