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