aboutsummaryrefslogtreecommitdiff
path: root/lib/libfetch
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2020-02-15 19:31:40 +0000
committerKyle Evans <kevans@FreeBSD.org>2020-02-15 19:31:40 +0000
commit0f3fa96016c82f700733941d4c148cefff4c4f1c (patch)
treef4e76620bb28276d84d6e1c85ddf2ecfc7ed8920 /lib/libfetch
parentbacffcd4b18648b09442559236433c5521e6387e (diff)
downloadsrc-0f3fa96016c82f700733941d4c148cefff4c4f1c.tar.gz
src-0f3fa96016c82f700733941d4c148cefff4c4f1c.zip
fetch(3): move bits of fetch_socks5_getenv around
This commit separates out port parsing and validation from grabbing the host from the env var. The only related bit really is that we need to be more specific with the delimiter in the IPv6 case.
Notes
Notes: svn path=/head/; revision=357977
Diffstat (limited to 'lib/libfetch')
-rw-r--r--lib/libfetch/common.c68
1 files changed, 32 insertions, 36 deletions
diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c
index e480c311f84c..12da7e6ee988 100644
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -525,54 +525,50 @@ int
fetch_socks5_getenv(char **host, int *port)
{
char *socks5env, *endptr, *ext;
+ const char *portDelim;
+ size_t slen;
+ portDelim = ":";
if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
*host = NULL;
*port = -1;
return (-1);
}
- /* IPv6 addresses begin and end in brackets */
+ /*
+ * IPv6 addresses begin and end in brackets. Set the port delimiter
+ * accordingly and search for it so we can do appropriate validation.
+ */
+ if (socks5env[0] == '[')
+ portDelim = "]:";
+
+ slen = strlen(socks5env);
+ ext = strstr(socks5env, portDelim);
if (socks5env[0] == '[') {
- if (socks5env[strlen(socks5env) - 1] == ']') {
- *host = strndup(socks5env, strlen(socks5env));
- if (*host == NULL)
- goto fail;
- *port = 1080; /* Default port as defined in RFC1928 */
- } else {
- ext = strstr(socks5env, "]:");
- if (ext == NULL) {
- socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
- return (0);
- }
- ext=ext+1;
- *host = strndup(socks5env, ext - socks5env);
+ if (socks5env[slen - 1] == ']') {
+ *host = strndup(socks5env, slen);
if (*host == NULL)
goto fail;
- errno = 0;
- *port = strtoimax(ext + 1, (char **)&endptr, 10);
- if (*endptr != '\0' || errno != 0 || *port < 0 ||
- *port > 65535) {
- socks5_seterr(SOCKS5_ERR_BAD_PORT);
- return (0);
- }
+ } else if (ext == NULL) {
+ socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
+ return (0);
}
} else {
- ext = strrchr(socks5env, ':');
- if (ext == NULL) {
- *host = strdup(socks5env);
- *port = 1080;
- } else {
- *host = strndup(socks5env, ext-socks5env);
- if (*host == NULL)
- goto fail;
- errno = 0;
- *port = strtoimax(ext + 1, (char **)&endptr, 10);
- if (*endptr != '\0' || errno != 0 || *port < 0 ||
- *port > 65535) {
- socks5_seterr(SOCKS5_ERR_BAD_PORT);
- return (0);
- }
+ *host = strndup(socks5env, ext - socks5env);
+ if (*host == NULL)
+ goto fail;
+ }
+
+ if (ext == NULL) {
+ *port = 1080; /* Default port as defined in RFC1928 */
+ } else {
+ ext += strlen(portDelim);
+ errno = 0;
+ *port = strtoimax(ext, (char **)&endptr, 10);
+ if (*endptr != '\0' || errno != 0 || *port < 0 ||
+ *port > 65535) {
+ socks5_seterr(SOCKS5_ERR_BAD_PORT);
+ return (0);
}
}