fb61a5bbdc0c61138823fbae0fd143bffb328f65
[lttng-tools.git] / src / common / uri.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <arpa/inet.h>
21 #include <common/compat/netdb.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/utils.h>
29
30 #include "uri.h"
31
32 enum uri_proto_code {
33 P_NET, P_NET6, P_FILE, P_TCP, P_TCP6,
34 };
35
36 struct uri_proto {
37 const char *name;
38 const char *leading_string;
39 enum uri_proto_code code;
40 enum lttng_proto_type type;
41 enum lttng_dst_type dtype;
42 };
43
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 }
55 };
56
57 /*
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).
60 */
61 static const inline char *strpbrk_or_eos(const char *s, const char *accept)
62 {
63 char *p = strpbrk(s, accept);
64 if (p == NULL) {
65 p = strchr(s, '\0');
66 }
67
68 return p;
69 }
70
71 /*
72 * Validate if proto is a supported protocol from proto_uri array.
73 */
74 static const struct uri_proto *get_uri_proto(const char *uri_str)
75 {
76 const struct uri_proto *supported = NULL;
77
78 /* Safety net */
79 if (uri_str == NULL) {
80 goto end;
81 }
82
83 for (supported = &proto_uri[0];
84 supported->leading_string != NULL; ++supported) {
85 if (strncasecmp(uri_str, supported->leading_string,
86 strlen(supported->leading_string)) == 0) {
87 goto end;
88 }
89 }
90
91 /* Proto not found */
92 return NULL;
93
94 end:
95 return supported;
96 }
97
98 /*
99 * Set network address from string into dst. Supports both IP string and
100 * hostname.
101 */
102 static int set_ip_address(const char *addr, int af, char *dst, size_t size)
103 {
104 int ret;
105 unsigned char buf[sizeof(struct in6_addr)];
106 struct hostent *record;
107
108 assert(addr);
109 assert(dst);
110
111 memset(dst, 0, size);
112
113 /* Network protocol */
114 ret = inet_pton(af, addr, buf);
115 if (ret < 1) {
116 /* We consider the dst to be an hostname or an invalid IP char */
117 record = lttng_gethostbyname2(addr, af);
118 if (record == NULL) {
119 /* At this point, the IP or the hostname is bad */
120 ERR("URI parse bad hostname %s for af %d", addr, af);
121 goto error;
122 }
123
124 /* Translate IP to string */
125 (void) inet_ntop(af, record->h_addr_list[0], dst, size);
126 } else {
127 if (size > 0) {
128 strncpy(dst, addr, size);
129 dst[size - 1] = '\0';
130 }
131 }
132
133 DBG2("IP address resolved to %s", dst);
134
135 return 0;
136
137 error:
138 return -1;
139 }
140
141 /*
142 * Set default URI attribute which is basically the given stream type and the
143 * default port if none is set in the URI.
144 */
145 static void set_default_uri_attr(struct lttng_uri *uri,
146 enum lttng_stream_type stype)
147 {
148 uri->stype = stype;
149 if (uri->dtype != LTTNG_DST_PATH && uri->port == 0) {
150 uri->port = (stype == LTTNG_STREAM_CONTROL) ?
151 DEFAULT_NETWORK_CONTROL_PORT : DEFAULT_NETWORK_DATA_PORT;
152 }
153 }
154
155 /*
156 * Compare two URL destination.
157 *
158 * Return 0 is equal else is not equal.
159 */
160 static int compare_destination(struct lttng_uri *ctrl, struct lttng_uri *data)
161 {
162 int ret;
163
164 assert(ctrl);
165 assert(data);
166
167 switch (ctrl->dtype) {
168 case LTTNG_DST_IPV4:
169 ret = strncmp(ctrl->dst.ipv4, data->dst.ipv4, sizeof(ctrl->dst.ipv4));
170 break;
171 case LTTNG_DST_IPV6:
172 ret = strncmp(ctrl->dst.ipv6, data->dst.ipv6, sizeof(ctrl->dst.ipv6));
173 break;
174 default:
175 ret = -1;
176 break;
177 }
178
179 return ret;
180 }
181
182 /*
183 * Build a string URL from a lttng_uri object.
184 */
185 LTTNG_HIDDEN
186 int uri_to_str_url(struct lttng_uri *uri, char *dst, size_t size)
187 {
188 int ipver, ret;
189 const char *addr;
190 char proto[5], port[7];
191
192 assert(uri);
193 assert(dst);
194
195 if (uri->dtype == LTTNG_DST_PATH) {
196 ipver = 0;
197 addr = uri->dst.path;
198 (void) snprintf(proto, sizeof(proto), "file");
199 (void) snprintf(port, sizeof(port), "%s", "");
200 } else {
201 ipver = (uri->dtype == LTTNG_DST_IPV4) ? 4 : 6;
202 addr = (ipver == 4) ? uri->dst.ipv4 : uri->dst.ipv6;
203 (void) snprintf(proto, sizeof(proto), "tcp%d", ipver);
204 (void) snprintf(port, sizeof(port), ":%d", uri->port);
205 }
206
207 ret = snprintf(dst, size, "%s://%s%s%s%s/%s", proto,
208 (ipver == 6) ? "[" : "", addr, (ipver == 6) ? "]" : "",
209 port, uri->subdir);
210 if (ret < 0) {
211 PERROR("snprintf uri to url");
212 }
213
214 return ret;
215 }
216
217 /*
218 * Compare two URIs.
219 *
220 * Return 0 if equal else 1.
221 */
222 LTTNG_HIDDEN
223 int uri_compare(struct lttng_uri *uri1, struct lttng_uri *uri2)
224 {
225 return memcmp(uri1, uri2, sizeof(struct lttng_uri));
226 }
227
228 /*
229 * Free URI memory.
230 */
231 LTTNG_HIDDEN
232 void uri_free(struct lttng_uri *uri)
233 {
234 free(uri);
235 }
236
237 /*
238 * Return an allocated URI.
239 */
240 LTTNG_HIDDEN
241 struct lttng_uri *uri_create(void)
242 {
243 struct lttng_uri *uri;
244
245 uri = zmalloc(sizeof(struct lttng_uri));
246 if (uri == NULL) {
247 PERROR("zmalloc uri");
248 }
249
250 return uri;
251 }
252
253 /*
254 * Parses a string URI to a lttng_uri. This function can potentially return
255 * more than one URI in uris so the size of the array is returned and uris is
256 * allocated and populated. Caller must free(3) the array.
257 *
258 * This function can not detect the stream type of the URI so the caller has to
259 * make sure the correct type (stype) is set on the return URI(s). The default
260 * port must also be set by the caller if the returned URI has its port set to
261 * zero.
262 *
263 * NOTE: A good part of the following code was inspired from the "wget" source
264 * tree from the src/url.c file and url_parse() function. Also, the
265 * strpbrk_or_eos() function found above is also inspired by the same code.
266 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
267 * Foundation here for the work and to make sure we are compliant with it.
268 */
269 LTTNG_HIDDEN
270 ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
271 {
272 int ret, i = 0;
273 /* Size of the uris array. Default is 1 */
274 ssize_t size = 1;
275 char subdir[PATH_MAX];
276 unsigned int ctrl_port = 0;
277 unsigned int data_port = 0;
278 struct lttng_uri *tmp_uris;
279 char *addr_f = NULL;
280 const struct uri_proto *proto;
281 const char *purl, *addr_e, *addr_b, *subdir_b = NULL;
282 const char *seps = ":/\0";
283
284 /*
285 * The first part is the protocol portion of a maximum of 5 bytes for now.
286 * The second part is the hostname or IP address. The 255 bytes size is the
287 * limit found in the RFC 1035 for the total length of a domain name
288 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
289 * protocol, two ports CAN be specified.
290 */
291
292 DBG3("URI string: %s", str_uri);
293
294 proto = get_uri_proto(str_uri);
295 if (proto == NULL) {
296 ERR("URI parse unknown protocol %s", str_uri);
297 goto error;
298 }
299
300 purl = str_uri;
301
302 if (proto->code == P_NET || proto->code == P_NET6) {
303 /* Special case for net:// which requires two URI objects */
304 size = 2;
305 }
306
307 /* Allocate URI array */
308 tmp_uris = zmalloc(sizeof(struct lttng_uri) * size);
309 if (tmp_uris == NULL) {
310 PERROR("zmalloc uri");
311 goto error;
312 }
313
314 memset(subdir, 0, sizeof(subdir));
315 purl += strlen(proto->leading_string);
316
317 /* Copy known value to the first URI. */
318 tmp_uris[0].dtype = proto->dtype;
319 tmp_uris[0].proto = proto->type;
320
321 if (proto->code == P_FILE) {
322 if (*purl != '/') {
323 ERR("Missing destination full path.");
324 goto free_error;
325 }
326
327 strncpy(tmp_uris[0].dst.path, purl, sizeof(tmp_uris[0].dst.path));
328 tmp_uris[0].dst.path[sizeof(tmp_uris[0].dst.path) - 1] = '\0';
329 DBG3("URI file destination: %s", purl);
330 goto end;
331 }
332
333 /* Assume we are at the beginning of an address or host of some sort. */
334 addr_b = purl;
335
336 /*
337 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
338 * address that does not start AND end with brackets will be rejected even
339 * if valid.
340 *
341 * proto://[<addr>]...
342 * ^
343 */
344 if (*purl == '[') {
345 /* Address begins after '[' */
346 addr_b = purl + 1;
347 addr_e = strchr(addr_b, ']');
348 if (addr_e == NULL || addr_b == addr_e) {
349 ERR("Broken IPv6 address %s", addr_b);
350 goto free_error;
351 }
352
353 /* Moving parsed URL pointer after the final bracket ']' */
354 purl = addr_e + 1;
355
356 /*
357 * The closing bracket must be followed by a seperator or NULL char.
358 */
359 if (strchr(seps, *purl) == NULL) {
360 ERR("Unknown symbol after IPv6 address: %s", purl);
361 goto free_error;
362 }
363 } else {
364 purl = strpbrk_or_eos(purl, seps);
365 addr_e = purl;
366 }
367
368 /* Check if we at least have a char for the addr or hostname. */
369 if (addr_b == addr_e) {
370 ERR("No address or hostname detected.");
371 goto free_error;
372 }
373
374 addr_f = utils_strdupdelim(addr_b, addr_e);
375 if (addr_f == NULL) {
376 goto free_error;
377 }
378
379 /*
380 * Detect PORT after address. The net/net6 protocol allows up to two port
381 * so we can define the control and data port.
382 */
383 while (*purl == ':') {
384 const char *port_b, *port_e;
385 char *port_f;
386
387 /* Update pass counter */
388 i++;
389
390 /*
391 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
392 * two much stuff.
393 */
394 if ((i == 2 && (proto->code != P_NET && proto->code != P_NET6))
395 || i > 2) {
396 break;
397 }
398
399 /*
400 * Move parsed URL to port value.
401 * proto://addr_host:PORT1:PORT2/foo/bar
402 * ^
403 */
404 ++purl;
405 port_b = purl;
406 purl = strpbrk_or_eos(purl, seps);
407 port_e = purl;
408
409 if (port_b != port_e) {
410 int port;
411
412 port_f = utils_strdupdelim(port_b, port_e);
413 if (port_f == NULL) {
414 goto free_error;
415 }
416
417 port = atoi(port_f);
418 if (port > 0xffff || port <= 0x0) {
419 ERR("Invalid port number %d", port);
420 free(port_f);
421 goto free_error;
422 }
423 free(port_f);
424
425 if (i == 1) {
426 ctrl_port = port;
427 } else {
428 data_port = port;
429 }
430 }
431 };
432
433 /* Check for a valid subdir or trailing garbage */
434 if (*purl == '/') {
435 /*
436 * Move to subdir value.
437 * proto://addr_host:PORT1:PORT2/foo/bar
438 * ^
439 */
440 ++purl;
441 subdir_b = purl;
442 } else if (*purl != '\0') {
443 ERR("Trailing characters not recognized: %s", purl);
444 goto free_error;
445 }
446
447 /* We have enough valid information to create URI(s) object */
448
449 /* Copy generic information */
450 tmp_uris[0].port = ctrl_port;
451
452 /* Copy subdirectory if one. */
453 if (subdir_b) {
454 strncpy(tmp_uris[0].subdir, subdir_b, sizeof(tmp_uris[0].subdir));
455 tmp_uris[0].subdir[sizeof(tmp_uris[0].subdir) - 1] = '\0';
456 }
457
458 switch (proto->code) {
459 case P_NET:
460 ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
461 sizeof(tmp_uris[0].dst.ipv4));
462 if (ret < 0) {
463 goto free_error;
464 }
465
466 memcpy(tmp_uris[1].dst.ipv4, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[1].dst.ipv4));
467
468 tmp_uris[1].dtype = proto->dtype;
469 tmp_uris[1].proto = proto->type;
470 tmp_uris[1].port = data_port;
471 break;
472 case P_NET6:
473 ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
474 sizeof(tmp_uris[0].dst.ipv6));
475 if (ret < 0) {
476 goto free_error;
477 }
478
479 memcpy(tmp_uris[1].dst.ipv6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[1].dst.ipv6));
480
481 tmp_uris[1].dtype = proto->dtype;
482 tmp_uris[1].proto = proto->type;
483 tmp_uris[1].port = data_port;
484 break;
485 case P_TCP:
486 ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
487 sizeof(tmp_uris[0].dst.ipv4));
488 if (ret < 0) {
489 goto free_error;
490 }
491 break;
492 case P_TCP6:
493 ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
494 sizeof(tmp_uris[0].dst.ipv6));
495 if (ret < 0) {
496 goto free_error;
497 }
498 break;
499 default:
500 goto free_error;
501 }
502
503 end:
504 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
505 proto->dtype, proto->type, (addr_f == NULL) ? "" : addr_f,
506 (subdir_b == NULL) ? "" : subdir_b, ctrl_port, data_port);
507
508 free(addr_f);
509
510 *uris = tmp_uris;
511 return size;
512
513 free_error:
514 free(addr_f);
515 free(tmp_uris);
516 error:
517 return -1;
518 }
519
520 /*
521 * Parse a string URL and creates URI(s) returning the size of the populated
522 * array.
523 */
524 LTTNG_HIDDEN
525 ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
526 struct lttng_uri **uris)
527 {
528 unsigned int equal = 1, idx = 0;
529 /* Add the "file://" size to the URL maximum size */
530 char url[PATH_MAX + 7];
531 ssize_t size_ctrl = 0, size_data = 0, size;
532 struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL;
533 struct lttng_uri *tmp_uris = NULL;
534
535 /* No URL(s) is allowed. This means that the consumer will be disabled. */
536 if (ctrl_url == NULL && data_url == NULL) {
537 return 0;
538 }
539
540 /* Check if URLs are equal and if so, only use the control URL */
541 if ((ctrl_url && *ctrl_url != '\0') && (data_url && *data_url != '\0')) {
542 equal = !strcmp(ctrl_url, data_url);
543 }
544
545 /*
546 * Since we allow the str_url to be a full local filesystem path, we are
547 * going to create a valid file:// URL if it's the case.
548 *
549 * Check if first character is a '/' or else reject the URL.
550 */
551 if (ctrl_url && ctrl_url[0] == '/') {
552 int ret;
553
554 ret = snprintf(url, sizeof(url), "file://%s", ctrl_url);
555 if (ret < 0) {
556 PERROR("snprintf file url");
557 goto parse_error;
558 }
559 ctrl_url = url;
560 }
561
562 /* Parse the control URL if there is one */
563 if (ctrl_url && *ctrl_url != '\0') {
564 size_ctrl = uri_parse(ctrl_url, &ctrl_uris);
565 if (size_ctrl < 1) {
566 ERR("Unable to parse the URL %s", ctrl_url);
567 goto parse_error;
568 }
569
570 /* At this point, we know there is at least one URI in the array */
571 set_default_uri_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL);
572
573 if (ctrl_uris[0].dtype == LTTNG_DST_PATH &&
574 (data_url && *data_url != '\0')) {
575 ERR("Can not have a data URL when destination is file://");
576 goto error;
577 }
578
579 /* URL are not equal but the control URL uses a net:// protocol */
580 if (size_ctrl == 2) {
581 if (!equal) {
582 ERR("Control URL uses the net:// protocol and the data URL is "
583 "different. Not allowed.");
584 goto error;
585 } else {
586 set_default_uri_attr(&ctrl_uris[1], LTTNG_STREAM_DATA);
587 /*
588 * The data_url and ctrl_url are equal and the ctrl_url
589 * contains a net:// protocol so we just skip the data part.
590 */
591 data_url = NULL;
592 }
593 }
594 }
595
596 if (data_url && *data_url != '\0') {
597 int ret;
598
599 /* We have to parse the data URL in this case */
600 size_data = uri_parse(data_url, &data_uris);
601 if (size_data < 1) {
602 ERR("Unable to parse the URL %s", data_url);
603 goto error;
604 } else if (size_data == 2) {
605 ERR("Data URL can not be set with the net[4|6]:// protocol");
606 goto error;
607 }
608
609 set_default_uri_attr(&data_uris[0], LTTNG_STREAM_DATA);
610
611 ret = compare_destination(&ctrl_uris[0], &data_uris[0]);
612 if (ret != 0) {
613 ERR("Control and data destination mismatch");
614 goto error;
615 }
616 }
617
618 /* Compute total size */
619 size = size_ctrl + size_data;
620
621 tmp_uris = zmalloc(sizeof(struct lttng_uri) * size);
622 if (tmp_uris == NULL) {
623 PERROR("zmalloc uris");
624 goto error;
625 }
626
627 if (ctrl_uris) {
628 /* It's possible the control URIs array contains more than one URI */
629 memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * size_ctrl);
630 ++idx;
631 free(ctrl_uris);
632 }
633
634 if (data_uris) {
635 memcpy(&tmp_uris[idx], data_uris, sizeof(struct lttng_uri));
636 free(data_uris);
637 }
638
639 *uris = tmp_uris;
640
641 return size;
642
643 error:
644 free(ctrl_uris);
645 free(data_uris);
646 free(tmp_uris);
647 parse_error:
648 return -1;
649 }
This page took 0.077524 seconds and 3 git commands to generate.