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