diff options
Diffstat (limited to 'net/lwip/wget.c')
| -rw-r--r-- | net/lwip/wget.c | 107 |
1 files changed, 61 insertions, 46 deletions
diff --git a/net/lwip/wget.c b/net/lwip/wget.c index 502c0faebb2..247ece18e2b 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -292,46 +292,22 @@ static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct #if CONFIG_IS_ENABLED(WGET_CACERT) #endif -int wget_do_request(ulong dst_addr, char *uri) +static int wget_handle_request(struct wget_ctx *ctx, bool is_https, + struct udevice *udev, struct netif *netif) { #if CONFIG_IS_ENABLED(WGET_HTTPS) altcp_allocator_t tls_allocator; #endif httpc_connection_t conn; httpc_state_t *state; - struct udevice *udev; - struct netif *netif; - struct wget_ctx ctx; - char *path; - bool is_https; - - ctx.daddr = dst_addr; - ctx.saved_daddr = dst_addr; - ctx.done = NOT_DONE; - ctx.size = 0; - ctx.prevsize = 0; - ctx.start_time = 0; - ctx.content_len = 0; - ctx.hash_count = 0; - - if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https)) - return CMD_RET_USAGE; - - if (net_lwip_eth_start() < 0) - return CMD_RET_FAILURE; - - if (!wget_info) - wget_info = &default_wget_info; - - udev = eth_get_dev(); - - netif = net_lwip_new_netif(udev); - if (!netif) - return -1; + int ret; /* if URL with hostname init dns */ - if (!ipaddr_aton(ctx.server_name, NULL) && net_lwip_dns_init()) - return CMD_RET_FAILURE; + if (!ipaddr_aton(ctx->server_name, NULL)) { + ret = net_lwip_dns_init(); + if (ret) + return ret; + } memset(&conn, 0, sizeof(conn)); #if CONFIG_IS_ENABLED(WGET_HTTPS) @@ -353,7 +329,7 @@ int wget_do_request(ulong dst_addr, char *uri) printf("Error: cacert authentication " "mode is 'required' but no CA " "certificates given\n"); - return CMD_RET_FAILURE; + return -EINVAL; } } else if (cacert_auth_mode == AUTH_NONE) { ca = NULL; @@ -374,12 +350,11 @@ int wget_do_request(ulong dst_addr, char *uri) tls_allocator.alloc = &altcp_tls_alloc; tls_allocator.arg = altcp_tls_create_config_client(ca, ca_sz, - ctx.server_name); + ctx->server_name); if (!tls_allocator.arg) { log_err("error: Cannot create a TLS connection\n"); - net_lwip_remove_netif(netif); - return -1; + return -ENODEV; } conn.altcp_allocator = &tls_allocator; @@ -388,30 +363,70 @@ int wget_do_request(ulong dst_addr, char *uri) conn.result_fn = httpc_result_cb; conn.headers_done_fn = httpc_headers_done_cb; - ctx.path = path; - if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb, - &ctx, &state)) { - net_lwip_remove_netif(netif); - return CMD_RET_FAILURE; + if (httpc_get_file_dns(ctx->server_name, ctx->port, ctx->path, &conn, + httpc_recv_cb, ctx, &state)) { + return -ENODEV; } errno = 0; - while (!ctx.done) { + while (!ctx->done) { net_lwip_rx(udev, netif); if (ctrlc()) break; } - net_lwip_remove_netif(netif); - - if (ctx.done == SUCCESS) + if (ctx->done == SUCCESS) return 0; if (errno == EPERM && !wget_info->silent) printf("Certificate verification failed\n"); - return -1; + return -errno ?: -EIO; +} + +int wget_do_request(ulong dst_addr, char *uri) +{ + struct udevice *udev; + struct wget_ctx ctx; + struct netif *netif; + bool is_https; + int ret; + + ctx.daddr = dst_addr; + ctx.saved_daddr = dst_addr; + ctx.done = NOT_DONE; + ctx.size = 0; + ctx.prevsize = 0; + ctx.start_time = 0; + ctx.content_len = 0; + ctx.hash_count = 0; + + ret = parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https); + if (ret) + return ret; + + ret = net_lwip_eth_start(); + if (ret) + return ret; + + if (!wget_info) + wget_info = &default_wget_info; + + udev = eth_get_dev(); + + netif = net_lwip_new_netif(udev); + if (!netif) { + net_lwip_eth_stop(); + return -ENODEV; + } + + ret = wget_handle_request(&ctx, is_https, udev, netif); + + net_lwip_remove_netif(netif); + net_lwip_eth_stop(); + + return ret; } /** |
