Cleanup: consumerd: lines over 80 columns
[lttng-tools.git] / src / common / uri.c
CommitLineData
3a5713da
DG
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
6c1c0768 19#define _LGPL_SOURCE
13083fa6 20#include <assert.h>
3a5713da
DG
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>
a4b92340 29#include <common/utils.h>
3a5713da
DG
30
31#include "uri.h"
32
33enum uri_proto_code {
34 P_NET, P_NET6, P_FILE, P_TCP, P_TCP6,
35};
36
37struct uri_proto {
a4b92340
DG
38 const char *name;
39 const char *leading_string;
3a5713da
DG
40 enum uri_proto_code code;
41 enum lttng_proto_type type;
42 enum lttng_dst_type dtype;
43};
44
45/* Supported protocols */
46static const struct uri_proto proto_uri[] = {
a4b92340
DG
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 },
60782118 49 { .name = "net4", .leading_string = "net4://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
a4b92340
DG
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 },
60782118 52 { .name = "tcp4", .leading_string = "tcp4://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
a4b92340
DG
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 }
3a5713da
DG
56};
57
a4b92340
DG
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 */
32dd26fb 62static const inline char *strpbrk_or_eos(const char *s, const char *accept)
a4b92340
DG
63{
64 char *p = strpbrk(s, accept);
65 if (p == NULL) {
66 p = strchr(s, '\0');
67 }
68
69 return p;
70}
71
72
3a5713da
DG
73/*
74 * Validate if proto is a supported protocol from proto_uri array.
75 */
a4b92340 76static const struct uri_proto *get_uri_proto(const char *uri_str)
3a5713da 77{
a4b92340 78 const struct uri_proto *supported = NULL;
3a5713da
DG
79
80 /* Safety net */
a4b92340 81 if (uri_str == NULL) {
3a5713da
DG
82 goto end;
83 }
84
85 for (supported = &proto_uri[0];
a4b92340
DG
86 supported->leading_string != NULL; ++supported) {
87 if (strncasecmp(uri_str, supported->leading_string,
88 strlen(supported->leading_string)) == 0) {
3a5713da
DG
89 goto end;
90 }
91 }
92
93 /* Proto not found */
94 return NULL;
95
96end:
97 return supported;
98}
99
00e2e675
DG
100/*
101 * Set network address from string into dst. Supports both IP string and
102 * hostname.
103 */
104static 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
13083fa6
DG
110 assert(addr);
111 assert(dst);
112
113 memset(dst, 0, size);
114
00e2e675
DG
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 */
b3e12287 122 ERR("URI parse bad hostname %s for af %d", addr, af);
00e2e675
DG
123 goto error;
124 }
125
126 /* Translate IP to string */
127 (void) inet_ntop(af, record->h_addr_list[0], dst, size);
128 } else {
13083fa6
DG
129 if (size > 0) {
130 strncpy(dst, addr, size);
131 dst[size - 1] = '\0';
132 }
00e2e675
DG
133 }
134
a4b92340
DG
135 DBG2("IP address resolved to %s", dst);
136
00e2e675
DG
137 return 0;
138
139error:
140 return -1;
141}
142
bc894455
DG
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 */
147static 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 */
162static 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
ad20f474
DG
184/*
185 * Build a string URL from a lttng_uri object.
186 */
90e535ef 187LTTNG_HIDDEN
ad20f474
DG
188int uri_to_str_url(struct lttng_uri *uri, char *dst, size_t size)
189{
190 int ipver, ret;
191 const char *addr;
07b86b52 192 char proto[5], port[7];
ad20f474
DG
193
194 assert(uri);
195 assert(dst);
196
197 if (uri->dtype == LTTNG_DST_PATH) {
198 ipver = 0;
199 addr = uri->dst.path;
28fcbaeb
JD
200 (void) snprintf(proto, sizeof(proto), "file");
201 (void) snprintf(port, sizeof(port), "%s", "");
ad20f474
DG
202 } else {
203 ipver = (uri->dtype == LTTNG_DST_IPV4) ? 4 : 6;
204 addr = (ipver == 4) ? uri->dst.ipv4 : uri->dst.ipv6;
b664f89a 205 (void) snprintf(proto, sizeof(proto), "tcp%d", ipver);
28fcbaeb 206 (void) snprintf(port, sizeof(port), ":%d", uri->port);
ad20f474
DG
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
3a5713da
DG
219/*
220 * Compare two URIs.
221 *
222 * Return 0 if equal else 1.
223 */
90e535ef 224LTTNG_HIDDEN
3a5713da
DG
225int 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 */
90e535ef 233LTTNG_HIDDEN
3a5713da
DG
234void uri_free(struct lttng_uri *uri)
235{
0e428499 236 free(uri);
3a5713da
DG
237}
238
239/*
240 * Return an allocated URI.
241 */
90e535ef 242LTTNG_HIDDEN
3a5713da
DG
243struct 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
00e2e675
DG
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.
a4b92340
DG
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.
00e2e675 270 */
90e535ef 271LTTNG_HIDDEN
3a5713da
DG
272ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
273{
a4b92340 274 int ret, i = 0;
3a5713da
DG
275 /* Size of the uris array. Default is 1 */
276 ssize_t size = 1;
a4b92340 277 char subdir[PATH_MAX];
b35d8a57
DG
278 unsigned int ctrl_port = 0;
279 unsigned int data_port = 0;
a4b92340
DG
280 struct lttng_uri *tmp_uris;
281 char *addr_f = NULL;
3a5713da 282 const struct uri_proto *proto;
a4b92340
DG
283 const char *purl, *addr_e, *addr_b, *subdir_b = NULL;
284 const char *seps = ":/\0";
3a5713da
DG
285
286 /*
287 * The first part is the protocol portion of a maximum of 5 bytes for now.
b35d8a57
DG
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.
3a5713da
DG
292 */
293
00e2e675 294 DBG3("URI string: %s", str_uri);
3a5713da 295
a4b92340 296 proto = get_uri_proto(str_uri);
3a5713da 297 if (proto == NULL) {
a4b92340 298 ERR("URI parse unknown protocol %s", str_uri);
3a5713da
DG
299 goto error;
300 }
301
a4b92340
DG
302 purl = str_uri;
303
3a5713da 304 if (proto->code == P_NET || proto->code == P_NET6) {
a4b92340 305 /* Special case for net:// which requires two URI objects */
3a5713da
DG
306 size = 2;
307 }
308
a4b92340
DG
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
00e2e675 316 memset(subdir, 0, sizeof(subdir));
a4b92340
DG
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;
00e2e675 327 }
a4b92340
DG
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;
3a5713da
DG
333 }
334
a4b92340
DG
335 /* Assume we are at the beginning of an address or host of some sort. */
336 addr_b = purl;
3a5713da 337
a4b92340
DG
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;
3a5713da
DG
379 }
380
a4b92340
DG
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 == ':') {
a4b92340
DG
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) {
c617c0c6
MD
412 int port;
413
a4b92340
DG
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
3a5713da 451 /* Copy generic information */
a4b92340 452 tmp_uris[0].port = ctrl_port;
3a5713da 453
a4b92340
DG
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 }
3a5713da
DG
459
460 switch (proto->code) {
3a5713da 461 case P_NET:
a4b92340
DG
462 ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
463 sizeof(tmp_uris[0].dst.ipv4));
3a5713da
DG
464 if (ret < 0) {
465 goto free_error;
466 }
467
a4b92340 468 memcpy(tmp_uris[1].dst.ipv4, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[1].dst.ipv4));
3a5713da 469
a4b92340
DG
470 tmp_uris[1].dtype = proto->dtype;
471 tmp_uris[1].proto = proto->type;
472 tmp_uris[1].port = data_port;
3a5713da
DG
473 break;
474 case P_NET6:
a4b92340
DG
475 ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
476 sizeof(tmp_uris[0].dst.ipv6));
3a5713da
DG
477 if (ret < 0) {
478 goto free_error;
479 }
480
a4b92340 481 memcpy(tmp_uris[1].dst.ipv6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[1].dst.ipv6));
3a5713da 482
a4b92340
DG
483 tmp_uris[1].dtype = proto->dtype;
484 tmp_uris[1].proto = proto->type;
485 tmp_uris[1].port = data_port;
3a5713da
DG
486 break;
487 case P_TCP:
a4b92340
DG
488 ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
489 sizeof(tmp_uris[0].dst.ipv4));
3a5713da
DG
490 if (ret < 0) {
491 goto free_error;
492 }
493 break;
494 case P_TCP6:
a4b92340
DG
495 ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
496 sizeof(tmp_uris[0].dst.ipv6));
3a5713da
DG
497 if (ret < 0) {
498 goto free_error;
499 }
500 break;
501 default:
502 goto free_error;
503 }
504
a4b92340
DG
505end:
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);
3a5713da 511
a4b92340 512 *uris = tmp_uris;
3a5713da
DG
513 return size;
514
515free_error:
a4b92340
DG
516 free(addr_f);
517 free(tmp_uris);
3a5713da
DG
518error:
519 return -1;
520}
bc894455
DG
521
522/*
523 * Parse a string URL and creates URI(s) returning the size of the populated
524 * array.
525 */
526LTTNG_HIDDEN
527ssize_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
645error:
646 free(ctrl_uris);
647 free(data_uris);
648 free(tmp_uris);
649parse_error:
650 return -1;
651}
This page took 0.059959 seconds and 4 git commands to generate.