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