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>
24 #include <sys/socket.h>
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/utils.h>
33 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
38 const char *leading_string
;
39 enum uri_proto_code code
;
40 enum lttng_proto_type type
;
41 enum lttng_dst_type dtype
;
44 /* Supported protocols */
45 static const struct uri_proto proto_uri
[] = {
46 { .name
= "file", .leading_string
= "file://", .code
= P_FILE
, .type
= 0, .dtype
= LTTNG_DST_PATH
},
47 { .name
= "net", .leading_string
= "net://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
48 { .name
= "net4", .leading_string
= "net4://", .code
= P_NET
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
49 { .name
= "net6", .leading_string
= "net6://", .code
= P_NET6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
50 { .name
= "tcp", .leading_string
= "tcp://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
51 { .name
= "tcp4", .leading_string
= "tcp4://", .code
= P_TCP
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV4
},
52 { .name
= "tcp6", .leading_string
= "tcp6://", .code
= P_TCP6
, .type
= LTTNG_TCP
, .dtype
= LTTNG_DST_IPV6
},
53 /* Invalid proto marking the end of the array. */
54 { NULL
, NULL
, 0, 0, 0 }
58 * Return pointer to the character in s matching one of the characters in
59 * accept. If nothing is found, return pointer to the end of string (eos).
61 static const inline char *strpbrk_or_eos(const char *s
, const char *accept
)
63 char *p
= strpbrk(s
, accept
);
73 * Validate if proto is a supported protocol from proto_uri array.
75 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
77 const struct uri_proto
*supported
= NULL
;
80 if (uri_str
== NULL
) {
84 for (supported
= &proto_uri
[0];
85 supported
->leading_string
!= NULL
; ++supported
) {
86 if (strncasecmp(uri_str
, supported
->leading_string
,
87 strlen(supported
->leading_string
)) == 0) {
100 * Set network address from string into dst. Supports both IP string and
103 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
106 unsigned char buf
[sizeof(struct in6_addr
)];
107 struct hostent
*record
;
112 memset(dst
, 0, size
);
114 /* Network protocol */
115 ret
= inet_pton(af
, addr
, buf
);
117 /* We consider the dst to be an hostname or an invalid IP char */
118 record
= gethostbyname2(addr
, af
);
119 if (record
== NULL
) {
120 /* At this point, the IP or the hostname is bad */
121 ERR("URI parse bad hostname %s for af %d", addr
, af
);
125 /* Translate IP to string */
126 (void) inet_ntop(af
, record
->h_addr_list
[0], dst
, size
);
129 strncpy(dst
, addr
, size
);
130 dst
[size
- 1] = '\0';
134 DBG2("IP address resolved to %s", dst
);
143 * Set default URI attribute which is basically the given stream type and the
144 * default port if none is set in the URI.
146 static void set_default_uri_attr(struct lttng_uri
*uri
,
147 enum lttng_stream_type stype
)
150 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
151 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
152 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
157 * Compare two URL destination.
159 * Return 0 is equal else is not equal.
161 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
168 switch (ctrl
->dtype
) {
170 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
173 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
184 * Build a string URL from a lttng_uri object.
187 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
191 char proto
[5], port
[7];
196 if (uri
->dtype
== LTTNG_DST_PATH
) {
198 addr
= uri
->dst
.path
;
199 (void) snprintf(proto
, sizeof(proto
), "file");
200 (void) snprintf(port
, sizeof(port
), "%s", "");
202 ipver
= (uri
->dtype
== LTTNG_DST_IPV4
) ? 4 : 6;
203 addr
= (ipver
== 4) ? uri
->dst
.ipv4
: uri
->dst
.ipv6
;
204 (void) snprintf(proto
, sizeof(proto
), "tcp%d", ipver
);
205 (void) snprintf(port
, sizeof(port
), ":%d", uri
->port
);
208 ret
= snprintf(dst
, size
, "%s://%s%s%s%s/%s", proto
,
209 (ipver
== 6) ? "[" : "", addr
, (ipver
== 6) ? "]" : "",
212 PERROR("snprintf uri to url");
221 * Return 0 if equal else 1.
224 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
226 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
233 void uri_free(struct lttng_uri
*uri
)
239 * Return an allocated URI.
242 struct lttng_uri
*uri_create(void)
244 struct lttng_uri
*uri
;
246 uri
= zmalloc(sizeof(struct lttng_uri
));
248 PERROR("zmalloc uri");
255 * Parses a string URI to a lttng_uri. This function can potentially return
256 * more than one URI in uris so the size of the array is returned and uris is
257 * allocated and populated. Caller must free(3) the array.
259 * This function can not detect the stream type of the URI so the caller has to
260 * make sure the correct type (stype) is set on the return URI(s). The default
261 * port must also be set by the caller if the returned URI has its port set to
264 * NOTE: A good part of the following code was inspired from the "wget" source
265 * tree from the src/url.c file and url_parse() function. Also, the
266 * strpbrk_or_eos() function found above is also inspired by the same code.
267 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
268 * Foundation here for the work and to make sure we are compliant with it.
271 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
274 /* Size of the uris array. Default is 1 */
276 char subdir
[PATH_MAX
];
277 unsigned int ctrl_port
= 0;
278 unsigned int data_port
= 0;
279 struct lttng_uri
*tmp_uris
;
281 const struct uri_proto
*proto
;
282 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
283 const char *seps
= ":/\0";
286 * The first part is the protocol portion of a maximum of 5 bytes for now.
287 * The second part is the hostname or IP address. The 255 bytes size is the
288 * limit found in the RFC 1035 for the total length of a domain name
289 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
290 * protocol, two ports CAN be specified.
293 DBG3("URI string: %s", str_uri
);
295 proto
= get_uri_proto(str_uri
);
297 ERR("URI parse unknown protocol %s", str_uri
);
303 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
304 /* Special case for net:// which requires two URI objects */
308 /* Allocate URI array */
309 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
310 if (tmp_uris
== NULL
) {
311 PERROR("zmalloc uri");
315 memset(subdir
, 0, sizeof(subdir
));
316 purl
+= strlen(proto
->leading_string
);
318 /* Copy known value to the first URI. */
319 tmp_uris
[0].dtype
= proto
->dtype
;
320 tmp_uris
[0].proto
= proto
->type
;
322 if (proto
->code
== P_FILE
) {
324 ERR("Missing destination full path.");
328 strncpy(tmp_uris
[0].dst
.path
, purl
, sizeof(tmp_uris
[0].dst
.path
));
329 tmp_uris
[0].dst
.path
[sizeof(tmp_uris
[0].dst
.path
) - 1] = '\0';
330 DBG3("URI file destination: %s", purl
);
334 /* Assume we are at the beginning of an address or host of some sort. */
338 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
339 * address that does not start AND end with brackets will be rejected even
342 * proto://[<addr>]...
346 /* Address begins after '[' */
348 addr_e
= strchr(addr_b
, ']');
349 if (addr_e
== NULL
|| addr_b
== addr_e
) {
350 ERR("Broken IPv6 address %s", addr_b
);
354 /* Moving parsed URL pointer after the final bracket ']' */
358 * The closing bracket must be followed by a seperator or NULL char.
360 if (strchr(seps
, *purl
) == NULL
) {
361 ERR("Unknown symbol after IPv6 address: %s", purl
);
365 purl
= strpbrk_or_eos(purl
, seps
);
369 /* Check if we at least have a char for the addr or hostname. */
370 if (addr_b
== addr_e
) {
371 ERR("No address or hostname detected.");
375 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
376 if (addr_f
== NULL
) {
381 * Detect PORT after address. The net/net6 protocol allows up to two port
382 * so we can define the control and data port.
384 while (*purl
== ':') {
385 const char *port_b
, *port_e
;
388 /* Update pass counter */
392 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
395 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
401 * Move parsed URL to port value.
402 * proto://addr_host:PORT1:PORT2/foo/bar
407 purl
= strpbrk_or_eos(purl
, seps
);
410 if (port_b
!= port_e
) {
413 port_f
= utils_strdupdelim(port_b
, port_e
);
414 if (port_f
== NULL
) {
419 if (port
> 0xffff || port
<= 0x0) {
420 ERR("Invalid port number %d", port
);
434 /* Check for a valid subdir or trailing garbage */
437 * Move to subdir value.
438 * proto://addr_host:PORT1:PORT2/foo/bar
443 } else if (*purl
!= '\0') {
444 ERR("Trailing characters not recognized: %s", purl
);
448 /* We have enough valid information to create URI(s) object */
450 /* Copy generic information */
451 tmp_uris
[0].port
= ctrl_port
;
453 /* Copy subdirectory if one. */
455 strncpy(tmp_uris
[0].subdir
, subdir_b
, sizeof(tmp_uris
[0].subdir
));
456 tmp_uris
[0].subdir
[sizeof(tmp_uris
[0].subdir
) - 1] = '\0';
459 switch (proto
->code
) {
461 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
462 sizeof(tmp_uris
[0].dst
.ipv4
));
467 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
469 tmp_uris
[1].dtype
= proto
->dtype
;
470 tmp_uris
[1].proto
= proto
->type
;
471 tmp_uris
[1].port
= data_port
;
474 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
475 sizeof(tmp_uris
[0].dst
.ipv6
));
480 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
482 tmp_uris
[1].dtype
= proto
->dtype
;
483 tmp_uris
[1].proto
= proto
->type
;
484 tmp_uris
[1].port
= data_port
;
487 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
488 sizeof(tmp_uris
[0].dst
.ipv4
));
494 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
495 sizeof(tmp_uris
[0].dst
.ipv6
));
505 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
506 proto
->dtype
, proto
->type
, (addr_f
== NULL
) ? "" : addr_f
,
507 (subdir_b
== NULL
) ? "" : subdir_b
, ctrl_port
, data_port
);
522 * Parse a string URL and creates URI(s) returning the size of the populated
526 ssize_t
uri_parse_str_urls(const char *ctrl_url
, const char *data_url
,
527 struct lttng_uri
**uris
)
529 unsigned int equal
= 1, idx
= 0;
530 /* Add the "file://" size to the URL maximum size */
531 char url
[PATH_MAX
+ 7];
532 ssize_t size_ctrl
= 0, size_data
= 0, size
;
533 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
534 struct lttng_uri
*tmp_uris
= NULL
;
536 /* No URL(s) is allowed. This means that the consumer will be disabled. */
537 if (ctrl_url
== NULL
&& data_url
== NULL
) {
541 /* Check if URLs are equal and if so, only use the control URL */
542 if ((ctrl_url
&& *ctrl_url
!= '\0') && (data_url
&& *data_url
!= '\0')) {
543 equal
= !strcmp(ctrl_url
, data_url
);
547 * Since we allow the str_url to be a full local filesystem path, we are
548 * going to create a valid file:// URL if it's the case.
550 * Check if first character is a '/' or else reject the URL.
552 if (ctrl_url
&& ctrl_url
[0] == '/') {
555 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
557 PERROR("snprintf file url");
563 /* Parse the control URL if there is one */
564 if (ctrl_url
&& *ctrl_url
!= '\0') {
565 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
567 ERR("Unable to parse the URL %s", ctrl_url
);
571 /* At this point, we know there is at least one URI in the array */
572 set_default_uri_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
574 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&&
575 (data_url
&& *data_url
!= '\0')) {
576 ERR("Can not have a data URL when destination is file://");
580 /* URL are not equal but the control URL uses a net:// protocol */
581 if (size_ctrl
== 2) {
583 ERR("Control URL uses the net:// protocol and the data URL is "
584 "different. Not allowed.");
587 set_default_uri_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
589 * The data_url and ctrl_url are equal and the ctrl_url
590 * contains a net:// protocol so we just skip the data part.
597 if (data_url
&& *data_url
!= '\0') {
600 /* We have to parse the data URL in this case */
601 size_data
= uri_parse(data_url
, &data_uris
);
603 ERR("Unable to parse the URL %s", data_url
);
605 } else if (size_data
== 2) {
606 ERR("Data URL can not be set with the net[4|6]:// protocol");
610 set_default_uri_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
612 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
614 ERR("Control and data destination mismatch");
619 /* Compute total size */
620 size
= size_ctrl
+ size_data
;
622 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
623 if (tmp_uris
== NULL
) {
624 PERROR("zmalloc uris");
629 /* It's possible the control URIs array contains more than one URI */
630 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
636 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));