From patchwork Sat Jun 18 07:54:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashijeet Acharya X-Patchwork-Id: 9185447 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E7F10601C0 for ; Sat, 18 Jun 2016 07:54:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CB6B02656B for ; Sat, 18 Jun 2016 07:54:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC80427D45; Sat, 18 Jun 2016 07:54:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 129992656B for ; Sat, 18 Jun 2016 07:54:52 +0000 (UTC) Received: from localhost ([::1]:33506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bEB5e-0007xl-CO for patchwork-qemu-devel@patchwork.kernel.org; Sat, 18 Jun 2016 03:54:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43515) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bEB5O-0007xf-U6 for qemu-devel@nongnu.org; Sat, 18 Jun 2016 03:54:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bEB5K-0004NA-M8 for qemu-devel@nongnu.org; Sat, 18 Jun 2016 03:54:33 -0400 Received: from mail-pa0-x241.google.com ([2607:f8b0:400e:c03::241]:34045) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bEB5K-0004N6-Ag for qemu-devel@nongnu.org; Sat, 18 Jun 2016 03:54:30 -0400 Received: by mail-pa0-x241.google.com with SMTP id us13so7204791pab.1 for ; Sat, 18 Jun 2016 00:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FfNe30i+VkOOFz1h9x8qzjCD6kkSXVMT2mcJGKJV0Hw=; b=sZRM+otLb+Ff4kf7vlYVUhwIYiAafxcLr5gqkcWTP6IZCPby0pcNl/Xy7EYjjvq4BV E6r7tqAUcZQJgnlM55OCiCXLrw3OjZKdjZuLLLSfZVM4KSg08gjWlT19mP+asL6wxdIB MTH/JwvDbwOB4u49aUkkes+V1tphYeMhYh+GGF0VMiytpxDVK5Jeo1I0KcEPE1fd0KP6 Xh1bHXd5F175QaONzCwlyAyuYgDJWNdtG2h31Gb3TpWERX8eZPrxNL7yGw+9ZBftOFSw G3QJDMbJ04s6tB0zTkUM/q3zWnMF4EjaLDuk/yzhffX9Ib1Wc3MN2miiONAAUs2ul/ar OXfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FfNe30i+VkOOFz1h9x8qzjCD6kkSXVMT2mcJGKJV0Hw=; b=euvXeevBIad/ecxhjAjeHQdHSq1J7no4hXMQ3E6mRxS5qjHHU6WkVHlg7HJbkLW2pT 8vNvvOahw6WmqzuUepwOnujJGkR/MpSfqpDHbRebx6LxLCVt19phdA73sX9uuemUjrIn i++0F5svs4ThQpernp5fozwG3pDdlK+oxrQHOYNDEUlg1n365KFq7qMVIERzvCI0Ee/O ZBupnjGkApdbAFbolJg5dW7ntD7UqXC0D0LFRb7u/ax3ejZbmSiu11brJRKvDEFF5yhy tbU9XBZwExLWKmeTJwMtzfOr4ntxukc/B42fXT6CBQXXmOf/pPPdvEGU24HBPd1K698+ fQCw== X-Gm-Message-State: ALyK8tLC0tUtAygmnPDyXTEMJT8hz4x7z6Y8ceqpIAt+KlBgBskUtNddChBr4C/G8zQ9cA== X-Received: by 10.66.12.102 with SMTP id x6mr7474251pab.151.1466236468930; Sat, 18 Jun 2016 00:54:28 -0700 (PDT) Received: from linux.local ([122.168.139.83]) by smtp.gmail.com with ESMTPSA id a87sm14999625pfc.63.2016.06.18.00.54.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 18 Jun 2016 00:54:28 -0700 (PDT) From: Ashijeet Acharya To: berrange@redhat.com Date: Sat, 18 Jun 2016 13:24:02 +0530 Message-Id: <1466236442-11513-1-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1466072448-16388-1-git-send-email-ashijeetacharya@gmail.com> References: <1466072448-16388-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c03::241 Subject: [Qemu-devel] [PATCH v2] Change net/socket.c to use socket_*() functions 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: stefanha@gmail.com, jasowang@redhat.com, qemu-devel@nongnu.org, kraxel@redhat.com, Ashijeet Acharya , pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on http://wiki.qemu.org/BiteSizedTasks page. Signed-off-by: Ashijeet Acharya Reviewed-by: Paolo Bonzini --- include/qemu/sockets.h | 16 ++++++++++++++- net/socket.c | 55 +++++++++++++++++++++++++------------------------- util/qemu-sockets.c | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 1bd9218..3a1a887 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -110,4 +110,18 @@ SocketAddress *socket_remote_address(int fd, Error **errp); void qapi_copy_SocketAddress(SocketAddress **p_dest, SocketAddress *src); -#endif /* QEMU_SOCKET_H */ +/** + * socket_address_to_string: + * @addr: the socket address struct + * @errp: pointer to uninitialized error object + * + * Get the string representation of the socket + * address. A pointer to the char array containing + * string format will be returned, the caller is + * required to release the returned value when no + * longer required with g_free. + * + * Returns: the socket address in string format, or NULL on error + */ +char *socket_address_to_string(struct SocketAddress *addr, Error **errp); +#endif /* QEMU_SOCKET_H */ \ No newline at end of file diff --git a/net/socket.c b/net/socket.c index 333fb9e..ae6f921 100644 --- a/net/socket.c +++ b/net/socket.c @@ -489,41 +489,30 @@ static int net_socket_listen_init(NetClientState *peer, { NetClientState *nc; NetSocketState *s; - struct sockaddr_in saddr; - int fd, ret; + SocketAddress *saddr; + int ret; + Error *local_error = NULL; - if (parse_host_port(&saddr, host_str) < 0) - return -1; - - fd = qemu_socket(PF_INET, SOCK_STREAM, 0); - if (fd < 0) { - perror("socket"); + saddr = socket_parse(host_str, &local_error); + if (saddr == NULL) { + error_report_err(local_error); return -1; } - qemu_set_nonblock(fd); - socket_set_fast_reuse(fd); - - ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); + ret = socket_listen(saddr, &local_error); if (ret < 0) { - perror("bind"); - closesocket(fd); - return -1; - } - ret = listen(fd, 0); - if (ret < 0) { - perror("listen"); - closesocket(fd); + error_report_err(local_error); return -1; } nc = qemu_new_net_client(&net_socket_info, peer, model, name); s = DO_UPCAST(NetSocketState, nc, nc); s->fd = -1; - s->listen_fd = fd; + s->listen_fd = ret; s->nc.link_down = true; qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); + qapi_free_SocketAddress(saddr); return 0; } @@ -534,10 +523,15 @@ static int net_socket_connect_init(NetClientState *peer, { NetSocketState *s; int fd, connected, ret; - struct sockaddr_in saddr; + char *addr_str; + SocketAddress *saddr; + Error *local_error = NULL; - if (parse_host_port(&saddr, host_str) < 0) + saddr = socket_parse(host_str, &local_error); + if (saddr == NULL) { + error_report_err(local_error); return -1; + } fd = qemu_socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -545,10 +539,9 @@ static int net_socket_connect_init(NetClientState *peer, return -1; } qemu_set_nonblock(fd); - connected = 0; for(;;) { - ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); + ret = socket_connect(saddr, &local_error, NULL, NULL); if (ret < 0) { if (errno == EINTR || errno == EWOULDBLOCK) { /* continue */ @@ -557,7 +550,7 @@ static int net_socket_connect_init(NetClientState *peer, errno == EINVAL) { break; } else { - perror("connect"); + error_report_err(local_error); closesocket(fd); return -1; } @@ -569,9 +562,15 @@ static int net_socket_connect_init(NetClientState *peer, s = net_socket_fd_init(peer, model, name, fd, connected); if (!s) return -1; + + addr_str = socket_address_to_string(saddr, &local_error); + if (addr_str == NULL) + return -1; + snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "socket: connect to %s:%d", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + "socket: connect to %s", addr_str); + qapi_free_SocketAddress(saddr); + g_free(addr_str); return 0; } diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 0d6cd1f..771b7db 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1151,3 +1151,39 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest, qmp_input_visitor_cleanup(qiv); qobject_decref(obj); } + +char *socket_address_to_string(struct SocketAddress *addr, Error **errp) +{ + char *buf; + InetSocketAddress *inet; + char host_port[INET6_ADDRSTRLEN + 5 + 4]; + + switch (addr->type) { + case SOCKET_ADDRESS_KIND_INET: + inet = addr->u.inet.data; + if (strchr(inet->host, ':') == NULL) { + snprintf(host_port, sizeof(host_port), "%s:%s", inet->host, + inet->port); + buf = g_strdup(host_port); + } else { + snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host, + inet->port); + buf = g_strdup(host_port); + } + break; + + case SOCKET_ADDRESS_KIND_UNIX: + buf = g_strdup(addr->u.q_unix.data->path); + break; + + case SOCKET_ADDRESS_KIND_FD: + buf = g_strdup(addr->u.fd.data->str); + break; + + default: + error_setg(errp, "socket family %d unsupported", + addr->type); + return NULL; + } + return buf; +}