qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 20/21] qom-chrdrv: add TCPServer class


From: Anthony Liguori
Subject: [Qemu-devel] [PATCH 20/21] qom-chrdrv: add TCPServer class
Date: Sun, 24 Jul 2011 20:44:52 -0500

This is roughly equivalent to -chardev socket,path=PATH,port=PORT,server=on

Signed-off-by: Anthony Liguori <address@hidden>
---
 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]