[patch] bind to specific interface in Xvnc

Tim Waugh twaugh "at" redhat.com
Wed, 08 Nov 2000 16:34:45 +0000


Here is a patch to add a parameter '-interface ipaddr' to Xvnc.  It
causes the RFB and HTTP sockets (but not the normal X one) to be bound
to the particular interface address.  It's against VNC 3.3.3r2.

Is this useful?

Tim.
*/

--- vnc_unixsrc/Xvnc/programs/Xserver/hw/vnc/init.c.iface	Wed Oct 25 20:12:30 2000
+++ vnc_unixsrc/Xvnc/programs/Xserver/hw/vnc/init.c	Wed Nov  8 16:02:33 2000
@@ -132,6 +132,8 @@
 int inetdSock = -1;
 static char inetdDisplayNumStr[10];
 
+/* Interface address to bind to. */
+in_addr_t interface;
 
 /*
  * ddxProcessArgument is our first entry point and will be called at the
@@ -155,6 +157,7 @@
 	rfbScreen.whitePixel = RFB_DEFAULT_WHITEPIXEL;
 	rfbScreen.pfbMemory = NULL;
 	gethostname(rfbThisHost, 255);
+	interface = htonl (INADDR_ANY);
         firstTime = FALSE;
     }
 
@@ -289,10 +292,42 @@
     }
 
     if (strcmp(argv[i], "-localhost") == 0) {
-	rfbLocalhostOnly = TRUE;
+	interface = htonl (INADDR_LOOPBACK);
 	return 1;
     }
 
+    if (strcmp(argv[i], "-interface") == 0) {	/* -interface ipaddr */
+	in_addr_t got;
+	unsigned long octet;
+	char *p, *end;
+	int q;
+	if (i + 1 >= argc) {
+		UseMsg();
+		return 2;
+	}
+	if (interface != htonl (INADDR_ANY)) {
+		/* Already set (-localhost?). */
+		return 2;
+	}
+	p = argv[i + 1];
+	for (q = 0; q < 4; q++) {
+		octet = strtoul (p, &end, 10);
+		if (p == end || octet > 255) {
+			UseMsg ();
+			return 2;
+		}
+		if (q < 3 && *end != '.' ||
+		    q == 3 && *end != '\0') {
+			UseMsg ();
+			return 2;
+		}
+		got = (got << 8) | octet;
+		p = end + 1;
+	}
+	interface = htonl (got);
+	return 2;
+    }
+
     if (strcmp(argv[i], "-inetd") == 0) {	/* -inetd */ 
 	int n;
 	for (n = 1; n < 100; n++) {
@@ -857,6 +892,7 @@
 	   "                       connection comes in (refuse new connection "
 								 "instead)\n");
     ErrorF("-localhost             only allow connections from localhost\n");
+    ErrorF("-interface ipaddr      only bind to specified interface address\n");
     ErrorF("-inetd                 Xvnc is launched by inetd\n");
     exit(1);
 }
--- vnc_unixsrc/Xvnc/programs/Xserver/hw/vnc/sockets.c.iface	Wed Oct 25 16:42:22 2000
+++ vnc_unixsrc/Xvnc/programs/Xserver/hw/vnc/sockets.c	Wed Nov  8 16:00:02 2000
@@ -55,7 +55,6 @@
 
 int rfbPort = 0;
 int rfbListenSock = -1;
-Bool rfbLocalhostOnly = FALSE;
 
 int udpPort = 0;
 int udpSock = -1;
@@ -473,10 +472,7 @@
 
     addr.sin_family = AF_INET;
     addr.sin_port = htons(port);
-    if (rfbLocalhostOnly)
-        addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-    else
-        addr.sin_addr.s_addr = htonl(INADDR_ANY);
+    addr.sin_addr.s_addr = interface;
 
     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 	return -1;
@@ -543,7 +539,7 @@
 
     addr.sin_family = AF_INET;
     addr.sin_port = htons(port);
-    addr.sin_addr.s_addr = INADDR_ANY;
+    addr.sin_addr.s_addr = interface;
 
     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
 	return -1;
--- vnc_unixsrc/Xvnc/programs/Xserver/hw/vnc/rfb.h.iface	Wed Oct 25 19:08:48 2000
+++ vnc_unixsrc/Xvnc/programs/Xserver/hw/vnc/rfb.h	Wed Nov  8 16:00:02 2000
@@ -21,6 +21,7 @@
  *  USA.
  */
 
+#include <netinet/in.h>
 #include "scrnintstr.h"
 #include "colormapst.h"
 #include "gcstruct.h"
@@ -285,6 +286,7 @@
 extern void rfbLog(char *format, ...);
 extern void rfbLogPerror(char *str);
 
+extern in_addr_t interface;
 
 /* sockets.c */
 
@@ -296,7 +298,6 @@
 
 extern int rfbPort;
 extern int rfbListenSock;
-extern Bool rfbLocalhostOnly;
 
 extern void rfbInitSockets();
 extern void rfbDisconnectUDPSock();
---------------------------------------------------------------------
To unsubscribe, send a message with the line: unsubscribe vnc-list
to majordomo "at" uk.research.att.com
See also: http://www.uk.research.att.com/vnc/intouch.html
---------------------------------------------------------------------