Skip to content

Commit 49f39a1

Browse files
committed
feat(data,transfer): implement outbound & inbound, swap roles, add handshake intent
1 parent d365cce commit 49f39a1

File tree

7 files changed

+327
-127
lines changed

7 files changed

+327
-127
lines changed

crates/protocol/src/packet/handshake.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,33 @@ use uuid::Uuid;
99

1010
use crate::ConnectionType;
1111
use crate::packet::{DeserializePacket, SerializePacket};
12-
12+
use crate::utils::ConnectionIntent;
1313

1414
pub enum HandshakePacketGuestToHost {
15-
// in
15+
/// Establish a connection with the other server
1616
Hello {
1717
connection_type: ConnectionType,
1818
},
19+
1920
/// Send my hostname to the other server
2021
Identify {
2122
hostname: String,
2223
},
24+
2325
/// Send the client-decrypted challenge bytes back to the server
2426
Verify {
2527
challenge: Vec<u8>,
2628
nonce: Uuid,
2729
},
30+
31+
/// Inform the other server of my intent.
32+
SetIntent {
33+
intent: ConnectionIntent,
34+
}
2835
}
2936

3037
pub enum HandshakePacketHostToGuest {
31-
// out
38+
/// Acknowledge the client connection
3239
Acknowledge {
3340
ok: bool,
3441
err: Option<String>,
@@ -39,6 +46,13 @@ pub enum HandshakePacketHostToGuest {
3946
encrypted_challenge: Vec<u8>,
4047
nonce: Uuid,
4148
},
49+
50+
/// Tell the client whether the challenge was successful
51+
ChallengeResponse {
52+
successful: bool,
53+
},
54+
55+
/// Close the Handshake frame
4256
Close {
4357
can_continue: bool,
4458
err: Option<String>
@@ -51,6 +65,7 @@ impl From<&HandshakePacketGuestToHost> for u8 {
5165
HandshakePacketGuestToHost::Hello { .. } => 1,
5266
HandshakePacketGuestToHost::Identify { .. } => 2,
5367
HandshakePacketGuestToHost::Verify { .. } => 3,
68+
HandshakePacketGuestToHost::SetIntent { .. } => 4,
5469
}
5570
}
5671
}
@@ -60,7 +75,8 @@ impl From<&HandshakePacketHostToGuest> for u8 {
6075
match pkt {
6176
HandshakePacketHostToGuest::Acknowledge { .. } => 1,
6277
HandshakePacketHostToGuest::Challenge { .. } => 2,
63-
HandshakePacketHostToGuest::Close { .. } => 3
78+
HandshakePacketHostToGuest::Close { .. } => 3,
79+
HandshakePacketHostToGuest::ChallengeResponse { .. } => 4,
6480
}
6581
}
6682
}
@@ -72,7 +88,7 @@ impl SerializePacket for HandshakePacketGuestToHost {
7288
let mut bytes_written: usize = 1;
7389
match self {
7490
HandshakePacketGuestToHost::Hello { connection_type } => {
75-
buf.put_u8(u8::from(connection_type));
91+
buf.put_u8(connection_type.into());
7692
bytes_written += 1
7793
}
7894
HandshakePacketGuestToHost::Identify { hostname } => {
@@ -85,6 +101,10 @@ impl SerializePacket for HandshakePacketGuestToHost {
85101
buf.put_slice(challenge);
86102
bytes_written += 256;
87103
}
104+
HandshakePacketGuestToHost::SetIntent { intent } => {
105+
buf.put_u8(intent.into());
106+
bytes_written += 1;
107+
}
88108
}
89109
Ok(bytes_written)
90110
}
@@ -115,6 +135,10 @@ impl SerializePacket for HandshakePacketHostToGuest {
115135

116136
bytes_written += self.write_optional_string(buf, err);
117137
}
138+
HandshakePacketHostToGuest::ChallengeResponse { successful } => {
139+
buf.put_u8(*successful as u8);
140+
bytes_written += 1;
141+
}
118142
}
119143

120144
Ok(bytes_written)
@@ -128,7 +152,7 @@ impl DeserializePacket for HandshakePacketGuestToHost {
128152
// We'll match the same `u8` that is used to recognize which request type this is
129153
match buf.get_u8() {
130154
1 => Ok(HandshakePacketGuestToHost::Hello {
131-
connection_type: ConnectionType::from_u8(buf.get_u8()),
155+
connection_type: ConnectionType::from(buf.get_u8()),
132156
}),
133157
2 => Ok(HandshakePacketGuestToHost::Identify {
134158
hostname: Self::read_string(buf)?,
@@ -143,6 +167,9 @@ impl DeserializePacket for HandshakePacketGuestToHost {
143167
nonce,
144168
})
145169
},
170+
4 => Ok(HandshakePacketGuestToHost::SetIntent {
171+
intent: ConnectionIntent::from(buf.get_u8()),
172+
}),
146173
_ => Err(io::Error::new(
147174
io::ErrorKind::InvalidData,
148175
"Invalid Request Type",
@@ -174,6 +201,9 @@ impl DeserializePacket for HandshakePacketHostToGuest {
174201
can_continue: buf.get_u8() != 0,
175202
err: Self::read_optional_string(buf)?,
176203
}),
204+
4 => Ok(HandshakePacketHostToGuest::ChallengeResponse {
205+
successful: buf.get_u8() != 0,
206+
}),
177207
_ => Err(io::Error::new(
178208
io::ErrorKind::InvalidData,
179209
"Invalid Request Type",
@@ -184,7 +214,8 @@ impl DeserializePacket for HandshakePacketHostToGuest {
184214

185215
#[cfg(test)]
186216
mod tests {
187-
async fn serialize_handshake_packets() {
217+
#[test]
218+
async fn serialize_gth_hello() {
188219

189220
}
190221
}

crates/protocol/src/packet/transfer.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use uuid::Uuid;
77

88
use crate::packet::{DeserializePacket, SerializePacket};
99

10-
pub enum TransferPacketGuestToHost {
10+
pub enum TransferPacketHostToGuest {
1111
AcknowledgeObject {
1212
can_send: bool
1313
}
1414
}
1515

16-
pub enum TransferPacketHostToGuest {
16+
pub enum TransferPacketGuestToHost {
1717
IdentifyObject {
1818
data_id: Uuid,
1919
data_len: usize,
@@ -27,20 +27,20 @@ pub enum TransferPacketHostToGuest {
2727

2828

2929

30-
impl From<&TransferPacketGuestToHost> for u8 {
31-
fn from(pkt: &TransferPacketGuestToHost) -> Self {
30+
impl From<&TransferPacketHostToGuest> for u8 {
31+
fn from(pkt: &TransferPacketHostToGuest) -> Self {
3232
match pkt {
33-
TransferPacketGuestToHost::AcknowledgeObject { .. } => 1
33+
TransferPacketHostToGuest::AcknowledgeObject { .. } => 1
3434
}
3535
}
3636
}
3737

38-
impl SerializePacket for TransferPacketGuestToHost {
38+
impl SerializePacket for TransferPacketHostToGuest {
3939
fn serialize(&self, buf: &mut BytesMut) -> io::Result<usize> {
4040
buf.put_u8(self.into());
4141
let mut bytes_written = 1;
4242
match self {
43-
TransferPacketGuestToHost::AcknowledgeObject { can_send } => {
43+
TransferPacketHostToGuest::AcknowledgeObject { can_send } => {
4444
buf.put_u8(*can_send as u8);
4545
bytes_written += 1;
4646
}
@@ -50,12 +50,12 @@ impl SerializePacket for TransferPacketGuestToHost {
5050
}
5151
}
5252

53-
impl DeserializePacket for TransferPacketGuestToHost {
54-
type Output = TransferPacketGuestToHost;
53+
impl DeserializePacket for TransferPacketHostToGuest {
54+
type Output = TransferPacketHostToGuest;
5555

5656
fn deserialize(buf: &mut BytesMut) -> io::Result<Self::Output> {
5757
match buf.get_u8() {
58-
1 => Ok(TransferPacketGuestToHost::AcknowledgeObject {
58+
1 => Ok(TransferPacketHostToGuest::AcknowledgeObject {
5959
can_send: buf.get_u8() != 0,
6060
}),
6161
_ => Err(io::Error::new(
@@ -66,28 +66,28 @@ impl DeserializePacket for TransferPacketGuestToHost {
6666
}
6767
}
6868

69-
impl From<&TransferPacketHostToGuest> for u8 {
70-
fn from(pkt: &TransferPacketHostToGuest) -> Self {
69+
impl From<&TransferPacketGuestToHost> for u8 {
70+
fn from(pkt: &TransferPacketGuestToHost) -> Self {
7171
match pkt {
72-
TransferPacketHostToGuest::IdentifyObject { .. } => 1,
73-
TransferPacketHostToGuest::SendChunk { .. } => 2
72+
TransferPacketGuestToHost::IdentifyObject { .. } => 1,
73+
TransferPacketGuestToHost::SendChunk { .. } => 2
7474
}
7575
}
7676
}
7777

78-
impl SerializePacket for TransferPacketHostToGuest {
78+
impl SerializePacket for TransferPacketGuestToHost {
7979
fn serialize(&self, buf: &mut BytesMut) -> io::Result<usize> {
8080
buf.put_u8(self.into());
8181
let mut bytes_written = 1;
8282

8383
match self {
84-
TransferPacketHostToGuest::IdentifyObject { data_chunks, data_len, data_id } => {
84+
TransferPacketGuestToHost::IdentifyObject { data_chunks, data_len, data_id } => {
8585
bytes_written += self.write_uuid(buf, data_id);
8686
buf.put_u64(*data_len as u64);
8787
buf.put_u64(*data_chunks as u64);
8888
bytes_written += 16; // two u64
8989
}
90-
TransferPacketHostToGuest::SendChunk { data, done } => {
90+
TransferPacketGuestToHost::SendChunk { data, done } => {
9191
buf.put_u64(data.len() as u64);
9292
bytes_written += 8;
9393
buf.put_slice(data);
@@ -101,12 +101,12 @@ impl SerializePacket for TransferPacketHostToGuest {
101101
}
102102
}
103103

104-
impl DeserializePacket for TransferPacketHostToGuest {
105-
type Output = TransferPacketHostToGuest;
104+
impl DeserializePacket for TransferPacketGuestToHost {
105+
type Output = TransferPacketGuestToHost;
106106

107107
fn deserialize(buf: &mut BytesMut) -> io::Result<Self::Output> {
108108
match buf.get_u8() {
109-
1 => Ok(TransferPacketHostToGuest::IdentifyObject {
109+
1 => Ok(TransferPacketGuestToHost::IdentifyObject {
110110
data_id: Self::read_uuid(buf),
111111
data_len: buf.get_u64() as usize,
112112
data_chunks: buf.get_u64() as usize,
@@ -116,7 +116,7 @@ impl DeserializePacket for TransferPacketHostToGuest {
116116
let mut data_buf = vec![0u8; data_len];
117117
buf.copy_to_slice(&mut data_buf);
118118

119-
Ok(TransferPacketHostToGuest::SendChunk {
119+
Ok(TransferPacketGuestToHost::SendChunk {
120120
data: data_buf,
121121
done: buf.get_u8() != 0,
122122
})

crates/protocol/src/utils.rs

+48-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
#[derive(Clone)]
1+
use std::fmt::{Display, Formatter};
2+
3+
#[derive(Clone, PartialEq)]
24
pub enum ConnectionType {
35
Unknown = 0,
46
Client = 1,
57
Server = 2
68
}
79

8-
impl ConnectionType {
9-
pub(crate) fn from_u8(t: u8) -> ConnectionType {
10+
impl From<u8> for ConnectionType {
11+
fn from(t: u8) -> ConnectionType {
1012
match t {
1113
1 => ConnectionType::Client,
1214
2 => ConnectionType::Server,
@@ -23,4 +25,47 @@ impl From<&ConnectionType> for u8 {
2325
ConnectionType::Server => 2,
2426
}
2527
}
28+
}
29+
30+
#[derive(Clone, PartialEq)]
31+
pub enum ConnectionIntent {
32+
Subscribe,
33+
TransferData,
34+
Unknown,
35+
}
36+
37+
impl From<u8> for ConnectionIntent {
38+
fn from(i: u8) -> Self {
39+
match i {
40+
1 => ConnectionIntent::Subscribe,
41+
2 => ConnectionIntent::TransferData,
42+
_ => ConnectionIntent::Unknown,
43+
}
44+
}
45+
}
46+
47+
impl From<&ConnectionIntent> for u8 {
48+
fn from(i: &ConnectionIntent) -> Self {
49+
match i {
50+
ConnectionIntent::Unknown => 0,
51+
ConnectionIntent::Subscribe => 1,
52+
ConnectionIntent::TransferData => 2,
53+
}
54+
}
55+
}
56+
57+
impl Display for ConnectionIntent {
58+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59+
match self {
60+
ConnectionIntent::Subscribe => {
61+
write!(f, "subscribe")
62+
}
63+
ConnectionIntent::TransferData => {
64+
write!(f, "transfer-data")
65+
}
66+
ConnectionIntent::Unknown => {
67+
write!(f, "unknown")
68+
}
69+
}
70+
}
2671
}

0 commit comments

Comments
 (0)