From 8a2129f4c808600d6151f724dcbd816188d3d1c6 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 4 Jul 2014 01:03:56 +0200 Subject: [PATCH] Fix some issues in parsing IPv6 multicast addresses. --- input.cpp | 27 +++++++++++++++++++++++++-- udpinput.cpp | 5 +++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/input.cpp b/input.cpp index 0ee7d1c..102207f 100644 --- a/input.cpp +++ b/input.cpp @@ -37,10 +37,33 @@ bool parse_url(const string &url, string *protocol, string *user, string *host, *protocol = string(url.begin(), url.begin() + split); string rest = string(url.begin() + split + 3, url.end()); - split = rest.find_first_of(":/"); - if (split == string::npos) { + + // Split at the first slash, or the first colon that's not within []. + bool within_brackets = false; + for (split = 0; split < rest.size(); ++split) { + if (rest[split] == '[') { + if (within_brackets) { + // Can't nest brackets. + return false; + } + within_brackets = true; + } else if (rest[split] == ']') { + if (!within_brackets) { + // ] without matching [. + return false; + } + within_brackets = false; + } else if (rest[split] == '/') { + break; + } else if (rest[split] == ':' && !within_brackets) { + break; + } + } + + if (split == rest.size()) { // http://foo split_user_host(rest, user, host); + fprintf(stderr, "ooo user='%s' host='%s'\n", user->c_str(), host->c_str()); *port = *protocol; *path = "/"; return true; diff --git a/udpinput.cpp b/udpinput.cpp index 001ef5f..559e9cd 100644 --- a/udpinput.cpp +++ b/udpinput.cpp @@ -40,8 +40,9 @@ bool parse_ip_address(const string &ip, sockaddr_storage *addr) ip.c_str()); return false; } - if (inet_pton(AF_INET6, ip.c_str(), &addr6->sin6_addr) != 1) { - log(ERROR, "'%s' is not a valid IPv6 address"); + string raw_ip(ip.begin() + 1, ip.end() - 1); + if (inet_pton(AF_INET6, raw_ip.c_str(), &addr6->sin6_addr) != 1) { + log(ERROR, "'%s' is not a valid IPv6 address", raw_ip.c_str()); return false; } } else { -- 2.39.2