*/
#define _LGPL_SOURCE
+#include "uri.hpp"
+
+#include <common/common.hpp>
+#include <common/compat/netdb.hpp>
+#include <common/defaults.hpp>
+#include <common/utils.hpp>
+
#include <arpa/inet.h>
-#include <common/compat/netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
-#include <common/common.h>
-#include <common/defaults.h>
-#include <common/utils.h>
-
-#include "uri.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;
};
/* 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. */
- { 0 }
-};
+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
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');
}
*/
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;
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,
* 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) {
* 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;
}
}
(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");
}
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";
/*
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;
}
}
/* Allocate URI array */
- tmp_uris = (lttng_uri *) zmalloc(sizeof(struct lttng_uri) * size);
- if (tmp_uris == NULL) {
+ tmp_uris = calloc<lttng_uri>(size);
+ if (tmp_uris == nullptr) {
PERROR("zmalloc uri");
goto error;
}
/* 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;
}
/*
* 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;
}
}
addr_f = utils_strdupdelim(addr_b, addr_e);
- if (addr_f == NULL) {
+ if (addr_f == nullptr) {
goto free_error;
}
* 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;
}
int port;
port_f = utils_strdupdelim(port_b, port_e);
- if (port_f == NULL) {
+ if (port_f == nullptr) {
goto free_error;
}
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;
}
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;
}
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;
}
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);
* 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;
}
} else if (ret >= sizeof(url)) {
PERROR("snprintf file url is too long");
goto parse_error;
-
}
ctrl_url = 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;
}
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);
* 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;
}
}
}
goto error;
}
- tmp_uris = (lttng_uri *) zmalloc(sizeof(struct lttng_uri) * uri_count);
- if (tmp_uris == NULL) {
+ tmp_uris = calloc<lttng_uri>(uri_count);
+ if (tmp_uris == nullptr) {
PERROR("zmalloc uris");
goto error;
}