From b61bdc9acc1df765eec9b43e6f83846dfb8a70d0 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Wed, 6 Apr 2016 13:02:13 -0700 Subject: [PATCH] libupload: use url_set_ip() We already have a core function for setting the IP address of an URL object based on network lookup or the server default. Export and use it instead of open-coding the equivalent logic in upload_tftp.c. Signed-off-by: H. Peter Anvin --- com32/libupload/upload_tftp.c | 50 +++++++++++++---------------------- core/fs/pxe/pxe.c | 18 ++++++++++--- core/fs/pxe/url.h | 3 ++- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/com32/libupload/upload_tftp.c b/com32/libupload/upload_tftp.c index e8ead2c7..8ca87528 100644 --- a/com32/libupload/upload_tftp.c +++ b/com32/libupload/upload_tftp.c @@ -34,12 +34,9 @@ const char *tftp_string_error_message[]={ static bool have_real_network(void); static int upload_tftp_write(struct upload_backend *be) { - const union syslinux_derivative_info *sdi = - syslinux_derivative_info(); struct url_info url; struct inode inode; - char url_path[255] = {0}; - uint32_t ip; + char url_path[255]; int err; if (!have_real_network()) { @@ -47,37 +44,26 @@ static int upload_tftp_write(struct upload_backend *be) { return -TFTP_ERR_NO_NETWORK; } - if (be->argv[1]) { - ip = pxe_dns(be->argv[1]); - if (!ip) { - dprintf("\nUnable to resolve hostname: %s\n", be->argv[1]); - return -TFTP_ERR_UNABLE_TO_RESOLVE; - } - } else { - ip = sdi->pxe.ipinfo->serverip; - if (!ip) { - dprintf("\nNo server IP address\n"); - return -TFTP_ERR_UNABLE_TO_CONNECT; - } - } - - snprintf(url_path, sizeof(url_path), "tftp://%u.%u.%u.%u/%s", - ((uint8_t *)&ip)[0], - ((uint8_t *)&ip)[1], - ((uint8_t *)&ip)[2], - ((uint8_t *)&ip)[3], - be->argv[0]); + if (!strncmp(be->argv[0], "tftp://", 7)) + strlcpy(url_path, be->argv[0], sizeof(url_path)); + else + snprintf(url_path, sizeof(url_path), "tftp://%s/%s", + be->argv[1] ? be->argv[1] : "", be->argv[0]); parse_url(&url, url_path); - url.ip = ip; + err = -url_set_ip(&url); + if (err != TFTP_OK) + return err; dprintf("Connecting to %s to send %s\n", url.host, url.path); - err = tftp_put(&url, 0, &inode, NULL, be->outbuf, be->zbytes); + err = -tftp_put(&url, 0, &inode, NULL, be->outbuf, be->zbytes); - if (-err != TFTP_OK) - printf("upload_tftp_write: TFTP server returned error %d : %s\n", err, tftp_string_error_message[-err]); + if (err != TFTP_OK) { + printf("upload_tftp_write: TFTP server returned error %d : %s\n", + err, tftp_string_error_message[err]); + } - return -err; + return err; } struct upload_backend upload_tftp = { @@ -117,11 +103,11 @@ static bool have_real_network(void) return tftp_put != _dummy_tftp_put; } -__weak uint32_t pxe_dns(const char *host) +__weak int url_set_ip(struct url_info *ui) { - (void)host; + (void)ui; - return 0; + return -TFTP_ERR_NO_NETWORK; } __weak void parse_url(struct url_info *ui, char *url) diff --git a/core/fs/pxe/pxe.c b/core/fs/pxe/pxe.c index 9b1a7329..21763395 100644 --- a/core/fs/pxe/pxe.c +++ b/core/fs/pxe/pxe.c @@ -237,13 +237,25 @@ static uint32_t pxe_getfssec(struct file *file, char *buf, /* * Assign an IP address to a URL */ -static void url_set_ip(struct url_info *url) +__export int url_set_ip(struct url_info *url) { + int err = -ntohs(TFTP_OK); + url->ip = 0; - if (url->host) + if (url->host && url->host[0]) { url->ip = pxe_dns(url->host); - if (!url->ip) + if (!url->ip) + err = -ntohs(TFTP_ERESOLVE); + } + + /* Note: default to the server IP on resolve failure */ + if (!url->ip) { url->ip = IPInfo.serverip; + if (!url->ip) + err = -ntohs(TFTP_NONETWORK); + }make + + return err; } /** diff --git a/core/fs/pxe/url.h b/core/fs/pxe/url.h index 53984f3a..93462004 100644 --- a/core/fs/pxe/url.h +++ b/core/fs/pxe/url.h @@ -19,7 +19,7 @@ struct url_info { char *user; char *passwd; char *host; - uint32_t ip; /* Placeholder field not set by parse_url() */ + uint32_t ip; /* Not set by parse_url(), use url_set_ip() */ unsigned int port; char *path; /* Includes query */ enum url_type type; @@ -29,5 +29,6 @@ enum url_type url_type(const char *url); void parse_url(struct url_info *ui, char *url); size_t url_escape_unsafe(char *output, const char *input, size_t bufsize); char *url_unescape(char *buffer, char terminator); +int url_set_ip(struct url_info *ui); #endif /* CORE_PXE_URL_H */