LTTng live: use common url parser
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 15 May 2017 14:48:10 +0000 (10:48 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 16:57:43 +0000 (12:57 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
cli/babeltrace-cfg-cli-args.c
plugins/ctf/Makefile.am
plugins/ctf/lttng-live/viewer-connection.c
plugins/ctf/lttng-live/viewer-connection.h

index f717f90b0c535338510a33596d975613331543f8..c11545177326e4a5ddac3267ae5eacdd51e65437 100644 (file)
@@ -4305,7 +4305,7 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
                if (implicit_lttng_live_args.exists) {
                        lttng_live_url_parts =
                                bt_common_parse_lttng_live_url(leftover,
-                                       error_buf, 256);
+                                       error_buf, sizeof(error_buf));
                        if (!lttng_live_url_parts.proto) {
                                printf_err("Invalid LTTng live URL format: %s\n",
                                        error_buf);
index b2f60258e3f2249334268746ab2ca35e5561ba49..8814cd11dd5aee899a0af781cbb10b5c0d66b210 100644 (file)
@@ -18,4 +18,5 @@ libbabeltrace_plugin_ctf_la_LIBADD = \
        fs-src/libbabeltrace-plugin-ctf-fs.la \
        lttng-live/libbabeltrace-plugin-ctf-lttng-live.la \
        fs-sink/libbabeltrace-plugin-ctf-writer.la \
-       $(top_builddir)/logging/libbabeltrace-logging.la
+       $(top_builddir)/logging/libbabeltrace-logging.la \
+       $(top_builddir)/common/libbabeltrace-common.la
index e5d1fc13e6d5a7c366b734c55d56f35a60a19604..aab5c3958a6402890feaeb846e6178557ca8be88 100644 (file)
@@ -36,6 +36,7 @@
 
 #include <babeltrace/compat/send-internal.h>
 #include <babeltrace/compiler-internal.h>
+#include <babeltrace/common-internal.h>
 
 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-VIEWER"
 
@@ -75,114 +76,58 @@ static ssize_t lttng_live_send(int fd, const void *buf, size_t len)
        return ret;
 }
 
-/*
- * hostname parameter needs to hold MAXNAMLEN chars.
- */
 static int parse_url(struct bt_live_viewer_connection *viewer_connection)
 {
-       char remain[3][MAXNAMLEN];
-       int ret = -1, proto, proto_offset = 0;
+       char error_buf[256] = { 0 };
+       struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
+       int ret = -1;
        const char *path = viewer_connection->url->str;
-       size_t path_len;
 
        if (!path) {
                goto end;
        }
-       path_len = strlen(path); /* not accounting \0 */
 
-       /*
-        * Since sscanf API does not allow easily checking string length
-        * against a size defined by a macro. Test it beforehand on the
-        * input. We know the output is always <= than the input length.
-        */
-       if (path_len >= MAXNAMLEN) {
-               goto end;
-       }
-       ret = sscanf(path, "net%d://", &proto);
-       if (ret < 1) {
-               proto = 4;
-               /* net:// */
-               proto_offset = strlen("net://");
-       } else {
-               /* net4:// or net6:// */
-               proto_offset = strlen("netX://");
-       }
-       if (proto_offset > path_len) {
+       lttng_live_url_parts = bt_common_parse_lttng_live_url(path,
+                       error_buf, sizeof(error_buf));
+       if (!lttng_live_url_parts.proto) {
+               BT_LOGW("Invalid LTTng live URL format: %s", error_buf);
                goto end;
        }
-       if (proto == 6) {
-               BT_LOGW("IPv6 is currently unsupported by lttng-live");
-               goto end;
-       }
-       /* TODO : parse for IPv6 as well */
-       /* Parse the hostname or IP */
-       ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
-               viewer_connection->relay_hostname, remain[0]);
-       if (ret == 2) {
-               /* Optional port number */
-               switch (remain[0][0]) {
-               case ':':
-                       ret = sscanf(remain[0], ":%d%s", &viewer_connection->port, remain[1]);
-                       /* Optional session ID with port number */
-                       if (ret == 2) {
-                               ret = sscanf(remain[1], "/%s", remain[2]);
-                               /* Accept 0 or 1 (optional) */
-                               if (ret < 0) {
-                                       goto end;
-                               }
-                       } else if (ret == 0) {
-                               BT_LOGW("Missing port number after delimitor ':'");
-                               ret = -1;
-                               goto end;
-                       }
-                       break;
-               case '/':
-                       /* Optional session ID */
-                       ret = sscanf(remain[0], "/%s", remain[2]);
-                       /* Accept 0 or 1 (optional) */
-                       if (ret < 0) {
-                               goto end;
-                       }
-                       break;
-               default:
-                       BT_LOGW("wrong delimitor : %c", remain[0][0]);
-                       ret = -1;
-                       goto end;
-               }
-       }
 
-       if (viewer_connection->port < 0) {
+       viewer_connection->relay_hostname =
+                       lttng_live_url_parts.hostname;
+       lttng_live_url_parts.hostname = NULL;
+
+       if (lttng_live_url_parts.port >= 0) {
+               viewer_connection->port = lttng_live_url_parts.port;
+       } else {
                viewer_connection->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
        }
 
-       if (strlen(remain[2]) == 0) {
-               BT_LOGD("Connecting to hostname : %s, port : %d, "
-                               "proto : IPv%d",
-                               viewer_connection->relay_hostname,
-                               viewer_connection->port,
-                               proto);
-               ret = 0;
-               goto end;
-       }
-       ret = sscanf(remain[2], "host/%[a-zA-Z.0-9%-]/%s",
-                       viewer_connection->target_hostname,
-                       viewer_connection->session_name);
-       if (ret != 2) {
-               BT_LOGW("Format : "
-                       "net://<hostname>/host/<target_hostname>/<session_name>");
-               goto end;
+       viewer_connection->target_hostname =
+                       lttng_live_url_parts.target_hostname;
+       lttng_live_url_parts.target_hostname = NULL;
+
+       if (lttng_live_url_parts.session_name) {
+               viewer_connection->session_name =
+                               lttng_live_url_parts.session_name;
+               lttng_live_url_parts.session_name = NULL;
        }
 
        BT_LOGD("Connecting to hostname : %s, port : %d, "
                        "target hostname : %s, session name : %s, "
-                       "proto : IPv%d",
-                       viewer_connection->relay_hostname,
+                       "proto : %s",
+                       viewer_connection->relay_hostname->str,
                        viewer_connection->port,
-                       viewer_connection->target_hostname,
-                       viewer_connection->session_name, proto);
+                       viewer_connection->target_hostname == NULL ?
+                               "<none>" : viewer_connection->target_hostname->str,
+                       viewer_connection->session_name == NULL ?
+                               "<none>" : viewer_connection->session_name->str,
+                       lttng_live_url_parts.proto->str);
        ret = 0;
 
 end:
+       bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
        return ret;
 }
 
@@ -261,10 +206,10 @@ static int lttng_live_connect_viewer(struct bt_live_viewer_connection *viewer_co
                goto error;
        }
 
-       host = gethostbyname(viewer_connection->relay_hostname);
+       host = gethostbyname(viewer_connection->relay_hostname->str);
        if (!host) {
                BT_LOGE("Cannot lookup hostname %s",
-                       viewer_connection->relay_hostname);
+                       viewer_connection->relay_hostname->str);
                goto error;
        }
 
@@ -702,9 +647,9 @@ int lttng_live_query_session_ids(struct lttng_live_component *lttng_live)
                session_id = be64toh(lsession.id);
 
                if ((strncmp(lsession.session_name,
-                       viewer_connection->session_name,
+                       viewer_connection->session_name->str,
                        MAXNAMLEN) == 0) && (strncmp(lsession.hostname,
-                               viewer_connection->target_hostname,
+                               viewer_connection->target_hostname->str,
                                MAXNAMLEN) == 0)) {
                        if (lttng_live_add_session(lttng_live, session_id)) {
                                goto error;
@@ -1474,5 +1419,14 @@ void bt_live_viewer_connection_destroy(struct bt_live_viewer_connection *viewer_
        BT_LOGD("Closing connection to url \"%s\"", viewer_connection->url->str);
        lttng_live_disconnect_viewer(viewer_connection);
        g_string_free(viewer_connection->url, TRUE);
+       if (viewer_connection->relay_hostname) {
+               g_string_free(viewer_connection->relay_hostname, TRUE);
+       }
+       if (viewer_connection->target_hostname) {
+               g_string_free(viewer_connection->target_hostname, TRUE);
+       }
+       if (viewer_connection->session_name) {
+               g_string_free(viewer_connection->session_name, TRUE);
+       }
        g_free(viewer_connection);
 }
index da49e349e6d05885a56fb1588841efda08898989..822c17ed3b874820aa6dc29a06fa3736e3e9c3e0 100644 (file)
@@ -44,9 +44,10 @@ struct bt_live_viewer_connection {
 
        GString *url;
 
-       char relay_hostname[MAXNAMLEN];
-       char target_hostname[MAXNAMLEN];
-       char session_name[MAXNAMLEN];
+       GString *relay_hostname;
+       GString *target_hostname;
+       GString *session_name;
+
        int control_sock;
        int port;
 
This page took 0.031426 seconds and 4 git commands to generate.