Since r8305, Unix PuTTY has always "upgraded" an X11 display like "localhost:0"
[sgt/putty] / unix / uxnet.c
index 46c8749..4467d08 100644 (file)
@@ -301,7 +301,9 @@ static int sk_nextaddr(SockAddr addr, SockAddrStep *step)
 
 void sk_getaddr(SockAddr addr, char *buf, int buflen)
 {
-
+    /* XXX not clear what we should return for Unix-domain sockets; let's
+     * hope the question never arises */
+    assert(addr->superfamily != UNIX);
     if (addr->superfamily == UNRESOLVED) {
        strncpy(buf, addr->hostname, buflen);
        buf[buflen-1] = '\0';
@@ -326,7 +328,9 @@ void sk_getaddr(SockAddr addr, char *buf, int buflen)
 
 int sk_hostname_is_local(char *name)
 {
-    return !strcmp(name, "localhost");
+    return !strcmp(name, "localhost") ||
+          !strcmp(name, "::1") ||
+          !strncmp(name, "127.", 4);
 }
 
 #define ipv4_is_loopback(addr) \
@@ -357,9 +361,10 @@ static int sockaddr_is_loopback(struct sockaddr *sa)
 
 int sk_address_is_local(SockAddr addr)
 {
-
     if (addr->superfamily == UNRESOLVED)
        return 0;                      /* we don't know; assume not */
+    else if (addr->superfamily == UNIX)
+       return 1;
     else {
 #ifndef NO_IPV6
        return sockaddr_is_loopback(addr->ais->ai_addr);
@@ -1390,8 +1395,24 @@ int net_service_lookup(char *service)
        return 0;
 }
 
-SockAddr platform_get_x11_unix_address(const char *display, int displaynum,
-                                      char **canonicalname)
+char *get_hostname(void)
+{
+    int len = 128;
+    char *hostname = NULL;
+    do {
+       len *= 2;
+       hostname = sresize(hostname, len, char);
+       if ((gethostname(hostname, len) < 0) &&
+           (errno != ENAMETOOLONG)) {
+           sfree(hostname);
+           hostname = NULL;
+           break;
+       }
+    } while (strlen(hostname) >= len-1);
+    return hostname;
+}
+
+SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum)
 {
     SockAddr ret = snew(struct SockAddr_tag);
     int n;
@@ -1399,26 +1420,22 @@ SockAddr platform_get_x11_unix_address(const char *display, int displaynum,
     memset(ret, 0, sizeof *ret);
     ret->superfamily = UNIX;
     /*
-     * Mac OS X Leopard uses an innovative X display naming
-     * convention in which the entire display name is the path to
-     * the Unix socket, including the trailing :0 which only
-     * _looks_ like a display number. Heuristically, I think
-     * detecting this by means of a leading slash ought to be
-     * adequate.
+     * In special circumstances (notably Mac OS X Leopard), we'll
+     * have been passed an explicit Unix socket path.
      */
-    if (display[0] == '/') {
+    if (sockpath) {
        n = snprintf(ret->hostname, sizeof ret->hostname,
-                    "%s", display);
+                    "%s", sockpath);
     } else {
        n = snprintf(ret->hostname, sizeof ret->hostname,
                     "%s%d", X11_UNIX_PATH, displaynum);
     }
-    if(n < 0)
+
+    if (n < 0)
        ret->error = "snprintf failed";
-    else if(n >= sizeof ret->hostname)
+    else if (n >= sizeof ret->hostname)
        ret->error = "X11 UNIX name too long";
-    else
-       *canonicalname = dupstr(ret->hostname);
+
 #ifndef NO_IPV6
     ret->ais = NULL;
 #else