qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Bharata B Rao <bharata@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Anand Avati <aavati@redhat.com>,
	Vijay Bellur <vbellur@redhat.com>,
	Stefan Hajnoczi <stefanha@gmail.com>,
	Harsh Bora <harsh@linux.vnet.ibm.com>,
	Amar Tumballi <amarts@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Blue Swirl <blauwirbel@gmail.com>, Avi Kivity <avi@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH v8 2/6] sockets: Make port specification optional in inet_parse
Date: Mon, 24 Sep 2012 14:23:18 +0530	[thread overview]
Message-ID: <20120924085318.GE18470@in.ibm.com> (raw)
In-Reply-To: <20120924085127.GC18470@in.ibm.com>

sockets: Make port specification optional in inet_parse

From: Bharata B Rao <bharata@linux.vnet.ibm.com>

Change inet_parse() to work without explicit port specification. Add a
default_port argument to be used when port isn't specified.

Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
---

 qemu-sockets.c |   43 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 34 insertions(+), 9 deletions(-)


diff --git a/qemu-sockets.c b/qemu-sockets.c
index b292311..25285f2 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -406,26 +406,36 @@ err:
     return -1;
 }
 
-/* compatibility wrapper */
-static int inet_parse(QemuOpts *opts, const char *str)
+/*
+ * Converts inet address and port specification in @str to QemuOpts
+ * options.
+ *
+ * If port isn't specified in @str, @default_port will be set in the
+ * port option of @opts. If default_port is -1, then the parser will
+ * look for the port specification in @str mandatorily.
+ */
+static int inet_parse(QemuOpts *opts, const char *str, int default_port)
 {
     const char *optstr, *h;
     char addr[64];
     char port[33];
-    int pos;
+    int ret, pos, addr_pos = -1, port_pos = -1;
 
     /* parse address */
     if (str[0] == ':') {
         /* no host given */
         addr[0] = '\0';
-        if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
+        ret = sscanf(str, ":%32[^,]%n", port, &port_pos);
+        if (port_pos == -1 || ret == EOF) {
             fprintf(stderr, "%s: portonly parse error (%s)\n",
                     __FUNCTION__, str);
             return -1;
         }
     } else if (str[0] == '[') {
         /* IPv6 addr */
-        if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
+        ret = sscanf(str, "[%64[^]]]%n:%32[^,]%n", addr, &addr_pos,
+            port, &port_pos);
+        if (addr_pos == -1 || ret == EOF) {
             fprintf(stderr, "%s: ipv6 parse error (%s)\n",
                     __FUNCTION__, str);
             return -1;
@@ -433,7 +443,9 @@ static int inet_parse(QemuOpts *opts, const char *str)
         qemu_opt_set(opts, "ipv6", "on");
     } else if (qemu_isdigit(str[0])) {
         /* IPv4 addr */
-        if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
+        ret = sscanf(str, "%64[0-9.]%n:%32[^,]%n", addr, &addr_pos,
+            port, &port_pos);
+        if (addr_pos == -1 || ret == EOF) {
             fprintf(stderr, "%s: ipv4 parse error (%s)\n",
                     __FUNCTION__, str);
             return -1;
@@ -441,12 +453,25 @@ static int inet_parse(QemuOpts *opts, const char *str)
         qemu_opt_set(opts, "ipv4", "on");
     } else {
         /* hostname */
-        if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
+        ret = sscanf(str, "%64[^:]%n:%32[^,]%n", addr, &addr_pos,
+            port, &port_pos);
+        if (addr_pos == -1 || ret == EOF) {
             fprintf(stderr, "%s: hostname parse error (%s)\n",
                     __FUNCTION__, str);
             return -1;
         }
     }
+
+    if (port_pos != -1) {
+        pos = port_pos;
+    } else {
+        pos = addr_pos;
+        if (default_port == -1) {
+            return -1;
+        }
+        snprintf(port, 33, "%d", default_port);
+    }
+
     qemu_opt_set(opts, "host", addr);
     qemu_opt_set(opts, "port", port);
 
@@ -470,7 +495,7 @@ int inet_listen(const char *str, char *ostr, int olen,
     int sock = -1;
 
     opts = qemu_opts_create(&socket_opts, NULL, 0, NULL);
-    if (inet_parse(opts, str) == 0) {
+    if (inet_parse(opts, str, -1) == 0) {
         sock = inet_listen_opts(opts, port_offset, errp);
         if (sock != -1 && ostr) {
             optstr = strchr(str, ',');
@@ -499,7 +524,7 @@ int inet_connect(const char *str, bool block, bool *in_progress, Error **errp)
     int sock = -1;
 
     opts = qemu_opts_create(&socket_opts, NULL, 0, NULL);
-    if (inet_parse(opts, str) == 0) {
+    if (inet_parse(opts, str, -1) == 0) {
         if (block) {
             qemu_opt_set(opts, "block", "on");
         }

  parent reply	other threads:[~2012-09-24  8:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-24  8:51 [Qemu-devel] [PATCH v8 0/6] GlusterFS support in QEMU - v8 Bharata B Rao
2012-09-24  8:52 ` [Qemu-devel] [PATCH v8 1/6] sockets: publish dummy_opts Bharata B Rao
2012-09-24  8:53 ` Bharata B Rao [this message]
2012-09-24  8:54 ` [Qemu-devel] [PATCH v8 3/6] sockets: Make inet_parse() non static Bharata B Rao
2012-09-24  8:54 ` [Qemu-devel] [PATCH v8 4/6] aio: Fix qemu_aio_wait() to maintain correct walking_handlers count Bharata B Rao
2012-09-24  8:55 ` [Qemu-devel] [PATCH v8 5/6] configure: Add a config option for GlusterFS as block backend Bharata B Rao
2012-09-24  8:56 ` [Qemu-devel] [PATCH v8 6/6] block: Support GlusterFS as a QEMU " Bharata B Rao

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=20120924085318.GE18470@in.ibm.com \
    --to=bharata@linux.vnet.ibm.com \
    --cc=aavati@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=amarts@redhat.com \
    --cc=armbru@redhat.com \
    --cc=avi@redhat.com \
    --cc=blauwirbel@gmail.com \
    --cc=harsh@linux.vnet.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --cc=vbellur@redhat.com \
    /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).