cpw.darkrain42.xmpp.bosh: 96588eb4: Hide PurpleTxtResponse and use a more pu...

paul at darkrain42.org paul at darkrain42.org
Sat Apr 11 19:05:44 EDT 2009


-----------------------------------------------------------------
Revision: 96588eb41bf6a5544a8d0ffb7f7f7e5f82b4c38b
Ancestor: d9471e1c592f9cd3e25d4ff182665758cc3da445
Author: paul at darkrain42.org
Date: 2009-04-11T21:20:31
Branch: im.pidgin.cpw.darkrain42.xmpp.bosh
URL: http://d.pidgin.im/viewmtn/revision/info/96588eb41bf6a5544a8d0ffb7f7f7e5f82b4c38b

Modified files:
        libpurple/dnssrv.c libpurple/dnssrv.h
        libpurple/protocols/jabber/jabber.c

ChangeLog: 

Hide PurpleTxtResponse and use a more purple API

-------------- next part --------------
============================================================
--- libpurple/dnssrv.c	d7b4c6b3fd803d6f86f1c97411aa2b6f67ed2957
+++ libpurple/dnssrv.c	23d612c2405b30ec13754349eaf09e0588f1413e
@@ -65,6 +65,10 @@ static void (WINAPI *MyDnsRecordListFree
 	DNS_FREE_TYPE FreeType) = NULL;
 #endif
 
+struct _PurpleTxtResponse {
+    char *content;
+};
+
 struct _PurpleSrvQueryData {
 	union {
 		PurpleSrvCallback srv;
@@ -246,29 +250,30 @@ resolved(gpointer data, gint source, Pur
 			}
 			cb(res, size, query_data->extradata);
 		} else if (type == T_TXT) {
+			GSList *responses = NULL;
 			PurpleTxtResponse *res;
-			PurpleTxtResponse *tmp;
 			PurpleTxtCallback cb = query_data->cb.txt;
 			if (read(source, &size, sizeof(int)) == sizeof(int)) {
 				ssize_t red;
 				purple_debug_info("dnssrv","found %d TXT entries\n", size);
-				tmp = res = g_new0(PurpleTxtResponse, size);
+				res = g_new0(PurpleTxtResponse, 1);
 				for (i = 0; i < size; i++) {
-					red = read(source, tmp++, sizeof(PurpleTxtResponse));
+					red = read(source, res, sizeof(PurpleTxtResponse));
 					if (red != sizeof(PurpleTxtResponse)) {
 						purple_debug_error("dnssrv","unable to read txt "
 								"response: %s\n", g_strerror(errno));
 						size = 0;
 						g_free(res);
-						res = NULL;
+						g_slist_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
+						g_slist_free(responses);
+						responses = NULL;
+						break;
 					}
 				}
 			} else {
 				purple_debug_info("dnssrv","found 0 TXT entries; errno is %i\n", errno);
-				size = 0;
-				res = NULL;
 			}
-			cb(res, size, query_data->extradata);			
+			cb(responses, query_data->extradata);			
 		} else {
 			purple_debug_info("dnssrv","type unknown of DNS result entry; errno is %i\n", errno);
 		}
@@ -316,22 +321,12 @@ res_main_thread_cb(gpointer data)
 			PurpleTxtResponse *txtres_tmp = NULL;
 			GSList *lst = query_data->results;
 
-			size = g_slist_length(lst);
-
-			if(query_data->cb.txt && size > 0)
-				txtres_tmp = txtres = g_new0(PurpleTxtResponse, size);
-			while (lst) {
-				if(query_data->cb.txt)
-					memcpy(txtres_tmp++, lst->data, sizeof(PurpleTxtResponse));
-				g_free(lst->data);
-				lst = g_slist_remove(lst, lst->data);
+			purple_debug_info("dnssrv", "found %d TXT entries\n", g_slist_length(lst));
+			
+			if (query_data->cb.txt) {
+				query_data->results = NULL;
+				query_data->cb.txt(lst, query_data->extradata);
 			}
-
-			query_data->results = NULL;
-
-			purple_debug_info("dnssrv", "found %d TXT entries\n", size);
-			
-			if(query_data->cb.txt) query_data->cb.txt(txtres, size, query_data->extradata);
 		} else {
 			purple_debug_error("dnssrv", "unknown query type");
 		}
@@ -557,14 +552,14 @@ PurpleSrvQueryData *purple_txt_resolve(c
 	if(pipe(in) || pipe(out)) {
 		purple_debug_error("dnssrv", "Could not create pipe\n");
 		g_free(query);
-		cb(NULL, 0, extradata);
+		cb(NULL, extradata);
 		return NULL;
 	}
 
 	pid = fork();
 	if (pid == -1) {
 		purple_debug_error("dnssrv", "Could not create process!\n");
-		cb(NULL, 0, extradata);
+		cb(NULL, extradata);
 		g_free(query);
 		return NULL;
 	}
@@ -625,7 +620,7 @@ PurpleSrvQueryData *purple_txt_resolve(c
 		}
 	}
 
-	/* The query isn't going to happen, so finish the SRV lookup now.
+	/* The query isn't going to happen, so finish the TXT lookup now.
 	 * Asynchronously call the callback since stuff may not expect
 	 * the callback to be called before this returns */
 	if (query_data->error_message != NULL)
@@ -665,3 +660,19 @@ purple_txt_cancel(PurpleSrvQueryData *qu
 {
 	purple_srv_cancel(query_data);
 }
+
+const gchar *
+purple_txt_response_get_content(PurpleTxtResponse *resp)
+{
+	g_return_val_if_fail(resp != NULL, NULL);
+
+	return resp->content;
+}
+
+void purple_txt_response_destroy(PurpleTxtResponse *resp)
+{
+	g_return_if_fail(resp != NULL);
+
+	g_free(resp->content);
+	g_free(resp);
+}
============================================================
--- libpurple/dnssrv.h	56d16a20798f296adf3349135351db73a5c49632
+++ libpurple/dnssrv.h	b38c7db2e5f57b20e11e6b47776d239002063997
@@ -32,6 +32,8 @@ typedef struct _PurpleTxtResponse Purple
 typedef struct _PurpleSrvResponse PurpleSrvResponse;
 typedef struct _PurpleTxtResponse PurpleTxtResponse;
 
+#include <glib.h>
+
 struct _PurpleSrvResponse {
 	char hostname[256];
 	int port;
@@ -39,12 +41,15 @@ struct _PurpleSrvResponse {
 	int pref;
 };
 
-struct _PurpleTxtResponse {
-    char *content;
-};
+typedef void (*PurpleSrvCallback)(PurpleSrvResponse *resp, int results, gpointer data);
 
-typedef void (*PurpleSrvCallback)(PurpleSrvResponse *resp, int results, gpointer data);
-typedef void (*PurpleTxtCallback)(PurpleTxtResponse *resp, int results, gpointer data);
+/**
+ * Callback that returns the data retrieved from a DNS TXT lookup.
+ *
+ * @param responses   A GSList of PurpleTxtResponse objects.
+ * @param data        The extra data passed to purple_txt_resolve.
+ */
+typedef void (*PurpleTxtCallback)(GSList *responses, gpointer data);
 
 /**
  * Queries an SRV record.
@@ -71,6 +76,8 @@ void purple_srv_cancel(PurpleSrvQueryDat
  * @param domain Domain name to query (e.g. "blubb.com")
  * @param cb A callback which will be called with the results
  * @param extradata Extra data to be passed to the callback
+ *
+ * @since 2.6.0
  */
 PurpleSrvQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata);
 
@@ -78,9 +85,27 @@ PurpleSrvQueryData *purple_txt_resolve(c
  * Cancel an TXT DNS query.
  *
  * @param query_data The request to cancel.
+ * @since 2.6.0
  */
 void purple_txt_cancel(PurpleSrvQueryData *query_data);
 
+/**
+ * Get the value of the current TXT record.
+ *
+ * @param resp  The TXT response record
+ * @returns The value of the current TXT record.
+ * @since 2.6.0
+ */
+const gchar *purple_txt_response_get_content(PurpleTxtResponse *resp);
+
+/**
+ * Destroy a TXT DNS response object.
+ *
+ * @param response The PurpleTxtResponse to destroy.
+ * @since 2.6.0
+ */
+void purple_txt_response_destroy(PurpleTxtResponse *resp);
+
 #ifdef __cplusplus
 }
 #endif
============================================================
--- libpurple/protocols/jabber/jabber.c	1d88f1b0990414b8136a0288dfca5f7045520329
+++ libpurple/protocols/jabber/jabber.c	9e0de918cd41bfa2b899748e54272d0879f7917a
@@ -584,14 +584,13 @@ static void 
 }
 
 static void 
-txt_resolved_cb(PurpleTxtResponse *resp, int results, gpointer data)
+txt_resolved_cb(GSList *responses, gpointer data)
 {
 	JabberStream *js = data;
-	int n;
-	
+
 	js->srv_query_data = NULL;
 
-	if (results == 0) {
+	if (responses == NULL) {
 		gchar *tmp;
 		tmp = g_strdup_printf(_("Could not find alternative XMPP connection methods after failing to connect directly.\n"));
 		purple_connection_error_reason (js->gc,
@@ -599,10 +598,11 @@ txt_resolved_cb(PurpleTxtResponse *resp,
 		g_free(tmp);
 		return;	
 	}
-	
-	for (n = 0; n < results; n++) {
+
+	while (responses) {
+		PurpleTxtResponse *resp = responses->data;
 		gchar **token;
-		token = g_strsplit(resp[n].content, "=", 2);
+		token = g_strsplit(purple_txt_response_get_content(resp), "=", 2);
 		if (!strcmp(token[0], "_xmpp-client-xbosh")) {
 			purple_debug_info("jabber","Found alternative connection method using %s at %s.\n", token[0], token[1]);
 			js->bosh = jabber_bosh_connection_init(js, token[1]);
@@ -611,17 +611,20 @@ txt_resolved_cb(PurpleTxtResponse *resp,
 			break;
 		}
 		g_strfreev(token);
+		purple_txt_response_destroy(resp);
+		responses = g_slist_delete_link(responses, responses);
 	}
+
 	if (js->bosh) {
 		jabber_bosh_connection_connect(js->bosh);
 	} else {
 		purple_debug_info("jabber","Didn't find an alternative connection method.\n");
 	}
 
-	for (n = 0; n < results; ++n)
-		g_free(resp[n].content);
-
-	g_free(resp);
+	if (responses) {
+		g_slist_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
+		g_slist_free(responses);
+	}
 }
 
 static void


More information about the Commits mailing list