@@ -9,26 +9,33 @@ use uuid::Uuid;
9
9
10
10
use crate :: ConnectionType ;
11
11
use crate :: packet:: { DeserializePacket , SerializePacket } ;
12
-
12
+ use crate :: utils :: ConnectionIntent ;
13
13
14
14
pub enum HandshakePacketGuestToHost {
15
- // in
15
+ /// Establish a connection with the other server
16
16
Hello {
17
17
connection_type : ConnectionType ,
18
18
} ,
19
+
19
20
/// Send my hostname to the other server
20
21
Identify {
21
22
hostname : String ,
22
23
} ,
24
+
23
25
/// Send the client-decrypted challenge bytes back to the server
24
26
Verify {
25
27
challenge : Vec < u8 > ,
26
28
nonce : Uuid ,
27
29
} ,
30
+
31
+ /// Inform the other server of my intent.
32
+ SetIntent {
33
+ intent : ConnectionIntent ,
34
+ }
28
35
}
29
36
30
37
pub enum HandshakePacketHostToGuest {
31
- // out
38
+ /// Acknowledge the client connection
32
39
Acknowledge {
33
40
ok : bool ,
34
41
err : Option < String > ,
@@ -39,6 +46,13 @@ pub enum HandshakePacketHostToGuest {
39
46
encrypted_challenge : Vec < u8 > ,
40
47
nonce : Uuid ,
41
48
} ,
49
+
50
+ /// Tell the client whether the challenge was successful
51
+ ChallengeResponse {
52
+ successful : bool ,
53
+ } ,
54
+
55
+ /// Close the Handshake frame
42
56
Close {
43
57
can_continue : bool ,
44
58
err : Option < String >
@@ -51,6 +65,7 @@ impl From<&HandshakePacketGuestToHost> for u8 {
51
65
HandshakePacketGuestToHost :: Hello { .. } => 1 ,
52
66
HandshakePacketGuestToHost :: Identify { .. } => 2 ,
53
67
HandshakePacketGuestToHost :: Verify { .. } => 3 ,
68
+ HandshakePacketGuestToHost :: SetIntent { .. } => 4 ,
54
69
}
55
70
}
56
71
}
@@ -60,7 +75,8 @@ impl From<&HandshakePacketHostToGuest> for u8 {
60
75
match pkt {
61
76
HandshakePacketHostToGuest :: Acknowledge { .. } => 1 ,
62
77
HandshakePacketHostToGuest :: Challenge { .. } => 2 ,
63
- HandshakePacketHostToGuest :: Close { .. } => 3
78
+ HandshakePacketHostToGuest :: Close { .. } => 3 ,
79
+ HandshakePacketHostToGuest :: ChallengeResponse { .. } => 4 ,
64
80
}
65
81
}
66
82
}
@@ -72,7 +88,7 @@ impl SerializePacket for HandshakePacketGuestToHost {
72
88
let mut bytes_written: usize = 1 ;
73
89
match self {
74
90
HandshakePacketGuestToHost :: Hello { connection_type } => {
75
- buf. put_u8 ( u8 :: from ( connection_type) ) ;
91
+ buf. put_u8 ( connection_type. into ( ) ) ;
76
92
bytes_written += 1
77
93
}
78
94
HandshakePacketGuestToHost :: Identify { hostname } => {
@@ -85,6 +101,10 @@ impl SerializePacket for HandshakePacketGuestToHost {
85
101
buf. put_slice ( challenge) ;
86
102
bytes_written += 256 ;
87
103
}
104
+ HandshakePacketGuestToHost :: SetIntent { intent } => {
105
+ buf. put_u8 ( intent. into ( ) ) ;
106
+ bytes_written += 1 ;
107
+ }
88
108
}
89
109
Ok ( bytes_written)
90
110
}
@@ -115,6 +135,10 @@ impl SerializePacket for HandshakePacketHostToGuest {
115
135
116
136
bytes_written += self . write_optional_string ( buf, err) ;
117
137
}
138
+ HandshakePacketHostToGuest :: ChallengeResponse { successful } => {
139
+ buf. put_u8 ( * successful as u8 ) ;
140
+ bytes_written += 1 ;
141
+ }
118
142
}
119
143
120
144
Ok ( bytes_written)
@@ -128,7 +152,7 @@ impl DeserializePacket for HandshakePacketGuestToHost {
128
152
// We'll match the same `u8` that is used to recognize which request type this is
129
153
match buf. get_u8 ( ) {
130
154
1 => Ok ( HandshakePacketGuestToHost :: Hello {
131
- connection_type : ConnectionType :: from_u8 ( buf. get_u8 ( ) ) ,
155
+ connection_type : ConnectionType :: from ( buf. get_u8 ( ) ) ,
132
156
} ) ,
133
157
2 => Ok ( HandshakePacketGuestToHost :: Identify {
134
158
hostname : Self :: read_string ( buf) ?,
@@ -143,6 +167,9 @@ impl DeserializePacket for HandshakePacketGuestToHost {
143
167
nonce,
144
168
} )
145
169
} ,
170
+ 4 => Ok ( HandshakePacketGuestToHost :: SetIntent {
171
+ intent : ConnectionIntent :: from ( buf. get_u8 ( ) ) ,
172
+ } ) ,
146
173
_ => Err ( io:: Error :: new (
147
174
io:: ErrorKind :: InvalidData ,
148
175
"Invalid Request Type" ,
@@ -174,6 +201,9 @@ impl DeserializePacket for HandshakePacketHostToGuest {
174
201
can_continue : buf. get_u8 ( ) != 0 ,
175
202
err : Self :: read_optional_string ( buf) ?,
176
203
} ) ,
204
+ 4 => Ok ( HandshakePacketHostToGuest :: ChallengeResponse {
205
+ successful : buf. get_u8 ( ) != 0 ,
206
+ } ) ,
177
207
_ => Err ( io:: Error :: new (
178
208
io:: ErrorKind :: InvalidData ,
179
209
"Invalid Request Type" ,
@@ -184,7 +214,8 @@ impl DeserializePacket for HandshakePacketHostToGuest {
184
214
185
215
#[ cfg( test) ]
186
216
mod tests {
187
- async fn serialize_handshake_packets ( ) {
217
+ #[ test]
218
+ async fn serialize_gth_hello ( ) {
188
219
189
220
}
190
221
}
0 commit comments