[spice] improve connection code to use a single buffer

This commit is contained in:
Geoffrey McRae 2020-01-29 16:52:23 +11:00
parent 2e32ceb6e0
commit 29f221d547
2 changed files with 36 additions and 39 deletions

View File

@ -1 +1 @@
B1-137-g2cbc9b6426+1 B1-138-g2e32ceb6e0+1

View File

@ -746,66 +746,63 @@ bool spice_connect_channel(struct SpiceChannel * channel)
} }
channel->connected = true; channel->connected = true;
typedef struct
{
SpiceLinkHeader header;
SpiceLinkMess message;
uint32_t supportCaps[COMMON_CAPS_BYTES / sizeof(uint32_t)]; uint32_t supportCaps[COMMON_CAPS_BYTES / sizeof(uint32_t)];
uint32_t channelCaps[MAIN_CAPS_BYTES / sizeof(uint32_t)]; uint32_t channelCaps[MAIN_CAPS_BYTES / sizeof(uint32_t)];
memset(supportCaps, 0, sizeof(supportCaps)); }
memset(channelCaps, 0, sizeof(channelCaps)); __attribute__((packed)) ConnectPacket;
COMMON_SET_CAPABILITY(supportCaps, SPICE_COMMON_CAP_PROTOCOL_AUTH_SELECTION); ConnectPacket p =
COMMON_SET_CAPABILITY(supportCaps, SPICE_COMMON_CAP_AUTH_SPICE );
COMMON_SET_CAPABILITY(supportCaps, SPICE_COMMON_CAP_MINI_HEADER );
if (channel == &spice.scMain)
MAIN_SET_CAPABILITY(channelCaps, SPICE_MAIN_CAP_AGENT_CONNECTED_TOKENS);
SpiceLinkHeader header =
{ {
.header = {
.magic = SPICE_MAGIC , .magic = SPICE_MAGIC ,
.major_version = SPICE_VERSION_MAJOR, .major_version = SPICE_VERSION_MAJOR,
.minor_version = SPICE_VERSION_MINOR, .minor_version = SPICE_VERSION_MINOR,
.size = .size = sizeof(ConnectPacket) - sizeof(SpiceLinkHeader)
sizeof(SpiceLinkMess) + },
sizeof(supportCaps ) + .message = {
sizeof(channelCaps )
};
SpiceLinkMess message =
{
.connection_id = spice.sessionID, .connection_id = spice.sessionID,
.channel_type = channel->channelType, .channel_type = channel->channelType,
.channel_id = spice.channelID, .channel_id = spice.channelID,
.num_common_caps = sizeof(supportCaps) / sizeof(uint32_t), .num_common_caps = COMMON_CAPS_BYTES / sizeof(uint32_t),
.num_channel_caps = sizeof(channelCaps) / sizeof(uint32_t), .num_channel_caps = MAIN_CAPS_BYTES / sizeof(uint32_t),
.caps_offset = sizeof(SpiceLinkMess) .caps_offset = sizeof(SpiceLinkMess)
}
}; };
if ( COMMON_SET_CAPABILITY(p.supportCaps, SPICE_COMMON_CAP_PROTOCOL_AUTH_SELECTION);
!spice_write_nl(channel, &header , sizeof(header )) || COMMON_SET_CAPABILITY(p.supportCaps, SPICE_COMMON_CAP_AUTH_SPICE );
!spice_write_nl(channel, &message , sizeof(message )) || COMMON_SET_CAPABILITY(p.supportCaps, SPICE_COMMON_CAP_MINI_HEADER );
!spice_write_nl(channel, &supportCaps, sizeof(supportCaps)) ||
!spice_write_nl(channel, &channelCaps, sizeof(channelCaps)) if (channel == &spice.scMain)
) MAIN_SET_CAPABILITY(p.channelCaps, SPICE_MAIN_CAP_AGENT_CONNECTED_TOKENS);
if (!spice_write_nl(channel, &p, sizeof(p)))
{ {
DEBUG_ERROR("failed to write the initial payload"); DEBUG_ERROR("failed to write the initial payload");
spice_disconnect_channel(channel); spice_disconnect_channel(channel);
return false; return false;
} }
if (!spice_read_nl(channel, &header, sizeof(header))) if (!spice_read_nl(channel, &p.header, sizeof(p.header)))
{ {
DEBUG_ERROR("failed to read SpiceLinkHeader"); DEBUG_ERROR("failed to read SpiceLinkHeader");
spice_disconnect_channel(channel); spice_disconnect_channel(channel);
return false; return false;
} }
if (header.magic != SPICE_MAGIC || header.major_version != SPICE_VERSION_MAJOR) if (p.header.magic != SPICE_MAGIC ||
p.header.major_version != SPICE_VERSION_MAJOR)
{ {
DEBUG_ERROR("invalid or unsupported protocol version"); DEBUG_ERROR("invalid or unsupported protocol version");
spice_disconnect_channel(channel); spice_disconnect_channel(channel);
return false; return false;
} }
if (header.size < sizeof(SpiceLinkReply)) if (p.header.size < sizeof(SpiceLinkReply))
{ {
DEBUG_ERROR("reported data size too small"); DEBUG_ERROR("reported data size too small");
spice_disconnect_channel(channel); spice_disconnect_channel(channel);