Set FD_CLOEXEC in a little convenience function that does the right thing
[u/mdw/putty] / unix / uxnet.c
index 46959e2..e1dfce3 100644 (file)
 #include "network.h"
 #include "tree234.h"
 
+/* Solaris needs <sys/sockio.h> for SIOCATMARK. */
+#ifndef SIOCATMARK
+#include <sys/sockio.h>
+#endif
+
 #ifndef X11_UNIX_PATH
 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
 #endif
@@ -231,7 +236,7 @@ SockAddr sk_nonamelookup(const char *host)
 static int sk_nextaddr(SockAddr addr)
 {
 #ifndef NO_IPV6
-    if (addr->ai->ai_next) {
+    if (addr->ai && addr->ai->ai_next) {
        addr->ai = addr->ai->ai_next;
        addr->family = addr->ai->ai_family;
        return TRUE;
@@ -465,6 +470,8 @@ static int try_connect(Actual_Socket sock)
        goto ret;
     }
 
+    cloexec(s);
+
     if (sock->oobinline) {
        int b = TRUE;
        setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
@@ -576,6 +583,7 @@ static int try_connect(Actual_Socket sock)
 
       default:
        assert(0 && "unknown address family");
+       exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
     }
 
     fl = fcntl(s, F_GETFL);
@@ -685,7 +693,10 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, i
      * into local reality.
      */
     address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
-                     address_family == ADDRTYPE_IPV6 ? AF_INET6 : AF_UNSPEC);
+#ifndef NO_IPV6
+                     address_family == ADDRTYPE_IPV6 ? AF_INET6 :
+#endif
+                     AF_UNSPEC);
 
 #ifndef NO_IPV6
     /* Let's default to IPv6.
@@ -701,17 +712,21 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, i
      */
     s = socket(address_family, SOCK_STREAM, 0);
 
+#ifndef NO_IPV6
     /* If the host doesn't support IPv6 try fallback to IPv4. */
     if (s < 0 && address_family == AF_INET6) {
        address_family = AF_INET;
        s = socket(address_family, SOCK_STREAM, 0);
     }
+#endif
 
     if (s < 0) {
        ret->error = strerror(errno);
        return (Socket) ret;
     }
 
+    cloexec(s);
+
     ret->oobinline = 0;
 
     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
@@ -1271,9 +1286,10 @@ SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname)
     else
        *canonicalname = dupstr(ret->hostname);
 #ifndef NO_IPV6
-    ret->ais = NULL;
+    ret->ai = ret->ais = NULL;
 #else
     ret->addresses = NULL;
+    ret->curraddr = ret->naddresses = 0;
 #endif
     return ret;
 }