Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / uri.cpp
index 8e663c46a732116b84c9e5c20615c6c018354034..af78bd35585e2ee1cb1b23f241eaf4ab31506960 100644 (file)
@@ -6,25 +6,30 @@
  */
 
 #define _LGPL_SOURCE
-#include <arpa/inet.h>
-#include <common/compat/netdb.hpp>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
+#include "uri.hpp"
 
 #include <common/common.hpp>
+#include <common/compat/netdb.hpp>
 #include <common/defaults.hpp>
 #include <common/utils.hpp>
 
-#include "uri.hpp"
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
 
 #define LOOPBACK_ADDR_IPV4 "127.0.0.1"
 #define LOOPBACK_ADDR_IPV6 "::1"
 
 enum uri_proto_code {
-       P_NET, P_NET6, P_FILE, P_TCP, P_TCP6,
+       P_NET,
+       P_NET6,
+       P_FILE,
+       P_TCP,
+       P_TCP6,
 };
 
+namespace {
 struct uri_proto {
        const char *name;
        const char *leading_string;
@@ -34,17 +39,44 @@ struct uri_proto {
 };
 
 /* Supported protocols */
-static const struct uri_proto proto_uri[] = {
-       { .name = "file", .leading_string = "file://", .code = P_FILE, .type = LTTNG_PROTO_TYPE_NONE, .dtype = LTTNG_DST_PATH },
-       { .name = "net", .leading_string = "net://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
-       { .name = "net4", .leading_string = "net4://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
-       { .name = "net6", .leading_string = "net6://", .code = P_NET6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 },
-       { .name = "tcp", .leading_string = "tcp://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
-       { .name = "tcp4", .leading_string = "tcp4://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
-       { .name = "tcp6", .leading_string = "tcp6://", .code = P_TCP6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 },
-       /* Invalid proto marking the end of the array. */
-       {}
-};
+const struct uri_proto proto_uri[] = { { .name = "file",
+                                        .leading_string = "file://",
+                                        .code = P_FILE,
+                                        .type = LTTNG_PROTO_TYPE_NONE,
+                                        .dtype = LTTNG_DST_PATH },
+                                      { .name = "net",
+                                        .leading_string = "net://",
+                                        .code = P_NET,
+                                        .type = LTTNG_TCP,
+                                        .dtype = LTTNG_DST_IPV4 },
+                                      { .name = "net4",
+                                        .leading_string = "net4://",
+                                        .code = P_NET,
+                                        .type = LTTNG_TCP,
+                                        .dtype = LTTNG_DST_IPV4 },
+                                      { .name = "net6",
+                                        .leading_string = "net6://",
+                                        .code = P_NET6,
+                                        .type = LTTNG_TCP,
+                                        .dtype = LTTNG_DST_IPV6 },
+                                      { .name = "tcp",
+                                        .leading_string = "tcp://",
+                                        .code = P_TCP,
+                                        .type = LTTNG_TCP,
+                                        .dtype = LTTNG_DST_IPV4 },
+                                      { .name = "tcp4",
+                                        .leading_string = "tcp4://",
+                                        .code = P_TCP,
+                                        .type = LTTNG_TCP,
+                                        .dtype = LTTNG_DST_IPV4 },
+                                      { .name = "tcp6",
+                                        .leading_string = "tcp6://",
+                                        .code = P_TCP6,
+                                        .type = LTTNG_TCP,
+                                        .dtype = LTTNG_DST_IPV6 },
+                                      /* Invalid proto marking the end of the array. */
+                                      {} };
+} /* namespace */
 
 /*
  * Return pointer to the character in s matching one of the characters in
@@ -53,7 +85,7 @@ static const struct uri_proto proto_uri[] = {
 static inline const char *strpbrk_or_eos(const char *s, const char *accept)
 {
        char *p = (char *) strpbrk(s, accept);
-       if (p == NULL) {
+       if (p == nullptr) {
                p = (char *) strchr(s, '\0');
        }
 
@@ -65,23 +97,23 @@ static inline const char *strpbrk_or_eos(const char *s, const char *accept)
  */
 static const struct uri_proto *get_uri_proto(const char *uri_str)
 {
-       const struct uri_proto *supported = NULL;
+       const struct uri_proto *supported = nullptr;
 
        /* Safety net */
-       if (uri_str == NULL) {
+       if (uri_str == nullptr) {
                goto end;
        }
 
-       for (supported = &proto_uri[0];
-                       supported->leading_string != NULL; ++supported) {
-               if (strncasecmp(uri_str, supported->leading_string,
-                                       strlen(supported->leading_string)) == 0) {
+       for (supported = &proto_uri[0]; supported->leading_string != nullptr; ++supported) {
+               if (strncasecmp(uri_str,
+                               supported->leading_string,
+                               strlen(supported->leading_string)) == 0) {
                        goto end;
                }
        }
 
        /* Proto not found */
-       return NULL;
+       return nullptr;
 
 end:
        return supported;
@@ -113,8 +145,7 @@ static int set_ip_address(const char *addr, int af, char *dst, size_t size)
                                PERROR("inet_ntop");
                                goto error;
                        }
-               } else if (!strcmp(addr, "localhost") &&
-                               (af == AF_INET || af == AF_INET6)) {
+               } else if (!strcmp(addr, "localhost") && (af == AF_INET || af == AF_INET6)) {
                        /*
                         * Some systems may not have "localhost" defined in
                         * accordance with IETF RFC 6761. According to this RFC,
@@ -128,11 +159,11 @@ static int set_ip_address(const char *addr, int af, char *dst, size_t size)
                         * done to accommodates systems which may want to start
                         * tracing before their network configured.
                         */
-                       const char *loopback_addr = af == AF_INET ?
-                                       LOOPBACK_ADDR_IPV4 : LOOPBACK_ADDR_IPV6;
+                       const char *loopback_addr = af == AF_INET ? LOOPBACK_ADDR_IPV4 :
+                                                                   LOOPBACK_ADDR_IPV6;
                        const size_t loopback_addr_len = af == AF_INET ?
-                                       sizeof(LOOPBACK_ADDR_IPV4) :
-                                       sizeof(LOOPBACK_ADDR_IPV6);
+                               sizeof(LOOPBACK_ADDR_IPV4) :
+                               sizeof(LOOPBACK_ADDR_IPV6);
 
                        DBG2("Could not resolve localhost address, using fallback");
                        if (loopback_addr_len > size) {
@@ -163,13 +194,12 @@ error:
  * Set default URI attribute which is basically the given stream type and the
  * default port if none is set in the URI.
  */
-static void set_default_uri_attr(struct lttng_uri *uri,
-               enum lttng_stream_type stype)
+static void set_default_uri_attr(struct lttng_uri *uri, enum lttng_stream_type stype)
 {
        uri->stype = stype;
        if (uri->dtype != LTTNG_DST_PATH && uri->port == 0) {
-               uri->port = (stype == LTTNG_STREAM_CONTROL) ?
-                       DEFAULT_NETWORK_CONTROL_PORT : DEFAULT_NETWORK_DATA_PORT;
+               uri->port = (stype == LTTNG_STREAM_CONTROL) ? DEFAULT_NETWORK_CONTROL_PORT :
+                                                             DEFAULT_NETWORK_DATA_PORT;
        }
 }
 
@@ -219,14 +249,20 @@ int uri_to_str_url(struct lttng_uri *uri, char *dst, size_t size)
                (void) snprintf(port, sizeof(port), "%s", "");
        } else {
                ipver = (uri->dtype == LTTNG_DST_IPV4) ? 4 : 6;
-               addr = (ipver == 4) ?  uri->dst.ipv4 : uri->dst.ipv6;
+               addr = (ipver == 4) ? uri->dst.ipv4 : uri->dst.ipv6;
                (void) snprintf(proto, sizeof(proto), "tcp%d", ipver);
                (void) snprintf(port, sizeof(port), ":%d", uri->port);
        }
 
-       ret = snprintf(dst, size, "%s://%s%s%s%s/%s", proto,
-                       (ipver == 6) ? "[" : "", addr, (ipver == 6) ? "]" : "",
-                       port, uri->subdir);
+       ret = snprintf(dst,
+                      size,
+                      "%s://%s%s%s%s/%s",
+                      proto,
+                      (ipver == 6) ? "[" : "",
+                      addr,
+                      (ipver == 6) ? "]" : "",
+                      port,
+                      uri->subdir);
        if (ret < 0) {
                PERROR("snprintf uri to url");
        }
@@ -277,9 +313,9 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
        unsigned int ctrl_port = 0;
        unsigned int data_port = 0;
        struct lttng_uri *tmp_uris;
-       char *addr_f = NULL;
+       char *addr_f = nullptr;
        const struct uri_proto *proto;
-       const char *purl, *addr_e, *addr_b, *subdir_b = NULL;
+       const char *purl, *addr_e, *addr_b, *subdir_b = nullptr;
        const char *seps = ":/\0";
 
        /*
@@ -293,7 +329,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
        DBG3("URI string: %s", str_uri);
 
        proto = get_uri_proto(str_uri);
-       if (proto == NULL) {
+       if (proto == nullptr) {
                ERR("URI parse unknown protocol %s", str_uri);
                goto error;
        }
@@ -307,7 +343,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
 
        /* Allocate URI array */
        tmp_uris = calloc<lttng_uri>(size);
-       if (tmp_uris == NULL) {
+       if (tmp_uris == nullptr) {
                PERROR("zmalloc uri");
                goto error;
        }
@@ -346,7 +382,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
                /* Address begins after '[' */
                addr_b = purl + 1;
                addr_e = strchr(addr_b, ']');
-               if (addr_e == NULL || addr_b == addr_e) {
+               if (addr_e == nullptr || addr_b == addr_e) {
                        ERR("Broken IPv6 address %s", addr_b);
                        goto free_error;
                }
@@ -357,7 +393,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
                /*
                 * The closing bracket must be followed by a seperator or NULL char.
                 */
-               if (strchr(seps, *purl) == NULL) {
+               if (strchr(seps, *purl) == nullptr) {
                        ERR("Unknown symbol after IPv6 address: %s", purl);
                        goto free_error;
                }
@@ -373,7 +409,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
        }
 
        addr_f = utils_strdupdelim(addr_b, addr_e);
-       if (addr_f == NULL) {
+       if (addr_f == nullptr) {
                goto free_error;
        }
 
@@ -392,8 +428,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
                 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
                 * two much stuff.
                 */
-               if ((i == 2 && (proto->code != P_NET && proto->code != P_NET6))
-                               || i > 2) {
+               if ((i == 2 && (proto->code != P_NET && proto->code != P_NET6)) || i > 2) {
                        break;
                }
 
@@ -411,7 +446,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
                        int port;
 
                        port_f = utils_strdupdelim(port_b, port_e);
-                       if (port_f == NULL) {
+                       if (port_f == nullptr) {
                                goto free_error;
                        }
 
@@ -458,8 +493,8 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
 
        switch (proto->code) {
        case P_NET:
-               ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
-                               sizeof(tmp_uris[0].dst.ipv4));
+               ret = set_ip_address(
+                       addr_f, AF_INET, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[0].dst.ipv4));
                if (ret < 0) {
                        goto free_error;
                }
@@ -471,8 +506,8 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
                tmp_uris[1].port = data_port;
                break;
        case P_NET6:
-               ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
-                               sizeof(tmp_uris[0].dst.ipv6));
+               ret = set_ip_address(
+                       addr_f, AF_INET6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[0].dst.ipv6));
                if (ret < 0) {
                        goto free_error;
                }
@@ -484,15 +519,15 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
                tmp_uris[1].port = data_port;
                break;
        case P_TCP:
-               ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
-                               sizeof(tmp_uris[0].dst.ipv4));
+               ret = set_ip_address(
+                       addr_f, AF_INET, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[0].dst.ipv4));
                if (ret < 0) {
                        goto free_error;
                }
                break;
        case P_TCP6:
-               ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
-                               sizeof(tmp_uris[0].dst.ipv6));
+               ret = set_ip_address(
+                       addr_f, AF_INET6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[0].dst.ipv6));
                if (ret < 0) {
                        goto free_error;
                }
@@ -503,8 +538,12 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
 
 end:
        DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
-                       proto->dtype, proto->type, (addr_f == NULL) ? "" : addr_f,
-                       (subdir_b == NULL) ? "" : subdir_b, ctrl_port, data_port);
+            proto->dtype,
+            proto->type,
+            (addr_f == NULL) ? "" : addr_f,
+            (subdir_b == NULL) ? "" : subdir_b,
+            ctrl_port,
+            data_port);
 
        free(addr_f);
 
@@ -523,18 +562,17 @@ error:
  * Parse a string URL and creates URI(s) returning the size of the populated
  * array.
  */
-ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
-               struct lttng_uri **uris)
+ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url, struct lttng_uri **uris)
 {
        unsigned int equal = 1, idx = 0;
        /* Add the "file://" size to the URL maximum size */
        char url[PATH_MAX + 7];
        ssize_t ctrl_uri_count = 0, data_uri_count = 0, uri_count;
-       struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL;
-       struct lttng_uri *tmp_uris = NULL;
+       struct lttng_uri *ctrl_uris = nullptr, *data_uris = nullptr;
+       struct lttng_uri *tmp_uris = nullptr;
 
        /* No URL(s) is allowed. This means that the consumer will be disabled. */
-       if (ctrl_url == NULL && data_url == NULL) {
+       if (ctrl_url == nullptr && data_url == nullptr) {
                return 0;
        }
 
@@ -559,7 +597,6 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
                } else if (ret >= sizeof(url)) {
                        PERROR("snprintf file url is too long");
                        goto parse_error;
-
                }
                ctrl_url = url;
        }
@@ -578,8 +615,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
                /* At this point, we know there is at least one URI in the array */
                set_default_uri_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL);
 
-               if (ctrl_uris[0].dtype == LTTNG_DST_PATH &&
-                               (data_url && *data_url != '\0')) {
+               if (ctrl_uris[0].dtype == LTTNG_DST_PATH && (data_url && *data_url != '\0')) {
                        ERR("Cannot have a data URL when destination is file://");
                        goto error;
                }
@@ -588,7 +624,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
                if (ctrl_uri_count == 2) {
                        if (!equal) {
                                ERR("Control URL uses the net:// protocol and the data URL is "
-                                               "different. Not allowed.");
+                                   "different. Not allowed.");
                                goto error;
                        } else {
                                set_default_uri_attr(&ctrl_uris[1], LTTNG_STREAM_DATA);
@@ -596,7 +632,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
                                 * The data_url and ctrl_url are equal and the ctrl_url
                                 * contains a net:// protocol so we just skip the data part.
                                 */
-                               data_url = NULL;
+                               data_url = nullptr;
                        }
                }
        }
@@ -635,7 +671,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
        }
 
        tmp_uris = calloc<lttng_uri>(uri_count);
-       if (tmp_uris == NULL) {
+       if (tmp_uris == nullptr) {
                PERROR("zmalloc uris");
                goto error;
        }
This page took 0.030677 seconds and 4 git commands to generate.