pidgin.vv: eff997b9: Run an asynchronous DNS lookup for the S...

malu at pidgin.im malu at pidgin.im
Fri Jan 16 20:41:08 EST 2009


-----------------------------------------------------------------
Revision: eff997b9e44a0c5c1e63980780ba7d91f430ed2f
Ancestor: 5d196493caa916646453553a2491661e9caa182b
Author: malu at pidgin.im
Date: 2009-01-17T01:38:44
Branch: im.pidgin.pidgin.vv
URL: http://d.pidgin.im/viewmtn/revision/info/eff997b9e44a0c5c1e63980780ba7d91f430ed2f

Modified files:
        libpurple/media.c libpurple/network.c libpurple/network.h
        pidgin/gtkprefs.c

ChangeLog: 

Run an asynchronous DNS lookup for the STUN server IP (to set as a property
to the nice transmitter for media).
Also update the IP when the preference changes and when NetworkManager support
is present, when NM reports connected.
TODO: add code to run DNS lookup on Win32 when the network connection is ready

-------------- next part --------------
============================================================
--- libpurple/media.c	df0140f830b30870cdcb0c3374318563358c3d35
+++ libpurple/media.c	2366c6e026c659d05589494165851ebb8d9236f3
@@ -32,6 +32,7 @@
 #include "media.h"
 #include "marshallers.h"
 #include "mediamanager.h"
+#include "network.h"
 
 #include "debug.h"
 
@@ -1549,27 +1550,6 @@ purple_media_src_pad_added_cb(FsStream *
 	g_timeout_add(0, (GSourceFunc)purple_media_connected_cb, stream);
 }
 
-static gchar *
-purple_media_get_stun_pref_ip()
-{
-	const gchar *stun_pref =
-			purple_prefs_get_string("/purple/network/stun_server");
-	struct hostent *host;
-
-	if ((host = gethostbyname(stun_pref)) && host->h_addr) {
-		gchar *stun_ip = g_strdup_printf("%hhu.%hhu.%hhu.%hhu",
-				host->h_addr[0], host->h_addr[1],
-				host->h_addr[2], host->h_addr[3]);
-		purple_debug_info("media", "IP address for %s found: %s\n",
-				stun_pref, stun_ip);
-		return stun_ip;
-	} else {
-		purple_debug_info("media", "Unable to resolve %s IP address\n",
-				stun_pref);
-		return NULL;
-	}
-}
-
 static gboolean
 purple_media_add_stream_internal(PurpleMedia *media, const gchar *sess_id,
 				 const gchar *who, FsMediaType type,
@@ -1653,22 +1633,25 @@ purple_media_add_stream_internal(PurpleM
 
 	if (!stream) {
 		GError *err = NULL;
-		gchar *stun_ip = NULL;
 		FsStream *fsstream = NULL;
-
-		if ((stun_ip = purple_media_get_stun_pref_ip())) {
+		const gchar *stun_ip = purple_network_get_stun_ip();
+		
+		
+		if (stun_ip) {
 			GParameter *param = g_new0(GParameter, num_params+1);
 			memcpy(param, params, sizeof(GParameter) * num_params);
 
+			purple_debug_info("media", 
+				"setting property stun-ip on new stream: %s\n", stun_ip);
+			
 			param[num_params].name = "stun-ip";
 			g_value_init(&param[num_params].value, G_TYPE_STRING);
-			g_value_take_string(&param[num_params].value, stun_ip);
+			g_value_set_string(&param[num_params].value, stun_ip);
 
 			fsstream = fs_session_new_stream(session->session,
 					participant, type_direction,
 					transmitter, num_params+1, param, &err);
 			g_free(param);
-			g_free(stun_ip);
 		} else {
 			fsstream = fs_session_new_stream(session->session,
 					participant, type_direction,
============================================================
--- libpurple/network.c	7343dc4097f5476bf304eaf8a10d3352ee36211a
+++ libpurple/network.c	63ec98a8d7e5fbcc1031d2b07acf8bd365946f9a
@@ -48,6 +48,7 @@
 #include "prefs.h"
 #include "stun.h"
 #include "upnp.h"
+#include "dnsquery.h"
 
 /*
  * Calling sizeof(struct ifreq) isn't always correct on
@@ -92,6 +93,9 @@ static NMState nm_get_network_state(void
 static NMState nm_get_network_state(void);
 #endif
 
+/* Cached IP address for STUN server */
+static gchar *stun_ip = NULL;
+
 const unsigned char *
 purple_network_ip_atoi(const char *ip)
 {
@@ -685,6 +689,12 @@ nm_update_state(NMState state)
 		case NM_STATE_CONNECTED:
 			/* Call res_init in case DNS servers have changed */
 			res_init();
+			/* update STUN IP in case we it changed (theoretically we could
+			   have gone from IPv4 to IPv6, f.ex. or we were previously
+			   offline */
+			purple_network_set_stun_server(
+				purple_prefs_get_string("/purple/network/stun_server"));
+			
 			if (ui_ops != NULL && ui_ops->network_connected != NULL)
 				ui_ops->network_connected();
 			break;
@@ -746,6 +756,61 @@ nm_dbus_name_owner_changed_cb(DBusGProxy
 
 #endif
 
+static void
+purple_network_stun_lookup_cb(GSList *hosts, gpointer data, 
+	const char *error_message)
+{
+	if (error_message) {
+		purple_debug_error("network", "lookup of STUN server IP failed: %s\n",
+			error_message);
+		g_slist_free(hosts);
+		return;
+	}
+	
+	if (hosts && g_slist_next(hosts)) {
+		size_t addr_len = (size_t) hosts->data;
+		struct sockaddr *addr = g_slist_next(hosts)->data; 
+		char dst[INET6_ADDRSTRLEN];
+		
+		if (addr->sa_family == AF_INET6) {
+			inet_ntop(addr->sa_family, &((struct sockaddr_in6 *) addr)->sin6_addr, 
+				dst, sizeof(dst));
+		} else {
+			inet_ntop(addr->sa_family, &((struct sockaddr_in *) addr)->sin_addr, 
+				dst, sizeof(dst));
+		}
+			
+		stun_ip = g_strdup(dst);
+		purple_debug_info("network", "set STUN IP: %s\n", stun_ip);
+	}
+	
+	g_slist_free(hosts);
+}
+
+void
+purple_network_set_stun_server(const gchar *stun_server)
+{
+	if (stun_server && stun_server[0] != '\0') {
+		if (purple_network_is_available()) {
+			purple_debug_info("network", "running DNS query for STUN server\n");
+			purple_dnsquery_a(stun_server, 3478, purple_network_stun_lookup_cb,
+				NULL);
+		} else {
+			purple_debug_info("network", 
+				"network is unavailable, don't try to update STUN IP");
+		}
+	} else if (stun_ip) {
+		g_free(stun_ip);
+		stun_ip = NULL;
+	}
+}
+
+const gchar *
+purple_network_get_stun_ip(void)
+{
+	return stun_ip;
+}
+
 void *
 purple_network_get_handle(void)
 {
@@ -777,6 +842,7 @@ purple_network_init(void)
 #endif
 
 	purple_prefs_add_none  ("/purple/network");
+	purple_prefs_add_string("/purple/network/stun_server", "");
 	purple_prefs_add_bool  ("/purple/network/auto_ip", TRUE);
 	purple_prefs_add_string("/purple/network/public_ip", "");
 	purple_prefs_add_bool  ("/purple/network/map_ports", TRUE);
@@ -815,6 +881,9 @@ purple_network_init(void)
 
 	purple_pmp_init();
 	purple_upnp_init();
+	
+	purple_network_set_stun_server(
+		purple_prefs_get_string("/purple/network/stun_server"));
 }
 
 void
@@ -854,4 +923,7 @@ purple_network_uninit(void)
 #endif
 	purple_signal_unregister(purple_network_get_handle(),
 	                         "network-configuration-changed");
+	
+	if (stun_ip)
+		g_free(stun_ip);
 }
============================================================
--- libpurple/network.h	c8d21a12c3c21bafcc6cc5f070fc9f39847b9bb0
+++ libpurple/network.h	1946d83757ae2f4c622fef1482b5901f924b5971
@@ -214,7 +214,22 @@ void *purple_network_get_handle(void);
  */
 void *purple_network_get_handle(void);
 
+/**	
+ * Update the STUN server name
+ * Will result in a DNS query being executed asynchronous
+ * 
+ * @param stun_server The host name of the STUN server to set
+ */
+void purple_network_set_stun_server(const gchar *stun_server);
+	
 /**
+ * Get the IP address of the STUN server as a string representation
+ *
+ * @return the IP address
+ */
+const gchar *purple_network_get_stun_ip(void);
+	
+/**
  * Initializes the network subsystem.
  */
 void purple_network_init(void);
============================================================
--- pidgin/gtkprefs.c	7a128c9282757c569a277232a06b6f107f539e99
+++ pidgin/gtkprefs.c	1a6dd0515df8adabd4578ce5c7260b93ff4fa9ac
@@ -1142,6 +1142,17 @@ static void network_ip_changed(GtkEntry 
 	purple_network_set_public_ip(gtk_entry_get_text(entry));
 }
 
+static gboolean network_stun_server_changed_cb(GtkWidget *widget, 
+	GdkEventFocus *event, gpointer data)
+{
+	GtkEntry *entry = GTK_ENTRY(widget);
+	purple_prefs_set_string("/purple/network/stun_server",
+		gtk_entry_get_text(entry));
+	purple_network_set_stun_server(gtk_entry_get_text(entry));
+	
+	return FALSE;
+}
+
 static void
 proxy_changed_cb(const char *name, PurplePrefType type,
 				 gconstpointer value, gpointer data)
@@ -1206,10 +1217,27 @@ network_page(void)
 	gtk_container_set_border_width (GTK_CONTAINER (ret), PIDGIN_HIG_BORDER);
 
 	vbox = pidgin_make_frame (ret, _("IP Address"));
+	
+	table = gtk_table_new(2, 2, FALSE);
+	gtk_container_set_border_width(GTK_CONTAINER(table), 0);
+	gtk_table_set_col_spacings(GTK_TABLE(table), 5);
+	gtk_table_set_row_spacings(GTK_TABLE(table), 10);
+	gtk_container_add(GTK_CONTAINER(vbox), table);
+
 	sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
-	pidgin_prefs_labeled_entry(vbox,_("ST_UN server:"),
-			"/purple/network/stun_server", sg);
+	label = gtk_label_new_with_mnemonic(_("ST_UN server:"));
+	gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
+	gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
+	gtk_size_group_add_widget(sg, label);
 
+	entry = gtk_entry_new();
+	gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
+	gtk_table_attach(GTK_TABLE(table), entry, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
+	g_signal_connect(G_OBJECT(entry), "focus-out-event",
+					 G_CALLBACK(network_stun_server_changed_cb), NULL);
+	gtk_entry_set_text(GTK_ENTRY(entry), 
+		purple_prefs_get_string("/purple/network/stun_server"));
+			
 	hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
 	gtk_container_add(GTK_CONTAINER(vbox), hbox);
 


More information about the Commits mailing list