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 | ||
32 | enum uri_proto_code { | |
33 | P_NET, P_NET6, P_FILE, P_TCP, P_TCP6, | |
34 | }; | |
35 | ||
36 | struct uri_proto { | |
a4b92340 DG |
37 | const char *name; |
38 | const char *leading_string; | |
3a5713da DG |
39 | enum uri_proto_code code; |
40 | enum lttng_proto_type type; | |
41 | enum lttng_dst_type dtype; | |
42 | }; | |
43 | ||
44 | /* Supported protocols */ | |
45 | static const struct uri_proto proto_uri[] = { | |
a4b92340 DG |
46 | { .name = "file", .leading_string = "file://", .code = P_FILE, .type = 0, .dtype = LTTNG_DST_PATH }, |
47 | { .name = "net", .leading_string = "net://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 }, | |
48 | { .name = "net6", .leading_string = "net6://", .code = P_NET6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 }, | |
49 | { .name = "tcp", .leading_string = "tcp://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 }, | |
50 | { .name = "tcp6", .leading_string = "tcp6://", .code = P_TCP6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 }, | |
51 | /* Invalid proto marking the end of the array. */ | |
52 | { NULL, NULL, 0, 0, 0 } | |
3a5713da DG |
53 | }; |
54 | ||
a4b92340 DG |
55 | /* |
56 | * Return pointer to the character in s matching one of the characters in | |
57 | * accept. If nothing is found, return pointer to the end of string (eos). | |
58 | */ | |
59 | const inline char *strpbrk_or_eos(const char *s, const char *accept) | |
60 | { | |
61 | char *p = strpbrk(s, accept); | |
62 | if (p == NULL) { | |
63 | p = strchr(s, '\0'); | |
64 | } | |
65 | ||
66 | return p; | |
67 | } | |
68 | ||
69 | ||
3a5713da DG |
70 | /* |
71 | * Validate if proto is a supported protocol from proto_uri array. | |
72 | */ | |
a4b92340 | 73 | static const struct uri_proto *get_uri_proto(const char *uri_str) |
3a5713da | 74 | { |
a4b92340 | 75 | const struct uri_proto *supported = NULL; |
3a5713da DG |
76 | |
77 | /* Safety net */ | |
a4b92340 | 78 | if (uri_str == NULL) { |
3a5713da DG |
79 | goto end; |
80 | } | |
81 | ||
82 | for (supported = &proto_uri[0]; | |
a4b92340 DG |
83 | supported->leading_string != NULL; ++supported) { |
84 | if (strncasecmp(uri_str, supported->leading_string, | |
85 | strlen(supported->leading_string)) == 0) { | |
3a5713da DG |
86 | goto end; |
87 | } | |
88 | } | |
89 | ||
90 | /* Proto not found */ | |
91 | return NULL; | |
92 | ||
93 | end: | |
94 | return supported; | |
95 | } | |
96 | ||
00e2e675 DG |
97 | /* |
98 | * Set network address from string into dst. Supports both IP string and | |
99 | * hostname. | |
100 | */ | |
101 | static int set_ip_address(const char *addr, int af, char *dst, size_t size) | |
102 | { | |
103 | int ret; | |
104 | unsigned char buf[sizeof(struct in6_addr)]; | |
105 | struct hostent *record; | |
106 | ||
13083fa6 DG |
107 | assert(addr); |
108 | assert(dst); | |
109 | ||
110 | memset(dst, 0, size); | |
111 | ||
00e2e675 DG |
112 | /* Network protocol */ |
113 | ret = inet_pton(af, addr, buf); | |
114 | if (ret < 1) { | |
115 | /* We consider the dst to be an hostname or an invalid IP char */ | |
116 | record = gethostbyname2(addr, af); | |
117 | if (record == NULL) { | |
118 | /* At this point, the IP or the hostname is bad */ | |
b3e12287 | 119 | ERR("URI parse bad hostname %s for af %d", addr, af); |
00e2e675 DG |
120 | goto error; |
121 | } | |
122 | ||
123 | /* Translate IP to string */ | |
124 | (void) inet_ntop(af, record->h_addr_list[0], dst, size); | |
125 | } else { | |
13083fa6 DG |
126 | if (size > 0) { |
127 | strncpy(dst, addr, size); | |
128 | dst[size - 1] = '\0'; | |
129 | } | |
00e2e675 DG |
130 | } |
131 | ||
a4b92340 DG |
132 | DBG2("IP address resolved to %s", dst); |
133 | ||
00e2e675 DG |
134 | return 0; |
135 | ||
136 | error: | |
137 | return -1; | |
138 | } | |
139 | ||
ad20f474 DG |
140 | /* |
141 | * Build a string URL from a lttng_uri object. | |
142 | */ | |
143 | int uri_to_str_url(struct lttng_uri *uri, char *dst, size_t size) | |
144 | { | |
145 | int ipver, ret; | |
146 | const char *addr; | |
147 | char proto[4], port[7]; | |
148 | ||
149 | assert(uri); | |
150 | assert(dst); | |
151 | ||
152 | if (uri->dtype == LTTNG_DST_PATH) { | |
153 | ipver = 0; | |
154 | addr = uri->dst.path; | |
155 | (void) snprintf(proto, sizeof(proto), "file"); | |
156 | (void) snprintf(port, sizeof(port), "%s", ""); | |
157 | } else { | |
158 | ipver = (uri->dtype == LTTNG_DST_IPV4) ? 4 : 6; | |
159 | addr = (ipver == 4) ? uri->dst.ipv4 : uri->dst.ipv6; | |
160 | (void) snprintf(proto, sizeof(proto), "net%d", ipver); | |
161 | (void) snprintf(port, sizeof(port), ":%d", uri->port); | |
162 | } | |
163 | ||
164 | ret = snprintf(dst, size, "%s://%s%s%s%s/%s", proto, | |
165 | (ipver == 6) ? "[" : "", addr, (ipver == 6) ? "]" : "", | |
166 | port, uri->subdir); | |
167 | if (ret < 0) { | |
168 | PERROR("snprintf uri to url"); | |
169 | } | |
170 | ||
171 | return ret; | |
172 | } | |
173 | ||
3a5713da DG |
174 | /* |
175 | * Compare two URIs. | |
176 | * | |
177 | * Return 0 if equal else 1. | |
178 | */ | |
179 | int uri_compare(struct lttng_uri *uri1, struct lttng_uri *uri2) | |
180 | { | |
181 | return memcmp(uri1, uri2, sizeof(struct lttng_uri)); | |
182 | } | |
183 | ||
184 | /* | |
185 | * Free URI memory. | |
186 | */ | |
187 | void uri_free(struct lttng_uri *uri) | |
188 | { | |
189 | /* Safety check */ | |
190 | if (uri != NULL) { | |
191 | free(uri); | |
192 | } | |
193 | } | |
194 | ||
195 | /* | |
196 | * Return an allocated URI. | |
197 | */ | |
198 | struct lttng_uri *uri_create(void) | |
199 | { | |
200 | struct lttng_uri *uri; | |
201 | ||
202 | uri = zmalloc(sizeof(struct lttng_uri)); | |
203 | if (uri == NULL) { | |
204 | PERROR("zmalloc uri"); | |
205 | } | |
206 | ||
207 | return uri; | |
208 | } | |
209 | ||
00e2e675 DG |
210 | /* |
211 | * Parses a string URI to a lttng_uri. This function can potentially return | |
212 | * more than one URI in uris so the size of the array is returned and uris is | |
213 | * allocated and populated. Caller must free(3) the array. | |
214 | * | |
215 | * This function can not detect the stream type of the URI so the caller has to | |
216 | * make sure the correct type (stype) is set on the return URI(s). The default | |
217 | * port must also be set by the caller if the returned URI has its port set to | |
218 | * zero. | |
a4b92340 DG |
219 | * |
220 | * NOTE: A good part of the following code was inspired from the "wget" source | |
221 | * tree from the src/url.c file and url_parse() function. Also, the | |
222 | * strpbrk_or_eos() function found above is also inspired by the same code. | |
223 | * This code was originally licensed GPLv2 so we acknolwedge the Free Software | |
224 | * Foundation here for the work and to make sure we are compliant with it. | |
00e2e675 | 225 | */ |
3a5713da DG |
226 | ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris) |
227 | { | |
a4b92340 | 228 | int ret, i = 0; |
3a5713da DG |
229 | /* Size of the uris array. Default is 1 */ |
230 | ssize_t size = 1; | |
a4b92340 | 231 | char subdir[PATH_MAX]; |
b35d8a57 DG |
232 | unsigned int ctrl_port = 0; |
233 | unsigned int data_port = 0; | |
a4b92340 DG |
234 | struct lttng_uri *tmp_uris; |
235 | char *addr_f = NULL; | |
3a5713da | 236 | const struct uri_proto *proto; |
a4b92340 DG |
237 | const char *purl, *addr_e, *addr_b, *subdir_b = NULL; |
238 | const char *seps = ":/\0"; | |
3a5713da DG |
239 | |
240 | /* | |
241 | * The first part is the protocol portion of a maximum of 5 bytes for now. | |
b35d8a57 DG |
242 | * The second part is the hostname or IP address. The 255 bytes size is the |
243 | * limit found in the RFC 1035 for the total length of a domain name | |
244 | * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net:// | |
245 | * protocol, two ports CAN be specified. | |
3a5713da DG |
246 | */ |
247 | ||
00e2e675 | 248 | DBG3("URI string: %s", str_uri); |
3a5713da | 249 | |
a4b92340 | 250 | proto = get_uri_proto(str_uri); |
3a5713da | 251 | if (proto == NULL) { |
a4b92340 | 252 | ERR("URI parse unknown protocol %s", str_uri); |
3a5713da DG |
253 | goto error; |
254 | } | |
255 | ||
a4b92340 DG |
256 | purl = str_uri; |
257 | ||
3a5713da | 258 | if (proto->code == P_NET || proto->code == P_NET6) { |
a4b92340 | 259 | /* Special case for net:// which requires two URI objects */ |
3a5713da DG |
260 | size = 2; |
261 | } | |
262 | ||
a4b92340 DG |
263 | /* Allocate URI array */ |
264 | tmp_uris = zmalloc(sizeof(struct lttng_uri) * size); | |
265 | if (tmp_uris == NULL) { | |
266 | PERROR("zmalloc uri"); | |
267 | goto error; | |
268 | } | |
269 | ||
00e2e675 | 270 | memset(subdir, 0, sizeof(subdir)); |
a4b92340 DG |
271 | purl += strlen(proto->leading_string); |
272 | ||
273 | /* Copy known value to the first URI. */ | |
274 | tmp_uris[0].dtype = proto->dtype; | |
275 | tmp_uris[0].proto = proto->type; | |
276 | ||
277 | if (proto->code == P_FILE) { | |
278 | if (*purl != '/') { | |
279 | ERR("Missing destination full path."); | |
280 | goto free_error; | |
00e2e675 | 281 | } |
a4b92340 DG |
282 | |
283 | strncpy(tmp_uris[0].dst.path, purl, sizeof(tmp_uris[0].dst.path)); | |
284 | tmp_uris[0].dst.path[sizeof(tmp_uris[0].dst.path) - 1] = '\0'; | |
285 | DBG3("URI file destination: %s", purl); | |
286 | goto end; | |
3a5713da DG |
287 | } |
288 | ||
a4b92340 DG |
289 | /* Assume we are at the beginning of an address or host of some sort. */ |
290 | addr_b = purl; | |
3a5713da | 291 | |
a4b92340 DG |
292 | /* |
293 | * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6 | |
294 | * address that does not start AND end with brackets will be rejected even | |
295 | * if valid. | |
296 | * | |
297 | * proto://[<addr>]... | |
298 | * ^ | |
299 | */ | |
300 | if (*purl == '[') { | |
301 | /* Address begins after '[' */ | |
302 | addr_b = purl + 1; | |
303 | addr_e = strchr(addr_b, ']'); | |
304 | if (addr_e == NULL || addr_b == addr_e) { | |
305 | ERR("Broken IPv6 address %s", addr_b); | |
306 | goto free_error; | |
307 | } | |
308 | ||
309 | /* Moving parsed URL pointer after the final bracket ']' */ | |
310 | purl = addr_e + 1; | |
311 | ||
312 | /* | |
313 | * The closing bracket must be followed by a seperator or NULL char. | |
314 | */ | |
315 | if (strchr(seps, *purl) == NULL) { | |
316 | ERR("Unknown symbol after IPv6 address: %s", purl); | |
317 | goto free_error; | |
318 | } | |
319 | } else { | |
320 | purl = strpbrk_or_eos(purl, seps); | |
321 | addr_e = purl; | |
322 | } | |
323 | ||
324 | /* Check if we at least have a char for the addr or hostname. */ | |
325 | if (addr_b == addr_e) { | |
326 | ERR("No address or hostname detected."); | |
327 | goto free_error; | |
328 | } | |
329 | ||
330 | addr_f = utils_strdupdelim(addr_b, addr_e); | |
331 | if (addr_f == NULL) { | |
332 | goto free_error; | |
3a5713da DG |
333 | } |
334 | ||
a4b92340 DG |
335 | /* |
336 | * Detect PORT after address. The net/net6 protocol allows up to two port | |
337 | * so we can define the control and data port. | |
338 | */ | |
339 | while (*purl == ':') { | |
340 | int port; | |
341 | const char *port_b, *port_e; | |
342 | char *port_f; | |
343 | ||
344 | /* Update pass counter */ | |
345 | i++; | |
346 | ||
347 | /* | |
348 | * Maximum of two ports is possible if P_NET/NET6. Bigger than that, | |
349 | * two much stuff. | |
350 | */ | |
351 | if ((i == 2 && (proto->code != P_NET && proto->code != P_NET6)) | |
352 | || i > 2) { | |
353 | break; | |
354 | } | |
355 | ||
356 | /* | |
357 | * Move parsed URL to port value. | |
358 | * proto://addr_host:PORT1:PORT2/foo/bar | |
359 | * ^ | |
360 | */ | |
361 | ++purl; | |
362 | port_b = purl; | |
363 | purl = strpbrk_or_eos(purl, seps); | |
364 | port_e = purl; | |
365 | ||
366 | if (port_b != port_e) { | |
367 | port_f = utils_strdupdelim(port_b, port_e); | |
368 | if (port_f == NULL) { | |
369 | goto free_error; | |
370 | } | |
371 | ||
372 | port = atoi(port_f); | |
373 | if (port > 0xffff || port <= 0x0) { | |
374 | ERR("Invalid port number %d", port); | |
375 | free(port_f); | |
376 | goto free_error; | |
377 | } | |
378 | free(port_f); | |
379 | ||
380 | if (i == 1) { | |
381 | ctrl_port = port; | |
382 | } else { | |
383 | data_port = port; | |
384 | } | |
385 | } | |
386 | }; | |
387 | ||
388 | /* Check for a valid subdir or trailing garbage */ | |
389 | if (*purl == '/') { | |
390 | /* | |
391 | * Move to subdir value. | |
392 | * proto://addr_host:PORT1:PORT2/foo/bar | |
393 | * ^ | |
394 | */ | |
395 | ++purl; | |
396 | subdir_b = purl; | |
397 | } else if (*purl != '\0') { | |
398 | ERR("Trailing characters not recognized: %s", purl); | |
399 | goto free_error; | |
400 | } | |
401 | ||
402 | /* We have enough valid information to create URI(s) object */ | |
403 | ||
3a5713da | 404 | /* Copy generic information */ |
a4b92340 | 405 | tmp_uris[0].port = ctrl_port; |
3a5713da | 406 | |
a4b92340 DG |
407 | /* Copy subdirectory if one. */ |
408 | if (subdir_b) { | |
409 | strncpy(tmp_uris[0].subdir, subdir_b, sizeof(tmp_uris[0].subdir)); | |
410 | tmp_uris[0].subdir[sizeof(tmp_uris[0].subdir) - 1] = '\0'; | |
411 | } | |
3a5713da DG |
412 | |
413 | switch (proto->code) { | |
3a5713da | 414 | case P_NET: |
a4b92340 DG |
415 | ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4, |
416 | sizeof(tmp_uris[0].dst.ipv4)); | |
3a5713da DG |
417 | if (ret < 0) { |
418 | goto free_error; | |
419 | } | |
420 | ||
a4b92340 | 421 | memcpy(tmp_uris[1].dst.ipv4, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[1].dst.ipv4)); |
3a5713da | 422 | |
a4b92340 DG |
423 | tmp_uris[1].dtype = proto->dtype; |
424 | tmp_uris[1].proto = proto->type; | |
425 | tmp_uris[1].port = data_port; | |
3a5713da DG |
426 | break; |
427 | case P_NET6: | |
a4b92340 DG |
428 | ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6, |
429 | sizeof(tmp_uris[0].dst.ipv6)); | |
3a5713da DG |
430 | if (ret < 0) { |
431 | goto free_error; | |
432 | } | |
433 | ||
a4b92340 | 434 | memcpy(tmp_uris[1].dst.ipv6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[1].dst.ipv6)); |
3a5713da | 435 | |
a4b92340 DG |
436 | tmp_uris[1].dtype = proto->dtype; |
437 | tmp_uris[1].proto = proto->type; | |
438 | tmp_uris[1].port = data_port; | |
3a5713da DG |
439 | break; |
440 | case P_TCP: | |
a4b92340 DG |
441 | ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4, |
442 | sizeof(tmp_uris[0].dst.ipv4)); | |
3a5713da DG |
443 | if (ret < 0) { |
444 | goto free_error; | |
445 | } | |
446 | break; | |
447 | case P_TCP6: | |
a4b92340 DG |
448 | ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6, |
449 | sizeof(tmp_uris[0].dst.ipv6)); | |
3a5713da DG |
450 | if (ret < 0) { |
451 | goto free_error; | |
452 | } | |
453 | break; | |
454 | default: | |
455 | goto free_error; | |
456 | } | |
457 | ||
a4b92340 DG |
458 | end: |
459 | DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d", | |
460 | proto->dtype, proto->type, (addr_f == NULL) ? "" : addr_f, | |
461 | (subdir_b == NULL) ? "" : subdir_b, ctrl_port, data_port); | |
462 | ||
463 | free(addr_f); | |
3a5713da | 464 | |
a4b92340 | 465 | *uris = tmp_uris; |
3a5713da DG |
466 | return size; |
467 | ||
468 | free_error: | |
a4b92340 DG |
469 | free(addr_f); |
470 | free(tmp_uris); | |
3a5713da DG |
471 | error: |
472 | return -1; | |
473 | } |