From: Zihan Yang <whois.zihan.yang@gmail.com>
To: qemu-devel@nongnu.org
Cc: Zihan Yang <whois.zihan.yang@gmail.com>,
Jason Wang <jasowang@redhat.com>
Subject: [Qemu-devel] [RFC 3/4] net/socket: change net_socket_listen_init to use functions in include/qemu/sockets.h
Date: Tue, 30 Jan 2018 03:13:43 +0800 [thread overview]
Message-ID: <1517253224-14361-4-git-send-email-whois.zihan.yang@gmail.com> (raw)
In-Reply-To: <1517253224-14361-1-git-send-email-whois.zihan.yang@gmail.com>
Some functions in net/socket.c still uses bind/connect/listen
directly. Change it to use functions in include/qemu/sockets.h
Signed-off-by: Zihan Yang <whois.zihan.yang@gmail.com>
---
net/socket.c | 41 +++++++++++++++++++++--------------------
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/net/socket.c b/net/socket.c
index 6917fbc..7b07223 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -494,35 +494,35 @@ static int net_socket_listen_init(NetClientState *peer,
{
NetClientState *nc;
NetSocketState *s;
- struct sockaddr_in saddr;
- int fd, ret;
+ SocketAddress *saddr;
+ QemuSocketConfig *sconf;
+ Error *local_err = NULL;
+ int fd;
- if (parse_host_port(&saddr, host_str, errp) < 0) {
+ saddr = socket_parse(host_str, &local_err);
+ if(NULL != local_err) {
+ error_setg_errno(errp, errno, "socket_parse failed");
return -1;
}
- fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
- if (fd < 0) {
- error_setg_errno(errp, errno, "can't create stream socket");
+ /* Manually prepare config because socket_listen
++ * will not set O_NONBLOCKING */
+ sconf = g_new0(QemuSocketConfig, 1);
+ if(NULL == sconf) {
+ qapi_free_SocketAddress(saddr);
+ error_setg_errno(errp, errno, "out of memory");
return -1;
}
- qemu_set_nonblock(fd);
+ sconf->nonblocking = true;
+ sconf->options = NULL;
- socket_set_fast_reuse(fd);
-
- ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
- if (ret < 0) {
- error_setg_errno(errp, errno, "can't bind ip=%s to socket",
- inet_ntoa(saddr.sin_addr));
- closesocket(fd);
- return -1;
- }
- ret = listen(fd, 0);
- if (ret < 0) {
- error_setg_errno(errp, errno, "can't listen on socket");
- closesocket(fd);
+ fd = socket_listen(saddr, errp, sconf);
+ if(fd < 0) {
+ error_setg_errno(errp, errno, "can't listen on address");
+ qapi_free_SocketAddress(saddr);
return -1;
}
+ g_free(sconf);
nc = qemu_new_net_client(&net_socket_info, peer, model, name);
s = DO_UPCAST(NetSocketState, nc, nc);
@@ -532,6 +532,7 @@ static int net_socket_listen_init(NetClientState *peer,
net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+ qapi_free_SocketAddress(saddr);
return 0;
}
--
2.7.4
next prev parent reply other threads:[~2018-01-29 19:14 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-29 19:13 [Qemu-devel] [RFC 0/4] Allow custom socket option in socket_listen and socket_connect Zihan Yang
2018-01-29 19:13 ` [Qemu-devel] [RFC 1/4] qemu-socket: Allow custom socket option in socket_listen Zihan Yang
2018-01-31 9:47 ` Daniel P. Berrangé
2018-01-29 19:13 ` [Qemu-devel] [RFC 2/4] qemu-socket: Allow custom socket options in socket_connect Zihan Yang
2018-01-31 9:51 ` Daniel P. Berrangé
2018-01-31 15:20 ` [Qemu-devel] Fwd: " Zihan Yang
2018-01-31 15:26 ` Daniel P. Berrangé
2018-01-29 19:13 ` Zihan Yang [this message]
2018-01-29 19:13 ` [Qemu-devel] [RFC 4/4] net/socket: change net_socket_connect_init to use functions in sockets.h Zihan Yang
2018-01-29 19:47 ` [Qemu-devel] [RFC 0/4] Allow custom socket option in socket_listen and socket_connect no-reply
2018-01-29 20:19 ` no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1517253224-14361-4-git-send-email-whois.zihan.yang@gmail.com \
--to=whois.zihan.yang@gmail.com \
--cc=jasowang@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).