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