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