[Spice-commits] 4 commits - server/reds.c server/reds-private.h server/smartcard.c server/smartcard.h

Jonathon Jongsma jjongsma at kemper.freedesktop.org
Wed Apr 6 16:57:55 UTC 2016


 server/reds-private.h |    4 
 server/reds.c         |  564 ++++++++++++++++++++++++++++----------------------
 server/smartcard.c    |  312 ++++++++++++++-------------
 server/smartcard.h    |   30 ++
 4 files changed, 511 insertions(+), 399 deletions(-)

New commits:
commit 1cec1c5118b65124de6bc6f984f376ff4e297bfb
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Fri Apr 1 13:37:47 2016 +0100

    reds: Make VDIPortState a GObject
    
    This inherits from RedCharDevice.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/server/reds.c b/server/reds.c
index 21b0d9b..9283288 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -182,7 +182,7 @@ enum {
 };
 
 struct RedCharDeviceVDIPortPrivate {
-    RedCharDevice *base;
+    gboolean agent_attached;
     uint32_t plug_generation;
     int client_agent_started;
 
@@ -206,10 +206,6 @@ struct RedCharDeviceVDIPortPrivate {
                                        before agent is attached */
 };
 
-struct RedCharDeviceVDIPort {
-    struct RedCharDeviceVDIPortPrivate priv[1];
-};
-
 /* messages that are addressed to the agent and are created in the server */
 typedef struct __attribute__ ((__packed__)) VDInternalBuf {
     VDIChunkHeader chunk_header;
@@ -220,6 +216,37 @@ typedef struct __attribute__ ((__packed__)) VDInternalBuf {
     u;
 } VDInternalBuf;
 
+#define RED_TYPE_CHAR_DEVICE_VDIPORT red_char_device_vdi_port_get_type()
+
+#define RED_CHAR_DEVICE_VDIPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), RED_TYPE_CHAR_DEVICE_VDIPORT, RedCharDeviceVDIPort))
+#define RED_CHAR_DEVICE_VDIPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), RED_TYPE_CHAR_DEVICE_VDIPORT, RedCharDeviceVDIPortClass))
+#define RED_IS_CHAR_DEVICE_VDIPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), RED_TYPE_CHAR_DEVICE_VDIPORT))
+#define RED_IS_CHAR_DEVICE_VDIPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), RED_TYPE_CHAR_DEVICE_VDIPORT))
+#define RED_CHAR_DEVICE_VDIPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), RED_TYPE_CHAR_DEVICE_VDIPORT, RedCharDeviceVDIPortClass))
+
+typedef struct RedCharDeviceVDIPort RedCharDeviceVDIPort;
+typedef struct RedCharDeviceVDIPortClass RedCharDeviceVDIPortClass;
+typedef struct RedCharDeviceVDIPortPrivate RedCharDeviceVDIPortPrivate;
+
+struct RedCharDeviceVDIPort
+{
+    RedCharDevice parent;
+
+    RedCharDeviceVDIPortPrivate *priv;
+};
+
+struct RedCharDeviceVDIPortClass
+{
+    RedCharDeviceClass parent_class;
+};
+
+static GType red_char_device_vdi_port_get_type(void) G_GNUC_CONST;
+
+G_DEFINE_TYPE(RedCharDeviceVDIPort, red_char_device_vdi_port, RED_TYPE_CHAR_DEVICE)
+
+#define RED_CHAR_DEVICE_VDIPORT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RED_TYPE_CHAR_DEVICE_VDIPORT, RedCharDeviceVDIPortPrivate))
+
+static RedCharDeviceVDIPort *red_char_device_vdi_port_new(RedsState *reds);
 
 static void migrate_timeout(void *opaque);
 static RedsMigTargetClient* reds_mig_target_client_find(RedsState *reds, RedClient *client);
@@ -489,10 +516,9 @@ static void reds_reset_vdp(RedsState *reds)
      */
     if (red_channel_test_remote_cap(&reds->main_channel->base,
                                     SPICE_MAIN_CAP_AGENT_CONNECTED_TOKENS)) {
-        red_char_device_destroy(dev->priv->base);
-        dev->priv->base = NULL;
+        dev->priv->agent_attached = FALSE;
     } else {
-        red_char_device_reset(dev->priv->base);
+        red_char_device_reset(RED_CHAR_DEVICE(dev));
     }
 
     sif = spice_char_device_get_interface(reds->vdagent);
@@ -540,11 +566,11 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
         reds_mig_remove_wait_disconnect_client(reds, client);
     }
 
-    if (reds->agent_dev->priv->base) {
+    if (reds->agent_dev->priv->agent_attached) {
         /* note that vdagent might be NULL, if the vdagent was once
          * up and than was removed */
-        if (red_char_device_client_exists(reds->agent_dev->priv->base, client)) {
-            red_char_device_client_remove(reds->agent_dev->priv->base, client);
+        if (red_char_device_client_exists(RED_CHAR_DEVICE(reds->agent_dev), client)) {
+            red_char_device_client_remove(RED_CHAR_DEVICE(reds->agent_dev), client);
         }
     }
 
@@ -556,14 +582,14 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
    // if we are in the middle of one from another client)
     if (reds->num_clients == 0) {
         /* Let the agent know the client is disconnected */
-        if (reds->agent_dev->priv->base) {
+        if (reds->agent_dev->priv->agent_attached) {
             RedCharDeviceWriteBuffer *char_dev_buf;
             VDInternalBuf *internal_buf;
             uint32_t total_msg_size;
 
             total_msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage);
             char_dev_buf = red_char_device_write_buffer_get_server_no_token(
-                               reds->agent_dev->priv->base, total_msg_size);
+                               RED_CHAR_DEVICE(reds->agent_dev), total_msg_size);
             char_dev_buf->buf_used = total_msg_size;
             internal_buf = (VDInternalBuf *)char_dev_buf->buf;
             internal_buf->chunk_header.port = VDP_SERVER_PORT;
@@ -573,8 +599,8 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
             internal_buf->header.opaque = 0;
             internal_buf->header.size = 0;
 
-            red_char_device_write_buffer_add(reds->agent_dev->priv->base,
-                                               char_dev_buf);
+            red_char_device_write_buffer_add(RED_CHAR_DEVICE(reds->agent_dev),
+                                             char_dev_buf);
         }
 
         /* Reset write filter to start with clean state on client reconnect */
@@ -753,8 +779,8 @@ static void vdi_port_read_buf_unref(VDIReadBuf *buf)
         ring was empty. So we call it again so it can complete its work if
         necessary. Note that since we can be called from red_char_device_wakeup
         this can cause recursion, but we have protection for that */
-        if (buf->dev->priv->base) {
-            red_char_device_wakeup(buf->dev->priv->base);
+        if (buf->dev->priv->agent_attached) {
+            red_char_device_wakeup(RED_CHAR_DEVICE(buf->dev));
         }
     }
 }
@@ -894,13 +920,13 @@ void reds_handle_agent_mouse_event(RedsState *reds, const VDAgentMouseState *mou
     VDInternalBuf *internal_buf;
     uint32_t total_msg_size;
 
-    if (!reds->inputs_channel || !reds->agent_dev->priv->base) {
+    if (!reds->inputs_channel || !reds->agent_dev->priv->agent_attached) {
         return;
     }
 
     total_msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) +
                      sizeof(VDAgentMouseState);
-    char_dev_buf = red_char_device_write_buffer_get(reds->agent_dev->priv->base,
+    char_dev_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(reds->agent_dev),
                                                     NULL,
                                                     total_msg_size);
 
@@ -921,7 +947,7 @@ void reds_handle_agent_mouse_event(RedsState *reds, const VDAgentMouseState *mou
     internal_buf->u.mouse_state = *mouse_state;
 
     char_dev_buf->buf_used = total_msg_size;
-    red_char_device_write_buffer_add(reds->agent_dev->priv->base, char_dev_buf);
+    red_char_device_write_buffer_add(RED_CHAR_DEVICE(reds->agent_dev), char_dev_buf);
 }
 
 int reds_get_n_channels(RedsState *reds)
@@ -977,7 +1003,7 @@ void reds_fill_channels(RedsState *reds, SpiceMsgChannels *channels_info)
 
 void reds_on_main_agent_start(RedsState *reds, MainChannelClient *mcc, uint32_t num_tokens)
 {
-    RedCharDevice *dev_state = reds->agent_dev->priv->base;
+    RedCharDevice *dev_state = RED_CHAR_DEVICE(reds->agent_dev);
     RedChannelClient *rcc;
 
     if (!reds->vdagent) {
@@ -1050,9 +1076,9 @@ uint8_t *reds_get_agent_data_buffer(RedsState *reds, MainChannelClient *mcc, siz
 
     spice_assert(dev->priv->recv_from_client_buf == NULL);
     client = main_channel_client_get_base(mcc)->client;
-    dev->priv->recv_from_client_buf = red_char_device_write_buffer_get(dev->priv->base,
-                                                                             client,
-                                                                             size + sizeof(VDIChunkHeader));
+    dev->priv->recv_from_client_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev),
+                                                                       client,
+                                                                       size + sizeof(VDIChunkHeader));
     dev->priv->recv_from_client_buf_pushed = FALSE;
     return dev->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader);
 }
@@ -1068,7 +1094,7 @@ void reds_release_agent_data_buffer(RedsState *reds, uint8_t *buf)
 
     spice_assert(buf == dev->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader));
     if (!dev->priv->recv_from_client_buf_pushed) {
-        red_char_device_write_buffer_release(reds->agent_dev->priv->base,
+        red_char_device_write_buffer_release(RED_CHAR_DEVICE(reds->agent_dev),
                                              dev->priv->recv_from_client_buf);
     }
     dev->priv->recv_from_client_buf = NULL;
@@ -1140,7 +1166,7 @@ void reds_on_main_agent_data(RedsState *reds, MainChannelClient *mcc, void *mess
     dev->priv->recv_from_client_buf->buf_used = sizeof(VDIChunkHeader) + size;
 
     dev->priv->recv_from_client_buf_pushed = TRUE;
-    red_char_device_write_buffer_add(reds->agent_dev->priv->base, dev->priv->recv_from_client_buf);
+    red_char_device_write_buffer_add(RED_CHAR_DEVICE(reds->agent_dev), dev->priv->recv_from_client_buf);
 }
 
 void reds_on_main_migrate_connected(RedsState *reds, int seamless)
@@ -1235,7 +1261,7 @@ void reds_marshall_migrate_data(RedsState *reds, SpiceMarshaller *m)
            is set to FALSE when the agent is disconnected and
            there is no need to track the client tokens
            (see reds_reset_vdp) */
-        spice_assert(!agent_dev->priv->base);
+        spice_assert(!agent_dev->priv->agent_attached);
         red_char_device_migrate_data_marshall_empty(m);
         null_agent_mig_data = spice_marshaller_reserve_space(m,
                                                              sizeof(SpiceMigrateDataMain) -
@@ -1246,7 +1272,7 @@ void reds_marshall_migrate_data(RedsState *reds, SpiceMarshaller *m)
         return;
     }
 
-    red_char_device_migrate_data_marshall(reds->agent_dev->priv->base, m);
+    red_char_device_migrate_data_marshall(RED_CHAR_DEVICE(reds->agent_dev), m);
     spice_marshaller_add_uint8(m, reds->agent_dev->priv->client_agent_started);
 
     mig_data.agent2client.chunk_header = agent_dev->priv->vdi_chunk_header;
@@ -1366,7 +1392,7 @@ static int reds_agent_state_restore(RedsState *reds, SpiceMigrateDataMain *mig_d
                 agent_dev->priv->read_filter.discard_all,
                 agent_dev->priv->read_filter.msg_data_to_read,
                 agent_dev->priv->read_filter.result);
-    return red_char_device_restore(agent_dev->priv->base, &mig_data->agent_base);
+    return red_char_device_restore(RED_CHAR_DEVICE(agent_dev), &mig_data->agent_base);
 }
 
 /*
@@ -1389,7 +1415,7 @@ int reds_handle_migrate_data(RedsState *reds, MainChannelClient *mcc,
         reds_send_mm_time(reds);
     }
     if (mig_data->agent_base.connected) {
-        if (agent_dev->priv->base) { // agent was attached before migration data has arrived
+        if (agent_dev->priv->agent_attached) { // agent was attached before migration data has arrived
             if (!reds->vdagent) {
                 spice_assert(agent_dev->priv->plug_generation > 0);
                 main_channel_push_agent_disconnected(reds->main_channel);
@@ -1415,7 +1441,7 @@ int reds_handle_migrate_data(RedsState *reds, MainChannelClient *mcc,
         spice_debug("agent was not attached on the source host");
         if (reds->vdagent) {
             /* red_char_device_client_remove disables waiting for migration data */
-            red_char_device_client_remove(agent_dev->priv->base,
+            red_char_device_client_remove(RED_CHAR_DEVICE(agent_dev),
                                           main_channel_client_get_base(mcc)->client);
             main_channel_push_agent_connected(reds->main_channel);
         }
@@ -3028,25 +3054,12 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
 {
     RedCharDeviceVDIPort *dev = reds->agent_dev;
     SpiceCharDeviceInterface *sif;
-    RedCharDeviceCallbacks char_dev_state_cbs;
-
-    if (!dev->priv->base) {
-        char_dev_state_cbs.read_one_msg_from_device = vdi_port_read_one_msg_from_device;
-        char_dev_state_cbs.ref_msg_to_client = vdi_port_ref_msg_to_client;
-        char_dev_state_cbs.unref_msg_to_client = vdi_port_unref_msg_to_client;
-        char_dev_state_cbs.send_msg_to_client = vdi_port_send_msg_to_client;
-        char_dev_state_cbs.send_tokens_to_client = vdi_port_send_tokens_to_client;
-        char_dev_state_cbs.remove_client = vdi_port_remove_client;
-        char_dev_state_cbs.on_free_self_token = vdi_port_on_free_self_token;
-
-        dev->priv->base = red_char_device_create(sin,
-                                                 reds,
-                                                 REDS_TOKENS_TO_SEND,
-                                                 REDS_NUM_INTERNAL_AGENT_MESSAGES,
-                                                 &char_dev_state_cbs,
-                                                 reds);
+
+    if (dev->priv->agent_attached) {
+        red_char_device_reset_dev_instance(RED_CHAR_DEVICE(dev), sin);
     } else {
-        red_char_device_reset_dev_instance(dev->priv->base, sin);
+        dev->priv->agent_attached = TRUE;
+        g_object_set(G_OBJECT(dev), "sin", sin, NULL);
     }
 
     reds->vdagent = sin;
@@ -3058,13 +3071,13 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
     }
 
     if (!reds_main_channel_connected(reds)) {
-        return dev->priv->base;
+        return RED_CHAR_DEVICE(dev);
     }
 
     dev->priv->read_filter.discard_all = FALSE;
-    reds->agent_dev->priv->plug_generation++;
+    dev->priv->plug_generation++;
 
-    if (reds->agent_dev->priv->mig_data ||
+    if (dev->priv->mig_data ||
         red_channel_is_waiting_for_migrate_data(&reds->main_channel->base)) {
         /* Migration in progress (code is running on the destination host):
          * 1.  Add the client to spice char device, if it was not already added.
@@ -3073,10 +3086,10 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
          * 2.b If this happens second ==> we already have spice migrate data
          *     then restore state
          */
-        if (!red_char_device_client_exists(reds->agent_dev->priv->base, reds_get_client(reds))) {
+        if (!red_char_device_client_exists(RED_CHAR_DEVICE(dev), reds_get_client(reds))) {
             int client_added;
 
-            client_added = red_char_device_client_add(reds->agent_dev->priv->base,
+            client_added = red_char_device_client_add(RED_CHAR_DEVICE(dev),
                                                       reds_get_client(reds),
                                                       TRUE, /* flow control */
                                                       REDS_VDI_PORT_NUM_RECEIVE_BUFFS,
@@ -3090,12 +3103,12 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
             }
         }
 
-        if (reds->agent_dev->priv->mig_data) {
-            spice_debug("restoring state from stored migration data");
-            spice_assert(reds->agent_dev->priv->plug_generation == 1);
-            reds_agent_state_restore(reds, reds->agent_dev->priv->mig_data);
-            free(reds->agent_dev->priv->mig_data);
-            reds->agent_dev->priv->mig_data = NULL;
+        if (dev->priv->mig_data) {
+            spice_debug("restoring dev from stored migration data");
+            spice_assert(dev->priv->plug_generation == 1);
+            reds_agent_state_restore(reds, dev->priv->mig_data);
+            free(dev->priv->mig_data);
+            dev->priv->mig_data = NULL;
         }
         else {
             spice_debug("waiting for migration data");
@@ -3106,7 +3119,7 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
         main_channel_push_agent_connected(reds->main_channel);
     }
 
-    return dev->priv->base;
+    return RED_CHAR_DEVICE(dev);
 }
 
 SPICE_GNUC_VISIBLE void spice_server_char_device_wakeup(SpiceCharDeviceInstance* sin)
@@ -3363,33 +3376,6 @@ SPICE_GNUC_VISIBLE int spice_server_remove_interface(SpiceBaseInstance *sin)
     return 0;
 }
 
-static void reds_init_vd_agent_resources(RedsState *reds)
-{
-    RedCharDeviceVDIPort *dev;
-    int i;
-
-    reds->agent_dev = g_new0(RedCharDeviceVDIPort, 1);
-    dev = reds->agent_dev;
-    ring_init(&dev->priv->read_bufs);
-    agent_msg_filter_init(&dev->priv->write_filter, reds->agent_copypaste,
-                          reds->agent_file_xfer,
-                          reds_use_client_monitors_config(reds), TRUE);
-    agent_msg_filter_init(&dev->priv->read_filter, reds->agent_copypaste,
-                          reds->agent_file_xfer,
-                          reds_use_client_monitors_config(reds), TRUE);
-
-    dev->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
-    dev->priv->receive_pos = (uint8_t *)&dev->priv->vdi_chunk_header;
-    dev->priv->receive_len = sizeof(dev->priv->vdi_chunk_header);
-
-    for (i = 0; i < REDS_VDI_PORT_NUM_RECEIVE_BUFFS; i++) {
-        VDIReadBuf *buf = spice_new0(VDIReadBuf, 1);
-        buf->dev = dev;
-        ring_item_init(&buf->link);
-        ring_add(&reds->agent_dev->priv->read_bufs, &buf->link);
-    }
-}
-
 static int do_spice_init(RedsState *reds, SpiceCoreInterface *core_interface)
 {
     spice_info("starting %s", VERSION);
@@ -3405,7 +3391,7 @@ static int do_spice_init(RedsState *reds, SpiceCoreInterface *core_interface)
     reds->core = &core_interface_adapter;
     reds->listen_socket = -1;
     reds->secure_listen_socket = -1;
-    reds_init_vd_agent_resources(reds);
+    reds->agent_dev = red_char_device_vdi_port_new(reds);
     ring_init(&reds->clients);
     reds->num_clients = 0;
     reds->main_dispatcher = main_dispatcher_new(reds, reds->core);
@@ -4282,3 +4268,87 @@ MainDispatcher* reds_get_main_dispatcher(RedsState *reds)
 {
     return reds->main_dispatcher;
 }
+
+static void red_char_device_vdi_port_constructed(GObject *object)
+{
+    RedCharDeviceVDIPort *dev = RED_CHAR_DEVICE_VDIPORT(object);
+    RedsState *reds;
+
+    G_OBJECT_CLASS(red_char_device_vdi_port_parent_class)->constructed(object);
+
+    g_object_get(dev, "spice-server", &reds, NULL);
+
+    agent_msg_filter_init(&dev->priv->write_filter, reds->agent_copypaste,
+                          reds->agent_file_xfer,
+                          reds_use_client_monitors_config(reds),
+                          TRUE);
+    agent_msg_filter_init(&dev->priv->read_filter, reds->agent_copypaste,
+                          reds->agent_file_xfer,
+                          reds_use_client_monitors_config(reds),
+                          TRUE);
+}
+
+static void
+red_char_device_vdi_port_init(RedCharDeviceVDIPort *self)
+{
+    int i;
+
+    self->priv = RED_CHAR_DEVICE_VDIPORT_PRIVATE(self);
+
+    ring_init(&self->priv->read_bufs);
+
+    self->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
+    self->priv->receive_pos = (uint8_t *)&self->priv->vdi_chunk_header;
+    self->priv->receive_len = sizeof(self->priv->vdi_chunk_header);
+
+    for (i = 0; i < REDS_VDI_PORT_NUM_RECEIVE_BUFFS; i++) {
+        VDIReadBuf *buf = spice_new0(VDIReadBuf, 1);
+        buf->dev = self;
+        ring_item_init(&buf->link);
+        ring_add(&self->priv->read_bufs, &buf->link);
+    }
+}
+
+static void
+red_char_device_vdi_port_finalize(GObject *object)
+{
+    RedCharDeviceVDIPort *dev = RED_CHAR_DEVICE_VDIPORT(object);
+
+   free(dev->priv->mig_data);
+   /* FIXME: need to free the VDIReadBuf allocated previously */
+}
+
+static void
+red_char_device_vdi_port_class_init(RedCharDeviceVDIPortClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof (RedCharDeviceVDIPortPrivate));
+
+    object_class->finalize = red_char_device_vdi_port_finalize;
+    object_class->constructed = red_char_device_vdi_port_constructed;
+}
+
+static RedCharDeviceVDIPort *red_char_device_vdi_port_new(RedsState *reds)
+{
+    RedCharDevice *char_dev;
+    RedCharDeviceCallbacks char_dev_cbs = {
+        .read_one_msg_from_device = vdi_port_read_one_msg_from_device,
+        .ref_msg_to_client = vdi_port_ref_msg_to_client,
+        .unref_msg_to_client = vdi_port_unref_msg_to_client,
+        .send_msg_to_client = vdi_port_send_msg_to_client,
+        .send_tokens_to_client = vdi_port_send_tokens_to_client,
+        .remove_client = vdi_port_remove_client,
+        .on_free_self_token = vdi_port_on_free_self_token,
+    };
+
+    char_dev = g_object_new(RED_TYPE_CHAR_DEVICE_VDIPORT,
+                            "spice-server", reds,
+                            "client-tokens-interval", REDS_TOKENS_TO_SEND,
+                            "self-tokens", REDS_NUM_INTERNAL_AGENT_MESSAGES,
+                            NULL);
+
+    red_char_device_set_callbacks(RED_CHAR_DEVICE(char_dev),
+                                  &char_dev_cbs, reds);
+    return RED_CHAR_DEVICE_VDIPORT(char_dev);
+}
commit eaaa8985fd7a936b5aa0c91426ee3d59bbc0a6d7
Author: Jonathon Jongsma <jjongsma at redhat.com>
Date:   Fri Apr 1 16:46:22 2016 -0500

    Rename VDIPortState to RedCharDeviceVDIPort
    
    Consistent with internal naming conventions, and prepares for
    conversion to GObject

diff --git a/server/reds-private.h b/server/reds-private.h
index 8842aad..80e2c41 100644
--- a/server/reds-private.h
+++ b/server/reds-private.h
@@ -88,14 +88,14 @@ typedef struct RedSSLParameters {
     char ciphersuite[256];
 } RedSSLParameters;
 
-typedef struct VDIPortState VDIPortState;
+typedef struct RedCharDeviceVDIPort RedCharDeviceVDIPort;
 
 struct RedsState {
     int listen_socket;
     int secure_listen_socket;
     SpiceWatch *listen_watch;
     SpiceWatch *secure_listen_watch;
-    VDIPortState *agent_state;
+    RedCharDeviceVDIPort *agent_dev;
     int pending_mouse_event;
     Ring clients;
     int num_clients;
diff --git a/server/reds.c b/server/reds.c
index fdfb54a..21b0d9b 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -167,7 +167,7 @@ struct ChannelSecurityOptions {
 };
 
 typedef struct VDIReadBuf {
-    VDIPortState *state;
+    RedCharDeviceVDIPort *dev;
     RingItem link;
     uint32_t refs;
 
@@ -181,7 +181,7 @@ enum {
     VDI_PORT_READ_STATE_READ_DATA,
 };
 
-struct VDIPortStatePrivate {
+struct RedCharDeviceVDIPortPrivate {
     RedCharDevice *base;
     uint32_t plug_generation;
     int client_agent_started;
@@ -206,8 +206,8 @@ struct VDIPortStatePrivate {
                                        before agent is attached */
 };
 
-struct VDIPortState {
-    struct VDIPortStatePrivate priv[1];
+struct RedCharDeviceVDIPort {
+    struct RedCharDeviceVDIPortPrivate priv[1];
 };
 
 /* messages that are addressed to the agent and are created in the server */
@@ -237,7 +237,7 @@ static void reds_set_mouse_mode(RedsState *reds, uint32_t mode);
 static uint32_t reds_qxl_ram_size(RedsState *reds);
 static int calc_compression_level(RedsState *reds);
 
-static VDIReadBuf *vdi_port_state_get_read_buf(VDIPortState *state);
+static VDIReadBuf *vdi_port_get_read_buf(RedCharDeviceVDIPort *dev);
 static VDIReadBuf *vdi_port_read_buf_ref(VDIReadBuf *buf);
 static void vdi_port_read_buf_unref(VDIReadBuf *buf);
 
@@ -451,19 +451,19 @@ static void reds_mig_cleanup(RedsState *reds)
 
 static void reds_reset_vdp(RedsState *reds)
 {
-    VDIPortState *state = reds->agent_state;
+    RedCharDeviceVDIPort *dev = reds->agent_dev;
     SpiceCharDeviceInterface *sif;
 
-    state->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
-    state->priv->receive_pos = (uint8_t *)&state->priv->vdi_chunk_header;
-    state->priv->receive_len = sizeof(state->priv->vdi_chunk_header);
-    state->priv->message_receive_len = 0;
-    if (state->priv->current_read_buf) {
-        vdi_port_read_buf_unref(state->priv->current_read_buf);
-        state->priv->current_read_buf = NULL;
+    dev->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
+    dev->priv->receive_pos = (uint8_t *)&dev->priv->vdi_chunk_header;
+    dev->priv->receive_len = sizeof(dev->priv->vdi_chunk_header);
+    dev->priv->message_receive_len = 0;
+    if (dev->priv->current_read_buf) {
+        vdi_port_read_buf_unref(dev->priv->current_read_buf);
+        dev->priv->current_read_buf = NULL;
     }
     /* Reset read filter to start with clean state when the agent reconnects */
-    agent_msg_filter_init(&state->priv->read_filter, reds->agent_copypaste,
+    agent_msg_filter_init(&dev->priv->read_filter, reds->agent_copypaste,
                           reds->agent_file_xfer,
                           reds_use_client_monitors_config(reds), TRUE);
     /* Throw away pending chunks from the current (if any) and future
@@ -472,27 +472,27 @@ static void reds_reset_vdp(RedsState *reds)
      * is disconnected. Currently, when an agent gets disconnected and reconnected,
      * messages that were directed to the previous instance of the agent continue
      * to be sent from the client. This TODO will require server, protocol, and client changes */
-    state->priv->write_filter.result = AGENT_MSG_FILTER_DISCARD;
-    state->priv->write_filter.discard_all = TRUE;
-    state->priv->client_agent_started = FALSE;
+    dev->priv->write_filter.result = AGENT_MSG_FILTER_DISCARD;
+    dev->priv->write_filter.discard_all = TRUE;
+    dev->priv->client_agent_started = FALSE;
 
-    /* resetting and not destroying the state as a workaround for a bad
+    /* resetting and not destroying the dev as a workaround for a bad
      * tokens management in the vdagent protocol:
      *  The client tokens' are set only once, when the main channel is initialized.
      *  Instead, it would have been more appropriate to reset them upon AGEN_CONNECT.
      *  The client tokens are tracked as part of the RedCharDeviceClient. Thus,
      *  in order to be backward compatible with the client, we need to track the tokens
-     *  even if the agent is detached. We don't destroy the char_device state, and
+     *  even if the agent is detached. We don't destroy the char_device, and
      *  instead we just reset it.
      *  In addition, there used to be a misshandling of AGENT_TOKENS message in spice-gtk: it
      *  overrides the amount of tokens, instead of adding the given amount.
      */
     if (red_channel_test_remote_cap(&reds->main_channel->base,
                                     SPICE_MAIN_CAP_AGENT_CONNECTED_TOKENS)) {
-        red_char_device_destroy(state->priv->base);
-        state->priv->base = NULL;
+        red_char_device_destroy(dev->priv->base);
+        dev->priv->base = NULL;
     } else {
-        red_char_device_reset(state->priv->base);
+        red_char_device_reset(dev->priv->base);
     }
 
     sif = spice_char_device_get_interface(reds->vdagent);
@@ -540,11 +540,11 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
         reds_mig_remove_wait_disconnect_client(reds, client);
     }
 
-    if (reds->agent_state->priv->base) {
+    if (reds->agent_dev->priv->base) {
         /* note that vdagent might be NULL, if the vdagent was once
          * up and than was removed */
-        if (red_char_device_client_exists(reds->agent_state->priv->base, client)) {
-            red_char_device_client_remove(reds->agent_state->priv->base, client);
+        if (red_char_device_client_exists(reds->agent_dev->priv->base, client)) {
+            red_char_device_client_remove(reds->agent_dev->priv->base, client);
         }
     }
 
@@ -556,14 +556,14 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
    // if we are in the middle of one from another client)
     if (reds->num_clients == 0) {
         /* Let the agent know the client is disconnected */
-        if (reds->agent_state->priv->base) {
+        if (reds->agent_dev->priv->base) {
             RedCharDeviceWriteBuffer *char_dev_buf;
             VDInternalBuf *internal_buf;
             uint32_t total_msg_size;
 
             total_msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage);
             char_dev_buf = red_char_device_write_buffer_get_server_no_token(
-                               reds->agent_state->priv->base, total_msg_size);
+                               reds->agent_dev->priv->base, total_msg_size);
             char_dev_buf->buf_used = total_msg_size;
             internal_buf = (VDInternalBuf *)char_dev_buf->buf;
             internal_buf->chunk_header.port = VDP_SERVER_PORT;
@@ -573,21 +573,21 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
             internal_buf->header.opaque = 0;
             internal_buf->header.size = 0;
 
-            red_char_device_write_buffer_add(reds->agent_state->priv->base,
+            red_char_device_write_buffer_add(reds->agent_dev->priv->base,
                                                char_dev_buf);
         }
 
         /* Reset write filter to start with clean state on client reconnect */
-        agent_msg_filter_init(&reds->agent_state->priv->write_filter, reds->agent_copypaste,
+        agent_msg_filter_init(&reds->agent_dev->priv->write_filter, reds->agent_copypaste,
                               reds->agent_file_xfer,
                               reds_use_client_monitors_config(reds), TRUE);
 
         /* Throw away pending chunks from the current (if any) and future
          *  messages read from the agent */
-        reds->agent_state->priv->read_filter.result = AGENT_MSG_FILTER_DISCARD;
-        reds->agent_state->priv->read_filter.discard_all = TRUE;
-        free(reds->agent_state->priv->mig_data);
-        reds->agent_state->priv->mig_data = NULL;
+        reds->agent_dev->priv->read_filter.result = AGENT_MSG_FILTER_DISCARD;
+        reds->agent_dev->priv->read_filter.discard_all = TRUE;
+        free(reds->agent_dev->priv->mig_data);
+        reds->agent_dev->priv->mig_data = NULL;
 
         reds_mig_cleanup(reds);
     }
@@ -691,15 +691,15 @@ static void vdi_port_read_buf_release(uint8_t *data, void *opaque)
 }
 
 /* returns TRUE if the buffer can be forwarded */
-static gboolean vdi_port_read_buf_process(VDIPortState *state, VDIReadBuf *buf, gboolean *error)
+static gboolean vdi_port_read_buf_process(RedCharDeviceVDIPort *dev, VDIReadBuf *buf, gboolean *error)
 {
     int res;
 
     *error = FALSE;
 
-    switch (state->priv->vdi_chunk_header.port) {
+    switch (dev->priv->vdi_chunk_header.port) {
     case VDP_CLIENT_PORT: {
-        res = agent_msg_filter_process_data(&state->priv->read_filter,
+        res = agent_msg_filter_process_data(&dev->priv->read_filter,
                                             buf->data, buf->len);
         switch (res) {
         case AGENT_MSG_FILTER_OK:
@@ -720,12 +720,12 @@ static gboolean vdi_port_read_buf_process(VDIPortState *state, VDIReadBuf *buf,
     }
 }
 
-static VDIReadBuf *vdi_port_state_get_read_buf(VDIPortState *state)
+static VDIReadBuf *vdi_port_get_read_buf(RedCharDeviceVDIPort *dev)
 {
     RingItem *item;
     VDIReadBuf *buf;
 
-    if (!(item = ring_get_head(&state->priv->read_bufs))) {
+    if (!(item = ring_get_head(&dev->priv->read_bufs))) {
         return NULL;
     }
 
@@ -747,14 +747,14 @@ static VDIReadBuf* vdi_port_read_buf_ref(VDIReadBuf *buf)
 static void vdi_port_read_buf_unref(VDIReadBuf *buf)
 {
     if (!--buf->refs) {
-        ring_add(&buf->state->priv->read_bufs, &buf->link);
+        ring_add(&buf->dev->priv->read_bufs, &buf->link);
 
         /* read_one_msg_from_vdi_port may have never completed because the read_bufs
         ring was empty. So we call it again so it can complete its work if
         necessary. Note that since we can be called from red_char_device_wakeup
         this can cause recursion, but we have protection for that */
-        if (buf->state->priv->base) {
-            red_char_device_wakeup(buf->state->priv->base);
+        if (buf->dev->priv->base) {
+            red_char_device_wakeup(buf->dev->priv->base);
         }
     }
 }
@@ -765,7 +765,7 @@ static RedCharDeviceMsgToClient *vdi_port_read_one_msg_from_device(SpiceCharDevi
                                                                    void *opaque)
 {
     RedsState *reds = opaque;
-    VDIPortState *state = reds->agent_state;
+    RedCharDeviceVDIPort *dev = reds->agent_dev;
     SpiceCharDeviceInterface *sif;
     VDIReadBuf *dispatch_buf;
     int n;
@@ -776,50 +776,50 @@ static RedCharDeviceMsgToClient *vdi_port_read_one_msg_from_device(SpiceCharDevi
     spice_assert(reds->vdagent == sin);
     sif = spice_char_device_get_interface(reds->vdagent);
     while (reds->vdagent) {
-        switch (state->priv->read_state) {
+        switch (dev->priv->read_state) {
         case VDI_PORT_READ_STATE_READ_HEADER:
-            n = sif->read(reds->vdagent, state->priv->receive_pos, state->priv->receive_len);
+            n = sif->read(reds->vdagent, dev->priv->receive_pos, dev->priv->receive_len);
             if (!n) {
                 return NULL;
             }
-            if ((state->priv->receive_len -= n)) {
-                state->priv->receive_pos += n;
+            if ((dev->priv->receive_len -= n)) {
+                dev->priv->receive_pos += n;
                 return NULL;
             }
-            state->priv->message_receive_len = state->priv->vdi_chunk_header.size;
-            state->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
+            dev->priv->message_receive_len = dev->priv->vdi_chunk_header.size;
+            dev->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
         case VDI_PORT_READ_STATE_GET_BUFF: {
-            if (!(state->priv->current_read_buf = vdi_port_state_get_read_buf(reds->agent_state))) {
+            if (!(dev->priv->current_read_buf = vdi_port_get_read_buf(reds->agent_dev))) {
                 return NULL;
             }
-            state->priv->receive_pos = state->priv->current_read_buf->data;
-            state->priv->receive_len = MIN(state->priv->message_receive_len,
-                                           sizeof(state->priv->current_read_buf->data));
-            state->priv->current_read_buf->len = state->priv->receive_len;
-            state->priv->message_receive_len -= state->priv->receive_len;
-            state->priv->read_state = VDI_PORT_READ_STATE_READ_DATA;
+            dev->priv->receive_pos = dev->priv->current_read_buf->data;
+            dev->priv->receive_len = MIN(dev->priv->message_receive_len,
+                                         sizeof(dev->priv->current_read_buf->data));
+            dev->priv->current_read_buf->len = dev->priv->receive_len;
+            dev->priv->message_receive_len -= dev->priv->receive_len;
+            dev->priv->read_state = VDI_PORT_READ_STATE_READ_DATA;
         }
         case VDI_PORT_READ_STATE_READ_DATA: {
             gboolean error = FALSE;
-            n = sif->read(reds->vdagent, state->priv->receive_pos, state->priv->receive_len);
+            n = sif->read(reds->vdagent, dev->priv->receive_pos, dev->priv->receive_len);
             if (!n) {
                 return NULL;
             }
-            if ((state->priv->receive_len -= n)) {
-                state->priv->receive_pos += n;
+            if ((dev->priv->receive_len -= n)) {
+                dev->priv->receive_pos += n;
                 break;
             }
-            dispatch_buf = state->priv->current_read_buf;
-            state->priv->current_read_buf = NULL;
-            state->priv->receive_pos = NULL;
-            if (state->priv->message_receive_len == 0) {
-                state->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
-                state->priv->receive_pos = (uint8_t *)&state->priv->vdi_chunk_header;
-                state->priv->receive_len = sizeof(state->priv->vdi_chunk_header);
+            dispatch_buf = dev->priv->current_read_buf;
+            dev->priv->current_read_buf = NULL;
+            dev->priv->receive_pos = NULL;
+            if (dev->priv->message_receive_len == 0) {
+                dev->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
+                dev->priv->receive_pos = (uint8_t *)&dev->priv->vdi_chunk_header;
+                dev->priv->receive_len = sizeof(dev->priv->vdi_chunk_header);
             } else {
-                state->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
+                dev->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
             }
-            if (vdi_port_read_buf_process(reds->agent_state, dispatch_buf, &error)) {
+            if (vdi_port_read_buf_process(reds->agent_dev, dispatch_buf, &error)) {
                 return dispatch_buf;
             } else {
                 if (error) {
@@ -894,13 +894,13 @@ void reds_handle_agent_mouse_event(RedsState *reds, const VDAgentMouseState *mou
     VDInternalBuf *internal_buf;
     uint32_t total_msg_size;
 
-    if (!reds->inputs_channel || !reds->agent_state->priv->base) {
+    if (!reds->inputs_channel || !reds->agent_dev->priv->base) {
         return;
     }
 
     total_msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) +
                      sizeof(VDAgentMouseState);
-    char_dev_buf = red_char_device_write_buffer_get(reds->agent_state->priv->base,
+    char_dev_buf = red_char_device_write_buffer_get(reds->agent_dev->priv->base,
                                                     NULL,
                                                     total_msg_size);
 
@@ -921,7 +921,7 @@ void reds_handle_agent_mouse_event(RedsState *reds, const VDAgentMouseState *mou
     internal_buf->u.mouse_state = *mouse_state;
 
     char_dev_buf->buf_used = total_msg_size;
-    red_char_device_write_buffer_add(reds->agent_state->priv->base, char_dev_buf);
+    red_char_device_write_buffer_add(reds->agent_dev->priv->base, char_dev_buf);
 }
 
 int reds_get_n_channels(RedsState *reds)
@@ -977,7 +977,7 @@ void reds_fill_channels(RedsState *reds, SpiceMsgChannels *channels_info)
 
 void reds_on_main_agent_start(RedsState *reds, MainChannelClient *mcc, uint32_t num_tokens)
 {
-    RedCharDevice *dev_state = reds->agent_state->priv->base;
+    RedCharDevice *dev_state = reds->agent_dev->priv->base;
     RedChannelClient *rcc;
 
     if (!reds->vdagent) {
@@ -985,7 +985,7 @@ void reds_on_main_agent_start(RedsState *reds, MainChannelClient *mcc, uint32_t
     }
     spice_assert(reds->vdagent->st && reds->vdagent->st == dev_state);
     rcc = main_channel_client_get_base(mcc);
-    reds->agent_state->priv->client_agent_started = TRUE;
+    reds->agent_dev->priv->client_agent_started = TRUE;
     /*
      * Note that in older releases, send_tokens were set to ~0 on both client
      * and server. The server ignored the client given tokens.
@@ -1015,10 +1015,10 @@ void reds_on_main_agent_start(RedsState *reds, MainChannelClient *mcc, uint32_t
                                                   num_tokens);
     }
 
-    agent_msg_filter_config(&reds->agent_state->priv->write_filter, reds->agent_copypaste,
+    agent_msg_filter_config(&reds->agent_dev->priv->write_filter, reds->agent_copypaste,
                             reds->agent_file_xfer,
                             reds_use_client_monitors_config(reds));
-    reds->agent_state->priv->write_filter.discard_all = FALSE;
+    reds->agent_dev->priv->write_filter.discard_all = FALSE;
 }
 
 void reds_on_main_agent_tokens(RedsState *reds, MainChannelClient *mcc, uint32_t num_tokens)
@@ -1034,45 +1034,45 @@ void reds_on_main_agent_tokens(RedsState *reds, MainChannelClient *mcc, uint32_t
 
 uint8_t *reds_get_agent_data_buffer(RedsState *reds, MainChannelClient *mcc, size_t size)
 {
-    VDIPortState *dev_state = reds->agent_state;
+    RedCharDeviceVDIPort *dev = reds->agent_dev;
     RedClient *client;
 
-    if (!dev_state->priv->client_agent_started) {
+    if (!dev->priv->client_agent_started) {
         /*
          * agent got disconnected, and possibly got reconnected, but we still can receive
          * msgs that are addressed to the agent's old instance, in case they were
          * sent by the client before it received the AGENT_DISCONNECTED msg.
          * In such case, we will receive and discard the msgs (reds_reset_vdp takes care
-         * of setting state->write_filter.result = AGENT_MSG_FILTER_DISCARD).
+         * of setting dev->write_filter.result = AGENT_MSG_FILTER_DISCARD).
          */
         return spice_malloc(size);
     }
 
-    spice_assert(dev_state->priv->recv_from_client_buf == NULL);
+    spice_assert(dev->priv->recv_from_client_buf == NULL);
     client = main_channel_client_get_base(mcc)->client;
-    dev_state->priv->recv_from_client_buf = red_char_device_write_buffer_get(dev_state->priv->base,
+    dev->priv->recv_from_client_buf = red_char_device_write_buffer_get(dev->priv->base,
                                                                              client,
                                                                              size + sizeof(VDIChunkHeader));
-    dev_state->priv->recv_from_client_buf_pushed = FALSE;
-    return dev_state->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader);
+    dev->priv->recv_from_client_buf_pushed = FALSE;
+    return dev->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader);
 }
 
 void reds_release_agent_data_buffer(RedsState *reds, uint8_t *buf)
 {
-    VDIPortState *dev_state = reds->agent_state;
+    RedCharDeviceVDIPort *dev = reds->agent_dev;
 
-    if (!dev_state->priv->recv_from_client_buf) {
+    if (!dev->priv->recv_from_client_buf) {
         free(buf);
         return;
     }
 
-    spice_assert(buf == dev_state->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader));
-    if (!dev_state->priv->recv_from_client_buf_pushed) {
-        red_char_device_write_buffer_release(reds->agent_state->priv->base,
-                                               dev_state->priv->recv_from_client_buf);
+    spice_assert(buf == dev->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader));
+    if (!dev->priv->recv_from_client_buf_pushed) {
+        red_char_device_write_buffer_release(reds->agent_dev->priv->base,
+                                             dev->priv->recv_from_client_buf);
     }
-    dev_state->priv->recv_from_client_buf = NULL;
-    dev_state->priv->recv_from_client_buf_pushed = FALSE;
+    dev->priv->recv_from_client_buf = NULL;
+    dev->priv->recv_from_client_buf_pushed = FALSE;
 }
 
 static void reds_client_monitors_config_cleanup(RedsState *reds)
@@ -1112,11 +1112,11 @@ static void reds_on_main_agent_monitors_config(RedsState *reds,
 
 void reds_on_main_agent_data(RedsState *reds, MainChannelClient *mcc, void *message, size_t size)
 {
-    VDIPortState *dev_state = reds->agent_state;
+    RedCharDeviceVDIPort *dev = reds->agent_dev;
     VDIChunkHeader *header;
     int res;
 
-    res = agent_msg_filter_process_data(&reds->agent_state->priv->write_filter,
+    res = agent_msg_filter_process_data(&reds->agent_dev->priv->write_filter,
                                         message, size);
     switch (res) {
     case AGENT_MSG_FILTER_OK:
@@ -1131,16 +1131,16 @@ void reds_on_main_agent_data(RedsState *reds, MainChannelClient *mcc, void *mess
         return;
     }
 
-    spice_assert(reds->agent_state->priv->recv_from_client_buf);
-    spice_assert(message == reds->agent_state->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader));
+    spice_assert(reds->agent_dev->priv->recv_from_client_buf);
+    spice_assert(message == reds->agent_dev->priv->recv_from_client_buf->buf + sizeof(VDIChunkHeader));
     // TODO - start tracking agent data per channel
-    header =  (VDIChunkHeader *)dev_state->priv->recv_from_client_buf->buf;
+    header =  (VDIChunkHeader *)dev->priv->recv_from_client_buf->buf;
     header->port = VDP_CLIENT_PORT;
     header->size = size;
-    dev_state->priv->recv_from_client_buf->buf_used = sizeof(VDIChunkHeader) + size;
+    dev->priv->recv_from_client_buf->buf_used = sizeof(VDIChunkHeader) + size;
 
-    dev_state->priv->recv_from_client_buf_pushed = TRUE;
-    red_char_device_write_buffer_add(reds->agent_state->priv->base, dev_state->priv->recv_from_client_buf);
+    dev->priv->recv_from_client_buf_pushed = TRUE;
+    red_char_device_write_buffer_add(reds->agent_dev->priv->base, dev->priv->recv_from_client_buf);
 }
 
 void reds_on_main_migrate_connected(RedsState *reds, int seamless)
@@ -1175,28 +1175,28 @@ void reds_on_main_mouse_mode_request(RedsState *reds, void *message, size_t size
  */
 void reds_on_main_channel_migrate(RedsState *reds, MainChannelClient *mcc)
 {
-    VDIPortState *agent_state = reds->agent_state;
+    RedCharDeviceVDIPort *agent_dev = reds->agent_dev;
     uint32_t read_data_len;
 
     spice_assert(reds->num_clients == 1);
 
-    if (agent_state->priv->read_state != VDI_PORT_READ_STATE_READ_DATA) {
+    if (agent_dev->priv->read_state != VDI_PORT_READ_STATE_READ_DATA) {
         return;
     }
-    spice_assert(agent_state->priv->current_read_buf->data &&
-                 agent_state->priv->receive_pos > agent_state->priv->current_read_buf->data);
-    read_data_len = agent_state->priv->receive_pos - agent_state->priv->current_read_buf->data;
+    spice_assert(agent_dev->priv->current_read_buf->data &&
+                 agent_dev->priv->receive_pos > agent_dev->priv->current_read_buf->data);
+    read_data_len = agent_dev->priv->receive_pos - agent_dev->priv->current_read_buf->data;
 
-    if (agent_state->priv->read_filter.msg_data_to_read ||
+    if (agent_dev->priv->read_filter.msg_data_to_read ||
         read_data_len > sizeof(VDAgentMessage)) { /* msg header has been read */
-        VDIReadBuf *read_buf = agent_state->priv->current_read_buf;
+        VDIReadBuf *read_buf = agent_dev->priv->current_read_buf;
         gboolean error = FALSE;
 
         spice_debug("push partial read %u (msg first chunk? %d)", read_data_len,
-                    !agent_state->priv->read_filter.msg_data_to_read);
+                    !agent_dev->priv->read_filter.msg_data_to_read);
 
         read_buf->len = read_data_len;
-        if (vdi_port_read_buf_process(reds->agent_state, read_buf, &error)) {
+        if (vdi_port_read_buf_process(reds->agent_dev, read_buf, &error)) {
             main_channel_client_push_agent_data(mcc,
                                                 read_buf->data,
                                                 read_buf->len,
@@ -1209,18 +1209,18 @@ void reds_on_main_channel_migrate(RedsState *reds, MainChannelClient *mcc)
             vdi_port_read_buf_unref(read_buf);
         }
 
-        spice_assert(agent_state->priv->receive_len);
-        agent_state->priv->message_receive_len += agent_state->priv->receive_len;
-        agent_state->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
-        agent_state->priv->current_read_buf = NULL;
-        agent_state->priv->receive_pos = NULL;
+        spice_assert(agent_dev->priv->receive_len);
+        agent_dev->priv->message_receive_len += agent_dev->priv->receive_len;
+        agent_dev->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
+        agent_dev->priv->current_read_buf = NULL;
+        agent_dev->priv->receive_pos = NULL;
     }
 }
 
 void reds_marshall_migrate_data(RedsState *reds, SpiceMarshaller *m)
 {
     SpiceMigrateDataMain mig_data;
-    VDIPortState *agent_state = reds->agent_state;
+    RedCharDeviceVDIPort *agent_dev = reds->agent_dev;
     SpiceMarshaller *m2;
 
     memset(&mig_data, 0, sizeof(mig_data));
@@ -1235,7 +1235,7 @@ void reds_marshall_migrate_data(RedsState *reds, SpiceMarshaller *m)
            is set to FALSE when the agent is disconnected and
            there is no need to track the client tokens
            (see reds_reset_vdp) */
-        spice_assert(!agent_state->priv->base);
+        spice_assert(!agent_dev->priv->base);
         red_char_device_migrate_data_marshall_empty(m);
         null_agent_mig_data = spice_marshaller_reserve_space(m,
                                                              sizeof(SpiceMigrateDataMain) -
@@ -1246,33 +1246,33 @@ void reds_marshall_migrate_data(RedsState *reds, SpiceMarshaller *m)
         return;
     }
 
-    red_char_device_migrate_data_marshall(reds->agent_state->priv->base, m);
-    spice_marshaller_add_uint8(m, reds->agent_state->priv->client_agent_started);
+    red_char_device_migrate_data_marshall(reds->agent_dev->priv->base, m);
+    spice_marshaller_add_uint8(m, reds->agent_dev->priv->client_agent_started);
 
-    mig_data.agent2client.chunk_header = agent_state->priv->vdi_chunk_header;
+    mig_data.agent2client.chunk_header = agent_dev->priv->vdi_chunk_header;
 
     /* agent to client partial msg */
-    if (agent_state->priv->read_state == VDI_PORT_READ_STATE_READ_HEADER) {
-        mig_data.agent2client.chunk_header_size = agent_state->priv->receive_pos -
-            (uint8_t *)&agent_state->priv->vdi_chunk_header;
+    if (agent_dev->priv->read_state == VDI_PORT_READ_STATE_READ_HEADER) {
+        mig_data.agent2client.chunk_header_size = agent_dev->priv->receive_pos -
+            (uint8_t *)&agent_dev->priv->vdi_chunk_header;
 
         mig_data.agent2client.msg_header_done = FALSE;
         mig_data.agent2client.msg_header_partial_len = 0;
-        spice_assert(!agent_state->priv->read_filter.msg_data_to_read);
+        spice_assert(!agent_dev->priv->read_filter.msg_data_to_read);
     } else {
         mig_data.agent2client.chunk_header_size = sizeof(VDIChunkHeader);
-        mig_data.agent2client.chunk_header.size = agent_state->priv->message_receive_len;
-        if (agent_state->priv->read_state == VDI_PORT_READ_STATE_READ_DATA) {
+        mig_data.agent2client.chunk_header.size = agent_dev->priv->message_receive_len;
+        if (agent_dev->priv->read_state == VDI_PORT_READ_STATE_READ_DATA) {
             /* in the middle of reading the message header (see reds_on_main_channel_migrate) */
             mig_data.agent2client.msg_header_done = FALSE;
             mig_data.agent2client.msg_header_partial_len =
-                agent_state->priv->receive_pos - agent_state->priv->current_read_buf->data;
+                agent_dev->priv->receive_pos - agent_dev->priv->current_read_buf->data;
             spice_assert(mig_data.agent2client.msg_header_partial_len < sizeof(VDAgentMessage));
-            spice_assert(!agent_state->priv->read_filter.msg_data_to_read);
+            spice_assert(!agent_dev->priv->read_filter.msg_data_to_read);
         } else {
             mig_data.agent2client.msg_header_done =  TRUE;
-            mig_data.agent2client.msg_remaining = agent_state->priv->read_filter.msg_data_to_read;
-            mig_data.agent2client.msg_filter_result = agent_state->priv->read_filter.result;
+            mig_data.agent2client.msg_remaining = agent_dev->priv->read_filter.msg_data_to_read;
+            mig_data.agent2client.msg_filter_result = agent_dev->priv->read_filter.result;
         }
     }
     spice_marshaller_add_uint32(m, mig_data.agent2client.chunk_header_size);
@@ -1282,40 +1282,40 @@ void reds_marshall_migrate_data(RedsState *reds, SpiceMarshaller *m)
     spice_marshaller_add_uint8(m, mig_data.agent2client.msg_header_done);
     spice_marshaller_add_uint32(m, mig_data.agent2client.msg_header_partial_len);
     m2 = spice_marshaller_get_ptr_submarshaller(m, 0);
-    spice_marshaller_add(m2, agent_state->priv->current_read_buf->data,
+    spice_marshaller_add(m2, agent_dev->priv->current_read_buf->data,
                          mig_data.agent2client.msg_header_partial_len);
     spice_marshaller_add_uint32(m, mig_data.agent2client.msg_remaining);
     spice_marshaller_add_uint8(m, mig_data.agent2client.msg_filter_result);
 
-    mig_data.client2agent.msg_remaining = agent_state->priv->write_filter.msg_data_to_read;
-    mig_data.client2agent.msg_filter_result = agent_state->priv->write_filter.result;
+    mig_data.client2agent.msg_remaining = agent_dev->priv->write_filter.msg_data_to_read;
+    mig_data.client2agent.msg_filter_result = agent_dev->priv->write_filter.result;
     spice_marshaller_add_uint32(m, mig_data.client2agent.msg_remaining);
     spice_marshaller_add_uint8(m, mig_data.client2agent.msg_filter_result);
     spice_debug("from agent filter: discard all %d, wait_msg %u, msg_filter_result %d",
-                agent_state->priv->read_filter.discard_all,
-                agent_state->priv->read_filter.msg_data_to_read,
-                 agent_state->priv->read_filter.result);
+                agent_dev->priv->read_filter.discard_all,
+                agent_dev->priv->read_filter.msg_data_to_read,
+                agent_dev->priv->read_filter.result);
     spice_debug("to agent filter: discard all %d, wait_msg %u, msg_filter_result %d",
-                agent_state->priv->write_filter.discard_all,
-                agent_state->priv->write_filter.msg_data_to_read,
-                 agent_state->priv->write_filter.result);
+                agent_dev->priv->write_filter.discard_all,
+                agent_dev->priv->write_filter.msg_data_to_read,
+                agent_dev->priv->write_filter.result);
 }
 
 static int reds_agent_state_restore(RedsState *reds, SpiceMigrateDataMain *mig_data)
 {
-    VDIPortState *agent_state = reds->agent_state;
+    RedCharDeviceVDIPort *agent_dev = reds->agent_dev;
     uint32_t chunk_header_remaining;
 
-    agent_state->priv->vdi_chunk_header = mig_data->agent2client.chunk_header;
+    agent_dev->priv->vdi_chunk_header = mig_data->agent2client.chunk_header;
     spice_assert(mig_data->agent2client.chunk_header_size <= sizeof(VDIChunkHeader));
     chunk_header_remaining = sizeof(VDIChunkHeader) - mig_data->agent2client.chunk_header_size;
     if (chunk_header_remaining) {
-        agent_state->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
-        agent_state->priv->receive_pos = (uint8_t *)&agent_state->priv->vdi_chunk_header +
+        agent_dev->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
+        agent_dev->priv->receive_pos = (uint8_t *)&agent_dev->priv->vdi_chunk_header +
             mig_data->agent2client.chunk_header_size;
-        agent_state->priv->receive_len = chunk_header_remaining;
+        agent_dev->priv->receive_len = chunk_header_remaining;
     } else {
-        agent_state->priv->message_receive_len = agent_state->priv->vdi_chunk_header.size;
+        agent_dev->priv->message_receive_len = agent_dev->priv->vdi_chunk_header.size;
     }
 
     if (!mig_data->agent2client.msg_header_done) {
@@ -1324,49 +1324,49 @@ static int reds_agent_state_restore(RedsState *reds, SpiceMigrateDataMain *mig_d
         if (!chunk_header_remaining) {
             uint32_t cur_buf_size;
 
-            agent_state->priv->read_state = VDI_PORT_READ_STATE_READ_DATA;
-            agent_state->priv->current_read_buf = vdi_port_state_get_read_buf(reds->agent_state);
-            spice_assert(agent_state->priv->current_read_buf);
+            agent_dev->priv->read_state = VDI_PORT_READ_STATE_READ_DATA;
+            agent_dev->priv->current_read_buf = vdi_port_get_read_buf(reds->agent_dev);
+            spice_assert(agent_dev->priv->current_read_buf);
             partial_msg_header = (uint8_t *)mig_data + mig_data->agent2client.msg_header_ptr -
                 sizeof(SpiceMiniDataHeader);
-            memcpy(agent_state->priv->current_read_buf->data,
+            memcpy(agent_dev->priv->current_read_buf->data,
                    partial_msg_header,
                    mig_data->agent2client.msg_header_partial_len);
-            agent_state->priv->receive_pos = agent_state->priv->current_read_buf->data +
+            agent_dev->priv->receive_pos = agent_dev->priv->current_read_buf->data +
                                       mig_data->agent2client.msg_header_partial_len;
-            cur_buf_size = sizeof(agent_state->priv->current_read_buf->data) -
+            cur_buf_size = sizeof(agent_dev->priv->current_read_buf->data) -
                            mig_data->agent2client.msg_header_partial_len;
-            agent_state->priv->receive_len = MIN(agent_state->priv->message_receive_len, cur_buf_size);
-            agent_state->priv->current_read_buf->len = agent_state->priv->receive_len +
+            agent_dev->priv->receive_len = MIN(agent_dev->priv->message_receive_len, cur_buf_size);
+            agent_dev->priv->current_read_buf->len = agent_dev->priv->receive_len +
                                                  mig_data->agent2client.msg_header_partial_len;
-            agent_state->priv->message_receive_len -= agent_state->priv->receive_len;
+            agent_dev->priv->message_receive_len -= agent_dev->priv->receive_len;
         } else {
             spice_assert(mig_data->agent2client.msg_header_partial_len == 0);
         }
     } else {
-            agent_state->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
-            agent_state->priv->current_read_buf = NULL;
-            agent_state->priv->receive_pos = NULL;
-            agent_state->priv->read_filter.msg_data_to_read = mig_data->agent2client.msg_remaining;
-            agent_state->priv->read_filter.result = mig_data->agent2client.msg_filter_result;
+            agent_dev->priv->read_state = VDI_PORT_READ_STATE_GET_BUFF;
+            agent_dev->priv->current_read_buf = NULL;
+            agent_dev->priv->receive_pos = NULL;
+            agent_dev->priv->read_filter.msg_data_to_read = mig_data->agent2client.msg_remaining;
+            agent_dev->priv->read_filter.result = mig_data->agent2client.msg_filter_result;
     }
 
-    agent_state->priv->read_filter.discard_all = FALSE;
-    agent_state->priv->write_filter.discard_all = !mig_data->client_agent_started;
-    agent_state->priv->client_agent_started = mig_data->client_agent_started;
+    agent_dev->priv->read_filter.discard_all = FALSE;
+    agent_dev->priv->write_filter.discard_all = !mig_data->client_agent_started;
+    agent_dev->priv->client_agent_started = mig_data->client_agent_started;
 
-    agent_state->priv->write_filter.msg_data_to_read = mig_data->client2agent.msg_remaining;
-    agent_state->priv->write_filter.result = mig_data->client2agent.msg_filter_result;
+    agent_dev->priv->write_filter.msg_data_to_read = mig_data->client2agent.msg_remaining;
+    agent_dev->priv->write_filter.result = mig_data->client2agent.msg_filter_result;
 
     spice_debug("to agent filter: discard all %d, wait_msg %u, msg_filter_result %d",
-                agent_state->priv->write_filter.discard_all,
-                agent_state->priv->write_filter.msg_data_to_read,
-                 agent_state->priv->write_filter.result);
+                agent_dev->priv->write_filter.discard_all,
+                agent_dev->priv->write_filter.msg_data_to_read,
+                agent_dev->priv->write_filter.result);
     spice_debug("from agent filter: discard all %d, wait_msg %u, msg_filter_result %d",
-                agent_state->priv->read_filter.discard_all,
-                agent_state->priv->read_filter.msg_data_to_read,
-                 agent_state->priv->read_filter.result);
-    return red_char_device_restore(agent_state->priv->base, &mig_data->agent_base);
+                agent_dev->priv->read_filter.discard_all,
+                agent_dev->priv->read_filter.msg_data_to_read,
+                agent_dev->priv->read_filter.result);
+    return red_char_device_restore(agent_dev->priv->base, &mig_data->agent_base);
 }
 
 /*
@@ -1377,7 +1377,7 @@ static int reds_agent_state_restore(RedsState *reds, SpiceMigrateDataMain *mig_d
 int reds_handle_migrate_data(RedsState *reds, MainChannelClient *mcc,
                              SpiceMigrateDataMain *mig_data, uint32_t size)
 {
-    VDIPortState *agent_state = reds->agent_state;
+    RedCharDeviceVDIPort *agent_dev = reds->agent_dev;
 
     spice_debug("main-channel: got migrate data");
     /*
@@ -1389,13 +1389,13 @@ int reds_handle_migrate_data(RedsState *reds, MainChannelClient *mcc,
         reds_send_mm_time(reds);
     }
     if (mig_data->agent_base.connected) {
-        if (agent_state->priv->base) { // agent was attached before migration data has arrived
+        if (agent_dev->priv->base) { // agent was attached before migration data has arrived
             if (!reds->vdagent) {
-                spice_assert(agent_state->priv->plug_generation > 0);
+                spice_assert(agent_dev->priv->plug_generation > 0);
                 main_channel_push_agent_disconnected(reds->main_channel);
                 spice_debug("agent is no longer connected");
             } else {
-                if (agent_state->priv->plug_generation > 1) {
+                if (agent_dev->priv->plug_generation > 1) {
                     /* red_char_device_state_reset takes care of not making the device wait for migration data */
                     spice_debug("agent has been detached and reattached before receiving migration data");
                     main_channel_push_agent_disconnected(reds->main_channel);
@@ -1408,14 +1408,14 @@ int reds_handle_migrate_data(RedsState *reds, MainChannelClient *mcc,
         } else {
             /* restore agent starte when the agent gets attached */
             spice_debug("saving mig_data");
-            spice_assert(agent_state->priv->plug_generation == 0);
-            agent_state->priv->mig_data = spice_memdup(mig_data, size);
+            spice_assert(agent_dev->priv->plug_generation == 0);
+            agent_dev->priv->mig_data = spice_memdup(mig_data, size);
         }
     } else {
         spice_debug("agent was not attached on the source host");
         if (reds->vdagent) {
             /* red_char_device_client_remove disables waiting for migration data */
-            red_char_device_client_remove(agent_state->priv->base,
+            red_char_device_client_remove(agent_dev->priv->base,
                                           main_channel_client_get_base(mcc)->client);
             main_channel_push_agent_connected(reds->main_channel);
         }
@@ -1750,12 +1750,12 @@ static void reds_handle_main_link(RedsState *reds, RedLinkInfo *link)
         if (mig_target) {
             spice_warning("unexpected: vdagent attached to destination during migration");
         }
-        agent_msg_filter_config(&reds->agent_state->priv->read_filter,
+        agent_msg_filter_config(&reds->agent_dev->priv->read_filter,
                                 reds->agent_copypaste,
                                 reds->agent_file_xfer,
                                 reds_use_client_monitors_config(reds));
-        reds->agent_state->priv->read_filter.discard_all = FALSE;
-        reds->agent_state->priv->plug_generation++;
+        reds->agent_dev->priv->read_filter.discard_all = FALSE;
+        reds->agent_dev->priv->plug_generation++;
     }
 
     if (!mig_target) {
@@ -3026,11 +3026,11 @@ void reds_disable_mm_time(RedsState *reds)
 
 static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstance *sin)
 {
-    VDIPortState *state = reds->agent_state;
+    RedCharDeviceVDIPort *dev = reds->agent_dev;
     SpiceCharDeviceInterface *sif;
     RedCharDeviceCallbacks char_dev_state_cbs;
 
-    if (!state->priv->base) {
+    if (!dev->priv->base) {
         char_dev_state_cbs.read_one_msg_from_device = vdi_port_read_one_msg_from_device;
         char_dev_state_cbs.ref_msg_to_client = vdi_port_ref_msg_to_client;
         char_dev_state_cbs.unref_msg_to_client = vdi_port_unref_msg_to_client;
@@ -3039,14 +3039,14 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
         char_dev_state_cbs.remove_client = vdi_port_remove_client;
         char_dev_state_cbs.on_free_self_token = vdi_port_on_free_self_token;
 
-        state->priv->base = red_char_device_create(sin,
-                                                   reds,
-                                                   REDS_TOKENS_TO_SEND,
-                                                   REDS_NUM_INTERNAL_AGENT_MESSAGES,
-                                                   &char_dev_state_cbs,
-                                                   reds);
+        dev->priv->base = red_char_device_create(sin,
+                                                 reds,
+                                                 REDS_TOKENS_TO_SEND,
+                                                 REDS_NUM_INTERNAL_AGENT_MESSAGES,
+                                                 &char_dev_state_cbs,
+                                                 reds);
     } else {
-        red_char_device_reset_dev_instance(state->priv->base, sin);
+        red_char_device_reset_dev_instance(dev->priv->base, sin);
     }
 
     reds->vdagent = sin;
@@ -3058,13 +3058,13 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
     }
 
     if (!reds_main_channel_connected(reds)) {
-        return state->priv->base;
+        return dev->priv->base;
     }
 
-    state->priv->read_filter.discard_all = FALSE;
-    reds->agent_state->priv->plug_generation++;
+    dev->priv->read_filter.discard_all = FALSE;
+    reds->agent_dev->priv->plug_generation++;
 
-    if (reds->agent_state->priv->mig_data ||
+    if (reds->agent_dev->priv->mig_data ||
         red_channel_is_waiting_for_migrate_data(&reds->main_channel->base)) {
         /* Migration in progress (code is running on the destination host):
          * 1.  Add the client to spice char device, if it was not already added.
@@ -3073,10 +3073,10 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
          * 2.b If this happens second ==> we already have spice migrate data
          *     then restore state
          */
-        if (!red_char_device_client_exists(reds->agent_state->priv->base, reds_get_client(reds))) {
+        if (!red_char_device_client_exists(reds->agent_dev->priv->base, reds_get_client(reds))) {
             int client_added;
 
-            client_added = red_char_device_client_add(reds->agent_state->priv->base,
+            client_added = red_char_device_client_add(reds->agent_dev->priv->base,
                                                       reds_get_client(reds),
                                                       TRUE, /* flow control */
                                                       REDS_VDI_PORT_NUM_RECEIVE_BUFFS,
@@ -3090,12 +3090,12 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
             }
         }
 
-        if (reds->agent_state->priv->mig_data) {
+        if (reds->agent_dev->priv->mig_data) {
             spice_debug("restoring state from stored migration data");
-            spice_assert(reds->agent_state->priv->plug_generation == 1);
-            reds_agent_state_restore(reds, reds->agent_state->priv->mig_data);
-            free(reds->agent_state->priv->mig_data);
-            reds->agent_state->priv->mig_data = NULL;
+            spice_assert(reds->agent_dev->priv->plug_generation == 1);
+            reds_agent_state_restore(reds, reds->agent_dev->priv->mig_data);
+            free(reds->agent_dev->priv->mig_data);
+            reds->agent_dev->priv->mig_data = NULL;
         }
         else {
             spice_debug("waiting for migration data");
@@ -3106,7 +3106,7 @@ static RedCharDevice *attach_to_red_agent(RedsState *reds, SpiceCharDeviceInstan
         main_channel_push_agent_connected(reds->main_channel);
     }
 
-    return state->priv->base;
+    return dev->priv->base;
 }
 
 SPICE_GNUC_VISIBLE void spice_server_char_device_wakeup(SpiceCharDeviceInstance* sin)
@@ -3365,28 +3365,28 @@ SPICE_GNUC_VISIBLE int spice_server_remove_interface(SpiceBaseInstance *sin)
 
 static void reds_init_vd_agent_resources(RedsState *reds)
 {
-    VDIPortState *state;
+    RedCharDeviceVDIPort *dev;
     int i;
 
-    reds->agent_state = g_new0(VDIPortState, 1);
-    state = reds->agent_state;
-    ring_init(&state->priv->read_bufs);
-    agent_msg_filter_init(&state->priv->write_filter, reds->agent_copypaste,
+    reds->agent_dev = g_new0(RedCharDeviceVDIPort, 1);
+    dev = reds->agent_dev;
+    ring_init(&dev->priv->read_bufs);
+    agent_msg_filter_init(&dev->priv->write_filter, reds->agent_copypaste,
                           reds->agent_file_xfer,
                           reds_use_client_monitors_config(reds), TRUE);
-    agent_msg_filter_init(&state->priv->read_filter, reds->agent_copypaste,
+    agent_msg_filter_init(&dev->priv->read_filter, reds->agent_copypaste,
                           reds->agent_file_xfer,
                           reds_use_client_monitors_config(reds), TRUE);
 
-    state->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
-    state->priv->receive_pos = (uint8_t *)&state->priv->vdi_chunk_header;
-    state->priv->receive_len = sizeof(state->priv->vdi_chunk_header);
+    dev->priv->read_state = VDI_PORT_READ_STATE_READ_HEADER;
+    dev->priv->receive_pos = (uint8_t *)&dev->priv->vdi_chunk_header;
+    dev->priv->receive_len = sizeof(dev->priv->vdi_chunk_header);
 
     for (i = 0; i < REDS_VDI_PORT_NUM_RECEIVE_BUFFS; i++) {
         VDIReadBuf *buf = spice_new0(VDIReadBuf, 1);
-        buf->state = state;
+        buf->dev = dev;
         ring_item_init(&buf->link);
-        ring_add(&reds->agent_state->priv->read_bufs, &buf->link);
+        ring_add(&reds->agent_dev->priv->read_bufs, &buf->link);
     }
 }
 
@@ -3876,16 +3876,16 @@ SPICE_GNUC_VISIBLE int spice_server_set_agent_mouse(SpiceServer *reds, int enabl
 SPICE_GNUC_VISIBLE int spice_server_set_agent_copypaste(SpiceServer *reds, int enable)
 {
     reds->agent_copypaste = enable;
-    reds->agent_state->priv->write_filter.copy_paste_enabled = reds->agent_copypaste;
-    reds->agent_state->priv->read_filter.copy_paste_enabled = reds->agent_copypaste;
+    reds->agent_dev->priv->write_filter.copy_paste_enabled = reds->agent_copypaste;
+    reds->agent_dev->priv->read_filter.copy_paste_enabled = reds->agent_copypaste;
     return 0;
 }
 
 SPICE_GNUC_VISIBLE int spice_server_set_agent_file_xfer(SpiceServer *reds, int enable)
 {
     reds->agent_file_xfer = enable;
-    reds->agent_state->priv->write_filter.file_xfer_enabled = reds->agent_file_xfer;
-    reds->agent_state->priv->read_filter.file_xfer_enabled = reds->agent_file_xfer;
+    reds->agent_dev->priv->write_filter.file_xfer_enabled = reds->agent_file_xfer;
+    reds->agent_dev->priv->read_filter.file_xfer_enabled = reds->agent_file_xfer;
     return 0;
 }
 
commit d2e6696fb3e45215461f98ebcc3d62c6df2ddebe
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Fri Apr 1 13:37:44 2016 +0100

    smartcard: Turn RedCharDeviceSmartcard into a GObject
    
    This inherits from RedCharDevice. Once all char device states are
    converted, we can turn the associated vfuncs into RedCharDeviceClass
    vfuncs.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/server/smartcard.c b/server/smartcard.c
index d138ea7..8d382ce 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 /*
-   Copyright (C) 2010 Red Hat, Inc.
+   Copyright (C) 2010-2016 Red Hat, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -49,8 +49,6 @@
 // Maximal length of APDU
 #define APDUBufSize 270
 
-typedef struct RedCharDeviceSmartcard RedCharDeviceSmartcard;
-
 typedef struct SmartCardChannelClient {
     RedChannelClient base;
     RedCharDeviceSmartcard *smartcard;
@@ -62,8 +60,11 @@ typedef struct SmartCardChannelClient {
                            * or was it explicitly malloced */
 } SmartCardChannelClient;
 
+G_DEFINE_TYPE(RedCharDeviceSmartcard, red_char_device_smartcard, RED_TYPE_CHAR_DEVICE)
+
+#define RED_CHAR_DEVICE_SMARTCARD_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RED_TYPE_CHAR_DEVICE_SMARTCARD, RedCharDeviceSmartcardPrivate))
+
 struct RedCharDeviceSmartcardPrivate {
-    RedCharDevice       *chardev;
     uint32_t             reader_id;
     /* read_from_device buffer */
     uint8_t             *buf;
@@ -75,10 +76,6 @@ struct RedCharDeviceSmartcardPrivate {
     int                  reader_added; // has reader_add been sent to the device
 };
 
-struct RedCharDeviceSmartcard {
-    struct RedCharDeviceSmartcardPrivate priv[1];
-};
-
 enum {
     PIPE_ITEM_TYPE_ERROR = PIPE_ITEM_TYPE_CHANNEL_BASE,
     PIPE_ITEM_TYPE_SMARTCARD_DATA,
@@ -122,7 +119,6 @@ static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_bu
 static MsgItem *smartcard_char_device_on_message_from_device(
     RedCharDeviceSmartcard *dev, VSCMsgHeader *header);
 static RedCharDeviceSmartcard *smartcard_device_new(RedsState *reds, SpiceCharDeviceInstance *sin);
-static void smartcard_device_free(RedCharDeviceSmartcard* dev);
 static void smartcard_init(RedsState *reds);
 
 static void smartcard_read_buf_prepare(RedCharDeviceSmartcard *dev, VSCMsgHeader *vheader)
@@ -275,48 +271,33 @@ static SpiceCharDeviceInstance *smartcard_readers_get_unattached(void)
 
 static RedCharDeviceSmartcard *smartcard_device_new(RedsState *reds, SpiceCharDeviceInstance *sin)
 {
-    RedCharDeviceSmartcard *dev;
-    RedCharDeviceCallbacks chardev_cbs = { NULL, };
-
-    chardev_cbs.read_one_msg_from_device = smartcard_read_msg_from_device;
-    chardev_cbs.ref_msg_to_client = smartcard_ref_msg_to_client;
-    chardev_cbs.unref_msg_to_client = smartcard_unref_msg_to_client;
-    chardev_cbs.send_msg_to_client = smartcard_send_msg_to_client;
-    chardev_cbs.send_tokens_to_client = smartcard_send_tokens_to_client;
-    chardev_cbs.remove_client = smartcard_remove_client;
-
-    dev = spice_new0(RedCharDeviceSmartcard, 1);
-    dev->priv->chardev = red_char_device_create(sin,
-                                                reds,
-                                                0, /* tokens interval */
-                                                ~0, /* self tokens */
-                                                &chardev_cbs,
-                                                dev);
-    dev->priv->reader_id = VSCARD_UNDEFINED_READER_ID;
-    dev->priv->reader_added = FALSE;
-    dev->priv->buf_size = APDUBufSize + sizeof(VSCMsgHeader);
-    dev->priv->buf = spice_malloc(dev->priv->buf_size);
-    dev->priv->buf_pos = dev->priv->buf;
-    dev->priv->buf_used = 0;
-    dev->priv->scc = NULL;
-    return dev;
-}
+    RedCharDevice *char_dev;
+    RedCharDeviceCallbacks char_dev_cbs = {
+        .read_one_msg_from_device = smartcard_read_msg_from_device,
+        .ref_msg_to_client = smartcard_ref_msg_to_client,
+        .unref_msg_to_client = smartcard_unref_msg_to_client,
+        .send_msg_to_client = smartcard_send_msg_to_client,
+        .send_tokens_to_client = smartcard_send_tokens_to_client,
+        .remove_client = smartcard_remove_client,
+    };
 
-static void smartcard_device_free(RedCharDeviceSmartcard* dev)
-{
-    if (dev->priv->scc) {
-        dev->priv->scc->smartcard = NULL;
-    }
-    free(dev->priv->buf);
-    red_char_device_destroy(dev->priv->chardev);
-    free(dev);
+    char_dev = g_object_new(RED_TYPE_CHAR_DEVICE_SMARTCARD,
+                            "sin", sin,
+                            "spice-server", reds,
+                            "client-tokens-interval", 0ULL,
+                            "self-tokens", ~0ULL,
+                            NULL);
+
+    red_char_device_set_callbacks(RED_CHAR_DEVICE(char_dev),
+                                  &char_dev_cbs, char_dev);
+    return RED_CHAR_DEVICE_SMARTCARD(char_dev);
 }
 
 void smartcard_device_disconnect(SpiceCharDeviceInstance *char_device)
 {
-    RedCharDeviceSmartcard *dev = red_char_device_opaque_get(char_device->st);
+    g_return_if_fail(RED_IS_CHAR_DEVICE_SMARTCARD(char_device->st));
 
-    smartcard_device_free(dev);
+    g_object_unref(char_device);
 }
 
 RedCharDevice *smartcard_device_connect(RedsState *reds, SpiceCharDeviceInstance *char_device)
@@ -325,10 +306,10 @@ RedCharDevice *smartcard_device_connect(RedsState *reds, SpiceCharDeviceInstance
 
     dev = smartcard_device_new(reds, char_device);
     if (smartcard_char_device_add_to_readers(reds, char_device) == -1) {
-        smartcard_device_free(dev);
+        g_object_unref(dev);
         return NULL;
     }
-    return dev->priv->chardev;
+    return RED_CHAR_DEVICE(dev);
 }
 
 static void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
@@ -336,7 +317,7 @@ static void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
     RedCharDeviceWriteBuffer *write_buf;
     VSCMsgHeader *vheader;
 
-    write_buf = red_char_device_write_buffer_get(dev->priv->chardev, NULL, sizeof(vheader));
+    write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev), NULL, sizeof(vheader));
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
         return;
@@ -358,7 +339,7 @@ static void smartcard_char_device_attach_client(SpiceCharDeviceInstance *char_de
     spice_assert(!scc->smartcard && !dev->priv->scc);
     dev->priv->scc = scc;
     scc->smartcard = dev;
-    client_added = red_char_device_client_add(dev->priv->chardev,
+    client_added = red_char_device_client_add(RED_CHAR_DEVICE(dev),
                                               scc->base.client,
                                               FALSE, /* no flow control yet */
                                               0, /* send queue size */
@@ -383,7 +364,7 @@ static void smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *d
         spice_debug("reader add was never sent to the device");
         return;
     }
-    write_buf = red_char_device_write_buffer_get(dev->priv->chardev, NULL, sizeof(vheader));
+    write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev), NULL, sizeof(vheader));
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
         return;
@@ -405,7 +386,7 @@ static void smartcard_char_device_detach_client(SmartCardChannelClient *scc)
     }
     dev = scc->smartcard;
     spice_assert(dev->priv->scc == scc);
-    red_char_device_client_remove(dev->priv->chardev, scc->base.client);
+    red_char_device_client_remove(RED_CHAR_DEVICE(dev), scc->base.client);
     scc->smartcard = NULL;
     dev->priv->scc = NULL;
 }
@@ -434,7 +415,7 @@ static uint8_t *smartcard_channel_alloc_msg_rcv_buf(RedChannelClient *rcc,
         dev = scc->smartcard;
         spice_assert(dev->priv->scc || scc->smartcard);
         spice_assert(!scc->write_buf);
-        scc->write_buf = red_char_device_write_buffer_get(dev->priv->chardev, rcc->client, size);
+        scc->write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev), rcc->client, size);
 
         if (!scc->write_buf) {
             spice_error("failed to allocate write buffer");
@@ -460,11 +441,9 @@ static void smartcard_channel_release_msg_rcv_buf(RedChannelClient *rcc,
         spice_assert(!scc->write_buf);
         free(msg);
     } else {
-        RedCharDevice *dev;
         if (scc->write_buf) { /* msg hasn't been pushed to the guest */
             spice_assert(scc->write_buf->buf == msg);
-            dev = scc->smartcard ? scc->smartcard->priv->chardev : NULL;
-            red_char_device_write_buffer_release(dev, scc->write_buf);
+            red_char_device_write_buffer_release(RED_CHAR_DEVICE(scc->smartcard), scc->write_buf);
             scc->write_buf = NULL;
         }
     }
@@ -518,7 +497,7 @@ static void smartcard_channel_send_migrate_data(RedChannelClient *rcc,
         spice_marshaller_add_uint32(m, 0);
         spice_debug("null char dev");
     } else {
-        red_char_device_migrate_data_marshall(dev->priv->chardev, m);
+        red_char_device_migrate_data_marshall(RED_CHAR_DEVICE(dev), m);
         spice_marshaller_add_uint8(m, dev->priv->reader_added);
         spice_marshaller_add_uint32(m, dev->priv->buf_used);
         m2 = spice_marshaller_get_ptr_submarshaller(m, 0);
@@ -746,7 +725,7 @@ static int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
     scc->smartcard->priv->reader_added = mig_data->reader_added;
 
     smartcard_device_restore_partial_read(scc->smartcard, mig_data);
-    return red_char_device_restore(scc->smartcard->priv->chardev, &mig_data->base);
+    return red_char_device_restore(RED_CHAR_DEVICE(scc->smartcard), &mig_data->base);
 }
 
 static int smartcard_channel_handle_message(RedChannelClient *rcc,
@@ -871,3 +850,38 @@ static void smartcard_init(RedsState *reds)
 
     reds_register_channel(reds, &g_smartcard_channel->base);
 }
+
+
+static void
+red_char_device_smartcard_finalize(GObject *object)
+{
+    RedCharDeviceSmartcard *self = RED_CHAR_DEVICE_SMARTCARD(object);
+
+    free(self->priv->buf);
+    if (self->priv->scc) {
+        self->priv->scc->smartcard = NULL;
+    }
+
+    G_OBJECT_CLASS(red_char_device_smartcard_parent_class)->finalize(object);
+}
+
+static void
+red_char_device_smartcard_class_init(RedCharDeviceSmartcardClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof (RedCharDeviceSmartcardPrivate));
+
+    object_class->finalize = red_char_device_smartcard_finalize;
+}
+
+static void
+red_char_device_smartcard_init(RedCharDeviceSmartcard *self)
+{
+    self->priv = RED_CHAR_DEVICE_SMARTCARD_PRIVATE(self);
+
+    self->priv->reader_id = VSCARD_UNDEFINED_READER_ID;
+    self->priv->buf_size = APDUBufSize + sizeof(VSCMsgHeader);
+    self->priv->buf = spice_malloc(self->priv->buf_size);
+    self->priv->buf_pos = self->priv->buf;
+}
diff --git a/server/smartcard.h b/server/smartcard.h
index 9900287..6bda594 100644
--- a/server/smartcard.h
+++ b/server/smartcard.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 /*
-   Copyright (C) 2010 Red Hat, Inc.
+   Copyright (C) 2010-2016 Red Hat, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,34 @@
 #ifndef __SMART_CARD_H__
 #define __SMART_CARD_H__
 
+#include <glib-object.h>
+
+#define RED_TYPE_CHAR_DEVICE_SMARTCARD red_char_device_smartcard_get_type()
+
+#define RED_CHAR_DEVICE_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), RED_TYPE_CHAR_DEVICE_SMARTCARD, RedCharDeviceSmartcard))
+#define RED_CHAR_DEVICE_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), RED_TYPE_CHAR_DEVICE_SMARTCARD, RedCharDeviceSmartcardClass))
+#define RED_IS_CHAR_DEVICE_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), RED_TYPE_CHAR_DEVICE_SMARTCARD))
+#define RED_IS_CHAR_DEVICE_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), RED_TYPE_CHAR_DEVICE_SMARTCARD))
+#define RED_CHAR_DEVICE_SMARTCARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), RED_TYPE_CHAR_DEVICE_SMARTCARD, RedCharDeviceSmartcardClass))
+
+typedef struct RedCharDeviceSmartcard RedCharDeviceSmartcard;
+typedef struct RedCharDeviceSmartcardClass RedCharDeviceSmartcardClass;
+typedef struct RedCharDeviceSmartcardPrivate RedCharDeviceSmartcardPrivate;
+
+struct RedCharDeviceSmartcard
+{
+    RedCharDevice parent;
+
+    RedCharDeviceSmartcardPrivate *priv;
+};
+
+struct RedCharDeviceSmartcardClass
+{
+    RedCharDeviceClass parent_class;
+};
+
+GType red_char_device_smartcard_get_type(void) G_GNUC_CONST;
+
 /*
  * connect to smartcard interface, used by smartcard channel
  */
commit 756f0d5d0b37598807c8e61a958a87a49593d763
Author: Jonathon Jongsma <jjongsma at redhat.com>
Date:   Fri Apr 1 14:41:13 2016 -0500

    Rename SmartCardDeviceState to RedCharDeviceSmartcard
    
    More consistent with naming conventions, and prepares for converting
    this type to a GObject which inherits from RedCharDevice.

diff --git a/server/smartcard.c b/server/smartcard.c
index 03782e2..d138ea7 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -49,11 +49,11 @@
 // Maximal length of APDU
 #define APDUBufSize 270
 
-typedef struct SmartCardDeviceState SmartCardDeviceState;
+typedef struct RedCharDeviceSmartcard RedCharDeviceSmartcard;
 
 typedef struct SmartCardChannelClient {
     RedChannelClient base;
-    SmartCardDeviceState *smartcard_state;
+    RedCharDeviceSmartcard *smartcard;
 
     /* read_from_client/write_to_device buffer.
      * The beginning of the buffer should always be VSCMsgHeader*/
@@ -75,7 +75,7 @@ struct RedCharDeviceSmartcardPrivate {
     int                  reader_added; // has reader_add been sent to the device
 };
 
-struct SmartCardDeviceState {
+struct RedCharDeviceSmartcard {
     struct RedCharDeviceSmartcardPrivate priv[1];
 };
 
@@ -120,52 +120,52 @@ static void smartcard_char_device_attach_client(
 static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_buf);
 
 static MsgItem *smartcard_char_device_on_message_from_device(
-    SmartCardDeviceState *state, VSCMsgHeader *header);
-static SmartCardDeviceState *smartcard_device_state_new(RedsState *reds, SpiceCharDeviceInstance *sin);
-static void smartcard_device_state_free(SmartCardDeviceState* st);
+    RedCharDeviceSmartcard *dev, VSCMsgHeader *header);
+static RedCharDeviceSmartcard *smartcard_device_new(RedsState *reds, SpiceCharDeviceInstance *sin);
+static void smartcard_device_free(RedCharDeviceSmartcard* dev);
 static void smartcard_init(RedsState *reds);
 
-static void smartcard_read_buf_prepare(SmartCardDeviceState *state, VSCMsgHeader *vheader)
+static void smartcard_read_buf_prepare(RedCharDeviceSmartcard *dev, VSCMsgHeader *vheader)
 {
     uint32_t msg_len;
 
     msg_len = ntohl(vheader->length);
-    if (msg_len > state->priv->buf_size) {
-        state->priv->buf_size = MAX(state->priv->buf_size * 2, msg_len + sizeof(VSCMsgHeader));
-        state->priv->buf = spice_realloc(state->priv->buf, state->priv->buf_size);
+    if (msg_len > dev->priv->buf_size) {
+        dev->priv->buf_size = MAX(dev->priv->buf_size * 2, msg_len + sizeof(VSCMsgHeader));
+        dev->priv->buf = spice_realloc(dev->priv->buf, dev->priv->buf_size);
     }
 }
 
 static RedCharDeviceMsgToClient *smartcard_read_msg_from_device(SpiceCharDeviceInstance *sin,
                                                                 void *opaque)
 {
-    SmartCardDeviceState *state = opaque;
+    RedCharDeviceSmartcard *dev = opaque;
     SpiceCharDeviceInterface *sif = spice_char_device_get_interface(sin);
-    VSCMsgHeader *vheader = (VSCMsgHeader*)state->priv->buf;
+    VSCMsgHeader *vheader = (VSCMsgHeader*)dev->priv->buf;
     int n;
     int remaining;
     int actual_length;
 
-    while ((n = sif->read(sin, state->priv->buf_pos, state->priv->buf_size - state->priv->buf_used)) > 0) {
+    while ((n = sif->read(sin, dev->priv->buf_pos, dev->priv->buf_size - dev->priv->buf_used)) > 0) {
         MsgItem *msg_to_client;
 
-        state->priv->buf_pos += n;
-        state->priv->buf_used += n;
-        if (state->priv->buf_used < sizeof(VSCMsgHeader)) {
+        dev->priv->buf_pos += n;
+        dev->priv->buf_used += n;
+        if (dev->priv->buf_used < sizeof(VSCMsgHeader)) {
             continue;
         }
-        smartcard_read_buf_prepare(state, vheader);
+        smartcard_read_buf_prepare(dev, vheader);
         actual_length = ntohl(vheader->length);
-        if (state->priv->buf_used - sizeof(VSCMsgHeader) < actual_length) {
+        if (dev->priv->buf_used - sizeof(VSCMsgHeader) < actual_length) {
             continue;
         }
-        msg_to_client = smartcard_char_device_on_message_from_device(state, vheader);
-        remaining = state->priv->buf_used - sizeof(VSCMsgHeader) - actual_length;
+        msg_to_client = smartcard_char_device_on_message_from_device(dev, vheader);
+        remaining = dev->priv->buf_used - sizeof(VSCMsgHeader) - actual_length;
         if (remaining > 0) {
-            memcpy(state->priv->buf, state->priv->buf_pos, remaining);
+            memcpy(dev->priv->buf, dev->priv->buf_pos, remaining);
         }
-        state->priv->buf_pos = state->priv->buf;
-        state->priv->buf_used = remaining;
+        dev->priv->buf_pos = dev->priv->buf;
+        dev->priv->buf_used = remaining;
         if (msg_to_client) {
             return msg_to_client;
         }
@@ -189,7 +189,7 @@ static void smartcard_send_msg_to_client(RedCharDeviceMsgToClient *msg,
                                          RedClient *client,
                                          void *opaque)
 {
-    SmartCardDeviceState *dev = opaque;
+    RedCharDeviceSmartcard *dev = opaque;
     spice_assert(dev->priv->scc && dev->priv->scc->base.client == client);
     smartcard_channel_client_pipe_add_push(&dev->priv->scc->base, &((MsgItem *)msg)->base);
 
@@ -202,14 +202,14 @@ static void smartcard_send_tokens_to_client(RedClient *client, uint32_t tokens,
 
 static void smartcard_remove_client(RedClient *client, void *opaque)
 {
-    SmartCardDeviceState *dev = opaque;
+    RedCharDeviceSmartcard *dev = opaque;
 
-    spice_printerr("smartcard  state %p, client %p", dev, client);
+    spice_printerr("smartcard  dev %p, client %p", dev, client);
     spice_assert(dev->priv->scc && dev->priv->scc->base.client == client);
     red_channel_client_shutdown(&dev->priv->scc->base);
 }
 
-MsgItem *smartcard_char_device_on_message_from_device(SmartCardDeviceState *state,
+MsgItem *smartcard_char_device_on_message_from_device(RedCharDeviceSmartcard *dev,
                                                       VSCMsgHeader *vheader)
 {
     VSCMsgHeader *sent_header;
@@ -225,27 +225,27 @@ MsgItem *smartcard_char_device_on_message_from_device(SmartCardDeviceState *stat
             break;
     }
     /* We pass any VSC_Error right now - might need to ignore some? */
-    if (state->priv->reader_id == VSCARD_UNDEFINED_READER_ID && vheader->type != VSC_Init) {
+    if (dev->priv->reader_id == VSCARD_UNDEFINED_READER_ID && vheader->type != VSC_Init) {
         spice_printerr("error: reader_id not assigned for message of type %d", vheader->type);
     }
-    if (state->priv->scc) {
+    if (dev->priv->scc) {
         sent_header = spice_memdup(vheader, sizeof(*vheader) + vheader->length);
         /* We patch the reader_id, since the device only knows about itself, and
          * we know about the sum of readers. */
-        sent_header->reader_id = state->priv->reader_id;
-        return smartcard_get_vsc_msg_item(&state->priv->scc->base, sent_header);
+        sent_header->reader_id = dev->priv->reader_id;
+        return smartcard_get_vsc_msg_item(&dev->priv->scc->base, sent_header);
     }
     return NULL;
 }
 
 static int smartcard_char_device_add_to_readers(RedsState *reds, SpiceCharDeviceInstance *char_device)
 {
-    SmartCardDeviceState *state = red_char_device_opaque_get(char_device->st);
+    RedCharDeviceSmartcard *dev = red_char_device_opaque_get(char_device->st);
 
     if (g_smartcard_readers.num >= SMARTCARD_MAX_READERS) {
         return -1;
     }
-    state->priv->reader_id = g_smartcard_readers.num;
+    dev->priv->reader_id = g_smartcard_readers.num;
     g_smartcard_readers.sin[g_smartcard_readers.num++] = char_device;
     smartcard_init(reds);
     return 0;
@@ -262,20 +262,20 @@ static SpiceCharDeviceInstance *smartcard_readers_get(uint32_t reader_id)
 static SpiceCharDeviceInstance *smartcard_readers_get_unattached(void)
 {
     int i;
-    SmartCardDeviceState* state;
+    RedCharDeviceSmartcard* dev;
 
     for (i = 0; i < g_smartcard_readers.num; ++i) {
-        state = red_char_device_opaque_get(g_smartcard_readers.sin[i]->st);
-        if (!state->priv->scc) {
+        dev = red_char_device_opaque_get(g_smartcard_readers.sin[i]->st);
+        if (!dev->priv->scc) {
             return g_smartcard_readers.sin[i];
         }
     }
     return NULL;
 }
 
-static SmartCardDeviceState *smartcard_device_state_new(RedsState *reds, SpiceCharDeviceInstance *sin)
+static RedCharDeviceSmartcard *smartcard_device_new(RedsState *reds, SpiceCharDeviceInstance *sin)
 {
-    SmartCardDeviceState *st;
+    RedCharDeviceSmartcard *dev;
     RedCharDeviceCallbacks chardev_cbs = { NULL, };
 
     chardev_cbs.read_one_msg_from_device = smartcard_read_msg_from_device;
@@ -285,66 +285,66 @@ static SmartCardDeviceState *smartcard_device_state_new(RedsState *reds, SpiceCh
     chardev_cbs.send_tokens_to_client = smartcard_send_tokens_to_client;
     chardev_cbs.remove_client = smartcard_remove_client;
 
-    st = spice_new0(SmartCardDeviceState, 1);
-    st->priv->chardev = red_char_device_create(sin,
-                                               reds,
-                                               0, /* tokens interval */
-                                               ~0, /* self tokens */
-                                               &chardev_cbs,
-                                               st);
-    st->priv->reader_id = VSCARD_UNDEFINED_READER_ID;
-    st->priv->reader_added = FALSE;
-    st->priv->buf_size = APDUBufSize + sizeof(VSCMsgHeader);
-    st->priv->buf = spice_malloc(st->priv->buf_size);
-    st->priv->buf_pos = st->priv->buf;
-    st->priv->buf_used = 0;
-    st->priv->scc = NULL;
-    return st;
+    dev = spice_new0(RedCharDeviceSmartcard, 1);
+    dev->priv->chardev = red_char_device_create(sin,
+                                                reds,
+                                                0, /* tokens interval */
+                                                ~0, /* self tokens */
+                                                &chardev_cbs,
+                                                dev);
+    dev->priv->reader_id = VSCARD_UNDEFINED_READER_ID;
+    dev->priv->reader_added = FALSE;
+    dev->priv->buf_size = APDUBufSize + sizeof(VSCMsgHeader);
+    dev->priv->buf = spice_malloc(dev->priv->buf_size);
+    dev->priv->buf_pos = dev->priv->buf;
+    dev->priv->buf_used = 0;
+    dev->priv->scc = NULL;
+    return dev;
 }
 
-static void smartcard_device_state_free(SmartCardDeviceState* st)
+static void smartcard_device_free(RedCharDeviceSmartcard* dev)
 {
-    if (st->priv->scc) {
-        st->priv->scc->smartcard_state = NULL;
+    if (dev->priv->scc) {
+        dev->priv->scc->smartcard = NULL;
     }
-    free(st->priv->buf);
-    red_char_device_destroy(st->priv->chardev);
-    free(st);
+    free(dev->priv->buf);
+    red_char_device_destroy(dev->priv->chardev);
+    free(dev);
 }
 
 void smartcard_device_disconnect(SpiceCharDeviceInstance *char_device)
 {
-    SmartCardDeviceState *st = red_char_device_opaque_get(char_device->st);
+    RedCharDeviceSmartcard *dev = red_char_device_opaque_get(char_device->st);
 
-    smartcard_device_state_free(st);
+    smartcard_device_free(dev);
 }
 
 RedCharDevice *smartcard_device_connect(RedsState *reds, SpiceCharDeviceInstance *char_device)
 {
-    SmartCardDeviceState *st;
+    RedCharDeviceSmartcard *dev;
 
-    st = smartcard_device_state_new(reds, char_device);
+    dev = smartcard_device_new(reds, char_device);
     if (smartcard_char_device_add_to_readers(reds, char_device) == -1) {
-        smartcard_device_state_free(st);
+        smartcard_device_free(dev);
         return NULL;
     }
-    return st->priv->chardev;
+    return dev->priv->chardev;
 }
 
-static void smartcard_char_device_notify_reader_add(SmartCardDeviceState *st)
+static void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
 {
     RedCharDeviceWriteBuffer *write_buf;
     VSCMsgHeader *vheader;
 
-    write_buf = red_char_device_write_buffer_get(st->priv->chardev, NULL, sizeof(vheader));
+    write_buf = red_char_device_write_buffer_get(dev->priv->chardev, NULL, sizeof(vheader));
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
         return;
     }
-    st->priv->reader_added = TRUE;
+    dev->priv->reader_added = TRUE;
     vheader = (VSCMsgHeader *)write_buf->buf;
     vheader->type = VSC_ReaderAdd;
-    vheader->reader_id = st->priv->reader_id;
+    vheader->reader_id = dev->priv->reader_id;
     vheader->length = 0;
     smartcard_channel_write_to_reader(write_buf);
 }
@@ -352,13 +352,13 @@ static void smartcard_char_device_notify_reader_add(SmartCardDeviceState *st)
 static void smartcard_char_device_attach_client(SpiceCharDeviceInstance *char_device,
                                                 SmartCardChannelClient *scc)
 {
-    SmartCardDeviceState *st = red_char_device_opaque_get(char_device->st);
+    RedCharDeviceSmartcard *dev = red_char_device_opaque_get(char_device->st);
     int client_added;
 
-    spice_assert(!scc->smartcard_state && !st->priv->scc);
-    st->priv->scc = scc;
-    scc->smartcard_state = st;
-    client_added = red_char_device_client_add(st->priv->chardev,
+    spice_assert(!scc->smartcard && !dev->priv->scc);
+    dev->priv->scc = scc;
+    scc->smartcard = dev;
+    client_added = red_char_device_client_add(dev->priv->chardev,
                                               scc->base.client,
                                               FALSE, /* no flow control yet */
                                               0, /* send queue size */
@@ -368,46 +368,46 @@ static void smartcard_char_device_attach_client(SpiceCharDeviceInstance *char_de
                                                   &scc->base));
     if (!client_added) {
         spice_warning("failed");
-        st->priv->scc = NULL;
-        scc->smartcard_state = NULL;
+        dev->priv->scc = NULL;
+        scc->smartcard = NULL;
         red_channel_client_disconnect(&scc->base);
     }
 }
 
-static void smartcard_char_device_notify_reader_remove(SmartCardDeviceState *st)
+static void smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *dev)
 {
     RedCharDeviceWriteBuffer *write_buf;
     VSCMsgHeader *vheader;
 
-    if (!st->priv->reader_added) {
+    if (!dev->priv->reader_added) {
         spice_debug("reader add was never sent to the device");
         return;
     }
-    write_buf = red_char_device_write_buffer_get(st->priv->chardev, NULL, sizeof(vheader));
+    write_buf = red_char_device_write_buffer_get(dev->priv->chardev, NULL, sizeof(vheader));
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
         return;
     }
-    st->priv->reader_added = FALSE;
+    dev->priv->reader_added = FALSE;
     vheader = (VSCMsgHeader *)write_buf->buf;
     vheader->type = VSC_ReaderRemove;
-    vheader->reader_id = st->priv->reader_id;
+    vheader->reader_id = dev->priv->reader_id;
     vheader->length = 0;
     smartcard_channel_write_to_reader(write_buf);
 }
 
 static void smartcard_char_device_detach_client(SmartCardChannelClient *scc)
 {
-    SmartCardDeviceState *st;
+    RedCharDeviceSmartcard *dev;
 
-    if (!scc->smartcard_state) {
+    if (!scc->smartcard) {
         return;
     }
-    st = scc->smartcard_state;
-    spice_assert(st->priv->scc == scc);
-    red_char_device_client_remove(st->priv->chardev, scc->base.client);
-    scc->smartcard_state = NULL;
-    st->priv->scc = NULL;
+    dev = scc->smartcard;
+    spice_assert(dev->priv->scc == scc);
+    red_char_device_client_remove(dev->priv->chardev, scc->base.client);
+    scc->smartcard = NULL;
+    dev->priv->scc = NULL;
 }
 
 static int smartcard_channel_client_config_socket(RedChannelClient *rcc)
@@ -424,17 +424,17 @@ static uint8_t *smartcard_channel_alloc_msg_rcv_buf(RedChannelClient *rcc,
     /* todo: only one reader is actually supported. When we fix the code to support
      * multiple readers, we will porbably associate different devices to
      * differenc channels */
-    if (!scc->smartcard_state) {
+    if (!scc->smartcard) {
         scc->msg_in_write_buf = FALSE;
         return spice_malloc(size);
     } else {
-        SmartCardDeviceState *st;
+        RedCharDeviceSmartcard *dev;
 
         spice_assert(g_smartcard_readers.num == 1);
-        st = scc->smartcard_state;
-        spice_assert(st->priv->scc || scc->smartcard_state);
+        dev = scc->smartcard;
+        spice_assert(dev->priv->scc || scc->smartcard);
         spice_assert(!scc->write_buf);
-        scc->write_buf = red_char_device_write_buffer_get(st->priv->chardev, rcc->client, size);
+        scc->write_buf = red_char_device_write_buffer_get(dev->priv->chardev, rcc->client, size);
 
         if (!scc->write_buf) {
             spice_error("failed to allocate write buffer");
@@ -463,7 +463,7 @@ static void smartcard_channel_release_msg_rcv_buf(RedChannelClient *rcc,
         RedCharDevice *dev;
         if (scc->write_buf) { /* msg hasn't been pushed to the guest */
             spice_assert(scc->write_buf->buf == msg);
-            dev = scc->smartcard_state ? scc->smartcard_state->priv->chardev : NULL;
+            dev = scc->smartcard ? scc->smartcard->priv->chardev : NULL;
             red_char_device_write_buffer_release(dev, scc->write_buf);
             scc->write_buf = NULL;
         }
@@ -502,28 +502,28 @@ static void smartcard_channel_send_migrate_data(RedChannelClient *rcc,
                                                 SpiceMarshaller *m, PipeItem *item)
 {
     SmartCardChannelClient *scc;
-    SmartCardDeviceState *state;
+    RedCharDeviceSmartcard *dev;
     SpiceMarshaller *m2;
 
     scc = SPICE_CONTAINEROF(rcc, SmartCardChannelClient, base);
-    state = scc->smartcard_state;
+    dev = scc->smartcard;
     red_channel_client_init_send_data(rcc, SPICE_MSG_MIGRATE_DATA, item);
     spice_marshaller_add_uint32(m, SPICE_MIGRATE_DATA_SMARTCARD_MAGIC);
     spice_marshaller_add_uint32(m, SPICE_MIGRATE_DATA_SMARTCARD_VERSION);
 
-    if (!state) {
+    if (!dev) {
         red_char_device_migrate_data_marshall_empty(m);
         spice_marshaller_add_uint8(m, 0);
         spice_marshaller_add_uint32(m, 0);
         spice_marshaller_add_uint32(m, 0);
-        spice_debug("null char dev state");
+        spice_debug("null char dev");
     } else {
-        red_char_device_migrate_data_marshall(state->priv->chardev, m);
-        spice_marshaller_add_uint8(m, state->priv->reader_added);
-        spice_marshaller_add_uint32(m, state->priv->buf_used);
+        red_char_device_migrate_data_marshall(dev->priv->chardev, m);
+        spice_marshaller_add_uint8(m, dev->priv->reader_added);
+        spice_marshaller_add_uint32(m, dev->priv->buf_used);
         m2 = spice_marshaller_get_ptr_submarshaller(m, 0);
-        spice_marshaller_add(m2, state->priv->buf, state->priv->buf_used);
-        spice_debug("reader added %d partial read size %u", state->priv->reader_added, state->priv->buf_used);
+        spice_marshaller_add(m2, dev->priv->buf, dev->priv->buf_used);
+        spice_debug("reader added %d partial read size %u", dev->priv->reader_added, dev->priv->buf_used);
     }
 }
 
@@ -563,11 +563,11 @@ static void smartcard_channel_on_disconnect(RedChannelClient *rcc)
 {
     SmartCardChannelClient *scc = SPICE_CONTAINEROF(rcc, SmartCardChannelClient, base);
 
-    if (scc->smartcard_state) {
-        SmartCardDeviceState *st = scc->smartcard_state;
+    if (scc->smartcard) {
+        RedCharDeviceSmartcard *dev = scc->smartcard;
 
         smartcard_char_device_detach_client(scc);
-        smartcard_char_device_notify_reader_remove(st);
+        smartcard_char_device_notify_reader_remove(dev);
     }
 }
 
@@ -620,7 +620,7 @@ static void smartcard_unref_vsc_msg_item(MsgItem *item)
 static void smartcard_remove_reader(SmartCardChannelClient *scc, uint32_t reader_id)
 {
     SpiceCharDeviceInstance *char_device = smartcard_readers_get(reader_id);
-    SmartCardDeviceState *state;
+    RedCharDeviceSmartcard *dev;
 
     if (char_device == NULL) {
         smartcard_push_error(&scc->base, reader_id,
@@ -628,19 +628,19 @@ static void smartcard_remove_reader(SmartCardChannelClient *scc, uint32_t reader
         return;
     }
 
-    state = red_char_device_opaque_get(char_device->st);
-    if (state->priv->reader_added == FALSE) {
+    dev = red_char_device_opaque_get(char_device->st);
+    if (dev->priv->reader_added == FALSE) {
         smartcard_push_error(&scc->base, reader_id,
             VSC_GENERAL_ERROR);
         return;
     }
-    spice_assert(scc->smartcard_state == state);
-    smartcard_char_device_notify_reader_remove(state);
+    spice_assert(scc->smartcard == dev);
+    smartcard_char_device_notify_reader_remove(dev);
 }
 
 static void smartcard_add_reader(SmartCardChannelClient *scc, uint8_t *name)
 {
-    if (!scc->smartcard_state) { /* we already tried to attach a reader to the client
+    if (!scc->smartcard) { /* we already tried to attach a reader to the client
                                     when it connected */
         SpiceCharDeviceInstance *char_device = smartcard_readers_get_unattached();
 
@@ -651,16 +651,16 @@ static void smartcard_add_reader(SmartCardChannelClient *scc, uint8_t *name)
         }
         smartcard_char_device_attach_client(char_device, scc);
     }
-    smartcard_char_device_notify_reader_add(scc->smartcard_state);
+    smartcard_char_device_notify_reader_add(scc->smartcard);
     // The device sends a VSC_Error message, we will let it through, no
     // need to send our own. We already set the correct reader_id, from
-    // our SmartCardDeviceState.
+    // our RedCharDeviceSmartcard.
 }
 
 static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_buf)
 {
     SpiceCharDeviceInstance *sin;
-    SmartCardDeviceState *st;
+    RedCharDeviceSmartcard *dev;
     VSCMsgHeader *vheader;
     uint32_t actual_length;
 
@@ -669,8 +669,8 @@ static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_bu
 
     spice_assert(vheader->reader_id <= g_smartcard_readers.num);
     sin = g_smartcard_readers.sin[vheader->reader_id];
-    st = (SmartCardDeviceState *)red_char_device_opaque_get(sin->st);
-    spice_assert(!st->priv->scc || st == st->priv->scc->smartcard_state);
+    dev = (RedCharDeviceSmartcard *)red_char_device_opaque_get(sin->st);
+    spice_assert(!dev->priv->scc || dev == dev->priv->scc->smartcard);
     /* protocol requires messages to be in network endianess */
     vheader->type = htonl(vheader->type);
     vheader->length = htonl(vheader->length);
@@ -679,8 +679,8 @@ static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_bu
     /* pushing the buffer to the write queue; It will be released
      * when it will be fully consumed by the device */
     red_char_device_write_buffer_add(sin->st, write_buf);
-    if (st->priv->scc && write_buf == st->priv->scc->write_buf) {
-        st->priv->scc->write_buf = NULL;
+    if (dev->priv->scc && write_buf == dev->priv->scc->write_buf) {
+        dev->priv->scc->write_buf = NULL;
     }
 }
 
@@ -690,21 +690,21 @@ static int smartcard_channel_client_handle_migrate_flush_mark(RedChannelClient *
     return TRUE;
 }
 
-static void smartcard_device_state_restore_partial_read(SmartCardDeviceState *state,
-                                                        SpiceMigrateDataSmartcard *mig_data)
+static void smartcard_device_restore_partial_read(RedCharDeviceSmartcard *dev,
+                                                  SpiceMigrateDataSmartcard *mig_data)
 {
     uint8_t *read_data;
 
     spice_debug("read_size  %u", mig_data->read_size);
     read_data = (uint8_t *)mig_data + mig_data->read_data_ptr - sizeof(SpiceMigrateDataHeader);
     if (mig_data->read_size < sizeof(VSCMsgHeader)) {
-        spice_assert(state->priv->buf_size >= mig_data->read_size);
+        spice_assert(dev->priv->buf_size >= mig_data->read_size);
     } else {
-        smartcard_read_buf_prepare(state, (VSCMsgHeader *)read_data);
+        smartcard_read_buf_prepare(dev, (VSCMsgHeader *)read_data);
     }
-    memcpy(state->priv->buf, read_data, mig_data->read_size);
-    state->priv->buf_used = mig_data->read_size;
-    state->priv->buf_pos = state->priv->buf + mig_data->read_size;
+    memcpy(dev->priv->buf, read_data, mig_data->read_size);
+    dev->priv->buf_used = mig_data->read_size;
+    dev->priv->buf_pos = dev->priv->buf + mig_data->read_size;
 }
 
 static int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
@@ -732,7 +732,7 @@ static int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
         return TRUE;
     }
 
-    if (!scc->smartcard_state) {
+    if (!scc->smartcard) {
         SpiceCharDeviceInstance *char_device = smartcard_readers_get_unattached();
 
         if (!char_device) {
@@ -743,10 +743,10 @@ static int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
         }
     }
     spice_debug("reader added %d partial read_size %u", mig_data->reader_added, mig_data->read_size);
-    scc->smartcard_state->priv->reader_added = mig_data->reader_added;
+    scc->smartcard->priv->reader_added = mig_data->reader_added;
 
-    smartcard_device_state_restore_partial_read(scc->smartcard_state, mig_data);
-    return red_char_device_restore(scc->smartcard_state->priv->chardev, &mig_data->base);
+    smartcard_device_restore_partial_read(scc->smartcard, mig_data);
+    return red_char_device_restore(scc->smartcard->priv->chardev, &mig_data->base);
 }
 
 static int smartcard_channel_handle_message(RedChannelClient *rcc,


More information about the Spice-commits mailing list