qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 19/23] convert udp chardev to QemuOpts.
Date: Mon,  7 Sep 2009 18:06:21 +0200	[thread overview]
Message-ID: <1252339585-27797-20-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1252339585-27797-1-git-send-email-kraxel@redhat.com>

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 <kraxel@redhat.com>
---
 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);
-- 
1.6.2.5

  parent reply	other threads:[~2009-09-07 16:06 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-07 16:06 [Qemu-devel] [PATCH 00/23] switch sockets+chardev to QemuOpts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 01/23] QemuOpts: split option parser into two functions Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 02/23] qemu-option.h include protectors Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 03/23] switch chardev to QemuOpts: infrastructure, null device Gerd Hoffmann
2009-09-07 16:52   ` Blue Swirl
2009-09-07 16:06 ` [Qemu-devel] [PATCH 04/23] convert file+pipe chardevs to QemuOpts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 05/23] sockets: add unix_connect_opts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 06/23] sockets: add unix_listen_opts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 07/23] sockets: add unix_*_opts for windows Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 08/23] sockets: add inet_connect_opts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 09/23] sockets: add inet_listen_opts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 10/23] convert unix+tcp chardevs to QemuOpts Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 11/23] convert pty chardev " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 12/23] convert stdio " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 13/23] convert msmouse " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 14/23] convert braille " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 15/23] convert windows console " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 16/23] convert tty + parport chardevs " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 17/23] convert vc chardev " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 18/23] convert mux " Gerd Hoffmann
2009-09-07 16:06 ` Gerd Hoffmann [this message]
2009-09-09 20:27   ` [Qemu-devel] Re: [PATCH 19/23] convert udp " Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 20/23] Allow -serial chardev:<name> Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 21/23] qdev: add parser for chardev properties Gerd Hoffmann
2009-09-07 16:06 ` [Qemu-devel] [PATCH 22/23] monitor: fix muxing Gerd Hoffmann
2009-09-09 19:51   ` [Qemu-devel] " Jan Kiszka
2009-09-09 20:18     ` Gerd Hoffmann
2009-09-09 20:27       ` Jan Kiszka
2009-09-07 16:06 ` [Qemu-devel] [PATCH 23/23] move mux focus field from CharDriverState to MuxDriver Gerd Hoffmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1252339585-27797-20-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).