diff mbox series

[PATCHv7,15/15] net/lwip/wget add port selection

Message ID 20230822093614.4717-16-maxim.uvarov@linaro.org
State Changes Requested
Delegated to: Ramon Fried
Headers show
Series net/lwip: add lwip library for the network stack | expand

Commit Message

Maxim Uvarov Aug. 22, 2023, 9:36 a.m. UTC
Allow to specify HTTP port instead of just using default for wget command.

Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
 include/net/lwip.h             |  2 +-
 net/lwip/apps/http/lwip-wget.c | 45 +++++++++++++++++++++++++---------
 2 files changed, 35 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/include/net/lwip.h b/include/net/lwip.h
index aa82d71715..c66551a14c 100644
--- a/include/net/lwip.h
+++ b/include/net/lwip.h
@@ -54,7 +54,7 @@  int ulwip_tftp(ulong addr, const char *filename);
  *
  *
  * @addr:  start address to download result
- * @url:   url in format http://host/url
+ * @url:   url in format http://host[:port]/url
  * Returns: 0 for success, !0 if error
 */
 int ulwip_wget(ulong addr, char *url);
diff --git a/net/lwip/apps/http/lwip-wget.c b/net/lwip/apps/http/lwip-wget.c
index a9bb29e156..01aa146b13 100644
--- a/net/lwip/apps/http/lwip-wget.c
+++ b/net/lwip/apps/http/lwip-wget.c
@@ -65,19 +65,42 @@  static int parse_url(char *url, char *host, u16 *port)
 	p += strlen("http://");
 
 	/* parse hostname */
-	pp = strchr(p, '/');
-	if (!pp) {
-		printf("wrong url\n");
-		return -2;
+	pp = strchr(p, ':');
+	if (pp) {
+#define PORT_STR_SIZE 5
+		char portstr[PORT_STR_SIZE];
+
+		if (pp - p >= SERVER_NAME_SIZE)
+			return -2;
+		memcpy(host, p, pp - p);
+		host[pp - p + 1] = '\0';
+
+		p = pp + 1;
+		pp = strchr(p, '/');
+		if (!pp) {
+			printf("wrong url\n");
+			return -3;
+		}
+
+		if (pp - p >= PORT_STR_SIZE)
+                        return -4;
+		memcpy(portstr, p, pp - p);
+		portstr[pp - p] = '\0';
+		*port = (u16)dectoul(portstr, NULL);
+	} else {
+		pp = strchr(p, '/');
+		if (!pp) {
+			printf("wrong url\n");
+			return -5;
+		}
+
+		if (pp - p >= SERVER_NAME_SIZE)
+			return -6;
+		memcpy(host, p, pp - p);
+		host[pp - p + 1] = '\0';
+		*port = HTTP_PORT_DEFAULT;
 	}
 
-	if (pp - p >= SERVER_NAME_SIZE)
-		return -3;
-
-	memcpy(host, p, pp - p);
-	host[pp - p + 1] = '\0';
-	*port = HTTP_PORT_DEFAULT;
-
 	return 0;
 }