From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>
Subject: [Qemu-devel] [PATCH 20/21] qom-chrdrv: add TCPServer class
Date: Sun, 24 Jul 2011 20:44:52 -0500 [thread overview]
Message-ID: <1311558293-5855-21-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1311558293-5855-1-git-send-email-aliguori@us.ibm.com>
This is roughly equivalent to -chardev socket,path=PATH,port=PORT,server=on
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
chrdrv/Makefile | 1 +
chrdrv/Qconfig | 7 ++
chrdrv/tcpsrv.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++++
include/qemu/tcpsrv.h | 40 ++++++++++
4 files changed, 251 insertions(+), 0 deletions(-)
create mode 100644 chrdrv/tcpsrv.c
create mode 100644 include/qemu/tcpsrv.h
diff --git a/chrdrv/Makefile b/chrdrv/Makefile
index 84bff48..50a3d50 100644
--- a/chrdrv/Makefile
+++ b/chrdrv/Makefile
@@ -1,3 +1,4 @@
chrdrv-obj-$(CONFIG_CHRDRV) := chrdrv.o
chrdrv-obj-$(CONFIG_CHRDRV_MEM) += memchr.o
chrdrv-obj-$(CONFIG_CHRDRV_SOCKET) += socketchr.o
+chrdrv-obj-$(CONFIG_CHRDRV_TCP_SERVER) += tcpsrv.o
diff --git a/chrdrv/Qconfig b/chrdrv/Qconfig
index 417c063..ea22b50 100644
--- a/chrdrv/Qconfig
+++ b/chrdrv/Qconfig
@@ -17,3 +17,10 @@ config CHRDRV_SOCKET
depends on CHRDRV
help
Character driver that implements a socket based transport.
+
+config CHRDRV_TCP_SERVER
+ bool "TCP server character driver"
+ default y
+ depends on CHRDRV_SOCKET
+ help
+ Character driver that implements a TCP server transport.
diff --git a/chrdrv/tcpsrv.c b/chrdrv/tcpsrv.c
new file mode 100644
index 0000000..bab94f0
--- /dev/null
+++ b/chrdrv/tcpsrv.c
@@ -0,0 +1,203 @@
+#include "qemu/tcpsrv.h"
+
+void tcp_server_initialize(TcpServer *obj, const char *id)
+{
+ type_initialize(obj, TYPE_TCP_SERVER, id);
+}
+
+void tcp_server_finalize(TcpServer *obj)
+{
+ type_finalize(obj);
+}
+
+const char *tcp_server_get_host(TcpServer *obj, Error **errp)
+{
+ return obj->host;
+}
+
+void tcp_server_set_host(TcpServer *obj, const char *value, Error **errp)
+{
+ qemu_free(obj->host);
+ obj->host = qemu_strdup(value);
+
+ socket_server_rebind(SOCKET_SERVER(obj));
+}
+
+const char *tcp_server_get_peername(TcpServer *obj, Error **errp)
+{
+ /* FIXME */
+ return "w00t!";
+}
+
+const char *tcp_server_get_port(TcpServer *obj, Error **errp)
+{
+ return obj->port;
+}
+
+void tcp_server_set_port(TcpServer *obj, const char *value, Error **errp)
+{
+ qemu_free(obj->port);
+ obj->port = qemu_strdup(value);
+
+ socket_server_rebind(SOCKET_SERVER(obj));
+}
+
+bool tcp_server_get_ipv4(TcpServer *obj, Error **errp)
+{
+ return obj->ipv4;
+}
+
+void tcp_server_set_ipv4(TcpServer *obj, bool value, Error **errp)
+{
+ obj->ipv4 = value;
+ socket_server_rebind(SOCKET_SERVER(obj));
+}
+
+bool tcp_server_get_ipv6(TcpServer *obj, Error **errp)
+{
+ return obj->ipv6;
+}
+
+void tcp_server_set_ipv6(TcpServer *obj, bool value, Error **errp)
+{
+ obj->ipv6 = value;
+ socket_server_rebind(SOCKET_SERVER(obj));
+}
+
+static int tcp_server_make_listen_socket(SocketServer *ss)
+{
+ TcpServer *s = TCP_SERVER(ss);
+ struct addrinfo ai, *res, *e;
+ int ret;
+ int fd = -1;
+
+ memset(&ai, 0, sizeof(ai));
+ ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
+ ai.ai_family = PF_UNSPEC;
+ ai.ai_socktype = SOCK_STREAM;
+
+ if (s->ipv4) {
+ ai.ai_family = PF_INET;
+ }
+
+ if (s->ipv6) {
+ ai.ai_family = PF_INET6;
+ }
+
+ ret = getaddrinfo(s->host, s->port, &ai, &res);
+ if (ret != 0) {
+ return -ret;
+ }
+
+ for (e = res; e != NULL; e = e->ai_next) {
+ char uaddr[INET6_ADDRSTRLEN + 1];
+ char uport[32 + 1];
+ int on = 1;
+ int off = 0;
+
+ getnameinfo((struct sockaddr *)e->ai_addr, e->ai_addrlen,
+ uaddr, INET6_ADDRSTRLEN,
+ uport, sizeof(uport) - 1,
+ NI_NUMERICHOST | NI_NUMERICSERV);
+
+ fd = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
+ if (fd == -1) {
+ continue;
+ }
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+#ifdef IPV6_V6ONLY
+ if (e->ai_family == PF_INET6) {
+ /* listen on both ipv4 and ipv6 */
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
+ }
+#endif
+
+ ret = bind(fd, e->ai_addr, e->ai_addrlen);
+ if (ret == 0) {
+ break;
+ }
+
+ closesocket(fd);
+ fd = -1;
+ }
+
+ listen(fd, 1);
+
+ return fd;
+}
+
+static int tcp_server_accept(SocketServer *ss)
+{
+ TcpServer *s = TCP_SERVER(ss);
+ int fd;
+
+ do {
+ socklen_t addrlen = sizeof(s->peer);
+ fd = qemu_accept(ss->listen_fd, (struct sockaddr *)&s->peer, &addrlen);
+ } while (fd == -1 && errno == EINTR);
+
+ return fd;
+}
+
+static void tcp_server_init(TypeInstance *inst)
+{
+ TcpServer *s = TCP_SERVER(inst);
+
+ plug_add_property_str(PLUG(s), "host",
+ (PlugPropertyGetterStr *)tcp_server_get_host,
+ (PlugPropertySetterStr *)tcp_server_set_host,
+ PROP_F_READWRITE);
+
+ plug_add_property_str(PLUG(s), "port",
+ (PlugPropertyGetterStr *)tcp_server_get_port,
+ (PlugPropertySetterStr *)tcp_server_set_port,
+ PROP_F_READWRITE);
+
+ plug_add_property_bool(PLUG(s), "ipv4",
+ (PlugPropertyGetterBool *)tcp_server_get_ipv4,
+ (PlugPropertySetterBool *)tcp_server_set_ipv4,
+ PROP_F_READWRITE);
+
+ plug_add_property_bool(PLUG(s), "ipv6",
+ (PlugPropertyGetterBool *)tcp_server_get_ipv6,
+ (PlugPropertySetterBool *)tcp_server_set_ipv6,
+ PROP_F_READWRITE);
+
+ plug_add_property_str(PLUG(s), "peername",
+ (PlugPropertyGetterStr *)tcp_server_get_peername,
+ NULL,
+ PROP_F_READ);
+}
+
+static void tcp_server_fini(TypeInstance *inst)
+{
+ TcpServer *s = TCP_SERVER(inst);
+
+ qemu_free(s->port);
+ qemu_free(s->host);
+}
+
+static void tcp_server_class_init(TypeClass *class)
+{
+ SocketServerClass *ssc = SOCKET_SERVER_CLASS(class);
+
+ ssc->accept = tcp_server_accept;
+ ssc->make_listen_socket = tcp_server_make_listen_socket;
+}
+
+static TypeInfo tcp_server_type_info = {
+ .name = TYPE_TCP_SERVER,
+ .parent = TYPE_SOCKET_SERVER,
+ .instance_size = sizeof(TcpServer),
+ .instance_init = tcp_server_init,
+ .instance_finalize = tcp_server_fini,
+ .class_init = tcp_server_class_init,
+};
+
+static void register_backends(void)
+{
+ type_register_static(&tcp_server_type_info);
+}
+
+device_init(register_backends);
diff --git a/include/qemu/tcpsrv.h b/include/qemu/tcpsrv.h
new file mode 100644
index 0000000..c398483
--- /dev/null
+++ b/include/qemu/tcpsrv.h
@@ -0,0 +1,40 @@
+#ifndef CHAR_DRIVER_TCP_H
+#define CHAR_DRIVER_TCP_H
+
+#include "qemu/socketchr.h"
+#include "qemu_socket.h"
+
+typedef struct TcpServer
+{
+ SocketServer parent;
+
+ struct sockaddr_in peer;
+
+ char *host;
+ char *port;
+
+ bool ipv4;
+ bool ipv6;
+} TcpServer;
+
+#define TYPE_TCP_SERVER "tcp-server"
+#define TCP_SERVER(obj) TYPE_CHECK(TcpServer, obj, TYPE_TCP_SERVER)
+
+void tcp_server_initialize(TcpServer *obj, const char *id);
+void tcp_server_finalize(TcpServer *obj);
+
+const char *tcp_server_get_host(TcpServer *obj, Error **errp);
+void tcp_server_set_host(TcpServer *obj, const char *value, Error **errp);
+
+const char *tcp_server_get_port(TcpServer *obj, Error **errp);
+void tcp_server_set_port(TcpServer *obj, const char *value, Error **errp);
+
+bool tcp_server_get_ipv4(TcpServer *obj, Error **errp);
+void tcp_server_set_ipv4(TcpServer *obj, bool value, Error **errp);
+
+bool tcp_server_get_ipv6(TcpServer *obj, Error **errp);
+void tcp_server_set_ipv6(TcpServer *obj, bool value, Error **errp);
+
+const char *tcp_server_get_peername(TcpServer *obj, Error **errp);
+
+#endif
--
1.7.4.1
next prev parent reply other threads:[~2011-07-25 1:45 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-25 1:44 [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 01/21] qom: add make infrastructure Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 02/21] qom: convert QAPI to use Qconfig build system Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 03/21] qom: Add core type system Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 04/21] qom: add Plug class Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 05/21] plug: add Plug property type Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 06/21] plug: add socket " Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 07/21] plug: add generated property types Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 08/21] qom: add plug_create QMP command Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 09/21] qom: add plug_list " Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 10/21] qom: add plug_get " Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 11/21] qom: add plug_set " Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 12/21] qom: add plug_list_props " Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 13/21] qom: add plug_destroy command Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 14/21] qom: add example qsh command Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 15/21] qom: add Device class Anthony Liguori
2011-07-27 15:10 ` Peter Maydell
2011-07-27 16:07 ` Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 16/21] qom-devices: add a Pin class Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 17/21] qom: add CharDriver class Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 18/21] qom-chrdrv: add memory character driver Anthony Liguori
2011-07-25 1:44 ` [Qemu-devel] [PATCH 19/21] qom-chrdrv: add Socket base class Anthony Liguori
2011-07-25 1:44 ` Anthony Liguori [this message]
2011-07-25 1:44 ` [Qemu-devel] [PATCH 21/21] qom-chrdrv: add UnixServer Anthony Liguori
2011-07-25 11:21 ` [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Kevin Wolf
2011-07-25 12:45 ` Anthony Liguori
2011-07-25 13:08 ` Kevin Wolf
2011-07-25 13:10 ` Anthony Liguori
2011-07-26 12:59 ` Paolo Bonzini
2011-07-26 14:02 ` Anthony Liguori
2011-07-26 14:35 ` Paolo Bonzini
2011-07-26 15:34 ` Anthony Liguori
2011-07-26 18:26 ` Paolo Bonzini
2011-07-26 19:23 ` Anthony Liguori
2011-07-27 8:55 ` Paolo Bonzini
2011-07-27 12:48 ` Anthony Liguori
2011-07-27 15:33 ` Paolo Bonzini
2011-07-27 16:19 ` Anthony Liguori
2011-07-27 16:28 ` Anthony Liguori
2011-07-27 18:51 ` Paolo Bonzini
2011-07-27 20:01 ` Anthony Liguori
2011-07-28 7:36 ` Paolo Bonzini
2011-07-28 12:46 ` Anthony Liguori
2011-07-28 13:50 ` Paolo Bonzini
2011-07-28 14:03 ` Anthony Liguori
2011-07-28 14:41 ` Paolo Bonzini
2011-07-28 15:04 ` Anthony Liguori
2011-07-28 15:47 ` Paolo Bonzini
2011-07-28 17:59 ` Anthony Liguori
2011-07-29 7:19 ` Paolo Bonzini
2011-07-27 21:33 ` Peter Maydell
2011-07-27 22:31 ` Anthony Liguori
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=1311558293-5855-21-git-send-email-aliguori@us.ibm.com \
--to=aliguori@us.ibm.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).