2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <arpa/inet.h>
25 #include <sys/socket.h>
27 #include <common/common.h>
28 #include <common/defaults.h>
29 #include <common/utils.h>
33 #define LOOPBACK_ADDR_IPV4 "127.0.0.1"
34 #define LOOPBACK_ADDR_IPV6 "::1"
37 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
42 const char *leading_string
;
43 enum uri_proto_code code
;
44 enum lttng_proto_type type
;
45 enum lttng_dst_type dtype
;
48 /* Supported protocols */
49 static const struct uri_proto proto_uri
[] = {
50 { .name
= "file", .leading_string
= "file://", .code
= P_FILE
, .type
= 0, .dtype
= LTTNG_DST_PATH
},
51 { .name
= "net", .leading_string
= "net://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
52 { .name
= "net4", .leading_string
= "net4://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
53 { .name
= "net6", .leading_string
= "net6://", .code
= P_NET6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
54 { .name
= "tcp", .leading_string
= "tcp://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
55 { .name
= "tcp4", .leading_string
= "tcp4://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
56 { .name
= "tcp6", .leading_string
= "tcp6://", .code
= P_TCP6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
57 /* Invalid proto marking the end of the array. */
58 { NULL
, NULL
, 0, 0, 0 }
62 * Return pointer to the character in s matching one of the characters in
63 * accept. If nothing is found, return pointer to the end of string (eos).
65 static const inline char *strpbrk_or_eos(const char *s
, const char *accept
)
67 char *p
= strpbrk(s
, accept
);
77 * Validate if proto is a supported protocol from proto_uri array.
79 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
81 const struct uri_proto
*supported
= NULL
;
84 if (uri_str
== NULL
) {
88 for (supported
= &proto_uri
[0];
89 supported
->leading_string
!= NULL
; ++supported
) {
90 if (strncasecmp(uri_str
, supported
->leading_string
,
91 strlen(supported
->leading_string
)) == 0) {
104 * Set network address from string into dst. Supports both IP string and
107 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
110 unsigned char buf
[sizeof(struct in6_addr
)];
111 struct hostent
*record
;
116 memset(dst
, 0, size
);
118 /* Network protocol */
119 ret
= inet_pton(af
, addr
, buf
);
121 /* We consider the dst to be an hostname or an invalid IP char */
122 record
= gethostbyname2(addr
, af
);
124 /* Translate IP to string */
125 if (!inet_ntop(af
, record
->h_addr_list
[0], dst
, size
)) {
129 } else if (!strcmp(addr
, "localhost") &&
130 (af
== AF_INET
|| af
== AF_INET6
)) {
132 * Some systems may not have "localhost" defined in
133 * accordance with IETF RFC 6761. According to this RFC,
134 * applications may recognize "localhost" names as
135 * special and resolve to the appropriate loopback
138 * We choose to use the system name resolution API first
139 * to honor its network configuration. If this fails, we
140 * resolve to the appropriate loopback address. This is
141 * done to accomodate systems which may want to start
142 * tracing before their network configured.
144 const char *loopback_addr
= af
== AF_INET
?
145 LOOPBACK_ADDR_IPV4
: LOOPBACK_ADDR_IPV6
;
146 const size_t loopback_addr_len
= af
== AF_INET
?
147 sizeof(LOOPBACK_ADDR_IPV4
) :
148 sizeof(LOOPBACK_ADDR_IPV6
);
150 DBG2("Could not resolve localhost address, using fallback");
151 if (loopback_addr_len
> size
) {
152 ERR("Could not resolve localhost address; destination string is too short");
155 strcpy(dst
, loopback_addr
);
157 /* At this point, the IP or the hostname is bad */
162 strncpy(dst
, addr
, size
);
163 dst
[size
- 1] = '\0';
167 DBG2("IP address resolved to %s", dst
);
171 ERR("URI parse bad hostname %s for af %d", addr
, af
);
176 * Set default URI attribute which is basically the given stream type and the
177 * default port if none is set in the URI.
179 static void set_default_uri_attr(struct lttng_uri
*uri
,
180 enum lttng_stream_type stype
)
183 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
184 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
185 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
190 * Compare two URL destination.
192 * Return 0 is equal else is not equal.
194 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
201 switch (ctrl
->dtype
) {
203 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
206 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
217 * Build a string URL from a lttng_uri object.
220 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
224 char proto
[5], port
[7];
229 if (uri
->dtype
== LTTNG_DST_PATH
) {
231 addr
= uri
->dst
.path
;
232 (void) snprintf(proto
, sizeof(proto
), "file");
233 (void) snprintf(port
, sizeof(port
), "%s", "");
235 ipver
= (uri
->dtype
== LTTNG_DST_IPV4
) ? 4 : 6;
236 addr
= (ipver
== 4) ? uri
->dst
.ipv4
: uri
->dst
.ipv6
;
237 (void) snprintf(proto
, sizeof(proto
), "tcp%d", ipver
);
238 (void) snprintf(port
, sizeof(port
), ":%d", uri
->port
);
241 ret
= snprintf(dst
, size
, "%s://%s%s%s%s/%s", proto
,
242 (ipver
== 6) ? "[" : "", addr
, (ipver
== 6) ? "]" : "",
245 PERROR("snprintf uri to url");
254 * Return 0 if equal else 1.
257 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
259 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
266 void uri_free(struct lttng_uri
*uri
)
272 * Return an allocated URI.
275 struct lttng_uri
*uri_create(void)
277 struct lttng_uri
*uri
;
279 uri
= zmalloc(sizeof(struct lttng_uri
));
281 PERROR("zmalloc uri");
288 * Parses a string URI to a lttng_uri. This function can potentially return
289 * more than one URI in uris so the size of the array is returned and uris is
290 * allocated and populated. Caller must free(3) the array.
292 * This function can not detect the stream type of the URI so the caller has to
293 * make sure the correct type (stype) is set on the return URI(s). The default
294 * port must also be set by the caller if the returned URI has its port set to
297 * NOTE: A good part of the following code was inspired from the "wget" source
298 * tree from the src/url.c file and url_parse() function. Also, the
299 * strpbrk_or_eos() function found above is also inspired by the same code.
300 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
301 * Foundation here for the work and to make sure we are compliant with it.
304 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
307 /* Size of the uris array. Default is 1 */
309 char subdir
[PATH_MAX
];
310 unsigned int ctrl_port
= 0;
311 unsigned int data_port
= 0;
312 struct lttng_uri
*tmp_uris
;
314 const struct uri_proto
*proto
;
315 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
316 const char *seps
= ":/\0";
319 * The first part is the protocol portion of a maximum of 5 bytes for now.
320 * The second part is the hostname or IP address. The 255 bytes size is the
321 * limit found in the RFC 1035 for the total length of a domain name
322 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
323 * protocol, two ports CAN be specified.
326 DBG3("URI string: %s", str_uri
);
328 proto
= get_uri_proto(str_uri
);
330 ERR("URI parse unknown protocol %s", str_uri
);
336 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
337 /* Special case for net:// which requires two URI objects */
341 /* Allocate URI array */
342 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
343 if (tmp_uris
== NULL
) {
344 PERROR("zmalloc uri");
348 memset(subdir
, 0, sizeof(subdir
));
349 purl
+= strlen(proto
->leading_string
);
351 /* Copy known value to the first URI. */
352 tmp_uris
[0].dtype
= proto
->dtype
;
353 tmp_uris
[0].proto
= proto
->type
;
355 if (proto
->code
== P_FILE
) {
357 ERR("Missing destination full path.");
361 strncpy(tmp_uris
[0].dst
.path
, purl
, sizeof(tmp_uris
[0].dst
.path
));
362 tmp_uris
[0].dst
.path
[sizeof(tmp_uris
[0].dst
.path
) - 1] = '\0';
363 DBG3("URI file destination: %s", purl
);
367 /* Assume we are at the beginning of an address or host of some sort. */
371 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
372 * address that does not start AND end with brackets will be rejected even
375 * proto://[<addr>]...
379 /* Address begins after '[' */
381 addr_e
= strchr(addr_b
, ']');
382 if (addr_e
== NULL
|| addr_b
== addr_e
) {
383 ERR("Broken IPv6 address %s", addr_b
);
387 /* Moving parsed URL pointer after the final bracket ']' */
391 * The closing bracket must be followed by a seperator or NULL char.
393 if (strchr(seps
, *purl
) == NULL
) {
394 ERR("Unknown symbol after IPv6 address: %s", purl
);
398 purl
= strpbrk_or_eos(purl
, seps
);
402 /* Check if we at least have a char for the addr or hostname. */
403 if (addr_b
== addr_e
) {
404 ERR("No address or hostname detected.");
408 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
409 if (addr_f
== NULL
) {
414 * Detect PORT after address. The net/net6 protocol allows up to two port
415 * so we can define the control and data port.
417 while (*purl
== ':') {
418 const char *port_b
, *port_e
;
421 /* Update pass counter */
425 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
428 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
434 * Move parsed URL to port value.
435 * proto://addr_host:PORT1:PORT2/foo/bar
440 purl
= strpbrk_or_eos(purl
, seps
);
443 if (port_b
!= port_e
) {
446 port_f
= utils_strdupdelim(port_b
, port_e
);
447 if (port_f
== NULL
) {
452 if (port
> 0xffff || port
<= 0x0) {
453 ERR("Invalid port number %d", port
);
467 /* Check for a valid subdir or trailing garbage */
470 * Move to subdir value.
471 * proto://addr_host:PORT1:PORT2/foo/bar
476 } else if (*purl
!= '\0') {
477 ERR("Trailing characters not recognized: %s", purl
);
481 /* We have enough valid information to create URI(s) object */
483 /* Copy generic information */
484 tmp_uris
[0].port
= ctrl_port
;
486 /* Copy subdirectory if one. */
488 strncpy(tmp_uris
[0].subdir
, subdir_b
, sizeof(tmp_uris
[0].subdir
));
489 tmp_uris
[0].subdir
[sizeof(tmp_uris
[0].subdir
) - 1] = '\0';
492 switch (proto
->code
) {
494 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
495 sizeof(tmp_uris
[0].dst
.ipv4
));
500 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
502 tmp_uris
[1].dtype
= proto
->dtype
;
503 tmp_uris
[1].proto
= proto
->type
;
504 tmp_uris
[1].port
= data_port
;
507 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
508 sizeof(tmp_uris
[0].dst
.ipv6
));
513 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
515 tmp_uris
[1].dtype
= proto
->dtype
;
516 tmp_uris
[1].proto
= proto
->type
;
517 tmp_uris
[1].port
= data_port
;
520 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
521 sizeof(tmp_uris
[0].dst
.ipv4
));
527 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
528 sizeof(tmp_uris
[0].dst
.ipv6
));
538 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
539 proto
->dtype
, proto
->type
, (addr_f
== NULL
) ? "" : addr_f
,
540 (subdir_b
== NULL
) ? "" : subdir_b
, ctrl_port
, data_port
);
555 * Parse a string URL and creates URI(s) returning the size of the populated
559 ssize_t
uri_parse_str_urls(const char *ctrl_url
, const char *data_url
,
560 struct lttng_uri
**uris
)
562 unsigned int equal
= 1, idx
= 0;
563 /* Add the "file://" size to the URL maximum size */
564 char url
[PATH_MAX
+ 7];
565 ssize_t size_ctrl
= 0, size_data
= 0, size
;
566 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
567 struct lttng_uri
*tmp_uris
= NULL
;
569 /* No URL(s) is allowed. This means that the consumer will be disabled. */
570 if (ctrl_url
== NULL
&& data_url
== NULL
) {
574 /* Check if URLs are equal and if so, only use the control URL */
575 if ((ctrl_url
&& *ctrl_url
!= '\0') && (data_url
&& *data_url
!= '\0')) {
576 equal
= !strcmp(ctrl_url
, data_url
);
580 * Since we allow the str_url to be a full local filesystem path, we are
581 * going to create a valid file:// URL if it's the case.
583 * Check if first character is a '/' or else reject the URL.
585 if (ctrl_url
&& ctrl_url
[0] == '/') {
588 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
590 PERROR("snprintf file url");
592 } else if (ret
>= sizeof(url
)) {
593 PERROR("snprintf file url is too long");
600 /* Parse the control URL if there is one */
601 if (ctrl_url
&& *ctrl_url
!= '\0') {
602 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
604 ERR("Unable to parse the URL %s", ctrl_url
);
608 /* At this point, we know there is at least one URI in the array */
609 set_default_uri_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
611 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&&
612 (data_url
&& *data_url
!= '\0')) {
613 ERR("Can not have a data URL when destination is file://");
617 /* URL are not equal but the control URL uses a net:// protocol */
618 if (size_ctrl
== 2) {
620 ERR("Control URL uses the net:// protocol and the data URL is "
621 "different. Not allowed.");
624 set_default_uri_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
626 * The data_url and ctrl_url are equal and the ctrl_url
627 * contains a net:// protocol so we just skip the data part.
634 if (data_url
&& *data_url
!= '\0') {
637 /* We have to parse the data URL in this case */
638 size_data
= uri_parse(data_url
, &data_uris
);
640 ERR("Unable to parse the URL %s", data_url
);
642 } else if (size_data
== 2) {
643 ERR("Data URL can not be set with the net[4|6]:// protocol");
647 set_default_uri_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
649 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
651 ERR("Control and data destination mismatch");
656 /* Compute total size */
657 size
= size_ctrl
+ size_data
;
659 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
660 if (tmp_uris
== NULL
) {
661 PERROR("zmalloc uris");
666 /* It's possible the control URIs array contains more than one URI */
667 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
673 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));