changeset 31479:0eceb1304f53

Add new DNS-related API to perform lookups in the context of an account. Combined with the new "Tor/Privacy" proxy setting, this allows us to prevent DNS lookups when the user has selected a proxy that they may want to use to for privacy. Refs #11110
author Daniel Atallah <daniel.atallah@gmail.com>
date Tue, 19 Apr 2011 04:58:17 +0000
parents 9a7c5d242521
children c6193e7def81
files libpurple/dnsquery.c libpurple/dnsquery.h libpurple/dnssrv.c libpurple/dnssrv.h libpurple/proxy.c libpurple/proxy.h
diffstat 6 files changed, 164 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnsquery.c	Tue Apr 19 04:41:59 2011 +0000
+++ b/libpurple/dnsquery.c	Tue Apr 19 04:58:17 2011 +0000
@@ -54,6 +54,7 @@
 	PurpleDnsQueryConnectFunction callback;
 	gpointer data;
 	guint timeout;
+	PurpleAccount *account;
 
 #if defined(PURPLE_DNSQUERY_USE_FORK)
 	PurpleDnsQueryResolverProcess *resolver;
@@ -861,6 +862,7 @@
 initiate_resolving(gpointer data)
 {
 	PurpleDnsQueryData *query_data;
+	PurpleProxyType proxy_type;
 
 	query_data = data;
 	query_data->timeout = 0;
@@ -869,6 +871,14 @@
 		/* resolve_ip calls purple_dnsquery_resolved */
 		return FALSE;
 
+	proxy_type = purple_proxy_info_get_type(
+		purple_proxy_get_setup(query_data->account));
+	if (proxy_type == PURPLE_PROXY_TOR) {
+		purple_dnsquery_failed(query_data,
+			_("Aborting DNS lookup in Tor Proxy mode."));
+		return FALSE;
+	}
+
 	if (purple_dnsquery_ui_resolve(query_data))
 		/* The UI is handling the resolve; we're done */
 		return FALSE;
@@ -878,9 +888,8 @@
 	return FALSE;
 }
 
-
 PurpleDnsQueryData *
-purple_dnsquery_a(const char *hostname, int port,
+purple_dnsquery_a_account(PurpleAccount *account, const char *hostname, int port,
 				PurpleDnsQueryConnectFunction callback, gpointer data)
 {
 	PurpleDnsQueryData *query_data;
@@ -897,6 +906,7 @@
 	query_data->port = port;
 	query_data->callback = callback;
 	query_data->data = data;
+	query_data->account = account;
 
 	if (*query_data->hostname == '\0')
 	{
@@ -909,6 +919,12 @@
 	return query_data;
 }
 
+PurpleDnsQueryData *
+purple_dnsquery_a(const char *hostname, int port,
+				PurpleDnsQueryConnectFunction callback, gpointer data)
+{
+	return purple_dnsquery_a_account(NULL, hostname, port, callback, data);
+}
 
 void
 purple_dnsquery_destroy(PurpleDnsQueryData *query_data)
--- a/libpurple/dnsquery.h	Tue Apr 19 04:41:59 2011 +0000
+++ b/libpurple/dnsquery.h	Tue Apr 19 04:58:17 2011 +0000
@@ -88,6 +88,7 @@
 /**
  * Perform an asynchronous DNS query.
  *
+ * @param account the account that the query is being done for (or NULL)
  * @param hostname The hostname to resolve.
  * @param port     A port number which is stored in the struct sockaddr.
  * @param callback The callback function to call after resolving.
@@ -96,8 +97,28 @@
  * @return NULL if there was an error, otherwise return a reference to
  *         a data structure that can be used to cancel the pending
  *         DNS query, if needed.
+ *
+ * @since 2.8.0
+ */
+PurpleDnsQueryData *purple_dnsquery_a_account(PurpleAccount *account, const char *hostname, int port, PurpleDnsQueryConnectFunction callback, gpointer data);
+
+#ifndef PURPLE_DISABLE_DEPRECATED
+/**
+ * Perform an asynchronous DNS query.
+ *
+ * @param hostname The hostname to resolve.
+ * @param port     A port number which is stored in the struct sockaddr.
+ * @param callback The callback function to call after resolving.
+ * @param data     Extra data to pass to the callback function.
+ *
+ * @return NULL if there was an error, otherwise return a reference to
+ *         a data structure that can be used to cancel the pending
+ *         DNS query, if needed.
+ *
+ * @deprecated Use purple_dnsquery_a_account instead
  */
 PurpleDnsQueryData *purple_dnsquery_a(const char *hostname, int port, PurpleDnsQueryConnectFunction callback, gpointer data);
+#endif
 
 /**
  * Cancel a DNS query and destroy the associated data structure.
--- a/libpurple/dnssrv.c	Tue Apr 19 04:41:59 2011 +0000
+++ b/libpurple/dnssrv.c	Tue Apr 19 04:58:17 2011 +0000
@@ -674,11 +674,22 @@
 #endif
 
 PurpleSrvTxtQueryData *
-purple_srv_resolve(const char *protocol, const char *transport, const char *domain, PurpleSrvCallback cb, gpointer extradata)
+purple_srv_resolve(const char *protocol, const char *transport,
+	const char *domain, PurpleSrvCallback cb, gpointer extradata)
+{
+	return purple_srv_resolve_account(NULL, protocol, transport, domain,
+			cb, extradata);
+}
+
+PurpleSrvTxtQueryData *
+purple_srv_resolve_account(PurpleAccount *account, const char *protocol,
+	const char *transport, const char *domain, PurpleSrvCallback cb,
+	gpointer extradata)
 {
 	char *query;
 	char *hostname;
 	PurpleSrvTxtQueryData *query_data;
+	PurpleProxyType proxy_type;
 #ifndef _WIN32
 	PurpleSrvInternalQuery internal_query;
 	int in[2], out[2];
@@ -694,6 +705,14 @@
 		g_return_val_if_reached(NULL);
 	}
 
+	proxy_type = purple_proxy_info_get_type(
+		purple_proxy_get_setup(account));
+	if (proxy_type == PURPLE_PROXY_TOR) {
+		purple_debug_info("dnssrv", "Aborting SRV lookup in Tor Proxy mode.");
+		cb(NULL, 0, extradata);
+		return NULL;
+	}
+
 #ifdef USE_IDN
 	if (!dns_str_is_ascii(domain)) {
 		int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
@@ -795,11 +814,20 @@
 #endif
 }
 
-PurpleSrvTxtQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata)
+PurpleSrvTxtQueryData *purple_txt_resolve(const char *owner,
+	const char *domain, PurpleTxtCallback cb, gpointer extradata)
+{
+	return purple_txt_resolve_account(NULL, owner, domain, cb, extradata);
+}
+
+PurpleSrvTxtQueryData *purple_txt_resolve_account(PurpleAccount *account,
+	const char *owner, const char *domain, PurpleTxtCallback cb,
+	gpointer extradata)
 {
 	char *query;
 	char *hostname;
 	PurpleSrvTxtQueryData *query_data;
+	PurpleProxyType proxy_type;
 #ifndef _WIN32
 	PurpleSrvInternalQuery internal_query;
 	int in[2], out[2];
@@ -809,6 +837,14 @@
 	static gboolean initialized = FALSE;
 #endif
 
+	proxy_type = purple_proxy_info_get_type(
+		purple_proxy_get_setup(account));
+	if (proxy_type == PURPLE_PROXY_TOR) {
+		purple_debug_info("dnssrv", "Aborting TXT lookup in Tor Proxy mode.");
+		cb(NULL, extradata);
+		return NULL;
+	}
+
 #ifdef USE_IDN
 	if (!dns_str_is_ascii(domain)) {
 		int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
--- a/libpurple/dnssrv.h	Tue Apr 19 04:41:59 2011 +0000
+++ b/libpurple/dnssrv.h	Tue Apr 19 04:58:17 2011 +0000
@@ -103,13 +103,31 @@
 /**
  * Queries an SRV record.
  *
+ * @param account the account that the query is being done for (or NULL)
  * @param protocol Name of the protocol (e.g. "sip")
  * @param transport Name of the transport ("tcp" or "udp")
  * @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.8.0
+ */
+PurpleSrvTxtQueryData *purple_srv_resolve_account(PurpleAccount *account, const char *protocol, const char *transport, const char *domain, PurpleSrvCallback cb, gpointer extradata);
+
+#ifndef PURPLE_DISABLE_DEPRECATED
+/**
+ * Queries an SRV record.
+ *
+ * @param protocol Name of the protocol (e.g. "sip")
+ * @param transport Name of the transport ("tcp" or "udp")
+ * @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
+ *
+ * @deprecated Use purple_srv_resolve_account instead
  */
 PurpleSrvTxtQueryData *purple_srv_resolve(const char *protocol, const char *transport, const char *domain, PurpleSrvCallback cb, gpointer extradata);
+#endif
 
 /**
  * Cancel an SRV or DNS query.
@@ -121,14 +139,31 @@
 /**
  * Queries an TXT record.
  *
+ * @param account the account that the query is being done for (or NULL)
  * @param owner Name of the protocol (e.g. "_xmppconnect")
  * @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.8.0
+ */
+PurpleSrvTxtQueryData *purple_txt_resolve_account(PurpleAccount *account, const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata);
+
+#ifndef PURPLE_DISABLE_DEPRECATED
+/**
+ * Queries an TXT record.
+ *
+ * @param owner Name of the protocol (e.g. "_xmppconnect")
+ * @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
+ *
+ * @deprecated Use purple_txt_resolve_account instead
+ *
  * @since 2.6.0
  */
 PurpleSrvTxtQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata);
+#endif
 
 /**
  * Cancel an TXT DNS query.
--- a/libpurple/proxy.c	Tue Apr 19 04:41:59 2011 +0000
+++ b/libpurple/proxy.c	Tue Apr 19 04:58:17 2011 +0000
@@ -69,6 +69,7 @@
 	guchar *read_buffer;
 	gsize read_buf_len;
 	gsize read_len;
+	PurpleAccount *account;
 };
 
 static const char * const socks5errors[] = {
@@ -1367,7 +1368,8 @@
 
 		proxy_do_write(connect_data, connect_data->fd, PURPLE_INPUT_WRITE);
 	} else {
-		connect_data->query_data = purple_dnsquery_a(connect_data->host,
+		connect_data->query_data = purple_dnsquery_a_account(
+				connect_data->account, connect_data->host,
 				connect_data->port, s4_host_resolved, connect_data);
 
 		if (connect_data->query_data == NULL) {
@@ -2282,6 +2284,7 @@
 	connect_data->host = g_strdup(host);
 	connect_data->port = port;
 	connect_data->gpi = purple_proxy_get_setup(account);
+	connect_data->account = account;
 
 	if ((purple_proxy_info_get_type(connect_data->gpi) != PURPLE_PROXY_NONE) &&
 		(purple_proxy_info_get_host(connect_data->gpi) == NULL ||
@@ -2313,7 +2316,7 @@
 			return NULL;
 	}
 
-	connect_data->query_data = purple_dnsquery_a(connecthost,
+	connect_data->query_data = purple_dnsquery_a_account(account, connecthost,
 			connectport, connection_host_resolved, connect_data);
 	if (connect_data->query_data == NULL)
 	{
@@ -2349,6 +2352,7 @@
 	connect_data->host = g_strdup(host);
 	connect_data->port = port;
 	connect_data->gpi = purple_proxy_get_setup(account);
+	connect_data->account = account;
 
 	if ((purple_proxy_info_get_type(connect_data->gpi) != PURPLE_PROXY_NONE) &&
 		(purple_proxy_info_get_host(connect_data->gpi) == NULL ||
@@ -2380,7 +2384,7 @@
 			return NULL;
 	}
 
-	connect_data->query_data = purple_dnsquery_a(connecthost,
+	connect_data->query_data = purple_dnsquery_a_account(account, connecthost,
 			connectport, connection_host_resolved, connect_data);
 	if (connect_data->query_data == NULL)
 	{
@@ -2402,6 +2406,19 @@
 						  PurpleProxyConnectFunction connect_cb,
 						  gpointer data)
 {
+	return purple_proxy_connect_socks5_account(NULL, handle, gpi,
+						  host, port, connect_cb, data);
+}
+/*
+ * Combine some of this code with purple_proxy_connect()
+ */
+PurpleProxyConnectData *
+purple_proxy_connect_socks5_account(void *handle, PurpleAccount *account,
+						  PurpleProxyInfo *gpi,
+						  const char *host, int port,
+						  PurpleProxyConnectFunction connect_cb,
+						  gpointer data)
+{
 	PurpleProxyConnectData *connect_data;
 
 	g_return_val_if_fail(host       != NULL, NULL);
@@ -2417,9 +2434,11 @@
 	connect_data->host = g_strdup(host);
 	connect_data->port = port;
 	connect_data->gpi = gpi;
+	connect_data->account = account;
 
 	connect_data->query_data =
-			purple_dnsquery_a(purple_proxy_info_get_host(gpi),
+			purple_dnsquery_a_account(account,
+					purple_proxy_info_get_host(gpi),
 					purple_proxy_info_get_port(gpi),
 					connection_host_resolved, connect_data);
 	if (connect_data->query_data == NULL)
--- a/libpurple/proxy.h	Tue Apr 19 04:41:59 2011 +0000
+++ b/libpurple/proxy.h	Tue Apr 19 04:58:17 2011 +0000
@@ -307,10 +307,39 @@
  *         opaque data structure that can be used to cancel
  *         the pending connection, if needed.
  */
+PurpleProxyConnectData *purple_proxy_connect_socks5_account(void *handle,
+			PurpleAccount *account, PurpleProxyInfo *gpi,
+			const char *host, int port,
+			PurpleProxyConnectFunction connect_cb, gpointer data);
+
+#ifndef PURPLE_DISABLE_DEPRECATED
+/**
+ * Makes a connection through a SOCKS5 proxy.
+ *
+ * @param handle     A handle that should be associated with this
+ *                   connection attempt.  The handle can be used
+ *                   to cancel the connection attempt using the
+ *                   purple_proxy_connect_cancel_with_handle()
+ *                   function.
+ * @param gpi        The PurpleProxyInfo specifying the proxy settings
+ * @param host       The destination host.
+ * @param port       The destination port.
+ * @param connect_cb The function to call when the connection is
+ *                   established.  If the connection failed then
+ *                   fd will be -1 and error message will be set
+ *                   to something descriptive (hopefully).
+ * @param data       User-defined data.
+ *
+ * @return NULL if there was an error, or a reference to an
+ *         opaque data structure that can be used to cancel
+ *         the pending connection, if needed.
+ * @deprecated Use purple_proxy_connect_socks5_account instead
+ */
 PurpleProxyConnectData *purple_proxy_connect_socks5(void *handle,
 			PurpleProxyInfo *gpi,
 			const char *host, int port,
 			PurpleProxyConnectFunction connect_cb, gpointer data);
+#endif
 
 /**
  * Cancel an in-progress connection attempt.  This should be called