qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] Allow -vnc to restrict what interface to listen on


From: Anthony Liguori
Subject: [Qemu-devel] [PATCH] Allow -vnc to restrict what interface to listen on
Date: Tue, 03 Oct 2006 20:16:01 -0500
User-agent: Thunderbird 1.5.0.7 (X11/20060918)

Howdy,

The attached patch changes the -vnc syntax from:

 -vnc display

To:

 -vnc [interface:]display

This allows a user to restrict the interface the VNC server listens on (for instance, to localhost). I factored out some of the code from the tcp: char device and fixed a minor bug that would mistakenly reject valid hostnames (for instance, 42.slashdot.org). If the interface portion of the option is not specified, the interface defaults to 0.0.0.0 (which is the old behavior).

Regards,

Anthony Liguori
# HG changeset patch
# User address@hidden
# Date 1159924183 18000
# Node ID 3eebcca40df8431aab7411ab585a7302eaebc9c8
# Parent  d12ee567e5b9f83821b3c28548a089af2aba2de7
Let the vnc option bind to a particular interface.  Also cleanup the telnet
options host parsing

diff -r d12ee567e5b9 -r 3eebcca40df8 qemu_socket.h
--- a/qemu_socket.h     Sat Sep 30 01:01:17 2006 +0000
+++ b/qemu_socket.h     Tue Oct 03 20:09:43 2006 -0500
@@ -27,4 +27,6 @@
 
 void socket_set_nonblock(int fd);
 
+int parse_host(struct sockaddr_in *saddr, const char *str);
+
 #endif /* QEMU_SOCKET_H */
diff -r d12ee567e5b9 -r 3eebcca40df8 vl.c
--- a/vl.c      Sat Sep 30 01:01:17 2006 +0000
+++ b/vl.c      Tue Oct 03 20:09:43 2006 -0500
@@ -151,7 +151,7 @@ int usb_enabled = 0;
 int usb_enabled = 0;
 static VLANState *first_vlan;
 int smp_cpus = 1;
-int vnc_display = -1;
+const char *vnc_interface = NULL;
 #if defined(TARGET_SPARC)
 #define MAX_CPUS 16
 #elif defined(TARGET_I386)
@@ -2706,10 +2706,23 @@ fail:
     return -1;
 }
 
+int parse_host(struct sockaddr_in *saddr, const char *str)
+{
+    struct hostent *pent;
+
+    pent = gethostbyname(str);
+    if (pent == NULL) {
+       if (!inet_aton(str, &saddr->sin_addr))
+           return -1;
+    } else
+       memcpy(&saddr->sin_addr, pent->h_addr, 4);
+
+    return 0;
+}
+
 int parse_host_port(struct sockaddr_in *saddr, const char *str)
 {
     char buf[512];
-    struct hostent *he;
     const char *p, *r;
     int port;
 
@@ -2719,16 +2732,9 @@ int parse_host_port(struct sockaddr_in *
     saddr->sin_family = AF_INET;
     if (buf[0] == '\0') {
         saddr->sin_addr.s_addr = 0;
-    } else {
-        if (isdigit(buf[0])) {
-            if (!inet_aton(buf, &saddr->sin_addr))
-                return -1;
-        } else {
-            if ((he = gethostbyname(buf)) == NULL)
-                return - 1;
-            saddr->sin_addr = *(struct in_addr *)he->h_addr;
-        }
-    }
+    } else if (parse_host(saddr, buf) == -1)
+       return -1;
+
     port = strtol(p, (char **)&r, 0);
     if (r == p)
         return -1;
@@ -5926,7 +5932,7 @@ void help(void)
            "-no-acpi        disable ACPI\n"
 #endif
            "-loadvm file    start right away with a saved state (loadvm in 
monitor)\n"
-          "-vnc display    start a VNC server on display\n"
+          "-vnc addr       start a VNC server on addr ([interface:]display)\n"
            "\n"
            "During emulation, the following keys are useful:\n"
            "ctrl-alt-f      toggle full screen\n"
@@ -6716,11 +6722,7 @@ int main(int argc, char **argv)
                 }
                 break;
            case QEMU_OPTION_vnc:
-               vnc_display = atoi(optarg);
-               if (vnc_display < 0) {
-                   fprintf(stderr, "Invalid VNC display\n");
-                   exit(1);
-               }
+               vnc_interface = optarg;
                break;
             case QEMU_OPTION_no_acpi:
                 acpi_enabled = 0;
@@ -6841,8 +6843,8 @@ int main(int argc, char **argv)
     /* terminal init */
     if (nographic) {
         dumb_display_init(ds);
-    } else if (vnc_display != -1) {
-       vnc_display_init(ds, vnc_display);
+    } else if (vnc_interface != NULL) {
+       vnc_display_init(ds, vnc_interface);
     } else {
 #if defined(CONFIG_SDL)
         sdl_display_init(ds, full_screen);
diff -r d12ee567e5b9 -r 3eebcca40df8 vl.h
--- a/vl.h      Sat Sep 30 01:01:17 2006 +0000
+++ b/vl.h      Tue Oct 03 20:09:43 2006 -0500
@@ -866,7 +866,7 @@ void cocoa_display_init(DisplayState *ds
 void cocoa_display_init(DisplayState *ds, int full_screen);
 
 /* vnc.c */
-void vnc_display_init(DisplayState *ds, int display);
+void vnc_display_init(DisplayState *ds, const char *interface);
 
 /* ide.c */
 #define MAX_DISKS 4
diff -r d12ee567e5b9 -r 3eebcca40df8 vnc.c
--- a/vnc.c     Sat Sep 30 01:01:17 2006 +0000
+++ b/vnc.c     Tue Oct 03 20:09:43 2006 -0500
@@ -1099,11 +1099,26 @@ static void vnc_listen_read(void *opaque
     }
 }
 
-void vnc_display_init(DisplayState *ds, int display)
+void vnc_display_init(DisplayState *ds, const char *vnc_interface)
 {
     struct sockaddr_in addr;
     int reuse_addr, ret;
     VncState *vs;
+    char host[512];
+    char *ptr;
+    const char *bind_to = "0.0.0.0";
+    int display;
+
+    snprintf(host, sizeof(host), "%s", vnc_interface);
+
+    ptr = strchr(host, ':');
+    if (ptr) {
+           *ptr = 0;
+           display = atoi(ptr + 1);
+           bind_to = host;
+    } else
+           display = atoi(host);
+                   
 
     vs = qemu_mallocz(sizeof(VncState));
     if (!vs)
@@ -1132,7 +1147,10 @@ void vnc_display_init(DisplayState *ds, 
 
     addr.sin_family = AF_INET;
     addr.sin_port = htons(5900 + display);
-    memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
+    if (parse_host(&addr, bind_to) == -1) {
+       fprintf(stderr, "Invalid host `%s'\n", bind_to);
+       exit(1);
+    }
 
     reuse_addr = 1;
     ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]