2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
10 #include <common/compat/netdb.hpp>
13 #include <sys/socket.h>
15 #include <common/common.hpp>
16 #include <common/defaults.hpp>
17 #include <common/utils.hpp>
21 #define LOOPBACK_ADDR_IPV4 "127.0.0.1"
22 #define LOOPBACK_ADDR_IPV6 "::1"
25 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
31 const char *leading_string
;
32 enum uri_proto_code code
;
33 enum lttng_proto_type type
;
34 enum lttng_dst_type dtype
;
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. */
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).
55 static inline const char *strpbrk_or_eos(const char *s
, const char *accept
)
57 char *p
= (char *) strpbrk(s
, accept
);
59 p
= (char *) strchr(s
, '\0');
66 * Validate if proto is a supported protocol from proto_uri array.
68 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
70 const struct uri_proto
*supported
= NULL
;
73 if (uri_str
== NULL
) {
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) {
93 * Set network address from string into dst. Supports both IP string and
96 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
99 unsigned char buf
[sizeof(struct in6_addr
)];
100 struct hostent
*record
;
105 memset(dst
, 0, size
);
107 /* Network protocol */
108 ret
= inet_pton(af
, addr
, buf
);
110 /* We consider the dst to be an hostname or an invalid IP char */
111 record
= lttng_gethostbyname2(addr
, af
);
113 /* Translate IP to string */
114 if (!inet_ntop(af
, record
->h_addr_list
[0], dst
, size
)) {
118 } else if (!strcmp(addr
, "localhost") &&
119 (af
== AF_INET
|| af
== AF_INET6
)) {
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
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.
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
);
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");
144 strcpy(dst
, loopback_addr
);
146 /* At this point, the IP or the hostname is bad */
151 strncpy(dst
, addr
, size
);
152 dst
[size
- 1] = '\0';
156 DBG2("IP address resolved to %s", dst
);
160 ERR("URI parse bad hostname %s for af %d", addr
, af
);
165 * Set default URI attribute which is basically the given stream type and the
166 * default port if none is set in the URI.
168 static void set_default_uri_attr(struct lttng_uri
*uri
,
169 enum lttng_stream_type 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
;
179 * Compare two URL destination.
181 * Return 0 is equal else is not equal.
183 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
190 switch (ctrl
->dtype
) {
192 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
195 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
206 * Build a string URL from a lttng_uri object.
208 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
212 char proto
[5], port
[7];
217 if (uri
->dtype
== LTTNG_DST_PATH
) {
219 addr
= uri
->dst
.path
;
220 (void) snprintf(proto
, sizeof(proto
), "file");
221 (void) snprintf(port
, sizeof(port
), "%s", "");
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
);
229 ret
= snprintf(dst
, size
, "%s://%s%s%s%s/%s", proto
,
230 (ipver
== 6) ? "[" : "", addr
, (ipver
== 6) ? "]" : "",
233 PERROR("snprintf uri to url");
242 * Return 0 if equal else 1.
244 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
246 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
252 void uri_free(struct lttng_uri
*uri
)
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.
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
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.
273 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
276 /* Size of the uris array. Default is 1 */
278 char subdir
[PATH_MAX
];
279 unsigned int ctrl_port
= 0;
280 unsigned int data_port
= 0;
281 struct lttng_uri
*tmp_uris
;
283 const struct uri_proto
*proto
;
284 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
285 const char *seps
= ":/\0";
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.
295 DBG3("URI string: %s", str_uri
);
297 proto
= get_uri_proto(str_uri
);
299 ERR("URI parse unknown protocol %s", str_uri
);
305 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
306 /* Special case for net:// which requires two URI objects */
310 /* Allocate URI array */
311 tmp_uris
= calloc
<lttng_uri
>(size
);
312 if (tmp_uris
== NULL
) {
313 PERROR("zmalloc uri");
317 memset(subdir
, 0, sizeof(subdir
));
318 purl
+= strlen(proto
->leading_string
);
320 /* Copy known value to the first URI. */
321 tmp_uris
[0].dtype
= proto
->dtype
;
322 tmp_uris
[0].proto
= proto
->type
;
324 if (proto
->code
== P_FILE
) {
326 ERR("Missing destination full path.");
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
);
336 /* Assume we are at the beginning of an address or host of some sort. */
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
344 * proto://[<addr>]...
348 /* Address begins after '[' */
350 addr_e
= strchr(addr_b
, ']');
351 if (addr_e
== NULL
|| addr_b
== addr_e
) {
352 ERR("Broken IPv6 address %s", addr_b
);
356 /* Moving parsed URL pointer after the final bracket ']' */
360 * The closing bracket must be followed by a seperator or NULL char.
362 if (strchr(seps
, *purl
) == NULL
) {
363 ERR("Unknown symbol after IPv6 address: %s", purl
);
367 purl
= strpbrk_or_eos(purl
, seps
);
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.");
377 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
378 if (addr_f
== NULL
) {
383 * Detect PORT after address. The net/net6 protocol allows up to two port
384 * so we can define the control and data port.
386 while (*purl
== ':') {
387 const char *port_b
, *port_e
;
390 /* Update pass counter */
394 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
397 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
403 * Move parsed URL to port value.
404 * proto://addr_host:PORT1:PORT2/foo/bar
409 purl
= strpbrk_or_eos(purl
, seps
);
412 if (port_b
!= port_e
) {
415 port_f
= utils_strdupdelim(port_b
, port_e
);
416 if (port_f
== NULL
) {
421 if (port
> 0xffff || port
<= 0x0) {
422 ERR("Invalid port number %d", port
);
436 /* Check for a valid subdir or trailing garbage */
439 * Move to subdir value.
440 * proto://addr_host:PORT1:PORT2/foo/bar
445 } else if (*purl
!= '\0') {
446 ERR("Trailing characters not recognized: %s", purl
);
450 /* We have enough valid information to create URI(s) object */
452 /* Copy generic information */
453 tmp_uris
[0].port
= ctrl_port
;
455 /* Copy subdirectory if one. */
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';
461 switch (proto
->code
) {
463 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
464 sizeof(tmp_uris
[0].dst
.ipv4
));
469 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
471 tmp_uris
[1].dtype
= proto
->dtype
;
472 tmp_uris
[1].proto
= proto
->type
;
473 tmp_uris
[1].port
= data_port
;
476 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
477 sizeof(tmp_uris
[0].dst
.ipv6
));
482 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
484 tmp_uris
[1].dtype
= proto
->dtype
;
485 tmp_uris
[1].proto
= proto
->type
;
486 tmp_uris
[1].port
= data_port
;
489 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
490 sizeof(tmp_uris
[0].dst
.ipv4
));
496 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
497 sizeof(tmp_uris
[0].dst
.ipv6
));
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
);
514 LTTNG_ASSERT(size
== 1 || size
== 2);
525 * Parse a string URL and creates URI(s) returning the size of the populated
528 ssize_t
uri_parse_str_urls(const char *ctrl_url
, const char *data_url
,
529 struct lttng_uri
**uris
)
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
;
538 /* No URL(s) is allowed. This means that the consumer will be disabled. */
539 if (ctrl_url
== NULL
&& data_url
== NULL
) {
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
);
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.
552 * Check if first character is a '/' or else reject the URL.
554 if (ctrl_url
&& ctrl_url
[0] == '/') {
557 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
559 PERROR("snprintf file url");
561 } else if (ret
>= sizeof(url
)) {
562 PERROR("snprintf file url is too long");
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
);
577 /* 1 and 2 are the only expected values on success. */
578 LTTNG_ASSERT(ctrl_uri_count
== 1 || ctrl_uri_count
== 2);
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
);
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://");
589 /* URL are not equal but the control URL uses a net:// protocol */
590 if (ctrl_uri_count
== 2) {
592 ERR("Control URL uses the net:// protocol and the data URL is "
593 "different. Not allowed.");
596 set_default_uri_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
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.
606 if (data_url
&& *data_url
!= '\0') {
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
);
614 } else if (data_uri_count
== 2) {
615 ERR("Data URL can not be set with the net[4|6]:// protocol");
618 /* 1 and 2 are the only expected values on success. */
619 LTTNG_ASSERT(data_uri_count
== 1);
622 set_default_uri_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
625 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
627 ERR("Control and data destination mismatch");
633 /* Compute total size. */
634 uri_count
= ctrl_uri_count
+ data_uri_count
;
635 if (uri_count
<= 0) {
639 tmp_uris
= calloc
<lttng_uri
>(uri_count
);
640 if (tmp_uris
== NULL
) {
641 PERROR("zmalloc 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
);
653 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));