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