From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1GUvMz-00039B-Q7 for qemu-devel@nongnu.org; Tue, 03 Oct 2006 21:16:21 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1GUvMx-00038q-6M for qemu-devel@nongnu.org; Tue, 03 Oct 2006 21:16:20 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1GUvMx-00038n-1J for qemu-devel@nongnu.org; Tue, 03 Oct 2006 21:16:19 -0400 Received: from [128.83.139.10] (helo=mail.cs.utexas.edu) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA:32) (Exim 4.52) id 1GUvTG-0004Nz-RB for qemu-devel@nongnu.org; Tue, 03 Oct 2006 21:22:51 -0400 Received: from [192.168.1.102] (cpe-70-112-17-156.austin.res.rr.com [70.112.17.156]) (authenticated bits=0) by mail.cs.utexas.edu (8.13.8/8.13.8) with ESMTP id k941G7dK029271 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 3 Oct 2006 20:16:14 -0500 (CDT) Message-ID: <45230B51.1070905@cs.utexas.edu> Date: Tue, 03 Oct 2006 20:16:01 -0500 From: Anthony Liguori MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------010703020800030708040107" Subject: [Qemu-devel] [PATCH] Allow -vnc to restrict what interface to listen on Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This is a multi-part message in MIME format. --------------010703020800030708040107 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------010703020800030708040107 Content-Type: text/x-patch; name="vnc-listen.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="vnc-listen.diff" # HG changeset patch # User anthony@rhesus # 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, --------------010703020800030708040107--