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