2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
11 #include <common/common.hpp>
12 #include <common/compat/netdb.hpp>
13 #include <common/defaults.hpp>
14 #include <common/utils.hpp>
16 #include <arpa/inet.h>
19 #include <sys/socket.h>
21 #define LOOPBACK_ADDR_IPV4 "127.0.0.1"
22 #define LOOPBACK_ADDR_IPV6 "::1"
35 const char *leading_string
;
36 enum uri_proto_code code
;
37 enum lttng_proto_type type
;
38 enum lttng_dst_type dtype
;
41 /* Supported protocols */
42 const struct uri_proto proto_uri
[] = { { .name
= "file",
43 .leading_string
= "file://",
45 .type
= LTTNG_PROTO_TYPE_NONE
,
46 .dtype
= LTTNG_DST_PATH
},
48 .leading_string
= "net://",
51 .dtype
= LTTNG_DST_IPV4
},
53 .leading_string
= "net4://",
56 .dtype
= LTTNG_DST_IPV4
},
58 .leading_string
= "net6://",
61 .dtype
= LTTNG_DST_IPV6
},
63 .leading_string
= "tcp://",
66 .dtype
= LTTNG_DST_IPV4
},
68 .leading_string
= "tcp4://",
71 .dtype
= LTTNG_DST_IPV4
},
73 .leading_string
= "tcp6://",
76 .dtype
= LTTNG_DST_IPV6
},
77 /* Invalid proto marking the end of the array. */
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).
85 static inline const char *strpbrk_or_eos(const char *s
, const char *accept
)
87 char *p
= (char *) strpbrk(s
, accept
);
89 p
= (char *) strchr(s
, '\0');
96 * Validate if proto is a supported protocol from proto_uri array.
98 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
100 const struct uri_proto
*supported
= nullptr;
103 if (uri_str
== nullptr) {
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) {
115 /* Proto not found */
123 * Set network address from string into dst. Supports both IP string and
126 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
129 unsigned char buf
[sizeof(struct in6_addr
)];
130 struct hostent
*record
;
135 memset(dst
, 0, size
);
137 /* Network protocol */
138 ret
= inet_pton(af
, addr
, buf
);
140 /* We consider the dst to be an hostname or an invalid IP char */
141 record
= lttng_gethostbyname2(addr
, af
);
143 /* Translate IP to string */
144 if (!inet_ntop(af
, record
->h_addr_list
[0], dst
, size
)) {
148 } else if (!strcmp(addr
, "localhost") && (af
== AF_INET
|| af
== AF_INET6
)) {
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
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.
162 const char *loopback_addr
= af
== AF_INET
? LOOPBACK_ADDR_IPV4
:
164 const size_t loopback_addr_len
= af
== AF_INET
?
165 sizeof(LOOPBACK_ADDR_IPV4
) :
166 sizeof(LOOPBACK_ADDR_IPV6
);
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");
173 strcpy(dst
, loopback_addr
);
175 /* At this point, the IP or the hostname is bad */
180 strncpy(dst
, addr
, size
);
181 dst
[size
- 1] = '\0';
185 DBG2("IP address resolved to %s", dst
);
189 ERR("URI parse bad hostname %s for af %d", addr
, af
);
194 * Set default URI attribute which is basically the given stream type and the
195 * default port if none is set in the URI.
197 static void set_default_uri_attr(struct lttng_uri
*uri
, enum lttng_stream_type 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
;
207 * Compare two URL destination.
209 * Return 0 is equal else is not equal.
211 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
218 switch (ctrl
->dtype
) {
220 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
223 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
234 * Build a string URL from a lttng_uri object.
236 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
240 char proto
[5], port
[7];
245 if (uri
->dtype
== LTTNG_DST_PATH
) {
247 addr
= uri
->dst
.path
;
248 (void) snprintf(proto
, sizeof(proto
), "file");
249 (void) snprintf(port
, sizeof(port
), "%s", "");
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
);
261 (ipver
== 6) ? "[" : "",
263 (ipver
== 6) ? "]" : "",
267 PERROR("snprintf uri to url");
276 * Return 0 if equal else 1.
278 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
280 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
286 void uri_free(struct lttng_uri
*uri
)
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.
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
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.
307 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
310 /* Size of the uris array. Default is 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";
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.
329 DBG3("URI string: %s", str_uri
);
331 proto
= get_uri_proto(str_uri
);
332 if (proto
== nullptr) {
333 ERR("URI parse unknown protocol %s", str_uri
);
339 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
340 /* Special case for net:// which requires two URI objects */
344 /* Allocate URI array */
345 tmp_uris
= calloc
<lttng_uri
>(size
);
346 if (tmp_uris
== nullptr) {
347 PERROR("zmalloc uri");
351 memset(subdir
, 0, sizeof(subdir
));
352 purl
+= strlen(proto
->leading_string
);
354 /* Copy known value to the first URI. */
355 tmp_uris
[0].dtype
= proto
->dtype
;
356 tmp_uris
[0].proto
= proto
->type
;
358 if (proto
->code
== P_FILE
) {
360 ERR("Missing destination full path.");
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
);
370 /* Assume we are at the beginning of an address or host of some sort. */
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
378 * proto://[<addr>]...
382 /* Address begins after '[' */
384 addr_e
= strchr(addr_b
, ']');
385 if (addr_e
== nullptr || addr_b
== addr_e
) {
386 ERR("Broken IPv6 address %s", addr_b
);
390 /* Moving parsed URL pointer after the final bracket ']' */
394 * The closing bracket must be followed by a seperator or NULL char.
396 if (strchr(seps
, *purl
) == nullptr) {
397 ERR("Unknown symbol after IPv6 address: %s", purl
);
401 purl
= strpbrk_or_eos(purl
, seps
);
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.");
411 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
412 if (addr_f
== nullptr) {
417 * Detect PORT after address. The net/net6 protocol allows up to two port
418 * so we can define the control and data port.
420 while (*purl
== ':') {
421 const char *port_b
, *port_e
;
424 /* Update pass counter */
428 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
431 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
)) || i
> 2) {
436 * Move parsed URL to port value.
437 * proto://addr_host:PORT1:PORT2/foo/bar
442 purl
= strpbrk_or_eos(purl
, seps
);
445 if (port_b
!= port_e
) {
448 port_f
= utils_strdupdelim(port_b
, port_e
);
449 if (port_f
== nullptr) {
454 if (port
> 0xffff || port
<= 0x0) {
455 ERR("Invalid port number %d", port
);
469 /* Check for a valid subdir or trailing garbage */
472 * Move to subdir value.
473 * proto://addr_host:PORT1:PORT2/foo/bar
478 } else if (*purl
!= '\0') {
479 ERR("Trailing characters not recognized: %s", purl
);
483 /* We have enough valid information to create URI(s) object */
485 /* Copy generic information */
486 tmp_uris
[0].port
= ctrl_port
;
488 /* Copy subdirectory if one. */
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';
494 switch (proto
->code
) {
496 ret
= set_ip_address(
497 addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[0].dst
.ipv4
));
502 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
504 tmp_uris
[1].dtype
= proto
->dtype
;
505 tmp_uris
[1].proto
= proto
->type
;
506 tmp_uris
[1].port
= data_port
;
509 ret
= set_ip_address(
510 addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[0].dst
.ipv6
));
515 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
517 tmp_uris
[1].dtype
= proto
->dtype
;
518 tmp_uris
[1].proto
= proto
->type
;
519 tmp_uris
[1].port
= data_port
;
522 ret
= set_ip_address(
523 addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[0].dst
.ipv4
));
529 ret
= set_ip_address(
530 addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[0].dst
.ipv6
));
540 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
543 (addr_f
== NULL
) ? "" : addr_f
,
544 (subdir_b
== NULL
) ? "" : subdir_b
,
551 LTTNG_ASSERT(size
== 1 || size
== 2);
562 * Parse a string URL and creates URI(s) returning the size of the populated
565 ssize_t
uri_parse_str_urls(const char *ctrl_url
, const char *data_url
, struct lttng_uri
**uris
)
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;
574 /* No URL(s) is allowed. This means that the consumer will be disabled. */
575 if (ctrl_url
== nullptr && data_url
== nullptr) {
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
);
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.
588 * Check if first character is a '/' or else reject the URL.
590 if (ctrl_url
&& ctrl_url
[0] == '/') {
593 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
595 PERROR("snprintf file url");
597 } else if (ret
>= sizeof(url
)) {
598 PERROR("snprintf file url is too long");
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
);
612 /* 1 and 2 are the only expected values on success. */
613 LTTNG_ASSERT(ctrl_uri_count
== 1 || ctrl_uri_count
== 2);
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
);
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://");
623 /* URL are not equal but the control URL uses a net:// protocol */
624 if (ctrl_uri_count
== 2) {
626 ERR("Control URL uses the net:// protocol and the data URL is "
627 "different. Not allowed.");
630 set_default_uri_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
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.
640 if (data_url
&& *data_url
!= '\0') {
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
);
648 } else if (data_uri_count
== 2) {
649 ERR("Data URL can not be set with the net[4|6]:// protocol");
652 /* 1 and 2 are the only expected values on success. */
653 LTTNG_ASSERT(data_uri_count
== 1);
656 set_default_uri_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
659 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
661 ERR("Control and data destination mismatch");
667 /* Compute total size. */
668 uri_count
= ctrl_uri_count
+ data_uri_count
;
669 if (uri_count
<= 0) {
673 tmp_uris
= calloc
<lttng_uri
>(uri_count
);
674 if (tmp_uris
== nullptr) {
675 PERROR("zmalloc 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
);
687 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));