Clean-up: uri_parse_str_urls: skip allocation when no uris are parsed
[lttng-tools.git] / src / common / uri.c
index 408b75cd73c7e9808a47c30196372a2ffbe8dde6..a774cb3cb70567d5c96f57e1b6878fb6d4c0d7ec 100644 (file)
@@ -1,24 +1,14 @@
 /*
- * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
+ * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2 only, as
- * published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <assert.h>
 #include <arpa/inet.h>
-#include <netdb.h>
+#include <common/compat/netdb.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
@@ -29,6 +19,9 @@
 
 #include "uri.h"
 
+#define LOOPBACK_ADDR_IPV4 "127.0.0.1"
+#define LOOPBACK_ADDR_IPV6 "::1"
+
 enum uri_proto_code {
        P_NET, P_NET6, P_FILE, P_TCP, P_TCP6,
 };
@@ -58,7 +51,7 @@ static const struct uri_proto proto_uri[] = {
  * Return pointer to the character in s matching one of the characters in
  * accept. If nothing is found, return pointer to the end of string (eos).
  */
-static const inline char *strpbrk_or_eos(const char *s, const char *accept)
+static inline const char *strpbrk_or_eos(const char *s, const char *accept)
 {
        char *p = strpbrk(s, accept);
        if (p == NULL) {
@@ -68,7 +61,6 @@ static const inline char *strpbrk_or_eos(const char *s, const char *accept)
        return p;
 }
 
-
 /*
  * Validate if proto is a supported protocol from proto_uri array.
  */
@@ -115,15 +107,44 @@ static int set_ip_address(const char *addr, int af, char *dst, size_t size)
        ret = inet_pton(af, addr, buf);
        if (ret < 1) {
                /* We consider the dst to be an hostname or an invalid IP char */
-               record = gethostbyname2(addr, af);
-               if (record == NULL) {
+               record = lttng_gethostbyname2(addr, af);
+               if (record) {
+                       /* Translate IP to string */
+                       if (!inet_ntop(af, record->h_addr_list[0], dst, size)) {
+                               PERROR("inet_ntop");
+                               goto error;
+                       }
+               } else if (!strcmp(addr, "localhost") &&
+                               (af == AF_INET || af == AF_INET6)) {
+                       /*
+                        * Some systems may not have "localhost" defined in
+                        * accordance with IETF RFC 6761. According to this RFC,
+                        * applications may recognize "localhost" names as
+                        * special and resolve to the appropriate loopback
+                        * address.
+                        *
+                        * We choose to use the system name resolution API first
+                        * to honor its network configuration. If this fails, we
+                        * resolve to the appropriate loopback address. This is
+                        * done to accommodates systems which may want to start
+                        * tracing before their network configured.
+                        */
+                       const char *loopback_addr = af == AF_INET ?
+                                       LOOPBACK_ADDR_IPV4 : LOOPBACK_ADDR_IPV6;
+                       const size_t loopback_addr_len = af == AF_INET ?
+                                       sizeof(LOOPBACK_ADDR_IPV4) :
+                                       sizeof(LOOPBACK_ADDR_IPV6);
+
+                       DBG2("Could not resolve localhost address, using fallback");
+                       if (loopback_addr_len > size) {
+                               ERR("Could not resolve localhost address; destination string is too short");
+                               goto error;
+                       }
+                       strcpy(dst, loopback_addr);
+               } else {
                        /* At this point, the IP or the hostname is bad */
-                       ERR("URI parse bad hostname %s for af %d", addr, af);
                        goto error;
                }
-
-               /* Translate IP to string */
-               (void) inet_ntop(af, record->h_addr_list[0], dst, size);
        } else {
                if (size > 0) {
                        strncpy(dst, addr, size);
@@ -132,10 +153,10 @@ static int set_ip_address(const char *addr, int af, char *dst, size_t size)
        }
 
        DBG2("IP address resolved to %s", dst);
-
        return 0;
 
 error:
+       ERR("URI parse bad hostname %s for af %d", addr, af);
        return -1;
 }
 
@@ -235,22 +256,6 @@ void uri_free(struct lttng_uri *uri)
        free(uri);
 }
 
-/*
- * Return an allocated URI.
- */
-LTTNG_HIDDEN
-struct lttng_uri *uri_create(void)
-{
-       struct lttng_uri *uri;
-
-       uri = zmalloc(sizeof(struct lttng_uri));
-       if (uri == NULL) {
-               PERROR("zmalloc uri");
-       }
-
-       return uri;
-}
-
 /*
  * Parses a string URI to a lttng_uri. This function can potentially return
  * more than one URI in uris so the size of the array is returned and uris is
@@ -509,6 +514,7 @@ end:
        free(addr_f);
 
        *uris = tmp_uris;
+       assert(size == 1 || size == 2);
        return size;
 
 free_error:
@@ -529,7 +535,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
        unsigned int equal = 1, idx = 0;
        /* Add the "file://" size to the URL maximum size */
        char url[PATH_MAX + 7];
-       ssize_t size_ctrl = 0, size_data = 0, size;
+       ssize_t ctrl_uri_count = 0, data_uri_count = 0, uri_count;
        struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL;
        struct lttng_uri *tmp_uris = NULL;
 
@@ -556,29 +562,36 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
                if (ret < 0) {
                        PERROR("snprintf file url");
                        goto parse_error;
+               } else if (ret >= sizeof(url)) {
+                       PERROR("snprintf file url is too long");
+                       goto parse_error;
+
                }
                ctrl_url = url;
        }
 
        /* Parse the control URL if there is one */
        if (ctrl_url && *ctrl_url != '\0') {
-               size_ctrl = uri_parse(ctrl_url, &ctrl_uris);
-               if (size_ctrl < 1) {
+               ctrl_uri_count = uri_parse(ctrl_url, &ctrl_uris);
+               if (ctrl_uri_count < 1) {
                        ERR("Unable to parse the URL %s", ctrl_url);
                        goto parse_error;
                }
 
+               /* 1 and 2 are the only expected values on success. */
+               assert(ctrl_uri_count == 1 || ctrl_uri_count == 2);
+
                /* At this point, we know there is at least one URI in the array */
                set_default_uri_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL);
 
                if (ctrl_uris[0].dtype == LTTNG_DST_PATH &&
                                (data_url && *data_url != '\0')) {
-                       ERR("Can not have a data URL when destination is file://");
+                       ERR("Cannot have a data URL when destination is file://");
                        goto error;
                }
 
                /* URL are not equal but the control URL uses a net:// protocol */
-               if (size_ctrl == 2) {
+               if (ctrl_uri_count == 2) {
                        if (!equal) {
                                ERR("Control URL uses the net:// protocol and the data URL is "
                                                "different. Not allowed.");
@@ -598,28 +611,36 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
                int ret;
 
                /* We have to parse the data URL in this case */
-               size_data = uri_parse(data_url, &data_uris);
-               if (size_data < 1) {
+               data_uri_count = uri_parse(data_url, &data_uris);
+               if (data_uri_count < 1) {
                        ERR("Unable to parse the URL %s", data_url);
                        goto error;
-               } else if (size_data == 2) {
+               } else if (data_uri_count == 2) {
                        ERR("Data URL can not be set with the net[4|6]:// protocol");
                        goto error;
+               } else {
+                       /* 1 and 2 are the only expected values on success. */
+                       assert(data_uri_count == 1);
                }
 
                set_default_uri_attr(&data_uris[0], LTTNG_STREAM_DATA);
 
-               ret = compare_destination(&ctrl_uris[0], &data_uris[0]);
-               if (ret != 0) {
-                       ERR("Control and data destination mismatch");
-                       goto error;
+               if (ctrl_uris) {
+                       ret = compare_destination(&ctrl_uris[0], &data_uris[0]);
+                       if (ret != 0) {
+                               ERR("Control and data destination mismatch");
+                               goto error;
+                       }
                }
        }
 
-       /* Compute total size */
-       size = size_ctrl + size_data;
+       /* Compute total size. */
+       uri_count = ctrl_uri_count + data_uri_count;
+       if (uri_count <= 0) {
+               goto error;
+       }
 
-       tmp_uris = zmalloc(sizeof(struct lttng_uri) * size);
+       tmp_uris = zmalloc(sizeof(struct lttng_uri) * uri_count);
        if (tmp_uris == NULL) {
                PERROR("zmalloc uris");
                goto error;
@@ -627,7 +648,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
 
        if (ctrl_uris) {
                /* It's possible the control URIs array contains more than one URI */
-               memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * size_ctrl);
+               memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * ctrl_uri_count);
                ++idx;
                free(ctrl_uris);
        }
@@ -639,7 +660,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url,
 
        *uris = tmp_uris;
 
-       return size;
+       return uri_count;
 
 error:
        free(ctrl_uris);
This page took 0.02635 seconds and 4 git commands to generate.