[PATCH] gnome-dns bug fix



The patch should correct the following: 

* failed host lookup & cache

  Currently, gnome_dns caches every ip addr, even the 0.0.0.0 that means
  "lookup failed". The patched gnome_dns returns 0.0.0.0 via the callback
  but retries the (async) lookup with gethostbyname if gnome_dns_lookup is
  called again.

* callback called too many times

  Every gnome_dns_lookup causes another gdk_input_add(...,callback,...)
  and gdk_input_remove is never called. 

These bugs aren't in the bug database AFAIK.
Although I think the patch is correct, it's a good idea to check it.

P.S.
I know that gnome_dns is deprecated, but it would be nice to keep it in
gnome-libs until a better replacement is available.

Thank you for your time,
Zun.
--- gnome/gnome-libs/libgnomeui/gnome-dns.c	Thu Sep  9 14:37:46 1999
+++ gnome/gnome-libs-socket/libgnomeui/gnome-dns.c	Sun Sep 12 01:09:25 1999
@@ -50,4 +50,6 @@
 	/* pipefd's to communicate to server with */
 	gint pipefd[2];
+	/* tag for gdk_input_add */
+	gint input_tag;
 } DnsServer;
 
@@ -128,8 +130,9 @@
 gnome_dns_server_req (gint server, const char *hostname) {
 	dns_server[server].in_use = TRUE;
-	gdk_input_add(dns_server[server].pipefd[0],
-		      GDK_INPUT_READ,
-		      (GdkInputFunction) gnome_dns_callback,
-		      GINT_TO_POINTER (server));
+	dns_server[server].input_tag = gdk_input_add(
+			dns_server[server].pipefd[0],
+			GDK_INPUT_READ,
+			(GdkInputFunction) gnome_dns_callback,
+			GINT_TO_POINTER (server));
 	write (dns_server[server].pipefd[1], hostname, strlen (hostname) + 1);
 }
@@ -169,6 +172,7 @@
 			break;
 	
-	/* if it hit, call the callback immediately. */
-	if (i < dns_cache_size && dns_cache[i].server == -1) {
+	/* if it hit a non null answer, call the callback immediately. */
+	if (i < dns_cache_size && dns_cache[i].server == -1 &&
+	    dns_cache[i].ip_addr != 0) {
 		callback (dns_cache[i].ip_addr, callback_data);
 		return 0;
@@ -177,14 +181,17 @@
 	/* It didn't hit in the cache with an answer - need to put request
 	 into the dns_con table. */
-	if (i < dns_cache_size) {
+	if (i < dns_cache_size && dns_cache[i].server != -1) {
 		/* hit in cache but answer hasn't come back yet. */
 		server = dns_cache[i].server;
 	} else {
-		/* missed in cache -- create a cache entry */
-		if (dns_cache_size == dns_cache_size_max) {
-			dns_cache_size_max <<= 1;
-			dns_cache = g_realloc (dns_cache, dns_cache_size_max * sizeof (GnomeDnsCache));
+		if (i == dns_cache_size) {
+			/* missed in cache -- create a cache entry */
+			if (dns_cache_size == dns_cache_size_max) {
+				dns_cache_size_max <<= 1;
+				dns_cache = g_realloc (dns_cache, dns_cache_size_max * sizeof (GnomeDnsCache));
+			}
+			dns_cache[i].hostname = g_strdup (hostname);
+			dns_cache_size++;
 		}
-		dns_cache[dns_cache_size].hostname = g_strdup (hostname);
 		/* Find a server we can send the request to. */
 		for (server = 0; server < num_servers; server++)
@@ -193,14 +200,12 @@
 			}
 		if (server < num_servers) {
-			dns_cache[dns_cache_size].server = server;
+			dns_cache[i].server = server;
 		} else {
 			/* no unused servers - fork a new one */
-			server = dns_cache[dns_cache_size].server = gnome_dns_create_server();
-			if (dns_cache[dns_cache_size].server < 0) {
+			server = dns_cache[i].server = gnome_dns_create_server();
+			if (dns_cache[i].server < 0) {
 				g_error ("Unable to fork: %s", g_strerror(errno));
 			}
-			
 		}
-		dns_cache_size++;
 
 		/* found the server - give it the request */
@@ -240,4 +245,7 @@
 	for (i = 0; i < dns_con_size; i++) {
 		if (dns_con[i].tag == tag) {
+			if (dns_con[i].server >= 0) {
+				gdk_input_remove(dns_server[dns_con[i].server].input_tag);
+			}
 			g_free (dns_con[i].hostname);
 			dns_con[i] = dns_con[--dns_con_size];
@@ -292,4 +300,8 @@
 	g_print("ip_addr in callback is %x\n", ip_addr);
 #endif
+	
+	/* Don't call this function anymore 
+	 * (unless gnome_dns_server_request is called again) */
+	gdk_input_remove(dns_server[server_num].input_tag);
 	
 	/* write ip address into cache. */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]