summaryrefslogtreecommitdiffstats
path: root/lib/url
diff options
context:
space:
mode:
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>2018-05-09 11:13:54 +1000
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>2018-07-10 14:00:08 +1000
commitbecf2b28d8b0aa561ac7e42db519071f8065d37f (patch)
tree0dfcc6ef5cbcf1a433ec2f116de119b1c5742f07 /lib/url
parent99a1f905f585480cca2c9a43ab18ed8e37365192 (diff)
downloadtalos-petitboot-becf2b28d8b0aa561ac7e42db519071f8065d37f.tar.gz
talos-petitboot-becf2b28d8b0aa561ac7e42db519071f8065d37f.zip
lib: Add support and helpers for IPv6 host addresses
Recognise IPv6 addresses and URLs, and allow an interface_info struct to have both an IPv4 and IPv6 address. The addr_scheme() helper returns the address family of a given address. Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Diffstat (limited to 'lib/url')
-rw-r--r--lib/url/url.c45
-rw-r--r--lib/url/url.h2
2 files changed, 42 insertions, 5 deletions
diff --git a/lib/url/url.c b/lib/url/url.c
index 6eeced3..f0e8b0e 100644
--- a/lib/url/url.c
+++ b/lib/url/url.c
@@ -197,19 +197,35 @@ struct pb_url *pb_url_parse(void *ctx, const char *url_str)
goto fail;
}
- col = strchr(p, ':');
+ col = strrchr(p, ':');
+ if (col <= p)
+ col = NULL;
+ if (col && strchr(col , ']')) {
+ /*
+ * This is likely an IPv6 address with no port.
+ * See https://www.ietf.org/rfc/rfc2732.txt
+ */
+ col = NULL;
+ }
if (col) {
len = path - col - 1;
url->port = len ? talloc_strndup(url, col + 1, len)
: NULL;
len = col - p;
- url->host = len ? talloc_strndup(url, p, len) : NULL;
} else {
url->port = NULL;
- url->host = talloc_strndup(url, p, path - p);
+ len = path - p;
}
+ if (p[0] == '[' && p[len - 1] == ']')
+ /* IPv6 */
+ url->host = talloc_strndup(url, p + 1, len - 2);
+ else
+ /* IPv4 */
+ url->host = talloc_strndup(url, p, len);
+
+
/* remove multiple leading slashes */
for (; *path && *(path+1) == '/'; path++)
;
@@ -231,13 +247,32 @@ bool is_url(const char *str)
return strstr(str, "://") != NULL;
}
+int addr_scheme(const char *address)
+{
+ uint8_t buf[sizeof(struct in6_addr)];
+ int rc;
+
+ rc = inet_pton(AF_INET, address , buf);
+ if (rc == 1)
+ return AF_INET;
+ rc = inet_pton(AF_INET6, address , buf);
+ if (rc == 1)
+ return AF_INET6;
+
+ return 0;
+}
+
char *pb_url_to_string(struct pb_url *url)
{
const struct pb_scheme_info *scheme = pb_url_scheme_info(url->scheme);
assert(scheme);
- return talloc_asprintf(url, "%s://%s%s", scheme->str,
- scheme->has_host ? url->host : "", url->path);
+ if (scheme->has_host && addr_scheme(url->host) == AF_INET6)
+ return talloc_asprintf(url, "%s://[%s]%s", scheme->str,
+ url->host, url->path);
+ else
+ return talloc_asprintf(url, "%s://%s%s", scheme->str,
+ scheme->has_host ? url->host : "", url->path);
}
static void pb_url_update_full(struct pb_url *url)
diff --git a/lib/url/url.h b/lib/url/url.h
index 9043615..49dff4a 100644
--- a/lib/url/url.h
+++ b/lib/url/url.h
@@ -19,6 +19,7 @@
#if !defined(_PB_URL_PARSER_H)
#define _PB_URL_PARSER_H
+#include <arpa/inet.h>
#include <stdbool.h>
/**
@@ -61,6 +62,7 @@ struct pb_url {
};
bool is_url(const char *str);
+int addr_scheme(const char *address);
struct pb_url *pb_url_parse(void *ctx, const char *url_str);
struct pb_url *pb_url_copy(void *ctx, const struct pb_url *url);
struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s);
OpenPOWER on IntegriCloud