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