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>
34 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
39 const char *leading_string
;
40 enum uri_proto_code code
;
41 enum lttng_proto_type type
;
42 enum lttng_dst_type dtype
;
45 /* Supported protocols */
46 static const struct uri_proto proto_uri
[] = {
47 { .name
= "file", .leading_string
= "file://", .code
= P_FILE
, .type
= 0, .dtype
= LTTNG_DST_PATH
},
48 { .name
= "net", .leading_string
= "net://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
49 { .name
= "net4", .leading_string
= "net4://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
50 { .name
= "net6", .leading_string
= "net6://", .code
= P_NET6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
51 { .name
= "tcp", .leading_string
= "tcp://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
52 { .name
= "tcp4", .leading_string
= "tcp4://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
53 { .name
= "tcp6", .leading_string
= "tcp6://", .code
= P_TCP6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
54 /* Invalid proto marking the end of the array. */
55 { NULL
, NULL
, 0, 0, 0 }
59 * Return pointer to the character in s matching one of the characters in
60 * accept. If nothing is found, return pointer to the end of string (eos).
62 static const inline char *strpbrk_or_eos(const char *s
, const char *accept
)
64 char *p
= strpbrk(s
, accept
);
74 * Validate if proto is a supported protocol from proto_uri array.
76 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
78 const struct uri_proto
*supported
= NULL
;
81 if (uri_str
== NULL
) {
85 for (supported
= &proto_uri
[0];
86 supported
->leading_string
!= NULL
; ++supported
) {
87 if (strncasecmp(uri_str
, supported
->leading_string
,
88 strlen(supported
->leading_string
)) == 0) {
101 * Set network address from string into dst. Supports both IP string and
104 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
107 unsigned char buf
[sizeof(struct in6_addr
)];
108 struct hostent
*record
;
113 memset(dst
, 0, size
);
115 /* Network protocol */
116 ret
= inet_pton(af
, addr
, buf
);
118 /* We consider the dst to be an hostname or an invalid IP char */
119 record
= gethostbyname2(addr
, af
);
120 if (record
== NULL
) {
121 /* At this point, the IP or the hostname is bad */
122 ERR("URI parse bad hostname %s for af %d", addr
, af
);
126 /* Translate IP to string */
127 (void) inet_ntop(af
, record
->h_addr_list
[0], dst
, size
);
130 strncpy(dst
, addr
, size
);
131 dst
[size
- 1] = '\0';
135 DBG2("IP address resolved to %s", dst
);
144 * Set default URI attribute which is basically the given stream type and the
145 * default port if none is set in the URI.
147 static void set_default_uri_attr(struct lttng_uri
*uri
,
148 enum lttng_stream_type stype
)
151 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
152 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
153 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
158 * Compare two URL destination.
160 * Return 0 is equal else is not equal.
162 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
169 switch (ctrl
->dtype
) {
171 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
174 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
185 * Build a string URL from a lttng_uri object.
188 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
192 char proto
[5], port
[7];
197 if (uri
->dtype
== LTTNG_DST_PATH
) {
199 addr
= uri
->dst
.path
;
200 (void) snprintf(proto
, sizeof(proto
), "file");
201 (void) snprintf(port
, sizeof(port
), "%s", "");
203 ipver
= (uri
->dtype
== LTTNG_DST_IPV4
) ? 4 : 6;
204 addr
= (ipver
== 4) ? uri
->dst
.ipv4
: uri
->dst
.ipv6
;
205 (void) snprintf(proto
, sizeof(proto
), "tcp%d", ipver
);
206 (void) snprintf(port
, sizeof(port
), ":%d", uri
->port
);
209 ret
= snprintf(dst
, size
, "%s://%s%s%s%s/%s", proto
,
210 (ipver
== 6) ? "[" : "", addr
, (ipver
== 6) ? "]" : "",
213 PERROR("snprintf uri to url");
222 * Return 0 if equal else 1.
225 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
227 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
234 void uri_free(struct lttng_uri
*uri
)
240 * Return an allocated URI.
243 struct lttng_uri
*uri_create(void)
245 struct lttng_uri
*uri
;
247 uri
= zmalloc(sizeof(struct lttng_uri
));
249 PERROR("zmalloc uri");
256 * Parses a string URI to a lttng_uri. This function can potentially return
257 * more than one URI in uris so the size of the array is returned and uris is
258 * allocated and populated. Caller must free(3) the array.
260 * This function can not detect the stream type of the URI so the caller has to
261 * make sure the correct type (stype) is set on the return URI(s). The default
262 * port must also be set by the caller if the returned URI has its port set to
265 * NOTE: A good part of the following code was inspired from the "wget" source
266 * tree from the src/url.c file and url_parse() function. Also, the
267 * strpbrk_or_eos() function found above is also inspired by the same code.
268 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
269 * Foundation here for the work and to make sure we are compliant with it.
272 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
275 /* Size of the uris array. Default is 1 */
277 char subdir
[PATH_MAX
];
278 unsigned int ctrl_port
= 0;
279 unsigned int data_port
= 0;
280 struct lttng_uri
*tmp_uris
;
282 const struct uri_proto
*proto
;
283 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
284 const char *seps
= ":/\0";
287 * The first part is the protocol portion of a maximum of 5 bytes for now.
288 * The second part is the hostname or IP address. The 255 bytes size is the
289 * limit found in the RFC 1035 for the total length of a domain name
290 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
291 * protocol, two ports CAN be specified.
294 DBG3("URI string: %s", str_uri
);
296 proto
= get_uri_proto(str_uri
);
298 ERR("URI parse unknown protocol %s", str_uri
);
304 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
305 /* Special case for net:// which requires two URI objects */
309 /* Allocate URI array */
310 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
311 if (tmp_uris
== NULL
) {
312 PERROR("zmalloc uri");
316 memset(subdir
, 0, sizeof(subdir
));
317 purl
+= strlen(proto
->leading_string
);
319 /* Copy known value to the first URI. */
320 tmp_uris
[0].dtype
= proto
->dtype
;
321 tmp_uris
[0].proto
= proto
->type
;
323 if (proto
->code
== P_FILE
) {
325 ERR("Missing destination full path.");
329 strncpy(tmp_uris
[0].dst
.path
, purl
, sizeof(tmp_uris
[0].dst
.path
));
330 tmp_uris
[0].dst
.path
[sizeof(tmp_uris
[0].dst
.path
) - 1] = '\0';
331 DBG3("URI file destination: %s", purl
);
335 /* Assume we are at the beginning of an address or host of some sort. */
339 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
340 * address that does not start AND end with brackets will be rejected even
343 * proto://[<addr>]...
347 /* Address begins after '[' */
349 addr_e
= strchr(addr_b
, ']');
350 if (addr_e
== NULL
|| addr_b
== addr_e
) {
351 ERR("Broken IPv6 address %s", addr_b
);
355 /* Moving parsed URL pointer after the final bracket ']' */
359 * The closing bracket must be followed by a seperator or NULL char.
361 if (strchr(seps
, *purl
) == NULL
) {
362 ERR("Unknown symbol after IPv6 address: %s", purl
);
366 purl
= strpbrk_or_eos(purl
, seps
);
370 /* Check if we at least have a char for the addr or hostname. */
371 if (addr_b
== addr_e
) {
372 ERR("No address or hostname detected.");
376 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
377 if (addr_f
== NULL
) {
382 * Detect PORT after address. The net/net6 protocol allows up to two port
383 * so we can define the control and data port.
385 while (*purl
== ':') {
386 const char *port_b
, *port_e
;
389 /* Update pass counter */
393 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
396 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
402 * Move parsed URL to port value.
403 * proto://addr_host:PORT1:PORT2/foo/bar
408 purl
= strpbrk_or_eos(purl
, seps
);
411 if (port_b
!= port_e
) {
414 port_f
= utils_strdupdelim(port_b
, port_e
);
415 if (port_f
== NULL
) {
420 if (port
> 0xffff || port
<= 0x0) {
421 ERR("Invalid port number %d", port
);
435 /* Check for a valid subdir or trailing garbage */
438 * Move to subdir value.
439 * proto://addr_host:PORT1:PORT2/foo/bar
444 } else if (*purl
!= '\0') {
445 ERR("Trailing characters not recognized: %s", purl
);
449 /* We have enough valid information to create URI(s) object */
451 /* Copy generic information */
452 tmp_uris
[0].port
= ctrl_port
;
454 /* Copy subdirectory if one. */
456 strncpy(tmp_uris
[0].subdir
, subdir_b
, sizeof(tmp_uris
[0].subdir
));
457 tmp_uris
[0].subdir
[sizeof(tmp_uris
[0].subdir
) - 1] = '\0';
460 switch (proto
->code
) {
462 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
463 sizeof(tmp_uris
[0].dst
.ipv4
));
468 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
470 tmp_uris
[1].dtype
= proto
->dtype
;
471 tmp_uris
[1].proto
= proto
->type
;
472 tmp_uris
[1].port
= data_port
;
475 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
476 sizeof(tmp_uris
[0].dst
.ipv6
));
481 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
483 tmp_uris
[1].dtype
= proto
->dtype
;
484 tmp_uris
[1].proto
= proto
->type
;
485 tmp_uris
[1].port
= data_port
;
488 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
489 sizeof(tmp_uris
[0].dst
.ipv4
));
495 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
496 sizeof(tmp_uris
[0].dst
.ipv6
));
506 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
507 proto
->dtype
, proto
->type
, (addr_f
== NULL
) ? "" : addr_f
,
508 (subdir_b
== NULL
) ? "" : subdir_b
, ctrl_port
, data_port
);
523 * Parse a string URL and creates URI(s) returning the size of the populated
527 ssize_t
uri_parse_str_urls(const char *ctrl_url
, const char *data_url
,
528 struct lttng_uri
**uris
)
530 unsigned int equal
= 1, idx
= 0;
531 /* Add the "file://" size to the URL maximum size */
532 char url
[PATH_MAX
+ 7];
533 ssize_t size_ctrl
= 0, size_data
= 0, size
;
534 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
535 struct lttng_uri
*tmp_uris
= NULL
;
537 /* No URL(s) is allowed. This means that the consumer will be disabled. */
538 if (ctrl_url
== NULL
&& data_url
== NULL
) {
542 /* Check if URLs are equal and if so, only use the control URL */
543 if ((ctrl_url
&& *ctrl_url
!= '\0') && (data_url
&& *data_url
!= '\0')) {
544 equal
= !strcmp(ctrl_url
, data_url
);
548 * Since we allow the str_url to be a full local filesystem path, we are
549 * going to create a valid file:// URL if it's the case.
551 * Check if first character is a '/' or else reject the URL.
553 if (ctrl_url
&& ctrl_url
[0] == '/') {
556 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
558 PERROR("snprintf file url");
564 /* Parse the control URL if there is one */
565 if (ctrl_url
&& *ctrl_url
!= '\0') {
566 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
568 ERR("Unable to parse the URL %s", ctrl_url
);
572 /* At this point, we know there is at least one URI in the array */
573 set_default_uri_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
575 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&&
576 (data_url
&& *data_url
!= '\0')) {
577 ERR("Can not have a data URL when destination is file://");
581 /* URL are not equal but the control URL uses a net:// protocol */
582 if (size_ctrl
== 2) {
584 ERR("Control URL uses the net:// protocol and the data URL is "
585 "different. Not allowed.");
588 set_default_uri_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
590 * The data_url and ctrl_url are equal and the ctrl_url
591 * contains a net:// protocol so we just skip the data part.
598 if (data_url
&& *data_url
!= '\0') {
601 /* We have to parse the data URL in this case */
602 size_data
= uri_parse(data_url
, &data_uris
);
604 ERR("Unable to parse the URL %s", data_url
);
606 } else if (size_data
== 2) {
607 ERR("Data URL can not be set with the net[4|6]:// protocol");
611 set_default_uri_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
613 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
615 ERR("Control and data destination mismatch");
620 /* Compute total size */
621 size
= size_ctrl
+ size_data
;
623 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
624 if (tmp_uris
== NULL
) {
625 PERROR("zmalloc uris");
630 /* It's possible the control URIs array contains more than one URI */
631 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
637 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));