about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorSteffen Jaeckel <jaeckel-floss@eyet-services.de>2022-02-01 16:33:22 +0100
committerSteffen Jaeckel <jaeckel-floss@eyet-services.de>2022-02-01 16:33:22 +0100
commit196f20a7d51ca4e5854432405547a6b90ea4196d (patch)
treea6785f2ccb8bdbadba8b33bb3599c9ab78fd1b77 /src
parent0e58509c161ae8409c9accabb9606e0c7006b880 (diff)
downloadprofani-tty-196f20a7d51ca4e5854432405547a6b90ea4196d.tar.gz
add fall-back for older GLib versions
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Diffstat (limited to 'src')
-rw-r--r--src/xmpp/connection.c72
1 files changed, 54 insertions, 18 deletions
diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c
index 304c65c8..c04f9df2 100644
--- a/src/xmpp/connection.c
+++ b/src/xmpp/connection.c
@@ -896,19 +896,10 @@ _get_soh_error(xmpp_stanza_t* error_stanza)
 }
 #endif
 
-static bool
-_get_other_host(xmpp_stanza_t* error_stanza, gchar** host, int* port)
+#if GLIB_CHECK_VERSION(2, 66, 0)
+static gboolean
+_split_url(const char* alturi, gchar** host, gint* port)
 {
-    xmpp_stanza_t* soh_error = _get_soh_error(error_stanza);
-    if (!soh_error || !xmpp_stanza_get_children(soh_error)) {
-        log_debug("_get_other_host: stream-error contains no see-other-host");
-        return false;
-    }
-    const char* alturi = xmpp_stanza_get_text_ptr(xmpp_stanza_get_children(soh_error));
-    if (!alturi) {
-        log_debug("_get_other_host: see-other-host contains no text");
-        return false;
-    }
     /* Construct a valid URI with `schema://` as `g_uri_split_network()`
      * requires this to be there.
      */
@@ -920,12 +911,7 @@ _get_other_host(xmpp_stanza_t* error_stanza, gchar** host, int* port)
     }
     memcpy(xmpp_uri, xmpp, strlen(xmpp));
     memcpy(xmpp_uri + strlen(xmpp), alturi, strlen(alturi) + 1);
-
-    if (!g_uri_split_network(xmpp_uri, 0, NULL, host, port, NULL)) {
-        log_debug("_get_other_host: Could not split \"%s\"", xmpp_uri);
-        free(xmpp_uri);
-        return false;
-    }
+    gboolean ret = g_uri_split_network(xmpp_uri, 0, NULL, host, port, NULL);
     free(xmpp_uri);
     /* fix-up `port` as g_uri_split_network() sets port to `-1` if it's missing
      * in the passed-in URI, but libstrophe expects a "missing port"
@@ -933,6 +919,56 @@ _get_other_host(xmpp_stanza_t* error_stanza, gchar** host, int* port)
      */
     if (*port == -1)
         *port = 0;
+    return ret;
+}
+#else
+/* poor-mans URL splitting */
+static gboolean
+_split_url(const char* alturi, gchar** host, gint* port)
+{
+    ptrdiff_t hostlen;
+    /* search ':' from start and end
+     * if `first` matches `last` it's a `hostname:port` combination
+     * if `first` is different than `last` it's `[ip:v6]:port`
+     */
+    char* first = strchr(alturi, ':');
+    char* last = strrchr(alturi, ':');
+    if (first && first == last) {
+        hostlen = last - alturi;
+        if (!strtoi_range(last + 1, port, 1, 65535, NULL))
+            return FALSE;
+    } else {
+        hostlen = strlen(alturi) + 1;
+        *port = 0;
+    }
+    gchar* buf = g_malloc(hostlen);
+    if (!buf)
+        return FALSE;
+    memcpy(buf, alturi, hostlen);
+    buf[hostlen - 1] = '\0';
+    *host = buf;
+    return TRUE;
+}
+#endif
+
+static bool
+_get_other_host(xmpp_stanza_t* error_stanza, gchar** host, int* port)
+{
+    xmpp_stanza_t* soh_error = _get_soh_error(error_stanza);
+    if (!soh_error || !xmpp_stanza_get_children(soh_error)) {
+        log_debug("_get_other_host: stream-error contains no see-other-host");
+        return false;
+    }
+    const char* alturi = xmpp_stanza_get_text_ptr(xmpp_stanza_get_children(soh_error));
+    if (!alturi) {
+        log_debug("_get_other_host: see-other-host contains no text");
+        return false;
+    }
+
+    if (!_split_url(alturi, host, port)) {
+        log_debug("_get_other_host: Could not split \"%s\"", alturi);
+        return false;
+    }
     return true;
 }