e38f15
From 12c53f98e44598b87d3f2308e0d892f49d7af8e4 Mon Sep 17 00:00:00 2001
e38f15
From: Roger Wolff <R.E.Wolff@BitWizard.nl>
e38f15
Date: Tue, 16 Jul 2013 13:59:52 +0200
e38f15
Subject: [PATCH] Fix bombout with ipv6 enabled binary on ipv4 only system.
e38f15
e38f15
---
e38f15
 dns.c    | 33 +++++++++++++++++++++------------
e38f15
 gtk.c    |  6 ++++--
e38f15
 select.c |  8 ++++++--
e38f15
 3 files changed, 31 insertions(+), 16 deletions(-)
e38f15
e38f15
diff --git a/dns.c b/dns.c
e38f15
index 371934f..221665d 100644
e38f15
--- a/dns.c
e38f15
+++ b/dns.c
e38f15
@@ -529,10 +529,12 @@ void dns_open(void)
e38f15
 #ifdef ENABLE_IPV6
e38f15
   resfd6 = socket(AF_INET6, SOCK_DGRAM, 0);
e38f15
   if (resfd6 == -1) {
e38f15
+    // consider making removing this warning. For now leave it in to see 
e38f15
+    // new code activated. -- REW
e38f15
     fprintf(stderr,
e38f15
             "Unable to allocate IPv6 socket for nameserver communication: %s\n",
e38f15
 	    strerror(errno));
e38f15
-    exit(-1);
e38f15
+    //    exit(-1);
e38f15
   }
e38f15
 #endif
e38f15
   option = 1;
e38f15
@@ -543,11 +545,13 @@ void dns_open(void)
e38f15
     exit(-1);
e38f15
   }
e38f15
 #ifdef ENABLE_IPV6
e38f15
-  if (setsockopt(resfd6,SOL_SOCKET,SO_BROADCAST,(char *)&option,sizeof(option))) {
e38f15
-    fprintf(stderr,
e38f15
-            "Unable to setsockopt() on IPv6 nameserver communication socket: %s\n",
e38f15
-	    strerror(errno));
e38f15
-    exit(-1);
e38f15
+  if (resfd6 > 0) {
e38f15
+    if (setsockopt(resfd6,SOL_SOCKET,SO_BROADCAST,(char *)&option,sizeof(option))) {
e38f15
+      fprintf(stderr,
e38f15
+	      "Unable to setsockopt() on IPv6 nameserver communication socket: %s\n",
e38f15
+	      strerror(errno));
e38f15
+      exit(-1);
e38f15
+    }
e38f15
   }
e38f15
 #endif
e38f15
   longipstr( "127.0.0.1", &localhost, AF_INET );
e38f15
@@ -933,12 +937,14 @@ void dorequest(char *s,int type,word id)
e38f15
   hp = (packetheader *)buf;
e38f15
   hp->id = id;	/* htons() deliberately left out (redundant) */
e38f15
 #ifdef ENABLE_IPV6
e38f15
-  for (i = 0;i < NSCOUNT6;i++) {
e38f15
-    if (!NSSOCKADDR6(i))
e38f15
-      continue;
e38f15
-    if (NSSOCKADDR6(i)->sin6_family == AF_INET6)
e38f15
-      (void)sendto(resfd6,buf,r,0,(struct sockaddr *) NSSOCKADDR6(i),
e38f15
-		   sizeof(struct sockaddr_in6));
e38f15
+  if (resfd6 > 0) {
e38f15
+    for (i = 0;i < NSCOUNT6;i++) {
e38f15
+      if (!NSSOCKADDR6(i))
e38f15
+	continue;
e38f15
+      if (NSSOCKADDR6(i)->sin6_family == AF_INET6)
e38f15
+	(void)sendto(resfd6,buf,r,0,(struct sockaddr *) NSSOCKADDR6(i),
e38f15
+		     sizeof(struct sockaddr_in6));
e38f15
+    }
e38f15
   }
e38f15
 #endif
e38f15
   for (i = 0;i < myres.nscount;i++)
e38f15
@@ -1327,6 +1333,9 @@ void dns_ack6(void)
e38f15
   int r,i;
e38f15
   static char addrstr[INET6_ADDRSTRLEN];
e38f15
 
e38f15
+  // Probably not necessary. -- REW
e38f15
+  if (resfd6 < 0) return; 
e38f15
+
e38f15
   r = recvfrom(resfd6,(byte *)resrecvbuf,MaxPacketsize,0,
e38f15
                from, &fromlen);
e38f15
   if (r > 0) {
e38f15
diff --git a/gtk.c b/gtk.c
e38f15
index d00f769..38ed507 100644
e38f15
--- a/gtk.c
e38f15
+++ b/gtk.c
e38f15
@@ -615,8 +615,10 @@ void gtk_loop(void)
e38f15
   net_iochannel = g_io_channel_unix_new(net_waitfd());
e38f15
   g_io_add_watch(net_iochannel, G_IO_IN, gtk_net_data, NULL);
e38f15
 #ifdef ENABLE_IPV6
e38f15
-  dns_iochannel = g_io_channel_unix_new(dns_waitfd6());
e38f15
-  g_io_add_watch(dns_iochannel, G_IO_IN, gtk_dns_data6, NULL);
e38f15
+  if (dns_waitfd6() > 0) {
e38f15
+    dns_iochannel = g_io_channel_unix_new(dns_waitfd6());
e38f15
+    g_io_add_watch(dns_iochannel, G_IO_IN, gtk_dns_data6, NULL);
e38f15
+  }
e38f15
 #endif
e38f15
   dns_iochannel = g_io_channel_unix_new(dns_waitfd());
e38f15
   g_io_add_watch(dns_iochannel, G_IO_IN, gtk_dns_data, NULL);
e38f15
diff --git a/select.c b/select.c
e38f15
index 0545d9f..e7c397e 100644
e38f15
--- a/select.c
e38f15
+++ b/select.c
e38f15
@@ -80,8 +80,12 @@ void select_loop(void) {
e38f15
 #ifdef ENABLE_IPV6
e38f15
     if (dns) {
e38f15
       dnsfd6 = dns_waitfd6();
e38f15
-      FD_SET(dnsfd6, &readfd);
e38f15
-      if(dnsfd6 >= maxfd) maxfd = dnsfd6 + 1;
e38f15
+      if (dnsfd6 >= 0) {
e38f15
+        FD_SET(dnsfd6, &readfd);
e38f15
+        if(dnsfd6 >= maxfd) maxfd = dnsfd6 + 1;
e38f15
+      } else {
e38f15
+        dnsfd6 = 0;
e38f15
+      }
e38f15
     } else
e38f15
       dnsfd6 = 0;
e38f15
 #endif
e38f15
-- 
e38f15
1.8.3.1
e38f15