From patchwork Mon Sep 26 19:50:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 12989469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 5602EC32771 for ; Mon, 26 Sep 2022 21:59:35 +0000 (UTC) Received: from localhost ([::1]:33784 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ocuU1-0004dA-U8 for qemu-devel@archiver.kernel.org; Mon, 26 Sep 2022 16:13:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34264) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ocu8E-0008Kz-6R for qemu-devel@nongnu.org; Mon, 26 Sep 2022 15:51:11 -0400 Received: from mout.kundenserver.de ([212.227.126.131]:49523) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ocu86-0005vk-Vb for qemu-devel@nongnu.org; Mon, 26 Sep 2022 15:51:05 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue012 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MJFpj-1owVLL2oyB-00Kh92; Mon, 26 Sep 2022 21:50:52 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Jason Wang , Greg Kurz , David Gibson , Thomas Huth , Eric Blake , "Dr. David Alan Gilbert" , Laurent Vivier , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Markus Armbruster , Stefano Brivio Subject: [PATCH v9 01/16] net: introduce convert_host_port() Date: Mon, 26 Sep 2022 21:50:33 +0200 Message-Id: <20220926195048.487915-2-lvivier@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926195048.487915-1-lvivier@redhat.com> References: <20220926195048.487915-1-lvivier@redhat.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:/5qTxHllPPNbDJqbkHiwdHXR7C0Vd3BMmUi+j0YlZGSmB7DQsbP k/Tf8Lnmd2OakNFdeuKgmeRD1soUw+WUEnWwdviyoC1yU1yrd8AsrlFxgBVc+PS9Mms3Pns HEXmOev1FPsuFAPUIXg7J3wkAPBaWmwIrPiy7kE4XuFZsfvKbDjS2mwuubcQLO4jHjC+IWv p+Rwwqy+d/ajGea1Pfowg== X-UI-Out-Filterresults: notjunk:1;V03:K0:j9rycoyXbQA=:HLM4HeHWqDl/8U+0L+XWhZ KwjpaOSg6KY9CE3+lABURMSGOzcog/20qR/8bgc4kunhSJ7tWmFUs2d5lLVNoam0iapX2hCjd BF13VWOF30vNdnklOgPCpQnFAJY7ALBUabJBjbxgBt+6CRXk+CCyzqLU+vh+GdWJWpcrewDJd 1aGLByxdQHbOs4C86FoQgoF7kccr9a0N54WsO0bIYVZNZBc1qk3apTrEeuvCf/20AxBEMnejH p7NBLRAaDQcnnSNHyOPAKKnsTGux2eoX9CTvdiitXLAQWWROqKhD8IsGBOBQ2sbQ0AVPS9DH4 wr7tOJKz6oC6WpXPQhjavnf51n6EltnWkXmDi+akzrtuRk+zO2jD6hYdKVYWxnPAnAj0+9QyW S7ZUYu9LacSX5puZ7gJlL9tvU4w8pmRQvvY1oIOH0V5VYkb5M4045ozEGtcC9KlfpHzhvZzLi ZuFdiYySg2iFRwKqIW6PJqbTL/qBEppSvH04dkxaLzM30LQdFEONE5aj8fwuskKbzPPo5cWiX nYoysch2gWaK+dEH+JoFzcVmSvxOQYtWL4Xut9NU/lXvR5V8ZoBizieNw6rXduLRRvwJeK5ce 7bVqxM+oIUuWJpx9zdNI3PqQkjmKomEZSJ/n01ZnWyQAoZy+EgpJoewQAkcY3JHumWRi3KQF1 EJ3Br62Z9jSXW5hTU6Q2aPl6PUadXPc4YjOnrqS9NIzL7wj9K+2vjJoWhu7y1sspOV7pJkpOH EeEU977Y38Jbfq2icaoPFDIpMjW9oL+3uqFY4YKXzQjU8AIH93QUtdBM3akYx31++BIy3eejo /N62LbJ Received-SPF: permerror client-ip=212.227.126.131; envelope-from=lvivier@redhat.com; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_FAIL=0.001, SPF_HELO_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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" Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Reviewed-by: David Gibson --- include/qemu/sockets.h | 2 ++ net/net.c | 62 ++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 038faa157f59..47194b9732f8 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -47,6 +47,8 @@ void socket_listen_cleanup(int fd, Error **errp); int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp); /* Old, ipv4 only bits. Don't use for new code. */ +int convert_host_port(struct sockaddr_in *saddr, const char *host, + const char *port, Error **errp); int parse_host_port(struct sockaddr_in *saddr, const char *str, Error **errp); int socket_init(void); diff --git a/net/net.c b/net/net.c index 2db160e0634d..d2288bd3a929 100644 --- a/net/net.c +++ b/net/net.c @@ -66,55 +66,57 @@ static QTAILQ_HEAD(, NetClientState) net_clients; /***********************************************************/ /* network device redirectors */ -int parse_host_port(struct sockaddr_in *saddr, const char *str, - Error **errp) +int convert_host_port(struct sockaddr_in *saddr, const char *host, + const char *port, Error **errp) { - gchar **substrings; struct hostent *he; - const char *addr, *p, *r; - int port, ret = 0; + const char *r; + long p; memset(saddr, 0, sizeof(*saddr)); - substrings = g_strsplit(str, ":", 2); - if (!substrings || !substrings[0] || !substrings[1]) { - error_setg(errp, "host address '%s' doesn't contain ':' " - "separating host from port", str); - ret = -1; - goto out; - } - - addr = substrings[0]; - p = substrings[1]; - saddr->sin_family = AF_INET; - if (addr[0] == '\0') { + if (host[0] == '\0') { saddr->sin_addr.s_addr = 0; } else { - if (qemu_isdigit(addr[0])) { - if (!inet_aton(addr, &saddr->sin_addr)) { + if (qemu_isdigit(host[0])) { + if (!inet_aton(host, &saddr->sin_addr)) { error_setg(errp, "host address '%s' is not a valid " - "IPv4 address", addr); - ret = -1; - goto out; + "IPv4 address", host); + return -1; } } else { - he = gethostbyname(addr); + he = gethostbyname(host); if (he == NULL) { - error_setg(errp, "can't resolve host address '%s'", addr); - ret = -1; - goto out; + error_setg(errp, "can't resolve host address '%s'", host); + return -1; } saddr->sin_addr = *(struct in_addr *)he->h_addr; } } - port = strtol(p, (char **)&r, 0); - if (r == p) { - error_setg(errp, "port number '%s' is invalid", p); + if (qemu_strtol(port, &r, 0, &p) != 0) { + error_setg(errp, "port number '%s' is invalid", port); + return -1; + } + saddr->sin_port = htons(p); + return 0; +} + +int parse_host_port(struct sockaddr_in *saddr, const char *str, + Error **errp) +{ + gchar **substrings; + int ret; + + substrings = g_strsplit(str, ":", 2); + if (!substrings || !substrings[0] || !substrings[1]) { + error_setg(errp, "host address '%s' doesn't contain ':' " + "separating host from port", str); ret = -1; goto out; } - saddr->sin_port = htons(port); + + ret = convert_host_port(saddr, substrings[0], substrings[1], errp); out: g_strfreev(substrings);