Message ID | 20230908135320.7066-7-maxim.uvarov@linaro.org |
---|---|
State | Superseded |
Headers | show |
Series | net/lwip: add lwip library for the network stack | expand |
> 4 files changed, 128 insertions(+) > create mode 100644 net/lwip/apps/http/Makefile > create mode 100644 net/lwip/apps/http/lwip-wget.c > > diff --git a/include/net/lwip.h b/include/net/lwip.h > index c9e2982a78..85f08343fd 100644 > --- a/include/net/lwip.h > +++ b/include/net/lwip.h > @@ -28,6 +28,7 @@ int ulwip_dns(char *name, char *varname); > * Returns: 0 if success > * Other value < 0, if error > */ > +int ulwip_dhcp(void); This seems unrelated to wget changes [...] > +// SPDX-License-Identifier: GPL-2.0 > + > +/* > + * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org> > + */ > + > +#include <common.h> > +#include <command.h> > +#include <console.h> > +#include <vsprintf.h> > + > +#include "http_client.h" > +#include <net/ulwip.h> > + > +static ulong daddr; > +static httpc_connection_t settings; > + > +#define SERVER_NAME_SIZE 200 > +#define HTTP_PORT_DEFAULT 80 > + > +static err_t httpc_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf, > + err_t unused_err) > +{ > + struct pbuf *buf; > + > + if (!pbuf) > + return ERR_BUF; > + > + for (buf = pbuf; buf != NULL; buf = buf->next) { > + memcpy((void *)daddr, buf->payload, buf->len); > + log_debug("downloaded chunk size %d, to addr 0x%lx\n", > + buf->len, daddr); > + daddr += buf->len; > + } > + > + altcp_recved(pcb, pbuf->tot_len); > + pbuf_free(pbuf); > + return ERR_OK; > +} > + > +static void httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, > + u32_t srv_res, err_t err) > +{ > + if (httpc_result == HTTPC_RESULT_OK) { > + log_info("\n%d bytes successfully downloaded.\n", rx_content_len); > + env_set_hex("filesize", rx_content_len); > + ulwip_exit(0); > + } else { > + log_err("\nhttp eroror: %d\n", httpc_result); > + ulwip_exit(-1); > + } > +} > + > +/* http://hostname:port/url */ The format doesn't expect a port (for now we can change that once we add https) > +static int parse_url(char *url, char *host, u16 *port) > +{ > + char *p, *pp; > + > + p = strstr(url, "http://"); > + if (!p) > + return -1; > + > + p += strlen("http://"); > + > + /* parse hostname */ > + pp = strchr(p, '/'); > + if (!pp) { > + return -2; -1, -2 -3 etc should be mapped to proper ERRNOs > + } > + > + if (pp - p >= SERVER_NAME_SIZE) Can you change this to p + SERVER_NAME_SIZE >= pp ? > + return -3; > + > + memcpy(host, p, pp - p); > + host[pp - p + 1] = '\0'; > + *port = HTTP_PORT_DEFAULT; > + > + return 0; > +} > + > +int ulwip_wget(ulong addr, char *url) > +{ > + err_t err; > + u16 port; > + char server_name[SERVER_NAME_SIZE]; > + httpc_state_t *connection; > + > + daddr = addr; > + > + err = parse_url(url, server_name, &port); > + if (err) > + return -ENOENT; > + > + log_info("downloading %s to addr 0x%lx\n", url, addr); > + memset(&settings, 0, sizeof(settings)); > + settings.result_fn = httpc_result; > + err = httpc_get_file_dns(server_name, port, url, &settings, > + httpc_recv, NULL, &connection); > + if (err != ERR_OK) > + return -EPERM; > + > + if (env_set_hex("fileaddr", addr)) > + return -EACCES; > + > + return 0; > +} > -- > 2.30.2 > Thanks /Ilias
diff --git a/include/net/lwip.h b/include/net/lwip.h index c9e2982a78..85f08343fd 100644 --- a/include/net/lwip.h +++ b/include/net/lwip.h @@ -28,6 +28,7 @@ int ulwip_dns(char *name, char *varname); * Returns: 0 if success * Other value < 0, if error */ +int ulwip_dhcp(void); /** * ulwip_tftp() - load file with tftp @@ -41,3 +42,17 @@ int ulwip_dns(char *name, char *varname); * Returns: 0 if success, !0 if error */ int ulwip_tftp(ulong addr, const char *filename); + +/** + * ulwip_wget() - creates the HTTP request to download file + * + * This function creates the HTTP request to download file from url to the address + * specified in parameters. After this function you need to invoke the polling + * loop to process network communication. + * + * + * @addr: start address to download result + * @url: url in format http://host/url + * Returns: 0 for success, !0 if error + */ +int ulwip_wget(ulong addr, char *url); diff --git a/net/lwip/Makefile b/net/lwip/Makefile index b348e5ca31..61042862e1 100644 --- a/net/lwip/Makefile +++ b/net/lwip/Makefile @@ -66,3 +66,4 @@ obj-$(CONFIG_NET) += port/sys-arch.o obj-y += apps/dhcp/lwip-dhcp.o obj-y += apps/dns/lwip-dns.o obj-y += apps/tftp/ +obj-y += apps/http/ diff --git a/net/lwip/apps/http/Makefile b/net/lwip/apps/http/Makefile new file mode 100644 index 0000000000..ac0b3ede0d --- /dev/null +++ b/net/lwip/apps/http/Makefile @@ -0,0 +1,6 @@ +ccflags-y += -I$(srctree)/net/lwip/port/include +ccflags-y += -I$(srctree)/net/lwip/lwip-external/src/include -I$(srctree)/net/lwip +ccflags-y += -I$(srctree)/net/lwip/lwip-external/src/include/lwip/apps + +obj-$(CONFIG_CMD_WGET) += ../../lwip-external/src/apps/http/http_client.o +obj-$(CONFIG_CMD_WGET) += lwip-wget.o diff --git a/net/lwip/apps/http/lwip-wget.c b/net/lwip/apps/http/lwip-wget.c new file mode 100644 index 0000000000..5c432056b1 --- /dev/null +++ b/net/lwip/apps/http/lwip-wget.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org> + */ + +#include <common.h> +#include <command.h> +#include <console.h> +#include <vsprintf.h> + +#include "http_client.h" +#include <net/ulwip.h> + +static ulong daddr; +static httpc_connection_t settings; + +#define SERVER_NAME_SIZE 200 +#define HTTP_PORT_DEFAULT 80 + +static err_t httpc_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf, + err_t unused_err) +{ + struct pbuf *buf; + + if (!pbuf) + return ERR_BUF; + + for (buf = pbuf; buf != NULL; buf = buf->next) { + memcpy((void *)daddr, buf->payload, buf->len); + log_debug("downloaded chunk size %d, to addr 0x%lx\n", + buf->len, daddr); + daddr += buf->len; + } + + altcp_recved(pcb, pbuf->tot_len); + pbuf_free(pbuf); + return ERR_OK; +} + +static void httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, + u32_t srv_res, err_t err) +{ + if (httpc_result == HTTPC_RESULT_OK) { + log_info("\n%d bytes successfully downloaded.\n", rx_content_len); + env_set_hex("filesize", rx_content_len); + ulwip_exit(0); + } else { + log_err("\nhttp eroror: %d\n", httpc_result); + ulwip_exit(-1); + } +} + +/* http://hostname:port/url */ +static int parse_url(char *url, char *host, u16 *port) +{ + char *p, *pp; + + p = strstr(url, "http://"); + if (!p) + return -1; + + p += strlen("http://"); + + /* parse hostname */ + pp = strchr(p, '/'); + if (!pp) { + return -2; + } + + if (pp - p >= SERVER_NAME_SIZE) + return -3; + + memcpy(host, p, pp - p); + host[pp - p + 1] = '\0'; + *port = HTTP_PORT_DEFAULT; + + return 0; +} + +int ulwip_wget(ulong addr, char *url) +{ + err_t err; + u16 port; + char server_name[SERVER_NAME_SIZE]; + httpc_state_t *connection; + + daddr = addr; + + err = parse_url(url, server_name, &port); + if (err) + return -ENOENT; + + log_info("downloading %s to addr 0x%lx\n", url, addr); + memset(&settings, 0, sizeof(settings)); + settings.result_fn = httpc_result; + err = httpc_get_file_dns(server_name, port, url, &settings, + httpc_recv, NULL, &connection); + if (err != ERR_OK) + return -EPERM; + + if (env_set_hex("fileaddr", addr)) + return -EACCES; + + return 0; +}
U-Boot recently got support for an alternative network stack using LWIP. Replace wget command with the LWIP variant while keeping the output and error messages identical. Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org> --- include/net/lwip.h | 15 +++++ net/lwip/Makefile | 1 + net/lwip/apps/http/Makefile | 6 ++ net/lwip/apps/http/lwip-wget.c | 106 +++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 net/lwip/apps/http/Makefile create mode 100644 net/lwip/apps/http/lwip-wget.c