| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include <arpa/inet.h> |
| 10 | #include <common/compat/netdb.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/socket.h> |
| 14 | |
| 15 | #include <common/common.h> |
| 16 | #include <common/defaults.h> |
| 17 | #include <common/utils.h> |
| 18 | |
| 19 | #include "uri.h" |
| 20 | |
| 21 | #define LOOPBACK_ADDR_IPV4 "127.0.0.1" |
| 22 | #define LOOPBACK_ADDR_IPV6 "::1" |
| 23 | |
| 24 | enum uri_proto_code { |
| 25 | P_NET, P_NET6, P_FILE, P_TCP, P_TCP6, |
| 26 | }; |
| 27 | |
| 28 | struct uri_proto { |
| 29 | const char *name; |
| 30 | const char *leading_string; |
| 31 | enum uri_proto_code code; |
| 32 | enum lttng_proto_type type; |
| 33 | enum lttng_dst_type dtype; |
| 34 | }; |
| 35 | |
| 36 | /* Supported protocols */ |
| 37 | static const struct uri_proto proto_uri[] = { |
| 38 | { .name = "file", .leading_string = "file://", .code = P_FILE, .type = 0, .dtype = LTTNG_DST_PATH }, |
| 39 | { .name = "net", .leading_string = "net://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 }, |
| 40 | { .name = "net4", .leading_string = "net4://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 }, |
| 41 | { .name = "net6", .leading_string = "net6://", .code = P_NET6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 }, |
| 42 | { .name = "tcp", .leading_string = "tcp://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 }, |
| 43 | { .name = "tcp4", .leading_string = "tcp4://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 }, |
| 44 | { .name = "tcp6", .leading_string = "tcp6://", .code = P_TCP6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 }, |
| 45 | /* Invalid proto marking the end of the array. */ |
| 46 | { NULL, NULL, 0, 0, 0 } |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Return pointer to the character in s matching one of the characters in |
| 51 | * accept. If nothing is found, return pointer to the end of string (eos). |
| 52 | */ |
| 53 | static inline const char *strpbrk_or_eos(const char *s, const char *accept) |
| 54 | { |
| 55 | char *p = strpbrk(s, accept); |
| 56 | if (p == NULL) { |
| 57 | p = strchr(s, '\0'); |
| 58 | } |
| 59 | |
| 60 | return p; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Validate if proto is a supported protocol from proto_uri array. |
| 65 | */ |
| 66 | static const struct uri_proto *get_uri_proto(const char *uri_str) |
| 67 | { |
| 68 | const struct uri_proto *supported = NULL; |
| 69 | |
| 70 | /* Safety net */ |
| 71 | if (uri_str == NULL) { |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | for (supported = &proto_uri[0]; |
| 76 | supported->leading_string != NULL; ++supported) { |
| 77 | if (strncasecmp(uri_str, supported->leading_string, |
| 78 | strlen(supported->leading_string)) == 0) { |
| 79 | goto end; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* Proto not found */ |
| 84 | return NULL; |
| 85 | |
| 86 | end: |
| 87 | return supported; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Set network address from string into dst. Supports both IP string and |
| 92 | * hostname. |
| 93 | */ |
| 94 | static int set_ip_address(const char *addr, int af, char *dst, size_t size) |
| 95 | { |
| 96 | int ret; |
| 97 | unsigned char buf[sizeof(struct in6_addr)]; |
| 98 | struct hostent *record; |
| 99 | |
| 100 | LTTNG_ASSERT(addr); |
| 101 | LTTNG_ASSERT(dst); |
| 102 | |
| 103 | memset(dst, 0, size); |
| 104 | |
| 105 | /* Network protocol */ |
| 106 | ret = inet_pton(af, addr, buf); |
| 107 | if (ret < 1) { |
| 108 | /* We consider the dst to be an hostname or an invalid IP char */ |
| 109 | record = lttng_gethostbyname2(addr, af); |
| 110 | if (record) { |
| 111 | /* Translate IP to string */ |
| 112 | if (!inet_ntop(af, record->h_addr_list[0], dst, size)) { |
| 113 | PERROR("inet_ntop"); |
| 114 | goto error; |
| 115 | } |
| 116 | } else if (!strcmp(addr, "localhost") && |
| 117 | (af == AF_INET || af == AF_INET6)) { |
| 118 | /* |
| 119 | * Some systems may not have "localhost" defined in |
| 120 | * accordance with IETF RFC 6761. According to this RFC, |
| 121 | * applications may recognize "localhost" names as |
| 122 | * special and resolve to the appropriate loopback |
| 123 | * address. |
| 124 | * |
| 125 | * We choose to use the system name resolution API first |
| 126 | * to honor its network configuration. If this fails, we |
| 127 | * resolve to the appropriate loopback address. This is |
| 128 | * done to accommodates systems which may want to start |
| 129 | * tracing before their network configured. |
| 130 | */ |
| 131 | const char *loopback_addr = af == AF_INET ? |
| 132 | LOOPBACK_ADDR_IPV4 : LOOPBACK_ADDR_IPV6; |
| 133 | const size_t loopback_addr_len = af == AF_INET ? |
| 134 | sizeof(LOOPBACK_ADDR_IPV4) : |
| 135 | sizeof(LOOPBACK_ADDR_IPV6); |
| 136 | |
| 137 | DBG2("Could not resolve localhost address, using fallback"); |
| 138 | if (loopback_addr_len > size) { |
| 139 | ERR("Could not resolve localhost address; destination string is too short"); |
| 140 | goto error; |
| 141 | } |
| 142 | strcpy(dst, loopback_addr); |
| 143 | } else { |
| 144 | /* At this point, the IP or the hostname is bad */ |
| 145 | goto error; |
| 146 | } |
| 147 | } else { |
| 148 | if (size > 0) { |
| 149 | strncpy(dst, addr, size); |
| 150 | dst[size - 1] = '\0'; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | DBG2("IP address resolved to %s", dst); |
| 155 | return 0; |
| 156 | |
| 157 | error: |
| 158 | ERR("URI parse bad hostname %s for af %d", addr, af); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Set default URI attribute which is basically the given stream type and the |
| 164 | * default port if none is set in the URI. |
| 165 | */ |
| 166 | static void set_default_uri_attr(struct lttng_uri *uri, |
| 167 | enum lttng_stream_type stype) |
| 168 | { |
| 169 | uri->stype = stype; |
| 170 | if (uri->dtype != LTTNG_DST_PATH && uri->port == 0) { |
| 171 | uri->port = (stype == LTTNG_STREAM_CONTROL) ? |
| 172 | DEFAULT_NETWORK_CONTROL_PORT : DEFAULT_NETWORK_DATA_PORT; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Compare two URL destination. |
| 178 | * |
| 179 | * Return 0 is equal else is not equal. |
| 180 | */ |
| 181 | static int compare_destination(struct lttng_uri *ctrl, struct lttng_uri *data) |
| 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | LTTNG_ASSERT(ctrl); |
| 186 | LTTNG_ASSERT(data); |
| 187 | |
| 188 | switch (ctrl->dtype) { |
| 189 | case LTTNG_DST_IPV4: |
| 190 | ret = strncmp(ctrl->dst.ipv4, data->dst.ipv4, sizeof(ctrl->dst.ipv4)); |
| 191 | break; |
| 192 | case LTTNG_DST_IPV6: |
| 193 | ret = strncmp(ctrl->dst.ipv6, data->dst.ipv6, sizeof(ctrl->dst.ipv6)); |
| 194 | break; |
| 195 | default: |
| 196 | ret = -1; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Build a string URL from a lttng_uri object. |
| 205 | */ |
| 206 | LTTNG_HIDDEN |
| 207 | int uri_to_str_url(struct lttng_uri *uri, char *dst, size_t size) |
| 208 | { |
| 209 | int ipver, ret; |
| 210 | const char *addr; |
| 211 | char proto[5], port[7]; |
| 212 | |
| 213 | LTTNG_ASSERT(uri); |
| 214 | LTTNG_ASSERT(dst); |
| 215 | |
| 216 | if (uri->dtype == LTTNG_DST_PATH) { |
| 217 | ipver = 0; |
| 218 | addr = uri->dst.path; |
| 219 | (void) snprintf(proto, sizeof(proto), "file"); |
| 220 | (void) snprintf(port, sizeof(port), "%s", ""); |
| 221 | } else { |
| 222 | ipver = (uri->dtype == LTTNG_DST_IPV4) ? 4 : 6; |
| 223 | addr = (ipver == 4) ? uri->dst.ipv4 : uri->dst.ipv6; |
| 224 | (void) snprintf(proto, sizeof(proto), "tcp%d", ipver); |
| 225 | (void) snprintf(port, sizeof(port), ":%d", uri->port); |
| 226 | } |
| 227 | |
| 228 | ret = snprintf(dst, size, "%s://%s%s%s%s/%s", proto, |
| 229 | (ipver == 6) ? "[" : "", addr, (ipver == 6) ? "]" : "", |
| 230 | port, uri->subdir); |
| 231 | if (ret < 0) { |
| 232 | PERROR("snprintf uri to url"); |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Compare two URIs. |
| 240 | * |
| 241 | * Return 0 if equal else 1. |
| 242 | */ |
| 243 | LTTNG_HIDDEN |
| 244 | int uri_compare(struct lttng_uri *uri1, struct lttng_uri *uri2) |
| 245 | { |
| 246 | return memcmp(uri1, uri2, sizeof(struct lttng_uri)); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Free URI memory. |
| 251 | */ |
| 252 | LTTNG_HIDDEN |
| 253 | void uri_free(struct lttng_uri *uri) |
| 254 | { |
| 255 | free(uri); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Parses a string URI to a lttng_uri. This function can potentially return |
| 260 | * more than one URI in uris so the size of the array is returned and uris is |
| 261 | * allocated and populated. Caller must free(3) the array. |
| 262 | * |
| 263 | * This function can not detect the stream type of the URI so the caller has to |
| 264 | * make sure the correct type (stype) is set on the return URI(s). The default |
| 265 | * port must also be set by the caller if the returned URI has its port set to |
| 266 | * zero. |
| 267 | * |
| 268 | * NOTE: A good part of the following code was inspired from the "wget" source |
| 269 | * tree from the src/url.c file and url_parse() function. Also, the |
| 270 | * strpbrk_or_eos() function found above is also inspired by the same code. |
| 271 | * This code was originally licensed GPLv2 so we acknolwedge the Free Software |
| 272 | * Foundation here for the work and to make sure we are compliant with it. |
| 273 | */ |
| 274 | LTTNG_HIDDEN |
| 275 | ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris) |
| 276 | { |
| 277 | int ret, i = 0; |
| 278 | /* Size of the uris array. Default is 1 */ |
| 279 | ssize_t size = 1; |
| 280 | char subdir[PATH_MAX]; |
| 281 | unsigned int ctrl_port = 0; |
| 282 | unsigned int data_port = 0; |
| 283 | struct lttng_uri *tmp_uris; |
| 284 | char *addr_f = NULL; |
| 285 | const struct uri_proto *proto; |
| 286 | const char *purl, *addr_e, *addr_b, *subdir_b = NULL; |
| 287 | const char *seps = ":/\0"; |
| 288 | |
| 289 | /* |
| 290 | * The first part is the protocol portion of a maximum of 5 bytes for now. |
| 291 | * The second part is the hostname or IP address. The 255 bytes size is the |
| 292 | * limit found in the RFC 1035 for the total length of a domain name |
| 293 | * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net:// |
| 294 | * protocol, two ports CAN be specified. |
| 295 | */ |
| 296 | |
| 297 | DBG3("URI string: %s", str_uri); |
| 298 | |
| 299 | proto = get_uri_proto(str_uri); |
| 300 | if (proto == NULL) { |
| 301 | ERR("URI parse unknown protocol %s", str_uri); |
| 302 | goto error; |
| 303 | } |
| 304 | |
| 305 | purl = str_uri; |
| 306 | |
| 307 | if (proto->code == P_NET || proto->code == P_NET6) { |
| 308 | /* Special case for net:// which requires two URI objects */ |
| 309 | size = 2; |
| 310 | } |
| 311 | |
| 312 | /* Allocate URI array */ |
| 313 | tmp_uris = zmalloc(sizeof(struct lttng_uri) * size); |
| 314 | if (tmp_uris == NULL) { |
| 315 | PERROR("zmalloc uri"); |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | memset(subdir, 0, sizeof(subdir)); |
| 320 | purl += strlen(proto->leading_string); |
| 321 | |
| 322 | /* Copy known value to the first URI. */ |
| 323 | tmp_uris[0].dtype = proto->dtype; |
| 324 | tmp_uris[0].proto = proto->type; |
| 325 | |
| 326 | if (proto->code == P_FILE) { |
| 327 | if (*purl != '/') { |
| 328 | ERR("Missing destination full path."); |
| 329 | goto free_error; |
| 330 | } |
| 331 | |
| 332 | strncpy(tmp_uris[0].dst.path, purl, sizeof(tmp_uris[0].dst.path)); |
| 333 | tmp_uris[0].dst.path[sizeof(tmp_uris[0].dst.path) - 1] = '\0'; |
| 334 | DBG3("URI file destination: %s", purl); |
| 335 | goto end; |
| 336 | } |
| 337 | |
| 338 | /* Assume we are at the beginning of an address or host of some sort. */ |
| 339 | addr_b = purl; |
| 340 | |
| 341 | /* |
| 342 | * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6 |
| 343 | * address that does not start AND end with brackets will be rejected even |
| 344 | * if valid. |
| 345 | * |
| 346 | * proto://[<addr>]... |
| 347 | * ^ |
| 348 | */ |
| 349 | if (*purl == '[') { |
| 350 | /* Address begins after '[' */ |
| 351 | addr_b = purl + 1; |
| 352 | addr_e = strchr(addr_b, ']'); |
| 353 | if (addr_e == NULL || addr_b == addr_e) { |
| 354 | ERR("Broken IPv6 address %s", addr_b); |
| 355 | goto free_error; |
| 356 | } |
| 357 | |
| 358 | /* Moving parsed URL pointer after the final bracket ']' */ |
| 359 | purl = addr_e + 1; |
| 360 | |
| 361 | /* |
| 362 | * The closing bracket must be followed by a seperator or NULL char. |
| 363 | */ |
| 364 | if (strchr(seps, *purl) == NULL) { |
| 365 | ERR("Unknown symbol after IPv6 address: %s", purl); |
| 366 | goto free_error; |
| 367 | } |
| 368 | } else { |
| 369 | purl = strpbrk_or_eos(purl, seps); |
| 370 | addr_e = purl; |
| 371 | } |
| 372 | |
| 373 | /* Check if we at least have a char for the addr or hostname. */ |
| 374 | if (addr_b == addr_e) { |
| 375 | ERR("No address or hostname detected."); |
| 376 | goto free_error; |
| 377 | } |
| 378 | |
| 379 | addr_f = utils_strdupdelim(addr_b, addr_e); |
| 380 | if (addr_f == NULL) { |
| 381 | goto free_error; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Detect PORT after address. The net/net6 protocol allows up to two port |
| 386 | * so we can define the control and data port. |
| 387 | */ |
| 388 | while (*purl == ':') { |
| 389 | const char *port_b, *port_e; |
| 390 | char *port_f; |
| 391 | |
| 392 | /* Update pass counter */ |
| 393 | i++; |
| 394 | |
| 395 | /* |
| 396 | * Maximum of two ports is possible if P_NET/NET6. Bigger than that, |
| 397 | * two much stuff. |
| 398 | */ |
| 399 | if ((i == 2 && (proto->code != P_NET && proto->code != P_NET6)) |
| 400 | || i > 2) { |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Move parsed URL to port value. |
| 406 | * proto://addr_host:PORT1:PORT2/foo/bar |
| 407 | * ^ |
| 408 | */ |
| 409 | ++purl; |
| 410 | port_b = purl; |
| 411 | purl = strpbrk_or_eos(purl, seps); |
| 412 | port_e = purl; |
| 413 | |
| 414 | if (port_b != port_e) { |
| 415 | int port; |
| 416 | |
| 417 | port_f = utils_strdupdelim(port_b, port_e); |
| 418 | if (port_f == NULL) { |
| 419 | goto free_error; |
| 420 | } |
| 421 | |
| 422 | port = atoi(port_f); |
| 423 | if (port > 0xffff || port <= 0x0) { |
| 424 | ERR("Invalid port number %d", port); |
| 425 | free(port_f); |
| 426 | goto free_error; |
| 427 | } |
| 428 | free(port_f); |
| 429 | |
| 430 | if (i == 1) { |
| 431 | ctrl_port = port; |
| 432 | } else { |
| 433 | data_port = port; |
| 434 | } |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | /* Check for a valid subdir or trailing garbage */ |
| 439 | if (*purl == '/') { |
| 440 | /* |
| 441 | * Move to subdir value. |
| 442 | * proto://addr_host:PORT1:PORT2/foo/bar |
| 443 | * ^ |
| 444 | */ |
| 445 | ++purl; |
| 446 | subdir_b = purl; |
| 447 | } else if (*purl != '\0') { |
| 448 | ERR("Trailing characters not recognized: %s", purl); |
| 449 | goto free_error; |
| 450 | } |
| 451 | |
| 452 | /* We have enough valid information to create URI(s) object */ |
| 453 | |
| 454 | /* Copy generic information */ |
| 455 | tmp_uris[0].port = ctrl_port; |
| 456 | |
| 457 | /* Copy subdirectory if one. */ |
| 458 | if (subdir_b) { |
| 459 | strncpy(tmp_uris[0].subdir, subdir_b, sizeof(tmp_uris[0].subdir)); |
| 460 | tmp_uris[0].subdir[sizeof(tmp_uris[0].subdir) - 1] = '\0'; |
| 461 | } |
| 462 | |
| 463 | switch (proto->code) { |
| 464 | case P_NET: |
| 465 | ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4, |
| 466 | sizeof(tmp_uris[0].dst.ipv4)); |
| 467 | if (ret < 0) { |
| 468 | goto free_error; |
| 469 | } |
| 470 | |
| 471 | memcpy(tmp_uris[1].dst.ipv4, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[1].dst.ipv4)); |
| 472 | |
| 473 | tmp_uris[1].dtype = proto->dtype; |
| 474 | tmp_uris[1].proto = proto->type; |
| 475 | tmp_uris[1].port = data_port; |
| 476 | break; |
| 477 | case P_NET6: |
| 478 | ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6, |
| 479 | sizeof(tmp_uris[0].dst.ipv6)); |
| 480 | if (ret < 0) { |
| 481 | goto free_error; |
| 482 | } |
| 483 | |
| 484 | memcpy(tmp_uris[1].dst.ipv6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[1].dst.ipv6)); |
| 485 | |
| 486 | tmp_uris[1].dtype = proto->dtype; |
| 487 | tmp_uris[1].proto = proto->type; |
| 488 | tmp_uris[1].port = data_port; |
| 489 | break; |
| 490 | case P_TCP: |
| 491 | ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4, |
| 492 | sizeof(tmp_uris[0].dst.ipv4)); |
| 493 | if (ret < 0) { |
| 494 | goto free_error; |
| 495 | } |
| 496 | break; |
| 497 | case P_TCP6: |
| 498 | ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6, |
| 499 | sizeof(tmp_uris[0].dst.ipv6)); |
| 500 | if (ret < 0) { |
| 501 | goto free_error; |
| 502 | } |
| 503 | break; |
| 504 | default: |
| 505 | goto free_error; |
| 506 | } |
| 507 | |
| 508 | end: |
| 509 | DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d", |
| 510 | proto->dtype, proto->type, (addr_f == NULL) ? "" : addr_f, |
| 511 | (subdir_b == NULL) ? "" : subdir_b, ctrl_port, data_port); |
| 512 | |
| 513 | free(addr_f); |
| 514 | |
| 515 | *uris = tmp_uris; |
| 516 | LTTNG_ASSERT(size == 1 || size == 2); |
| 517 | return size; |
| 518 | |
| 519 | free_error: |
| 520 | free(addr_f); |
| 521 | free(tmp_uris); |
| 522 | error: |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Parse a string URL and creates URI(s) returning the size of the populated |
| 528 | * array. |
| 529 | */ |
| 530 | LTTNG_HIDDEN |
| 531 | ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url, |
| 532 | struct lttng_uri **uris) |
| 533 | { |
| 534 | unsigned int equal = 1, idx = 0; |
| 535 | /* Add the "file://" size to the URL maximum size */ |
| 536 | char url[PATH_MAX + 7]; |
| 537 | ssize_t ctrl_uri_count = 0, data_uri_count = 0, uri_count; |
| 538 | struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL; |
| 539 | struct lttng_uri *tmp_uris = NULL; |
| 540 | |
| 541 | /* No URL(s) is allowed. This means that the consumer will be disabled. */ |
| 542 | if (ctrl_url == NULL && data_url == NULL) { |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | /* Check if URLs are equal and if so, only use the control URL */ |
| 547 | if ((ctrl_url && *ctrl_url != '\0') && (data_url && *data_url != '\0')) { |
| 548 | equal = !strcmp(ctrl_url, data_url); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Since we allow the str_url to be a full local filesystem path, we are |
| 553 | * going to create a valid file:// URL if it's the case. |
| 554 | * |
| 555 | * Check if first character is a '/' or else reject the URL. |
| 556 | */ |
| 557 | if (ctrl_url && ctrl_url[0] == '/') { |
| 558 | int ret; |
| 559 | |
| 560 | ret = snprintf(url, sizeof(url), "file://%s", ctrl_url); |
| 561 | if (ret < 0) { |
| 562 | PERROR("snprintf file url"); |
| 563 | goto parse_error; |
| 564 | } else if (ret >= sizeof(url)) { |
| 565 | PERROR("snprintf file url is too long"); |
| 566 | goto parse_error; |
| 567 | |
| 568 | } |
| 569 | ctrl_url = url; |
| 570 | } |
| 571 | |
| 572 | /* Parse the control URL if there is one */ |
| 573 | if (ctrl_url && *ctrl_url != '\0') { |
| 574 | ctrl_uri_count = uri_parse(ctrl_url, &ctrl_uris); |
| 575 | if (ctrl_uri_count < 1) { |
| 576 | ERR("Unable to parse the URL %s", ctrl_url); |
| 577 | goto parse_error; |
| 578 | } |
| 579 | |
| 580 | /* 1 and 2 are the only expected values on success. */ |
| 581 | LTTNG_ASSERT(ctrl_uri_count == 1 || ctrl_uri_count == 2); |
| 582 | |
| 583 | /* At this point, we know there is at least one URI in the array */ |
| 584 | set_default_uri_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL); |
| 585 | |
| 586 | if (ctrl_uris[0].dtype == LTTNG_DST_PATH && |
| 587 | (data_url && *data_url != '\0')) { |
| 588 | ERR("Cannot have a data URL when destination is file://"); |
| 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | /* URL are not equal but the control URL uses a net:// protocol */ |
| 593 | if (ctrl_uri_count == 2) { |
| 594 | if (!equal) { |
| 595 | ERR("Control URL uses the net:// protocol and the data URL is " |
| 596 | "different. Not allowed."); |
| 597 | goto error; |
| 598 | } else { |
| 599 | set_default_uri_attr(&ctrl_uris[1], LTTNG_STREAM_DATA); |
| 600 | /* |
| 601 | * The data_url and ctrl_url are equal and the ctrl_url |
| 602 | * contains a net:// protocol so we just skip the data part. |
| 603 | */ |
| 604 | data_url = NULL; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (data_url && *data_url != '\0') { |
| 610 | int ret; |
| 611 | |
| 612 | /* We have to parse the data URL in this case */ |
| 613 | data_uri_count = uri_parse(data_url, &data_uris); |
| 614 | if (data_uri_count < 1) { |
| 615 | ERR("Unable to parse the URL %s", data_url); |
| 616 | goto error; |
| 617 | } else if (data_uri_count == 2) { |
| 618 | ERR("Data URL can not be set with the net[4|6]:// protocol"); |
| 619 | goto error; |
| 620 | } else { |
| 621 | /* 1 and 2 are the only expected values on success. */ |
| 622 | LTTNG_ASSERT(data_uri_count == 1); |
| 623 | } |
| 624 | |
| 625 | set_default_uri_attr(&data_uris[0], LTTNG_STREAM_DATA); |
| 626 | |
| 627 | if (ctrl_uris) { |
| 628 | ret = compare_destination(&ctrl_uris[0], &data_uris[0]); |
| 629 | if (ret != 0) { |
| 630 | ERR("Control and data destination mismatch"); |
| 631 | goto error; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* Compute total size. */ |
| 637 | uri_count = ctrl_uri_count + data_uri_count; |
| 638 | if (uri_count <= 0) { |
| 639 | goto error; |
| 640 | } |
| 641 | |
| 642 | tmp_uris = zmalloc(sizeof(struct lttng_uri) * uri_count); |
| 643 | if (tmp_uris == NULL) { |
| 644 | PERROR("zmalloc uris"); |
| 645 | goto error; |
| 646 | } |
| 647 | |
| 648 | if (ctrl_uris) { |
| 649 | /* It's possible the control URIs array contains more than one URI */ |
| 650 | memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * ctrl_uri_count); |
| 651 | ++idx; |
| 652 | free(ctrl_uris); |
| 653 | } |
| 654 | |
| 655 | if (data_uris) { |
| 656 | memcpy(&tmp_uris[idx], data_uris, sizeof(struct lttng_uri)); |
| 657 | free(data_uris); |
| 658 | } |
| 659 | |
| 660 | *uris = tmp_uris; |
| 661 | |
| 662 | return uri_count; |
| 663 | |
| 664 | error: |
| 665 | free(ctrl_uris); |
| 666 | free(data_uris); |
| 667 | free(tmp_uris); |
| 668 | parse_error: |
| 669 | return -1; |
| 670 | } |