From 89ed84d15f3ff5eb17d693666a335e475e1acf68 Mon Sep 17 00:00:00 2001
From: Julien Cristau <jcristau@debian.org>
Date: Tue, 14 Feb 2012 20:32:03 +0100
Subject: [PATCH] Don't fall back to a local connection if $DISPLAY is remote

Commit f09c5299a381e2729e800a0ac43f1c0e371f65f6 added a fallback
to XOpenDisplay if the first call to _XConnectXCB failed.  The intention
was to copy the pre-xcb behaviour of falling back to TCP if the UNIX
socket connection failed.  However, the old code only did that if the
hostname part of $DISPLAY was empty, or if it matched the local host
name.  The new code didn't have such a check, so it ended up connecting
to a local X server even when DISPLAY specified a different host.  Fix
that, and only keep "tcp" as fallback protocol: xcb doesn't know about
"local", "unix" is what just failed, and "inet"/"inet6" are the same as
"tcp".

Debian bug#659558 <http://bugs.debian.org/659558>

Signed-off-by: Julien Cristau <jcristau@debian.org>
---
 src/OpenDis.c |   18 +++++++-----------
 1 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/src/OpenDis.c b/src/OpenDis.c
index e568a30..19e4cd6 100644
--- a/src/OpenDis.c
+++ b/src/OpenDis.c
@@ -127,20 +127,16 @@ XOpenDisplay (
  */
 
 	if(!_XConnectXCB(dpy, display, &iscreen)) {
-		/* Try falling back on other transports if no transport specified */
-		const char *slash = strrchr(display_name, '/');
-		if(slash == NULL) {
-			const char *protocols[] = {"local", "unix", "tcp", "inet6", "inet", NULL};
-			const char **s;
-			size_t buf_size = strlen(display_name) + 7; // max strlen + 2 (null + /)
+		/* Try falling back on TCP if no transport specified and
+		 * connecting to the local host */
+		if(display_name[0] == ':') {
+			size_t buf_size = strlen(display_name) + sizeof("tcp/localhost");
 			char *buf = Xmalloc(buf_size * sizeof(char));
 
 			if(buf) {
-				for(s = protocols; buf && *s; s++) {
-					snprintf(buf, buf_size, "%s/%s", *s, display_name);
-					if(_XConnectXCB(dpy, buf, &iscreen))
-						goto fallback_success;
-				}
+				snprintf(buf, buf_size, "tcp/localhost%s", display_name);
+				if(_XConnectXCB(dpy, buf, &iscreen))
+					goto fallback_success;
 				Xfree(buf);
 			}
 		}
-- 
1.7.9

