Made SRV handling more sane, and implemented fallback using jid domain name.
authorSenko Rasic <senko@phyrexia.(none)>
Thu, 06 Sep 2007 16:37:16 +0200
changeset 275 37937c612f62
parent 274 c32a7011e435
child 276 3b05eae99e1a
Made SRV handling more sane, and implemented fallback using jid domain name.
loudmouth/lm-connection.c
loudmouth/lm-socket.c
loudmouth/lm-socket.h
--- a/loudmouth/lm-connection.c	Thu Sep 06 15:20:04 2007 +0200
+++ b/loudmouth/lm-connection.c	Thu Sep 06 16:37:16 2007 +0200
@@ -421,7 +421,7 @@
 static gboolean
 connection_do_open (LmConnection *connection, GError **error) 
 {
-	gchar *connect_server;
+	gchar *domain;
 
 	if (lm_connection_is_open (connection)) {
 		g_set_error (error,
@@ -431,25 +431,12 @@
 		return FALSE;
 	}
 
-	if (!connection->server) {
-		if (!connection->use_srv) {
-			g_set_error (error,
-				     LM_ERROR,
-				     LM_ERROR_CONNECTION_FAILED,
-				     "You need to set the server hostname in the call to lm_connection_new()");
-			return FALSE;
-		} else {
-			if (!connection_get_server_from_jid (connection->jid,
-							     &connect_server)) {
-				g_set_error (error,
-					     LM_ERROR,
-					     LM_ERROR_CONNECTION_FAILED,
-					     "You need to either set server hostname or jid");
-				return FALSE;
-			}
-		}
-	} else {
-		connect_server = g_strdup (connection->server);
+	if (!connection_get_server_from_jid (connection->jid, &domain)) {
+		g_set_error (error,
+			LM_ERROR,
+			LM_ERROR_CONNECTION_FAILED,
+			"You need to either set server hostname or jid");
+		return FALSE;
 	}
 
 	lm_message_queue_attach (connection->queue, connection->context);
@@ -467,13 +454,12 @@
 					       connection,
 					       connection,
 					       connection->blocking,
-					       connect_server,
+					       connection->server,
+					       domain,
 					       connection->port,
-					       connection->use_srv,
 					       connection->ssl,
 					       connection->proxy,
 					       error);
-	g_free (connect_server);
 
 	if (!connection->socket) {
 		return FALSE;
--- a/loudmouth/lm-socket.c	Thu Sep 06 15:20:04 2007 +0200
+++ b/loudmouth/lm-socket.c	Thu Sep 06 16:37:16 2007 +0200
@@ -55,8 +55,6 @@
 	gchar        *server;
 	guint         port;
 
-	gboolean      use_srv;
-
 	gboolean      blocking;
 
 	LmSSL        *ssl;
@@ -883,6 +881,12 @@
 		}
 	}
 
+	/* If server wasn't specified and SRV failed, use domain */
+	if (!socket->server) {
+		lm_verbose ("SRV lookup failed, trying jid domain\n");
+		socket->server = g_strdup (socket->domain);
+	}
+
 	if (socket->proxy) {
 		remote_addr = lm_proxy_get_server (socket->proxy);
 	} else {
@@ -930,7 +934,7 @@
 _lm_socket_create_phase2 (LmSocket *socket, struct addrinfo *ans)
 {
 	if (ans == NULL) {
-		lm_verbose ("error while resolving, bailing out");
+		lm_verbose ("error while resolving, bailing out\n");
 		(socket->connect_func) (socket, FALSE, socket->user_data);
 		g_free (socket->connect_data);
 		socket->connect_data = NULL;
@@ -952,8 +956,8 @@
 		  LmConnection      *connection,
 		  gboolean           blocking,
 		  const gchar       *server,
+		  const gchar       *domain,
 		  guint              port, 
-		  gboolean           use_srv,
 		  LmSSL             *ssl,
 		  LmProxy           *proxy,
 		  GError           **error)
@@ -965,7 +969,7 @@
 	int              len;
 #endif
 	
-	g_return_val_if_fail (server != NULL, NULL);
+	g_return_val_if_fail (domain != NULL, NULL);
 	g_return_val_if_fail ((port >= MIN_PORT && port <= MAX_PORT), NULL);
 	g_return_val_if_fail (data_func != NULL, NULL);
 	g_return_val_if_fail (closed_func != NULL, NULL);
@@ -976,10 +980,9 @@
 	socket->ref_count = 1;
 
 	socket->connection = connection;
-	socket->domain = g_strdup (server);
+	socket->domain = g_strdup (domain);
 	socket->server = g_strdup (server);
 	socket->port = port;
-	socket->use_srv = use_srv;
 	socket->cancel_open = FALSE;
 	socket->ssl = NULL;
 	socket->proxy = NULL;
@@ -1003,9 +1006,9 @@
 	}
 
 
-	if (use_srv) {
+	if (!server) {
 		char          *srv;
-		srv = g_strdup_printf ("_xmpp-client._tcp.%s", socket->server);
+		srv = g_strdup_printf ("_xmpp-client._tcp.%s", socket->domain);
 		lm_verbose ("Performing a SRV lookup for %s\n", srv);
 
 #ifdef HAVE_ASYNCNS
--- a/loudmouth/lm-socket.h	Thu Sep 06 15:20:04 2007 +0200
+++ b/loudmouth/lm-socket.h	Thu Sep 06 16:37:16 2007 +0200
@@ -57,8 +57,8 @@
 					   LmConnection   *connection,
 					   gboolean        blocking,
 					   const gchar    *server, 
+					   const gchar    *domain,
 					   guint           port, 
-					   gboolean        use_srv,
 					   LmSSL          *ssl,
 					   LmProxy        *proxy,
 					   GError        **error);