First implementation of auto-configuration (bug #3140)
[claws.git] / src / common / utils.c
index ef8c6b60a8eb995b07f5a6d156b10dcc6adbfece..52f6bac343111275e539d4018cf4ec514d6c607d 100644 (file)
@@ -25,6 +25,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <gio/gio.h>
 
 #include <glib/gi18n.h>
 
@@ -5514,3 +5515,45 @@ int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) {
        slist_free_strings_full(canonical_parts);
        return 0;
 }
+
+#if (defined USE_GNUTLS && GLIB_CHECK_VERSION(2,22,0))
+gboolean auto_configure_service(const gchar *service, const gchar *domain, gchar **srvhost, guint16 *srvport)
+{
+       GResolver *resolver;
+       GList *answers, *cur;
+       GError *error = NULL;
+       gboolean result = FALSE;
+
+       cm_return_val_if_fail(service != NULL, FALSE);
+       cm_return_val_if_fail(domain != NULL, FALSE);
+
+       resolver = g_resolver_get_default();
+       if (resolver == NULL)
+               return FALSE;
+       
+       answers = g_resolver_lookup_service(resolver, service, "tcp", domain, NULL, &error);
+
+       *srvhost = NULL;
+       *srvport = 0;
+
+       if (answers) {
+               for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
+                       GSrvTarget *target = (GSrvTarget *)cur->data;
+                       const gchar *hostname = g_srv_target_get_hostname(target);
+                       guint16 port = g_srv_target_get_port(target);
+                       if (hostname && strcmp(hostname,"") && port > 0) {
+                               result = TRUE;
+                               *srvhost = g_strdup(hostname);
+                               *srvport = port;
+                               break;
+                       }
+               }
+               g_resolver_free_targets(answers);
+       } else if (error) {
+               g_error_free(error);
+       }
+
+       g_object_unref(resolver);
+       return result;
+}
+#endif
\ No newline at end of file