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