From patchwork Fri Oct 21 09:09:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 13014518 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 9763BC4332F for ; Fri, 21 Oct 2022 09:37:05 +0000 (UTC) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloSe-0007yN-8V for qemu-devel@archiver.kernel.org; Fri, 21 Oct 2022 05:37:04 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2f-00046y-Su for qemu-devel@archiver.kernel.org; Fri, 21 Oct 2022 05:10:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2C-0003FN-Cb for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:46 -0400 Received: from mout.kundenserver.de ([212.227.17.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2A-0001QE-CZ for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:44 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MsIbU-1p58Kj0saw-00tmMU; Fri, 21 Oct 2022 11:09:27 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Thomas Huth , xen-devel@lists.xenproject.org, "Dr. David Alan Gilbert" , Laurent Vivier , Anthony Perard , Stefan Weil , David Gibson , Stefano Stabellini , Paul Durrant , Eric Blake , "Michael S. Tsirkin" , Jason Wang , Paolo Bonzini , Markus Armbruster , Samuel Thibault , Greg Kurz , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Stefano Brivio Subject: [PATCH v14 01/17] net: introduce convert_host_port() Date: Fri, 21 Oct 2022 11:09:06 +0200 Message-Id: <20221021090922.170074-2-lvivier@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221021090922.170074-1-lvivier@redhat.com> References: <20221021090922.170074-1-lvivier@redhat.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:UNmkNBqxdJnZvFveDMtqcTp2IHg/srXUbTn7wvcQ6FXdR9eHdCI RymESjpbgHGgZLXTi7Cr9OYSRxR6uLn3c2MfVil4zr7qdmCHx/8lgPGug/eoDENeWDMVo/3 TIxSdTTk8O6yHb+ZZxasoaOAg3Uv/EhMd2YNxEoDuLo6ALd/Kd5hqO3zWj/PLYOw9ijj75m g5tzGHRaQlEIpdcpZHJuA== X-UI-Out-Filterresults: notjunk:1;V03:K0:s9mbqRMoCWg=:Vvu4jEFi7gSp1MxnnyOqdQ uNVgBU52KTHRU95T8ixNi4iL9ETl0St7D/EOr0FJi5lFVS1hzmI2SPUvcXK0SNzVUwJtonrDh pqI4uuRGIAS2liEZgp6rppwm39GXsLJhuW6qWSbLK6teWiWiIrg2bmxTDXab1itBBXETmjAoK AhCr6dkliXdZpRkDTD29Qw7KH3gbaQc/SmqBmizotxYRxJYUNW07+5x+tIQwY6pHaCxjGgL+y 4G37XXWiJklks26D84xZK9kiD3ZPpFOsUfpQJn2dD3LvzkWwrr9sygivnXhK7eVEQDd6s0qo+ sb8Kni9LeChuhiuvTjganF4tP4xpUP8sUkxXc+UwUuAH41TIa/3k45feMgJlYR2eh8GQv/kQf OlyuJeEbTmMjB4fI2sJPAJYZq95X0bJYxSu5u9LCojvchYBoCJdK+BS6ajh/HlnY374JzPu60 KgLBPJq/+cIM8TzgnXlk+dCaiEUx3I+pSjOX9BtDWaZ9eXK8abjfTVyhArsrla4/tTlTr8qlh 73J36nKWoYZjwE+oz5Nli9yGAO8QItb4+Oug4w3KiC/WGNGHres/DGo97Odnz1OEg2Icdozu9 xOVc/CUsBkjaJNNgJX1I+9sqvwSZxhmvkFZAw+9j/6M4/B+Lt6qQ6qGq4npvzBXrCpBzgYKLb FtchoTESY4u1Mn4+n4furBDxIUWaJGOOxDakOdC+9XxYVoZvU/MWcxSN8iHz02f1PzI9hzLJ2 2aHH+UOSsKFnPzLTe35+GiAYKU/1nVTWL/OYdbDpRDmRnFS1HHNdGVaJUwJ89DsA/+PF/z3N1 1zf05mt Received-SPF: permerror client-ip=212.227.17.13; 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 Acked-by: Michael S. Tsirkin --- 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 036745e58653..db4bedb6fa20 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -65,6 +65,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);