From patchwork Fri Aug 13 23:44:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Doug Evans X-Patchwork-Id: 12436383 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72E75C4338F for ; Fri, 13 Aug 2021 23:46:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DD4C361042 for ; Fri, 13 Aug 2021 23:46:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DD4C361042 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:34270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mEgsP-0005O8-1C for qemu-devel@archiver.kernel.org; Fri, 13 Aug 2021 19:46:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <33gMXYQMKClEw2xz77z4x.v759x5D-wxEx4676z6D.7Az@flex--dje.bounces.google.com>) id 1mEgqn-0003FP-VK for qemu-devel@nongnu.org; Fri, 13 Aug 2021 19:44:33 -0400 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]:35578) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <33gMXYQMKClEw2xz77z4x.v759x5D-wxEx4676z6D.7Az@flex--dje.bounces.google.com>) id 1mEgqm-0003kx-DC for qemu-devel@nongnu.org; Fri, 13 Aug 2021 19:44:33 -0400 Received: by mail-qv1-xf4a.google.com with SMTP id t3-20020a0cf9830000b0290359840930bdso4909740qvn.2 for ; Fri, 13 Aug 2021 16:44:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=QslOfOKGscIgx3K5boBWQYg/0chahCptrQjLmfipCMg=; b=n2GRCYvJFG5W3FCeCNKQCMkO+BolD7kaByFO8Eip0lZsfAV6T1+mYneyXTASf6iiXI 6ekv+FQ7HK3G5TgkZiPsEd0vU3hVcAcePUhaOuwtuErUOeX7CBOrONJD9ERjEJuXH6jB EqJ76f5f8UkKQnIhg4Madtcy8T9up+QuTTJ2MOQI2e/J9vwJvCz2MaPQmVLW7iSqw2OJ ewAiMqmyjRkLTuzIanIhtqWgULEtB033F3SbZsvVuxPkBCZA3VJHKxKD/Xy3jpqNUS1q S2+7uVAvV6Beos94titq0zrZEOBVINUmP14Mn0j1hB1WP+AIuSFKthU6Wgn/0l5WyQ+4 vJmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=QslOfOKGscIgx3K5boBWQYg/0chahCptrQjLmfipCMg=; b=LB1mRGIrRChGmMuOVGwMqu6+V93L6gyrqwAPq2GG/3bdatwBiBLCes9OGVn/lyGiyz h/t5Mjz5xTjhuyESqLjuxNXZsEgteuHIFhCBc/Glk/aDRxB6OVr+6YCWpp9KJZHyLpNV 3n+tBIrhTCp24EjMd5Lph8dhr4k8C1Fvh06GxdXcExnCcQ7ovNnaQFo4KD1hakRCyrew EADszj5+Ly25qLzGWgQaBDcH0/J0LHa3dXEk+WCqmTvVwvGDtxLWTokUzOIHvdGssmik P2Ieit1qwjv4NE4P+LLUjb1PvPZLkNcPJHvPl/R31VGusP+P27BxEa+Cq8w//a0Qwd76 TxuA== X-Gm-Message-State: AOAM532MazeQkAisYyw24YAtoD9MGdfVU11jmrjx2xR5Ldgz7fUMjjk9 zjfMv2MtruZH0gmalaQH1WZMU4rug6jp3qMVIJ/TAUDYTYpMwWn4A6UF8YpjSIP2lgdP0LiLC2L 7N0IVrOMyMvrhjrUxrAwrdtWcz7WZhKm9P25Eq5UJnkbTIT6+nK3o X-Google-Smtp-Source: ABdhPJwfkMAt3tasYkozIxtcssmae32BzONHVnHRnzQ0rh+TF0YEgu9+vpxaUHLjkBISCgK/tIsHf/Q= X-Received: from ruffy.mtv.corp.google.com ([2620:0:1000:1412:94ae:6a1e:bc04:4f63]) (user=dje job=sendgmr) by 2002:a0c:e885:: with SMTP id b5mr5164588qvo.48.1628898270663; Fri, 13 Aug 2021 16:44:30 -0700 (PDT) Date: Fri, 13 Aug 2021 16:44:22 -0700 In-Reply-To: <20210813234424.2029019-1-dje@google.com> Message-Id: <20210813234424.2029019-2-dje@google.com> Mime-Version: 1.0 References: <20210813234424.2029019-1-dje@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH v8 1/3] util/qemu-sockets.c: Split host:port parsing out of inet_parse From: Doug Evans To: qemu-devel@nongnu.org Cc: Samuel Thibault , " =?utf-8?q?Daniel_P=2E?= =?utf-8?q?_Berrang=C3=A9?= " , Jason Wang , " =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= " , Doug Evans , " =?utf-8?q?Marc-Andr=C3=A9_Lureau?= " Received-SPF: pass client-ip=2607:f8b0:4864:20::f4a; envelope-from=33gMXYQMKClEw2xz77z4x.v759x5D-wxEx4676z6D.7Az@flex--dje.bounces.google.com; helo=mail-qv1-xf4a.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" The parsing is moved into new function inet_parse_host_port. Also split out is ipv4=flag, ipv6=flag processing into inet_parse_ipv46. This is done in preparation for using these functions in net/slirp.c. Signed-off-by: Doug Evans Reviewed-by: Marc-André Lureau --- Changes from v7: No changes. Changes from v6: No changes. Changes from v5: Also split out parsing of ipv4=on|off, ipv6=on|off include/qemu/sockets.h | 3 ++ util/qemu-sockets.c | 65 +++++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 0c34bf2398..7b2d06ff6e 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -31,6 +31,9 @@ int socket_set_fast_reuse(int fd); int inet_ai_family_from_address(InetSocketAddress *addr, Error **errp); +const char *inet_parse_host_port(InetSocketAddress *addr, + const char *str, Error **errp); +int inet_parse_ipv46(InetSocketAddress *addr, const char *optstr, Error **errp); int inet_parse(InetSocketAddress *addr, const char *str, Error **errp); int inet_connect(const char *str, Error **errp); int inet_connect_saddr(InetSocketAddress *saddr, Error **errp); diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index f2f3676d1f..085b057c18 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -627,14 +627,12 @@ static int inet_parse_flag(const char *flagname, const char *optstr, bool *val, return 0; } -int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) +const char *inet_parse_host_port(InetSocketAddress *addr, const char *str, + Error **errp) { - const char *optstr, *h; char host[65]; char port[33]; - int to; int pos; - char *begin; memset(addr, 0, sizeof(*addr)); @@ -644,38 +642,32 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) host[0] = '\0'; if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) { error_setg(errp, "error parsing port in address '%s'", str); - return -1; + return NULL; } } else if (str[0] == '[') { /* IPv6 addr */ if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) { error_setg(errp, "error parsing IPv6 address '%s'", str); - return -1; + return NULL; } } else { /* hostname or IPv4 addr */ if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) { error_setg(errp, "error parsing address '%s'", str); - return -1; + return NULL; } } addr->host = g_strdup(host); addr->port = g_strdup(port); - /* parse options */ - optstr = str + pos; - h = strstr(optstr, ",to="); - if (h) { - h += 4; - if (sscanf(h, "%d%n", &to, &pos) != 1 || - (h[pos] != '\0' && h[pos] != ',')) { - error_setg(errp, "error parsing to= argument"); - return -1; - } - addr->has_to = true; - addr->to = to; - } + return str + pos; +} + +int inet_parse_ipv46(InetSocketAddress *addr, const char *optstr, Error **errp) +{ + char *begin; + begin = strstr(optstr, ",ipv4"); if (begin) { if (inet_parse_flag("ipv4", begin + 5, &addr->ipv4, errp) < 0) { @@ -690,6 +682,39 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) } addr->has_ipv6 = true; } + + return 0; +} + +int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) +{ + const char *optstr, *h; + int to; + int pos; + char *begin; + + optstr = inet_parse_host_port(addr, str, errp); + if (optstr == NULL) { + return -1; + } + + /* parse options */ + + if (inet_parse_ipv46(addr, optstr, errp) < 0) { + return -1; + } + + h = strstr(optstr, ",to="); + if (h) { + h += 4; + if (sscanf(h, "%d%n", &to, &pos) != 1 || + (h[pos] != '\0' && h[pos] != ',')) { + error_setg(errp, "error parsing to= argument"); + return -1; + } + addr->has_to = true; + addr->to = to; + } begin = strstr(optstr, ",keep-alive"); if (begin) { if (inet_parse_flag("keep-alive", begin + strlen(",keep-alive"),