From patchwork Wed Jun 28 13:08:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mao Zhongyi X-Patchwork-Id: 781684 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wyNS72xr5z9s7m for ; Wed, 28 Jun 2017 23:09:59 +1000 (AEST) Received: from localhost ([::1]:33304 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQCjF-0004AY-0V for incoming@patchwork.ozlabs.org; Wed, 28 Jun 2017 09:09:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52982) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQCia-00048I-39 for qemu-devel@nongnu.org; Wed, 28 Jun 2017 09:09:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dQCiY-0005eZ-Mq for qemu-devel@nongnu.org; Wed, 28 Jun 2017 09:09:16 -0400 Received: from [59.151.112.132] (port=42341 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQCiX-0005d2-82 for qemu-devel@nongnu.org; Wed, 28 Jun 2017 09:09:14 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="20600668" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 28 Jun 2017 21:09:07 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id F240B47EE1F4; Wed, 28 Jun 2017 21:09:03 +0800 (CST) Received: from maozy.g08.fujitsu.local (10.167.225.76) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 28 Jun 2017 21:09:02 +0800 From: Mao Zhongyi To: Date: Wed, 28 Jun 2017 21:08:49 +0800 Message-ID: X-Mailer: git-send-email 2.9.4 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.167.225.76] X-yoursite-MailScanner-ID: F240B47EE1F4.A3BF9 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: maozy.fnst@cn.fujitsu.com X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Subject: [Qemu-devel] [PATCH v6 3/4] net/net: Convert parse_host_port() to Error X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, jasowang@redhat.com, kraxel@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Cc: berrange@redhat.com Cc: kraxel@redhat.com Cc: pbonzini@redhat.com Cc: jasowang@redhat.com Cc: armbru@redhat.com Signed-off-by: Mao Zhongyi --- include/qemu/sockets.h | 3 ++- net/net.c | 23 ++++++++++++++++++----- net/socket.c | 19 ++++++++++++++----- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 5c326db..78e2b30 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -53,7 +53,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 parse_host_port(struct sockaddr_in *saddr, const char *str); +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 6235aab..29cc7df 100644 --- a/net/net.c +++ b/net/net.c @@ -100,7 +100,8 @@ static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) return 0; } -int parse_host_port(struct sockaddr_in *saddr, const char *str) +int parse_host_port(struct sockaddr_in *saddr, const char *str, + Error **errp) { char buf[512]; struct hostent *he; @@ -108,24 +109,36 @@ int parse_host_port(struct sockaddr_in *saddr, const char *str) int port; p = str; - if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) + if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { + error_setg(errp, "host address '%s' doesn't contain ':' " + "separating host from port", str); return -1; + } saddr->sin_family = AF_INET; if (buf[0] == '\0') { saddr->sin_addr.s_addr = 0; } else { if (qemu_isdigit(buf[0])) { - if (!inet_aton(buf, &saddr->sin_addr)) + if (!inet_aton(buf, &saddr->sin_addr)) { + error_setg(errp, "host address '%s' is not a valid " + "IPv4 address", buf); return -1; + } } else { - if ((he = gethostbyname(buf)) == NULL) + he = gethostbyname(buf); + if (he == NULL) { + error_setg(errp, "can't resolve host address '%s': " + "unknown host", buf); return - 1; + } saddr->sin_addr = *(struct in_addr *)he->h_addr; } } port = strtol(p, (char **)&r, 0); - if (r == p) + if (r == p) { + error_setg(errp, "port number '%s' is invalid", p); return -1; + } saddr->sin_port = htons(port); return 0; } diff --git a/net/socket.c b/net/socket.c index b6bacf4..90dd4c0 100644 --- a/net/socket.c +++ b/net/socket.c @@ -500,9 +500,12 @@ static int net_socket_listen_init(NetClientState *peer, NetSocketState *s; struct sockaddr_in saddr; int fd, ret; + Error *err = NULL; - if (parse_host_port(&saddr, host_str) < 0) + if (parse_host_port(&saddr, host_str, &err) < 0) { + error_report_err(err); return -1; + } fd = qemu_socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -547,8 +550,10 @@ static int net_socket_connect_init(NetClientState *peer, struct sockaddr_in saddr; Error *err = NULL; - if (parse_host_port(&saddr, host_str) < 0) + if (parse_host_port(&saddr, host_str, &err) < 0) { + error_report_err(err); return -1; + } fd = qemu_socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -600,8 +605,10 @@ static int net_socket_mcast_init(NetClientState *peer, struct in_addr localaddr, *param_localaddr; Error *err = NULL; - if (parse_host_port(&saddr, host_str) < 0) + if (parse_host_port(&saddr, host_str, &err) < 0) { + error_report_err(err); return -1; + } if (localaddr_str != NULL) { if (inet_aton(localaddr_str, &localaddr) == 0) @@ -643,11 +650,13 @@ static int net_socket_udp_init(NetClientState *peer, struct sockaddr_in laddr, raddr; Error *err = NULL; - if (parse_host_port(&laddr, lhost) < 0) { + if (parse_host_port(&laddr, lhost, &err) < 0) { + error_report_err(err); return -1; } - if (parse_host_port(&raddr, rhost) < 0) { + if (parse_host_port(&raddr, rhost, &err) < 0) { + error_report_err(err); return -1; }