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