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