From patchwork Mon Sep 7 16:06:11 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 33099 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 23E80B70D7 for ; Tue, 8 Sep 2009 02:35:33 +1000 (EST) Received: from localhost ([127.0.0.1]:52982 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MkhBh-0003Zh-U8 for incoming@patchwork.ozlabs.org; Mon, 07 Sep 2009 12:35:29 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mkgjy-0007YW-Dp for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:50 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mkgjs-0007TL-IS for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:49 -0400 Received: from [199.232.76.173] (port=34960 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mkgjs-0007TA-4K for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33097) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mkgjr-0003WO-CZ for qemu-devel@nongnu.org; Mon, 07 Sep 2009 12:06:43 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n87G6gic013965 for ; Mon, 7 Sep 2009 12:06:42 -0400 Received: from zweiblum.home.kraxel.org (vpn2-9-74.ams2.redhat.com [10.36.9.74]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id n87G6X5r010159; Mon, 7 Sep 2009 12:06:34 -0400 Received: by zweiblum.home.kraxel.org (Postfix, from userid 500) id 6125A700E5; Mon, 7 Sep 2009 18:06:26 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Mon, 7 Sep 2009 18:06:11 +0200 Message-Id: <1252339585-27797-10-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.16 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH 09/23] sockets: add inet_listen_opts 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 Add inet_listen_opts(). Does the same as inet_listen(), but uses QemuOpts. inet_listen() is a compatibility wrapper for inet_listen_opts() now and should go away some day. Signed-off-by: Gerd Hoffmann --- qemu-sockets.c | 103 ++++++++++++++++++++++++++------------------------------ qemu_socket.h | 1 + 2 files changed, 49 insertions(+), 55 deletions(-) diff --git a/qemu-sockets.c b/qemu-sockets.c index 65db94e..ec9777f 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -116,63 +116,31 @@ static void inet_print_addrinfo(const char *tag, struct addrinfo *res) } } -int inet_listen(const char *str, char *ostr, int olen, - int socktype, int port_offset) +int inet_listen_opts(QemuOpts *opts, int port_offset) { struct addrinfo ai,*res,*e; - char addr[64]; + const char *addr; char port[33]; char uaddr[INET6_ADDRSTRLEN+1]; char uport[33]; - const char *opts, *h; - int slisten,rc,pos,to,try_next; + int slisten,rc,to,try_next; memset(&ai,0, sizeof(ai)); ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; ai.ai_family = PF_UNSPEC; - ai.ai_socktype = socktype; + ai.ai_socktype = SOCK_STREAM; - /* parse address */ - if (str[0] == ':') { - /* no host given */ - addr[0] = '\0'; - if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { - 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)) { - fprintf(stderr, "%s: ipv6 parse error (%s)\n", - __FUNCTION__, str); - return -1; - } - ai.ai_family = PF_INET6; - } else if (qemu_isdigit(str[0])) { - /* IPv4 addr */ - if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { - fprintf(stderr, "%s: ipv4 parse error (%s)\n", - __FUNCTION__, str); - return -1; - } - ai.ai_family = PF_INET; - } else { - /* hostname */ - if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { - fprintf(stderr, "%s: hostname parse error (%s)\n", - __FUNCTION__, str); - return -1; - } + if (qemu_opt_get(opts, "port") == NULL) { + fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__); + return -1; } + pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); + addr = qemu_opt_get(opts, "host"); - /* parse options */ - opts = str + pos; - h = strstr(opts, ",to="); - to = h ? atoi(h+4) : 0; - if (strstr(opts, ",ipv4")) + to = qemu_opt_get_number(opts, "to", 0); + if (qemu_opt_get_bool(opts, "ipv4", 0)) ai.ai_family = PF_INET; - if (strstr(opts, ",ipv6")) + if (qemu_opt_get_bool(opts, "ipv6", 0)) ai.ai_family = PF_INET6; /* lookup */ @@ -180,8 +148,8 @@ int inet_listen(const char *str, char *ostr, int olen, snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res); if (rc != 0) { - fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__, - addr, port, gai_strerror(rc)); + fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, + gai_strerror(rc)); return -1; } if (sockets_debug) @@ -239,15 +207,11 @@ listen: freeaddrinfo(res); return -1; } - if (ostr) { - if (e->ai_family == PF_INET6) { - snprintf(ostr, olen, "[%s]:%d%s", uaddr, - inet_getport(e) - port_offset, opts); - } else { - snprintf(ostr, olen, "%s:%d%s", uaddr, - inet_getport(e) - port_offset, opts); - } - } + snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); + qemu_opt_set(opts, "host", uaddr); + qemu_opt_set(opts, "port", uport); + qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off"); + qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off"); freeaddrinfo(res); return slisten; } @@ -378,6 +342,35 @@ static int inet_parse(QemuOpts *opts, const char *str) return 0; } +int inet_listen(const char *str, char *ostr, int olen, + int socktype, int port_offset) +{ + QemuOpts *opts; + char *optstr; + int sock = -1; + + opts = qemu_opts_create(&dummy_opts, NULL, 0); + if (inet_parse(opts, str) == 0) { + sock = inet_listen_opts(opts, port_offset); + if (sock != -1 && ostr) { + optstr = strchr(str, ','); + if (qemu_opt_get_bool(opts, "ipv6", 0)) { + snprintf(ostr, olen, "[%s]:%s%s", + qemu_opt_get(opts, "host"), + qemu_opt_get(opts, "port"), + optstr ? optstr : ""); + } else { + snprintf(ostr, olen, "%s:%s%s", + qemu_opt_get(opts, "host"), + qemu_opt_get(opts, "port"), + optstr ? optstr : ""); + } + } + } + qemu_opts_del(opts); + return sock; +} + int inet_connect(const char *str, int socktype) { QemuOpts *opts; diff --git a/qemu_socket.h b/qemu_socket.h index 662f427..6ee9510 100644 --- a/qemu_socket.h +++ b/qemu_socket.h @@ -36,6 +36,7 @@ void socket_set_nonblock(int fd); int send_all(int fd, const void *buf, int len1); /* New, ipv6-ready socket helper functions, see qemu-sockets.c */ +int inet_listen_opts(QemuOpts *opts, int port_offset); int inet_listen(const char *str, char *ostr, int olen, int socktype, int port_offset); int inet_connect_opts(QemuOpts *opts);