Skip to content

Commit a971a93

Browse files
author
Roman Proskuryakov
authored
Merge pull request #386 from tox-rs/net_crypto_tcp
Send net crypto packets via TCP
2 parents b9e7e7e + 08f68d9 commit a971a93

File tree

3 files changed

+180
-85
lines changed

3 files changed

+180
-85
lines changed

src/toxcore/net_crypto/crypto_connection.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub const REQUEST_PACKETS_COMPARE_CONSTANT: f64 = 0.125 * 100.0;
6565
/// Packet that should be sent every second. Depending on `ConnectionStatus` it
6666
/// can be `CookieRequest` or `CryptoHandshake`
6767
#[derive(Clone, Debug, Eq, PartialEq)]
68-
enum StatusPacketEnum {
68+
pub enum StatusPacket {
6969
/// `CookieRequest` packet
7070
CookieRequest(CookieRequest),
7171
/// `CryptoHandshake` packet
@@ -75,43 +75,35 @@ enum StatusPacketEnum {
7575
/// Packet that should be sent to the peer every second together with info how
7676
/// many times it was sent and when it was sent last time
7777
#[derive(Clone, Debug, Eq, PartialEq)]
78-
pub struct StatusPacket {
78+
pub struct StatusPacketWithTime {
7979
/// Packet that should be sent every second. Depending on `ConnectionStatus`
8080
/// it can be `CookieRequest` or `CryptoHandshake`
81-
packet: StatusPacketEnum,
81+
pub packet: StatusPacket,
8282
/// When packet was sent last time
8383
pub sent_time: Instant,
8484
/// How many times packet was sent
8585
pub num_sent: u8
8686
}
8787

88-
impl StatusPacket {
88+
impl StatusPacketWithTime {
8989
/// Create new `StatusPacket` with `CookieRequest` packet
90-
pub fn new_cookie_request(packet: CookieRequest) -> StatusPacket {
91-
StatusPacket {
92-
packet: StatusPacketEnum::CookieRequest(packet),
90+
pub fn new_cookie_request(packet: CookieRequest) -> StatusPacketWithTime {
91+
StatusPacketWithTime {
92+
packet: StatusPacket::CookieRequest(packet),
9393
sent_time: clock_now(),
9494
num_sent: 0
9595
}
9696
}
9797

9898
/// Create new `StatusPacket` with `CryptoHandshake` packet
99-
pub fn new_crypto_handshake(packet: CryptoHandshake) -> StatusPacket {
100-
StatusPacket {
101-
packet: StatusPacketEnum::CryptoHandshake(packet),
99+
pub fn new_crypto_handshake(packet: CryptoHandshake) -> StatusPacketWithTime {
100+
StatusPacketWithTime {
101+
packet: StatusPacket::CryptoHandshake(packet),
102102
sent_time: clock_now(),
103103
num_sent: 0
104104
}
105105
}
106106

107-
/// Get `Packet` that should be sent every second
108-
pub fn dht_packet(&self) -> Packet {
109-
match self.packet {
110-
StatusPacketEnum::CookieRequest(ref packet) => Packet::CookieRequest(packet.clone()),
111-
StatusPacketEnum::CryptoHandshake(ref packet) => Packet::CryptoHandshake(packet.clone()),
112-
}
113-
}
114-
115107
/// Check if one second is elapsed since last time when the packet was sent
116108
fn is_time_elapsed(&self) -> bool {
117109
clock_elapsed(self.sent_time) > CRYPTO_SEND_PACKET_INTERVAL
@@ -156,15 +148,15 @@ pub enum ConnectionStatus {
156148
/// ID used in the cookie request packets for this connection
157149
cookie_request_id: u64,
158150
/// Packet that should be sent every second
159-
packet: StatusPacket,
151+
packet: StatusPacketWithTime,
160152
},
161153
/// We are sending handshake packets and haven't received handshake from the
162154
/// other side yet.
163155
HandshakeSending {
164156
/// Nonce that should be used to encrypt outgoing packets
165157
sent_nonce: Nonce,
166158
/// Packet that should be sent every second
167-
packet: StatusPacket,
159+
packet: StatusPacketWithTime,
168160
},
169161
/// A handshake packet has been received from the other side but no
170162
/// encrypted packets. Continue sending handshake packets because we can't
@@ -178,7 +170,7 @@ pub enum ConnectionStatus {
178170
/// decrypt data packets
179171
session_precomputed_key: PrecomputedKey,
180172
/// Packet that should be sent every second
181-
packet: StatusPacket,
173+
packet: StatusPacketWithTime,
182174
},
183175
/// A valid encrypted packet has been received from the other side.
184176
/// Connection is fully established.
@@ -339,7 +331,7 @@ impl CryptoConnection {
339331
let cookie_request = CookieRequest::new(dht_precomputed_key, &dht_pk, &cookie_request_payload);
340332
let status = ConnectionStatus::CookieRequesting {
341333
cookie_request_id,
342-
packet: StatusPacket::new_cookie_request(cookie_request)
334+
packet: StatusPacketWithTime::new_cookie_request(cookie_request)
343335
};
344336

345337
CryptoConnection {
@@ -398,7 +390,7 @@ impl CryptoConnection {
398390
sent_nonce,
399391
received_nonce,
400392
session_precomputed_key: precompute(&peer_session_pk, &session_sk),
401-
packet: StatusPacket::new_crypto_handshake(handshake)
393+
packet: StatusPacketWithTime::new_crypto_handshake(handshake)
402394
};
403395

404396
CryptoConnection {
@@ -431,15 +423,15 @@ impl CryptoConnection {
431423

432424
/// Get `CookieRequest` or `CryptoHandshake` if it should be sent depending
433425
/// on connection status and update sent counter
434-
pub fn packet_to_send(&mut self) -> Option<Packet> {
426+
pub fn packet_to_send(&mut self) -> Option<StatusPacket> {
435427
match self.status {
436428
ConnectionStatus::CookieRequesting { ref mut packet, .. }
437429
| ConnectionStatus::HandshakeSending { ref mut packet, .. }
438430
| ConnectionStatus::NotConfirmed { ref mut packet, .. } => {
439431
if packet.should_be_sent() {
440432
packet.num_sent += 1;
441433
packet.sent_time = clock_now();
442-
Some(packet.dht_packet())
434+
Some(packet.packet.clone())
443435
} else {
444436
None
445437
}
@@ -674,7 +666,7 @@ mod tests {
674666
fn status_packet_should_be_sent() {
675667
crypto_init().unwrap();
676668
// just created packet should be sent
677-
let mut packet = StatusPacket::new_cookie_request(CookieRequest {
669+
let mut packet = StatusPacketWithTime::new_cookie_request(CookieRequest {
678670
pk: gen_keypair().0,
679671
nonce: gen_nonce(),
680672
payload: vec![42; 88]
@@ -704,7 +696,7 @@ mod tests {
704696
fn status_packet_is_timed_out() {
705697
crypto_init().unwrap();
706698
// just created packet isn't timed out
707-
let mut packet = StatusPacket::new_cookie_request(CookieRequest {
699+
let mut packet = StatusPacketWithTime::new_cookie_request(CookieRequest {
708700
pk: gen_keypair().0,
709701
nonce: gen_nonce(),
710702
payload: vec![42; 88]
@@ -765,7 +757,7 @@ mod tests {
765757

766758
connection.status = ConnectionStatus::HandshakeSending {
767759
sent_nonce: gen_nonce(),
768-
packet: StatusPacket::new_crypto_handshake(crypto_handshake.clone())
760+
packet: StatusPacketWithTime::new_crypto_handshake(crypto_handshake.clone())
769761
};
770762

771763
let connection_c = connection.clone();
@@ -775,7 +767,7 @@ mod tests {
775767
sent_nonce: gen_nonce(),
776768
received_nonce: gen_nonce(),
777769
session_precomputed_key: precompute(&gen_keypair().0, &gen_keypair().1),
778-
packet: StatusPacket::new_crypto_handshake(crypto_handshake),
770+
packet: StatusPacketWithTime::new_crypto_handshake(crypto_handshake),
779771
};
780772

781773
let connection_c = connection.clone();
@@ -896,7 +888,7 @@ mod tests {
896888
sent_nonce: gen_nonce(),
897889
received_nonce: gen_nonce(),
898890
session_precomputed_key: precompute(&gen_keypair().0, &gen_keypair().1),
899-
packet: StatusPacket::new_crypto_handshake(crypto_handshake),
891+
packet: StatusPacketWithTime::new_crypto_handshake(crypto_handshake),
900892
};
901893

902894
assert!(connection.is_not_confirmed());

src/toxcore/net_crypto/errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,19 @@ error_kind! {
232232
SendToConnectionStatus,
233233
}
234234
}
235+
236+
error_kind! {
237+
#[doc = "Error that can happen during a packet sending."]
238+
#[derive(Debug)]
239+
SendPacketError,
240+
#[doc = "The specific kind of error that can occur."]
241+
#[derive(Debug, Eq, PartialEq, Fail)]
242+
SendPacketErrorKind {
243+
#[doc = "Failed to send TCP packet."]
244+
#[fail(display = "Failed to send TCP packet")]
245+
Tcp,
246+
#[doc = "Failed to send UDP packet."]
247+
#[fail(display = "Failed to send UDP packet")]
248+
Udp,
249+
}
250+
}

0 commit comments

Comments
 (0)