From patchwork Mon Sep 7 16:06:21 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 33104 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by bilbo.ozlabs.org (Postfix) with ESMTPS id 707A4B70CF for ; Tue, 8 Sep 2009 02:46:06 +1000 (EST) Received: from localhost ([127.0.0.1]:40336 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MkhLv-000433-7P for incoming@patchwork.ozlabs.org; Mon, 07 Sep 2009 12:46:03 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mkgk2-0007bV-Uo for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:55 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mkgjw-0007XQ-Mt for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:53 -0400 Received: from [199.232.76.173] (port=34965 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mkgjw-0007X1-7X for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:15990) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mkgjv-0003X7-E6 for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:47 -0400 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n87G6kkn013973 for ; Mon, 7 Sep 2009 12:06:46 -0400 Received: from zweiblum.home.kraxel.org (vpn2-9-74.ams2.redhat.com [10.36.9.74]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id n87G6XJc029400; Mon, 7 Sep 2009 12:06:42 -0400 Received: by zweiblum.home.kraxel.org (Postfix, from userid 500) id 996FD700F2; Mon, 7 Sep 2009 18:06:26 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Mon, 7 Sep 2009 18:06:21 +0200 Message-Id: <1252339585-27797-20-git-send-email-kraxel@redhat.com> In-Reply-To: <1252339585-27797-1-git-send-email-kraxel@redhat.com> References: <1252339585-27797-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.17 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH 19/23] convert udp chardev to QemuOpts. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org While being at it: create a new inet_dgram_opts() function for udp setup, so udp can handle IPv6 now. new cmd line syntax: -chardev udp,id=name,host=remotehost,port=remoteport,\ localaddr=bindaddr,localport=bindport Signed-off-by: Gerd Hoffmann --- qemu-char.c | 73 +++++++++++++++++------------------ qemu-config.c | 6 +++ qemu-sockets.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- qemu_socket.h | 1 + 4 files changed, 157 insertions(+), 39 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index 5442226..e31f97b 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -1729,7 +1729,6 @@ static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts) typedef struct { int fd; - struct sockaddr_in daddr; uint8_t buf[1024]; int bufcnt; int bufptr; @@ -1740,8 +1739,7 @@ static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) { NetCharDriver *s = chr->opaque; - return sendto(s->fd, (const void *)buf, len, 0, - (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); + return send(s->fd, (const void *)buf, len, 0); } static int udp_chr_read_poll(void *opaque) @@ -1803,30 +1801,18 @@ static void udp_chr_close(CharDriverState *chr) qemu_chr_event(chr, CHR_EVENT_CLOSED); } -static CharDriverState *qemu_chr_open_udp(const char *def) +static CharDriverState *qemu_chr_open_udp(QemuOpts *opts) { CharDriverState *chr = NULL; NetCharDriver *s = NULL; int fd = -1; - struct sockaddr_in saddr; chr = qemu_mallocz(sizeof(CharDriverState)); s = qemu_mallocz(sizeof(NetCharDriver)); - fd = socket(PF_INET, SOCK_DGRAM, 0); + fd = inet_dgram_opts(opts); if (fd < 0) { - perror("socket(PF_INET, SOCK_DGRAM)"); - goto return_err; - } - - if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { - printf("Could not parse: %s\n", def); - goto return_err; - } - - if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) - { - perror("bind"); + fprintf(stderr, "inet_dgram_opts failed\n"); goto return_err; } @@ -2297,6 +2283,31 @@ static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) qemu_opt_set(opts, "telnet", "on"); return opts; } + if (strstart(filename, "udp:", &p)) { + qemu_opt_set(opts, "backend", "udp"); + if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { + host[0] = 0; + if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { + fprintf(stderr, "udp #1\n"); + goto fail; + } + } + qemu_opt_set(opts, "host", host); + qemu_opt_set(opts, "port", port); + if (p[pos] == '@') { + p += pos + 1; + if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { + host[0] = 0; + if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { + fprintf(stderr, "udp #2\n"); + goto fail; + } + } + qemu_opt_set(opts, "localaddr", host); + qemu_opt_set(opts, "localport", port); + } + return opts; + } if (strstart(filename, "unix:", &p)) { qemu_opt_set(opts, "backend", "socket"); if (qemu_opts_do_parse(opts, p, "path") != 0) @@ -2327,6 +2338,7 @@ static struct { } backend_table[] = { { .name = "null", .open = qemu_chr_open_null }, { .name = "socket", .open = qemu_chr_open_socket }, + { .name = "udp", .open = qemu_chr_open_udp }, { .name = "msmouse", .open = qemu_chr_open_msmouse }, { .name = "vc", .open = text_console_init }, #ifdef _WIN32 @@ -2405,27 +2417,12 @@ CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*i QemuOpts *opts; opts = qemu_chr_parse_compat(label, filename); - if (opts) { - chr = qemu_chr_open_opts(opts, init); - if (qemu_opt_get_bool(opts, "mux", 0)) { - monitor_init(chr, MONITOR_USE_READLINE); - } - return chr; - } - - if (strstart(filename, "udp:", &p)) { - chr = qemu_chr_open_udp(p); - } else - { - chr = NULL; - } + if (!opts) + return NULL; - if (chr) { - if (!chr->filename) - chr->filename = qemu_strdup(filename); - chr->init = init; - chr->label = qemu_strdup(label); - TAILQ_INSERT_TAIL(&chardevs, chr, next); + chr = qemu_chr_open_opts(opts, init); + if (chr && qemu_opt_get_bool(opts, "mux", 0)) { + monitor_init(chr, MONITOR_USE_READLINE); } return chr; } diff --git a/qemu-config.c b/qemu-config.c index 09ef481..8404f1b 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -92,6 +92,12 @@ QemuOptsList qemu_chardev_opts = { .name = "port", .type = QEMU_OPT_STRING, },{ + .name = "localaddr", + .type = QEMU_OPT_STRING, + },{ + .name = "localport", + .type = QEMU_OPT_STRING, + },{ .name = "to", .type = QEMU_OPT_NUMBER, },{ diff --git a/qemu-sockets.c b/qemu-sockets.c index ec9777f..6248b24 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -26,7 +26,7 @@ # define AI_ADDRCONFIG 0 #endif -static int sockets_debug = 0; +static int sockets_debug = 1; static const int on=1, off=0; /* used temporarely until all users are converted to QemuOpts */ @@ -286,6 +286,120 @@ int inet_connect_opts(QemuOpts *opts) return -1; } +int inet_dgram_opts(QemuOpts *opts) +{ + struct addrinfo ai, *peer = NULL, *local = NULL; + const char *addr; + const char *port; + char uaddr[INET6_ADDRSTRLEN+1]; + char uport[33]; + int sock = -1, rc; + + /* lookup peer addr */ + memset(&ai,0, sizeof(ai)); + ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; + ai.ai_family = PF_UNSPEC; + ai.ai_socktype = SOCK_DGRAM; + + addr = qemu_opt_get(opts, "host"); + port = qemu_opt_get(opts, "port"); + if (addr == NULL || strlen(addr) == 0) { + addr = "localhost"; + } + if (port == NULL || strlen(port) == 0) { + fprintf(stderr, "inet_dgram: port not specified\n"); + return -1; + } + + if (qemu_opt_get_bool(opts, "ipv4", 0)) + ai.ai_family = PF_INET; + if (qemu_opt_get_bool(opts, "ipv6", 0)) + ai.ai_family = PF_INET6; + + if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { + fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, + gai_strerror(rc)); + return -1; + } + if (sockets_debug) { + fprintf(stderr, "%s: peer (%s:%s)\n", __FUNCTION__, addr, port); + inet_print_addrinfo(__FUNCTION__, peer); + } + + /* lookup local addr */ + memset(&ai,0, sizeof(ai)); + ai.ai_flags = AI_PASSIVE; + ai.ai_family = peer->ai_family; + ai.ai_socktype = SOCK_DGRAM; + + addr = qemu_opt_get(opts, "localaddr"); + port = qemu_opt_get(opts, "localport"); + if (addr == NULL || strlen(addr) == 0) { + addr = NULL; + } + if (!port || strlen(port) == 0) + port = "0"; + + if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { + fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, + gai_strerror(rc)); + return -1; + } + if (sockets_debug) { + fprintf(stderr, "%s: local (%s:%s)\n", __FUNCTION__, addr, port); + inet_print_addrinfo(__FUNCTION__, local); + } + + /* create socket */ + sock = socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); + if (sock < 0) { + fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, + inet_strfamily(peer->ai_family), strerror(errno)); + goto err; + } + setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); + + /* bind socket */ + if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, + uaddr,INET6_ADDRSTRLEN,uport,32, + NI_NUMERICHOST | NI_NUMERICSERV) != 0) { + fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); + goto err; + } + if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { + fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, + inet_strfamily(local->ai_family), uaddr, inet_getport(local)); + goto err; + } + + /* connect to peer */ + if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen, + uaddr, INET6_ADDRSTRLEN, uport, 32, + NI_NUMERICHOST | NI_NUMERICSERV) != 0) { + fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); + goto err; + } + if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { + fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, + inet_strfamily(peer->ai_family), + peer->ai_canonname, uaddr, uport, strerror(errno)); + goto err; + } + + freeaddrinfo(local); + freeaddrinfo(peer); + return sock; + +err: + if (-1 != sock) + closesocket(sock); + if (local) + freeaddrinfo(local); + if (peer) + freeaddrinfo(peer); + return -1; +} + /* compatibility wrapper */ static int inet_parse(QemuOpts *opts, const char *str) { diff --git a/qemu_socket.h b/qemu_socket.h index 6ee9510..c253b32 100644 --- a/qemu_socket.h +++ b/qemu_socket.h @@ -41,6 +41,7 @@ int inet_listen(const char *str, char *ostr, int olen, int socktype, int port_offset); int inet_connect_opts(QemuOpts *opts); int inet_connect(const char *str, int socktype); +int inet_dgram_opts(QemuOpts *opts); int unix_listen_opts(QemuOpts *opts); int unix_listen(const char *path, char *ostr, int olen);