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.
20 #include <arpa/inet.h>
21 #include <common/compat/netdb.h>
24 #include <sys/socket.h>
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/utils.h>
32 #define LOOPBACK_ADDR_IPV4 "127.0.0.1"
33 #define LOOPBACK_ADDR_IPV6 "::1"
36 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
41 const char *leading_string
;
42 enum uri_proto_code code
;
43 enum lttng_proto_type type
;
44 enum lttng_dst_type dtype
;
47 /* Supported protocols */
48 static const struct uri_proto proto_uri
[] = {
49 { .name
= "file", .leading_string
= "file://", .code
= P_FILE
, .type
= 0, .dtype
= LTTNG_DST_PATH
},
50 { .name
= "net", .leading_string
= "net://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
51 { .name
= "net4", .leading_string
= "net4://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
52 { .name
= "net6", .leading_string
= "net6://", .code
= P_NET6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
53 { .name
= "tcp", .leading_string
= "tcp://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
54 { .name
= "tcp4", .leading_string
= "tcp4://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
55 { .name
= "tcp6", .leading_string
= "tcp6://", .code
= P_TCP6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
56 /* Invalid proto marking the end of the array. */
57 { NULL
, NULL
, 0, 0, 0 }
61 * Return pointer to the character in s matching one of the characters in
62 * accept. If nothing is found, return pointer to the end of string (eos).
64 static const inline char *strpbrk_or_eos(const char *s
, const char *accept
)
66 char *p
= strpbrk(s
, accept
);
75 * Validate if proto is a supported protocol from proto_uri array.
77 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
79 const struct uri_proto
*supported
= NULL
;
82 if (uri_str
== NULL
) {
86 for (supported
= &proto_uri
[0];
87 supported
->leading_string
!= NULL
; ++supported
) {
88 if (strncasecmp(uri_str
, supported
->leading_string
,
89 strlen(supported
->leading_string
)) == 0) {
102 * Set network address from string into dst. Supports both IP string and
105 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
108 unsigned char buf
[sizeof(struct in6_addr
)];
109 struct hostent
*record
;
114 memset(dst
, 0, size
);
116 /* Network protocol */
117 ret
= inet_pton(af
, addr
, buf
);
119 /* We consider the dst to be an hostname or an invalid IP char */
120 record
= lttng_gethostbyname2(addr
, af
);
122 /* Translate IP to string */
123 if (!inet_ntop(af
, record
->h_addr_list
[0], dst
, size
)) {
127 } else if (!strcmp(addr
, "localhost") &&
128 (af
== AF_INET
|| af
== AF_INET6
)) {
130 * Some systems may not have "localhost" defined in
131 * accordance with IETF RFC 6761. According to this RFC,
132 * applications may recognize "localhost" names as
133 * special and resolve to the appropriate loopback
136 * We choose to use the system name resolution API first
137 * to honor its network configuration. If this fails, we
138 * resolve to the appropriate loopback address. This is
139 * done to accomodate systems which may want to start
140 * tracing before their network configured.
142 const char *loopback_addr
= af
== AF_INET
?
143 LOOPBACK_ADDR_IPV4
: LOOPBACK_ADDR_IPV6
;
144 const size_t loopback_addr_len
= af
== AF_INET
?
145 sizeof(LOOPBACK_ADDR_IPV4
) :
146 sizeof(LOOPBACK_ADDR_IPV6
);
148 DBG2("Could not resolve localhost address, using fallback");
149 if (loopback_addr_len
> size
) {
150 ERR("Could not resolve localhost address; destination string is too short");
153 strcpy(dst
, loopback_addr
);
155 /* At this point, the IP or the hostname is bad */
160 strncpy(dst
, addr
, size
);
161 dst
[size
- 1] = '\0';
165 DBG2("IP address resolved to %s", dst
);
169 ERR("URI parse bad hostname %s for af %d", addr
, af
);
174 * Set default URI attribute which is basically the given stream type and the
175 * default port if none is set in the URI.
177 static void set_default_uri_attr(struct lttng_uri
*uri
,
178 enum lttng_stream_type stype
)
181 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
182 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
183 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
188 * Compare two URL destination.
190 * Return 0 is equal else is not equal.
192 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
199 switch (ctrl
->dtype
) {
201 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
204 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
215 * Build a string URL from a lttng_uri object.
218 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
222 char proto
[5], port
[7];
227 if (uri
->dtype
== LTTNG_DST_PATH
) {
229 addr
= uri
->dst
.path
;
230 (void) snprintf(proto
, sizeof(proto
), "file");
231 (void) snprintf(port
, sizeof(port
), "%s", "");
233 ipver
= (uri
->dtype
== LTTNG_DST_IPV4
) ? 4 : 6;
234 addr
= (ipver
== 4) ? uri
->dst
.ipv4
: uri
->dst
.ipv6
;
235 (void) snprintf(proto
, sizeof(proto
), "tcp%d", ipver
);
236 (void) snprintf(port
, sizeof(port
), ":%d", uri
->port
);
239 ret
= snprintf(dst
, size
, "%s://%s%s%s%s/%s", proto
,
240 (ipver
== 6) ? "[" : "", addr
, (ipver
== 6) ? "]" : "",
243 PERROR("snprintf uri to url");
252 * Return 0 if equal else 1.
255 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
257 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
264 void uri_free(struct lttng_uri
*uri
)
270 * Return an allocated URI.
273 struct lttng_uri
*uri_create(void)
275 struct lttng_uri
*uri
;
277 uri
= zmalloc(sizeof(struct lttng_uri
));
279 PERROR("zmalloc uri");
286 * Parses a string URI to a lttng_uri. This function can potentially return
287 * more than one URI in uris so the size of the array is returned and uris is
288 * allocated and populated. Caller must free(3) the array.
290 * This function can not detect the stream type of the URI so the caller has to
291 * make sure the correct type (stype) is set on the return URI(s). The default
292 * port must also be set by the caller if the returned URI has its port set to
295 * NOTE: A good part of the following code was inspired from the "wget" source
296 * tree from the src/url.c file and url_parse() function. Also, the
297 * strpbrk_or_eos() function found above is also inspired by the same code.
298 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
299 * Foundation here for the work and to make sure we are compliant with it.
302 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
305 /* Size of the uris array. Default is 1 */
307 char subdir
[PATH_MAX
];
308 unsigned int ctrl_port
= 0;
309 unsigned int data_port
= 0;
310 struct lttng_uri
*tmp_uris
;
312 const struct uri_proto
*proto
;
313 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
314 const char *seps
= ":/\0";
317 * The first part is the protocol portion of a maximum of 5 bytes for now.
318 * The second part is the hostname or IP address. The 255 bytes size is the
319 * limit found in the RFC 1035 for the total length of a domain name
320 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
321 * protocol, two ports CAN be specified.
324 DBG3("URI string: %s", str_uri
);
326 proto
= get_uri_proto(str_uri
);
328 ERR("URI parse unknown protocol %s", str_uri
);
334 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
335 /* Special case for net:// which requires two URI objects */
339 /* Allocate URI array */
340 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
341 if (tmp_uris
== NULL
) {
342 PERROR("zmalloc uri");
346 memset(subdir
, 0, sizeof(subdir
));
347 purl
+= strlen(proto
->leading_string
);
349 /* Copy known value to the first URI. */
350 tmp_uris
[0].dtype
= proto
->dtype
;
351 tmp_uris
[0].proto
= proto
->type
;
353 if (proto
->code
== P_FILE
) {
355 ERR("Missing destination full path.");
359 strncpy(tmp_uris
[0].dst
.path
, purl
, sizeof(tmp_uris
[0].dst
.path
));
360 tmp_uris
[0].dst
.path
[sizeof(tmp_uris
[0].dst
.path
) - 1] = '\0';
361 DBG3("URI file destination: %s", purl
);
365 /* Assume we are at the beginning of an address or host of some sort. */
369 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
370 * address that does not start AND end with brackets will be rejected even
373 * proto://[<addr>]...
377 /* Address begins after '[' */
379 addr_e
= strchr(addr_b
, ']');
380 if (addr_e
== NULL
|| addr_b
== addr_e
) {
381 ERR("Broken IPv6 address %s", addr_b
);
385 /* Moving parsed URL pointer after the final bracket ']' */
389 * The closing bracket must be followed by a seperator or NULL char.
391 if (strchr(seps
, *purl
) == NULL
) {
392 ERR("Unknown symbol after IPv6 address: %s", purl
);
396 purl
= strpbrk_or_eos(purl
, seps
);
400 /* Check if we at least have a char for the addr or hostname. */
401 if (addr_b
== addr_e
) {
402 ERR("No address or hostname detected.");
406 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
407 if (addr_f
== NULL
) {
412 * Detect PORT after address. The net/net6 protocol allows up to two port
413 * so we can define the control and data port.
415 while (*purl
== ':') {
416 const char *port_b
, *port_e
;
419 /* Update pass counter */
423 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
426 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
432 * Move parsed URL to port value.
433 * proto://addr_host:PORT1:PORT2/foo/bar
438 purl
= strpbrk_or_eos(purl
, seps
);
441 if (port_b
!= port_e
) {
444 port_f
= utils_strdupdelim(port_b
, port_e
);
445 if (port_f
== NULL
) {
450 if (port
> 0xffff || port
<= 0x0) {
451 ERR("Invalid port number %d", port
);
465 /* Check for a valid subdir or trailing garbage */
468 * Move to subdir value.
469 * proto://addr_host:PORT1:PORT2/foo/bar
474 } else if (*purl
!= '\0') {
475 ERR("Trailing characters not recognized: %s", purl
);
479 /* We have enough valid information to create URI(s) object */
481 /* Copy generic information */
482 tmp_uris
[0].port
= ctrl_port
;
484 /* Copy subdirectory if one. */
486 strncpy(tmp_uris
[0].subdir
, subdir_b
, sizeof(tmp_uris
[0].subdir
));
487 tmp_uris
[0].subdir
[sizeof(tmp_uris
[0].subdir
) - 1] = '\0';
490 switch (proto
->code
) {
492 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
493 sizeof(tmp_uris
[0].dst
.ipv4
));
498 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
500 tmp_uris
[1].dtype
= proto
->dtype
;
501 tmp_uris
[1].proto
= proto
->type
;
502 tmp_uris
[1].port
= data_port
;
505 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
506 sizeof(tmp_uris
[0].dst
.ipv6
));
511 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
513 tmp_uris
[1].dtype
= proto
->dtype
;
514 tmp_uris
[1].proto
= proto
->type
;
515 tmp_uris
[1].port
= data_port
;
518 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
519 sizeof(tmp_uris
[0].dst
.ipv4
));
525 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
526 sizeof(tmp_uris
[0].dst
.ipv6
));
536 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
537 proto
->dtype
, proto
->type
, (addr_f
== NULL
) ? "" : addr_f
,
538 (subdir_b
== NULL
) ? "" : subdir_b
, ctrl_port
, data_port
);
553 * Parse a string URL and creates URI(s) returning the size of the populated
557 ssize_t
uri_parse_str_urls(const char *ctrl_url
, const char *data_url
,
558 struct lttng_uri
**uris
)
560 unsigned int equal
= 1, idx
= 0;
561 /* Add the "file://" size to the URL maximum size */
562 char url
[PATH_MAX
+ 7];
563 ssize_t size_ctrl
= 0, size_data
= 0, size
;
564 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
565 struct lttng_uri
*tmp_uris
= NULL
;
567 /* No URL(s) is allowed. This means that the consumer will be disabled. */
568 if (ctrl_url
== NULL
&& data_url
== NULL
) {
572 /* Check if URLs are equal and if so, only use the control URL */
573 if ((ctrl_url
&& *ctrl_url
!= '\0') && (data_url
&& *data_url
!= '\0')) {
574 equal
= !strcmp(ctrl_url
, data_url
);
578 * Since we allow the str_url to be a full local filesystem path, we are
579 * going to create a valid file:// URL if it's the case.
581 * Check if first character is a '/' or else reject the URL.
583 if (ctrl_url
&& ctrl_url
[0] == '/') {
586 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
588 PERROR("snprintf file url");
590 } else if (ret
>= sizeof(url
)) {
591 PERROR("snprintf file url is too long");
598 /* Parse the control URL if there is one */
599 if (ctrl_url
&& *ctrl_url
!= '\0') {
600 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
602 ERR("Unable to parse the URL %s", ctrl_url
);
606 /* At this point, we know there is at least one URI in the array */
607 set_default_uri_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
609 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&&
610 (data_url
&& *data_url
!= '\0')) {
611 ERR("Can not have a data URL when destination is file://");
615 /* URL are not equal but the control URL uses a net:// protocol */
616 if (size_ctrl
== 2) {
618 ERR("Control URL uses the net:// protocol and the data URL is "
619 "different. Not allowed.");
622 set_default_uri_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
624 * The data_url and ctrl_url are equal and the ctrl_url
625 * contains a net:// protocol so we just skip the data part.
632 if (data_url
&& *data_url
!= '\0') {
635 /* We have to parse the data URL in this case */
636 size_data
= uri_parse(data_url
, &data_uris
);
638 ERR("Unable to parse the URL %s", data_url
);
640 } else if (size_data
== 2) {
641 ERR("Data URL can not be set with the net[4|6]:// protocol");
645 set_default_uri_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
647 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
649 ERR("Control and data destination mismatch");
654 /* Compute total size */
655 size
= size_ctrl
+ size_data
;
657 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
658 if (tmp_uris
== NULL
) {
659 PERROR("zmalloc uris");
664 /* It's possible the control URIs array contains more than one URI */
665 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
671 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));