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