3581e307d33a0dfa5bdcb5932b0e5fb648b30114
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.
19 #include <arpa/inet.h>
23 #include <sys/socket.h>
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/utils.h>
32 P_NET
, P_NET6
, P_FILE
, P_TCP
, P_TCP6
,
37 const char *leading_string
;
38 enum uri_proto_code code
;
39 enum lttng_proto_type type
;
40 enum lttng_dst_type dtype
;
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 }
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).
58 const inline char *strpbrk_or_eos(const char *s
, const char *accept
)
60 char *p
= strpbrk(s
, accept
);
70 * Validate if proto is a supported protocol from proto_uri array.
72 static const struct uri_proto
*get_uri_proto(const char *uri_str
)
74 const struct uri_proto
*supported
= NULL
;
77 if (uri_str
== NULL
) {
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) {
97 * Set network address from string into dst. Supports both IP string and
100 static int set_ip_address(const char *addr
, int af
, char *dst
, size_t size
)
103 unsigned char buf
[sizeof(struct in6_addr
)];
104 struct hostent
*record
;
106 /* Network protocol */
107 ret
= inet_pton(af
, addr
, buf
);
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
);
117 /* Translate IP to string */
118 (void) inet_ntop(af
, record
->h_addr_list
[0], dst
, size
);
120 memcpy(dst
, addr
, size
);
123 DBG2("IP address resolved to %s", dst
);
134 * Return 0 if equal else 1.
136 int uri_compare(struct lttng_uri
*uri1
, struct lttng_uri
*uri2
)
138 return memcmp(uri1
, uri2
, sizeof(struct lttng_uri
));
144 void uri_free(struct lttng_uri
*uri
)
153 * Return an allocated URI.
155 struct lttng_uri
*uri_create(void)
157 struct lttng_uri
*uri
;
159 uri
= zmalloc(sizeof(struct lttng_uri
));
161 PERROR("zmalloc uri");
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.
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
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.
183 ssize_t
uri_parse(const char *str_uri
, struct lttng_uri
**uris
)
186 /* Size of the uris array. Default is 1 */
188 char subdir
[PATH_MAX
];
189 unsigned int ctrl_port
= 0;
190 unsigned int data_port
= 0;
191 struct lttng_uri
*tmp_uris
;
193 const struct uri_proto
*proto
;
194 const char *purl
, *addr_e
, *addr_b
, *subdir_b
= NULL
;
195 const char *seps
= ":/\0";
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.
205 DBG3("URI string: %s", str_uri
);
207 proto
= get_uri_proto(str_uri
);
209 ERR("URI parse unknown protocol %s", str_uri
);
215 if (proto
->code
== P_NET
|| proto
->code
== P_NET6
) {
216 /* Special case for net:// which requires two URI objects */
220 /* Allocate URI array */
221 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
222 if (tmp_uris
== NULL
) {
223 PERROR("zmalloc uri");
227 memset(subdir
, 0, sizeof(subdir
));
228 purl
+= strlen(proto
->leading_string
);
230 /* Copy known value to the first URI. */
231 tmp_uris
[0].dtype
= proto
->dtype
;
232 tmp_uris
[0].proto
= proto
->type
;
234 if (proto
->code
== P_FILE
) {
236 ERR("Missing destination full path.");
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
);
246 /* Assume we are at the beginning of an address or host of some sort. */
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
254 * proto://[<addr>]...
258 /* Address begins after '[' */
260 addr_e
= strchr(addr_b
, ']');
261 if (addr_e
== NULL
|| addr_b
== addr_e
) {
262 ERR("Broken IPv6 address %s", addr_b
);
266 /* Moving parsed URL pointer after the final bracket ']' */
270 * The closing bracket must be followed by a seperator or NULL char.
272 if (strchr(seps
, *purl
) == NULL
) {
273 ERR("Unknown symbol after IPv6 address: %s", purl
);
277 purl
= strpbrk_or_eos(purl
, seps
);
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.");
287 addr_f
= utils_strdupdelim(addr_b
, addr_e
);
288 if (addr_f
== NULL
) {
293 * Detect PORT after address. The net/net6 protocol allows up to two port
294 * so we can define the control and data port.
296 while (*purl
== ':') {
298 const char *port_b
, *port_e
;
301 /* Update pass counter */
305 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
308 if ((i
== 2 && (proto
->code
!= P_NET
&& proto
->code
!= P_NET6
))
314 * Move parsed URL to port value.
315 * proto://addr_host:PORT1:PORT2/foo/bar
320 purl
= strpbrk_or_eos(purl
, seps
);
323 if (port_b
!= port_e
) {
324 port_f
= utils_strdupdelim(port_b
, port_e
);
325 if (port_f
== NULL
) {
330 if (port
> 0xffff || port
<= 0x0) {
331 ERR("Invalid port number %d", port
);
345 /* Check for a valid subdir or trailing garbage */
348 * Move to subdir value.
349 * proto://addr_host:PORT1:PORT2/foo/bar
354 } else if (*purl
!= '\0') {
355 ERR("Trailing characters not recognized: %s", purl
);
359 /* We have enough valid information to create URI(s) object */
361 /* Copy generic information */
362 tmp_uris
[0].port
= ctrl_port
;
364 /* Copy subdirectory if one. */
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';
370 switch (proto
->code
) {
372 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
373 sizeof(tmp_uris
[0].dst
.ipv4
));
378 memcpy(tmp_uris
[1].dst
.ipv4
, tmp_uris
[0].dst
.ipv4
, sizeof(tmp_uris
[1].dst
.ipv4
));
380 tmp_uris
[1].dtype
= proto
->dtype
;
381 tmp_uris
[1].proto
= proto
->type
;
382 tmp_uris
[1].port
= data_port
;
385 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
386 sizeof(tmp_uris
[0].dst
.ipv6
));
391 memcpy(tmp_uris
[1].dst
.ipv6
, tmp_uris
[0].dst
.ipv6
, sizeof(tmp_uris
[1].dst
.ipv6
));
393 tmp_uris
[1].dtype
= proto
->dtype
;
394 tmp_uris
[1].proto
= proto
->type
;
395 tmp_uris
[1].port
= data_port
;
398 ret
= set_ip_address(addr_f
, AF_INET
, tmp_uris
[0].dst
.ipv4
,
399 sizeof(tmp_uris
[0].dst
.ipv4
));
405 ret
= set_ip_address(addr_f
, AF_INET6
, tmp_uris
[0].dst
.ipv6
,
406 sizeof(tmp_uris
[0].dst
.ipv6
));
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
);
This page took 0.044113 seconds and 4 git commands to generate.