2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
20 #include <arpa/inet.h>
24 #include <sys/socket.h>
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/utils.h>
33 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
38 const char *leading_string
;
39 enum uri_proto_code code
;
40 enum lttng_proto_type type
;
41 enum lttng_dst_type dtype
;
44 /* Supported protocols */
45 static const struct uri_proto proto_uri
[] = {
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 }
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).
59 static const inline char *strpbrk_or_eos(const char *s
, const char *accept
)
61 char *p
= strpbrk(s
, accept
);
71 * Validate if proto is a supported protocol from proto_uri array.
73 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
75 const struct uri_proto
*supported
= NULL
;
78 if (uri_str
== NULL
) {
82 for (supported
= &proto_uri
[0];
83 supported
->leading_string
!= NULL
; ++supported
) {
84 if (strncasecmp(uri_str
, supported
->leading_string
,
85 strlen(supported
->leading_string
)) == 0) {
98 * Set network address from string into dst. Supports both IP string and
101 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
104 unsigned char buf
[sizeof(struct in6_addr
)];
105 struct hostent
*record
;
110 memset(dst
, 0, size
);
112 /* Network protocol */
113 ret
= inet_pton(af
, addr
, buf
);
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 */
119 ERR("URI parse bad hostname %s for af %d", addr
, af
);
123 /* Translate IP to string */
124 (void) inet_ntop(af
, record
->h_addr_list
[0], dst
, size
);
127 strncpy(dst
, addr
, size
);
128 dst
[size
- 1] = '\0';
132 DBG2("IP address resolved to %s", dst
);
141 * Build a string URL from a lttng_uri object.
144 int uri_to_str_url(struct lttng_uri
*uri
, char *dst
, size_t size
)
148 char proto
[4], port
[7];
153 if (uri
->dtype
== LTTNG_DST_PATH
) {
155 addr
= uri
->dst
.path
;
156 (void) snprintf(proto
, sizeof(proto
), "file");
157 (void) snprintf(port
, sizeof(port
), "%s", "");
159 ipver
= (uri
->dtype
== LTTNG_DST_IPV4
) ? 4 : 6;
160 addr
= (ipver
== 4) ? uri
->dst
.ipv4
: uri
->dst
.ipv6
;
161 (void) snprintf(proto
, sizeof(proto
), "net%d", ipver
);
162 (void) snprintf(port
, sizeof(port
), ":%d", uri
->port
);
165 ret
= snprintf(dst
, size
, "%s://%s%s%s%s/%s", proto
,
166 (ipver
== 6) ? "[" : "", addr
, (ipver
== 6) ? "]" : "",
169 PERROR("snprintf uri to url");
178 * Return 0 if equal else 1.
181 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
183 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
190 void uri_free(struct lttng_uri
*uri
)
196 * Return an allocated URI.
199 struct lttng_uri
*uri_create(void)
201 struct lttng_uri
*uri
;
203 uri
= zmalloc(sizeof(struct lttng_uri
));
205 PERROR("zmalloc uri");
212 * Parses a string URI to a lttng_uri. This function can potentially return
213 * more than one URI in uris so the size of the array is returned and uris is
214 * allocated and populated. Caller must free(3) the array.
216 * This function can not detect the stream type of the URI so the caller has to
217 * make sure the correct type (stype) is set on the return URI(s). The default
218 * port must also be set by the caller if the returned URI has its port set to
221 * NOTE: A good part of the following code was inspired from the "wget" source
222 * tree from the src/url.c file and url_parse() function. Also, the
223 * strpbrk_or_eos() function found above is also inspired by the same code.
224 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
225 * Foundation here for the work and to make sure we are compliant with it.
228 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
231 /* Size of the uris array. Default is 1 */
233 char subdir
[PATH_MAX
];
234 unsigned int ctrl_port
= 0;
235 unsigned int data_port
= 0;
236 struct lttng_uri
*tmp_uris
;
238 const struct uri_proto
*proto
;
239 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
240 const char *seps
= ":/\0";
243 * The first part is the protocol portion of a maximum of 5 bytes for now.
244 * The second part is the hostname or IP address. The 255 bytes size is the
245 * limit found in the RFC 1035 for the total length of a domain name
246 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
247 * protocol, two ports CAN be specified.
250 DBG3("URI string: %s", str_uri
);
252 proto
= get_uri_proto(str_uri
);
254 ERR("URI parse unknown protocol %s", str_uri
);
260 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
261 /* Special case for net:// which requires two URI objects */
265 /* Allocate URI array */
266 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
267 if (tmp_uris
== NULL
) {
268 PERROR("zmalloc uri");
272 memset(subdir
, 0, sizeof(subdir
));
273 purl
+= strlen(proto
->leading_string
);
275 /* Copy known value to the first URI. */
276 tmp_uris
[0].dtype
= proto
->dtype
;
277 tmp_uris
[0].proto
= proto
->type
;
279 if (proto
->code
== P_FILE
) {
281 ERR("Missing destination full path.");
285 strncpy(tmp_uris
[0].dst
.path
, purl
, sizeof(tmp_uris
[0].dst
.path
));
286 tmp_uris
[0].dst
.path
[sizeof(tmp_uris
[0].dst
.path
) - 1] = '\0';
287 DBG3("URI file destination: %s", purl
);
291 /* Assume we are at the beginning of an address or host of some sort. */
295 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
296 * address that does not start AND end with brackets will be rejected even
299 * proto://[<addr>]...
303 /* Address begins after '[' */
305 addr_e
= strchr(addr_b
, ']');
306 if (addr_e
== NULL
|| addr_b
== addr_e
) {
307 ERR("Broken IPv6 address %s", addr_b
);
311 /* Moving parsed URL pointer after the final bracket ']' */
315 * The closing bracket must be followed by a seperator or NULL char.
317 if (strchr(seps
, *purl
) == NULL
) {
318 ERR("Unknown symbol after IPv6 address: %s", purl
);
322 purl
= strpbrk_or_eos(purl
, seps
);
326 /* Check if we at least have a char for the addr or hostname. */
327 if (addr_b
== addr_e
) {
328 ERR("No address or hostname detected.");
332 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
333 if (addr_f
== NULL
) {
338 * Detect PORT after address. The net/net6 protocol allows up to two port
339 * so we can define the control and data port.
341 while (*purl
== ':') {
342 const char *port_b
, *port_e
;
345 /* Update pass counter */
349 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
352 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
358 * Move parsed URL to port value.
359 * proto://addr_host:PORT1:PORT2/foo/bar
364 purl
= strpbrk_or_eos(purl
, seps
);
367 if (port_b
!= port_e
) {
370 port_f
= utils_strdupdelim(port_b
, port_e
);
371 if (port_f
== NULL
) {
376 if (port
> 0xffff || port
<= 0x0) {
377 ERR("Invalid port number %d", port
);
391 /* Check for a valid subdir or trailing garbage */
394 * Move to subdir value.
395 * proto://addr_host:PORT1:PORT2/foo/bar
400 } else if (*purl
!= '\0') {
401 ERR("Trailing characters not recognized: %s", purl
);
405 /* We have enough valid information to create URI(s) object */
407 /* Copy generic information */
408 tmp_uris
[0].port
= ctrl_port
;
410 /* Copy subdirectory if one. */
412 strncpy(tmp_uris
[0].subdir
, subdir_b
, sizeof(tmp_uris
[0].subdir
));
413 tmp_uris
[0].subdir
[sizeof(tmp_uris
[0].subdir
) - 1] = '\0';
416 switch (proto
->code
) {
418 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
419 sizeof(tmp_uris
[0].dst
.ipv4
));
424 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
426 tmp_uris
[1].dtype
= proto
->dtype
;
427 tmp_uris
[1].proto
= proto
->type
;
428 tmp_uris
[1].port
= data_port
;
431 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
432 sizeof(tmp_uris
[0].dst
.ipv6
));
437 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
439 tmp_uris
[1].dtype
= proto
->dtype
;
440 tmp_uris
[1].proto
= proto
->type
;
441 tmp_uris
[1].port
= data_port
;
444 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
445 sizeof(tmp_uris
[0].dst
.ipv4
));
451 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
452 sizeof(tmp_uris
[0].dst
.ipv6
));
462 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
463 proto
->dtype
, proto
->type
, (addr_f
== NULL
) ? "" : addr_f
,
464 (subdir_b
== NULL
) ? "" : subdir_b
, ctrl_port
, data_port
);
This page took 0.051009 seconds and 4 git commands to generate.