[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 05/17] Change net/socket.c to use socket_*() function
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 05/17] Change net/socket.c to use socket_*() functions |
Date: |
Thu, 15 Sep 2016 16:21:44 +0200 |
From: Marc-André Lureau <address@hidden>
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. The task was
listed on http://wiki.qemu.org/BiteSizedTasks page.
Signed-off-by: Ashijeet Acharya <address@hidden>
Signed-off-by: Marc-André Lureau <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
net/socket.c | 127 +++++++++++++++++++++++++++++++++--------------------------
1 file changed, 71 insertions(+), 56 deletions(-)
diff --git a/net/socket.c b/net/socket.c
index 3f98eef..982c8de 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -489,90 +489,105 @@ static int net_socket_listen_init(NetClientState *peer,
{
NetClientState *nc;
NetSocketState *s;
- struct sockaddr_in saddr;
- int fd, ret;
-
- if (parse_host_port(&saddr, host_str) < 0)
- return -1;
+ SocketAddress *saddr;
+ int ret;
+ Error *local_error = NULL;
- 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);
+ qapi_free_SocketAddress(saddr);
+ 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;
}
+typedef struct {
+ NetClientState *peer;
+ SocketAddress *saddr;
+ char *model;
+ char *name;
+} socket_connect_data;
+
+static void socket_connect_data_free(socket_connect_data *c)
+{
+ qapi_free_SocketAddress(c->saddr);
+ g_free(c->model);
+ g_free(c->name);
+ g_free(c);
+}
+
+static void net_socket_connected(int fd, Error *err, void *opaque)
+{
+ socket_connect_data *c = opaque;
+ NetSocketState *s;
+ char *addr_str = NULL;
+ Error *local_error = NULL;
+
+ addr_str = socket_address_to_string(c->saddr, &local_error);
+ if (addr_str == NULL) {
+ error_report_err(local_error);
+ closesocket(fd);
+ goto end;
+ }
+
+ s = net_socket_fd_init(c->peer, c->model, c->name, fd, true);
+ if (!s) {
+ closesocket(fd);
+ goto end;
+ }
+
+ snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+ "socket: connect to %s", addr_str);
+
+end:
+ g_free(addr_str);
+ socket_connect_data_free(c);
+}
+
static int net_socket_connect_init(NetClientState *peer,
const char *model,
const char *name,
const char *host_str)
{
- NetSocketState *s;
- int fd, connected, ret;
- struct sockaddr_in saddr;
+ socket_connect_data *c = g_new0(socket_connect_data, 1);
+ int fd = -1;
+ Error *local_error = NULL;
- if (parse_host_port(&saddr, host_str) < 0)
- return -1;
+ c->peer = peer;
+ c->model = g_strdup(model);
+ c->name = g_strdup(name);
+ c->saddr = socket_parse(host_str, &local_error);
+ if (c->saddr == NULL) {
+ goto err;
+ }
- fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+ fd = socket_connect(c->saddr, &local_error, net_socket_connected, c);
if (fd < 0) {
- perror("socket");
- return -1;
+ goto err;
}
- qemu_set_nonblock(fd);
- connected = 0;
- for(;;) {
- ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
- if (ret < 0) {
- if (errno == EINTR || errno == EWOULDBLOCK) {
- /* continue */
- } else if (errno == EINPROGRESS ||
- errno == EALREADY ||
- errno == EINVAL) {
- break;
- } else {
- perror("connect");
- closesocket(fd);
- return -1;
- }
- } else {
- connected = 1;
- break;
- }
- }
- s = net_socket_fd_init(peer, model, name, fd, connected);
- if (!s)
- 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));
return 0;
+
+err:
+ error_report_err(local_error);
+ socket_connect_data_free(c);
+ return -1;
}
static int net_socket_mcast_init(NetClientState *peer,
--
1.8.3.1
- [Qemu-devel] [PULL 00/17] Second batch of misc patches for QEMU 2.8, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 01/17] scsi-disk: Cleaning up around tray open state, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 03/17] scsi: mptsas: use g_new0 to allocate MPTSASRequest object, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 02/17] virtio-scsi: Don't abort when media is ejected, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 04/17] cutils: Rewrite x86 buffer zero checking, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 05/17] Change net/socket.c to use socket_*() functions,
Paolo Bonzini <=
- [Qemu-devel] [PULL 06/17] memory: remove memory_region_destructor_rom_device, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 07/17] scsi: pvscsi: limit process IO loop to ring size, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 08/17] pc: apic: fix touch LAPIC when irqchip is split, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 09/17] target-i386: fix ordering of fields in CPUX86State, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 10/17] linux-user: complete omission of removing uses of strdup, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 11/17] build-sys: add make 'help' target, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 12/17] qemu-char: avoid segfault if user lacks of permisson of a given logfile, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 13/17] log: fix parsing of multiple trace:PATTERN log args, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 14/17] target-i386: Fixed syscall posssible segfault, Paolo Bonzini, 2016/09/15
- [Qemu-devel] [PULL 15/17] pc: apic: introduce APIC macro, Paolo Bonzini, 2016/09/15