Skip to content

Commit 08f68d9

Browse files
committed
refactor(net_crypto): rename StatusPacketEnum
1 parent 8763b39 commit 08f68d9

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

src/toxcore/net_crypto/crypto_connection.rs

Lines changed: 21 additions & 21 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-
pub enum StatusPacketEnum {
68+
pub enum StatusPacket {
6969
/// `CookieRequest` packet
7070
CookieRequest(CookieRequest),
7171
/// `CryptoHandshake` packet
@@ -75,30 +75,30 @@ pub 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-
pub 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
}
@@ -148,15 +148,15 @@ pub enum ConnectionStatus {
148148
/// ID used in the cookie request packets for this connection
149149
cookie_request_id: u64,
150150
/// Packet that should be sent every second
151-
packet: StatusPacket,
151+
packet: StatusPacketWithTime,
152152
},
153153
/// We are sending handshake packets and haven't received handshake from the
154154
/// other side yet.
155155
HandshakeSending {
156156
/// Nonce that should be used to encrypt outgoing packets
157157
sent_nonce: Nonce,
158158
/// Packet that should be sent every second
159-
packet: StatusPacket,
159+
packet: StatusPacketWithTime,
160160
},
161161
/// A handshake packet has been received from the other side but no
162162
/// encrypted packets. Continue sending handshake packets because we can't
@@ -170,7 +170,7 @@ pub enum ConnectionStatus {
170170
/// decrypt data packets
171171
session_precomputed_key: PrecomputedKey,
172172
/// Packet that should be sent every second
173-
packet: StatusPacket,
173+
packet: StatusPacketWithTime,
174174
},
175175
/// A valid encrypted packet has been received from the other side.
176176
/// Connection is fully established.
@@ -331,7 +331,7 @@ impl CryptoConnection {
331331
let cookie_request = CookieRequest::new(dht_precomputed_key, &dht_pk, &cookie_request_payload);
332332
let status = ConnectionStatus::CookieRequesting {
333333
cookie_request_id,
334-
packet: StatusPacket::new_cookie_request(cookie_request)
334+
packet: StatusPacketWithTime::new_cookie_request(cookie_request)
335335
};
336336

337337
CryptoConnection {
@@ -390,7 +390,7 @@ impl CryptoConnection {
390390
sent_nonce,
391391
received_nonce,
392392
session_precomputed_key: precompute(&peer_session_pk, &session_sk),
393-
packet: StatusPacket::new_crypto_handshake(handshake)
393+
packet: StatusPacketWithTime::new_crypto_handshake(handshake)
394394
};
395395

396396
CryptoConnection {
@@ -423,7 +423,7 @@ impl CryptoConnection {
423423

424424
/// Get `CookieRequest` or `CryptoHandshake` if it should be sent depending
425425
/// on connection status and update sent counter
426-
pub fn packet_to_send(&mut self) -> Option<StatusPacketEnum> {
426+
pub fn packet_to_send(&mut self) -> Option<StatusPacket> {
427427
match self.status {
428428
ConnectionStatus::CookieRequesting { ref mut packet, .. }
429429
| ConnectionStatus::HandshakeSending { ref mut packet, .. }
@@ -666,7 +666,7 @@ mod tests {
666666
fn status_packet_should_be_sent() {
667667
crypto_init().unwrap();
668668
// just created packet should be sent
669-
let mut packet = StatusPacket::new_cookie_request(CookieRequest {
669+
let mut packet = StatusPacketWithTime::new_cookie_request(CookieRequest {
670670
pk: gen_keypair().0,
671671
nonce: gen_nonce(),
672672
payload: vec![42; 88]
@@ -696,7 +696,7 @@ mod tests {
696696
fn status_packet_is_timed_out() {
697697
crypto_init().unwrap();
698698
// just created packet isn't timed out
699-
let mut packet = StatusPacket::new_cookie_request(CookieRequest {
699+
let mut packet = StatusPacketWithTime::new_cookie_request(CookieRequest {
700700
pk: gen_keypair().0,
701701
nonce: gen_nonce(),
702702
payload: vec![42; 88]
@@ -757,7 +757,7 @@ mod tests {
757757

758758
connection.status = ConnectionStatus::HandshakeSending {
759759
sent_nonce: gen_nonce(),
760-
packet: StatusPacket::new_crypto_handshake(crypto_handshake.clone())
760+
packet: StatusPacketWithTime::new_crypto_handshake(crypto_handshake.clone())
761761
};
762762

763763
let connection_c = connection.clone();
@@ -767,7 +767,7 @@ mod tests {
767767
sent_nonce: gen_nonce(),
768768
received_nonce: gen_nonce(),
769769
session_precomputed_key: precompute(&gen_keypair().0, &gen_keypair().1),
770-
packet: StatusPacket::new_crypto_handshake(crypto_handshake),
770+
packet: StatusPacketWithTime::new_crypto_handshake(crypto_handshake),
771771
};
772772

773773
let connection_c = connection.clone();
@@ -888,7 +888,7 @@ mod tests {
888888
sent_nonce: gen_nonce(),
889889
received_nonce: gen_nonce(),
890890
session_precomputed_key: precompute(&gen_keypair().0, &gen_keypair().1),
891-
packet: StatusPacket::new_crypto_handshake(crypto_handshake),
891+
packet: StatusPacketWithTime::new_crypto_handshake(crypto_handshake),
892892
};
893893

894894
assert!(connection.is_not_confirmed());

src/toxcore/net_crypto/mod.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ enum Packet {
111111
CryptoData(CryptoData),
112112
}
113113

114-
impl From<StatusPacketEnum> for Packet {
115-
fn from(packet: StatusPacketEnum) -> Self {
114+
impl From<StatusPacket> for Packet {
115+
fn from(packet: StatusPacket) -> Self {
116116
match packet {
117-
StatusPacketEnum::CookieRequest(packet) => Packet::CookieRequest(packet),
118-
StatusPacketEnum::CryptoHandshake(packet) => Packet::CryptoHandshake(packet),
117+
StatusPacket::CookieRequest(packet) => Packet::CookieRequest(packet),
118+
StatusPacket::CryptoHandshake(packet) => Packet::CryptoHandshake(packet),
119119
}
120120
}
121121
}
@@ -432,7 +432,7 @@ impl NetCrypto {
432432

433433
connection.status = ConnectionStatus::HandshakeSending {
434434
sent_nonce,
435-
packet: StatusPacket::new_crypto_handshake(handshake)
435+
packet: StatusPacketWithTime::new_crypto_handshake(handshake)
436436
};
437437

438438
Either::B(self.send_status_packet(connection)
@@ -522,7 +522,7 @@ impl NetCrypto {
522522
sent_nonce,
523523
received_nonce: payload.base_nonce,
524524
session_precomputed_key: precompute(&payload.session_pk, &connection.session_sk),
525-
packet: StatusPacket::new_crypto_handshake(handshake)
525+
packet: StatusPacketWithTime::new_crypto_handshake(handshake)
526526
}
527527
},
528528
ConnectionStatus::HandshakeSending { sent_nonce, ref packet, .. }
@@ -1343,7 +1343,7 @@ mod tests {
13431343
net_crypto.handle_cookie_response(&mut connection, &cookie_response).wait().unwrap();
13441344

13451345
let packet = unpack!(connection.status, ConnectionStatus::HandshakeSending, packet);
1346-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
1346+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
13471347
assert_eq!(packet.cookie, cookie);
13481348

13491349
let payload = packet.get_payload(&precompute(&real_pk, &peer_real_sk)).unwrap();
@@ -1490,7 +1490,7 @@ mod tests {
14901490
let connection = connections.get(&peer_real_pk).unwrap().read().clone();
14911491

14921492
let packet = unpack!(connection.status, ConnectionStatus::HandshakeSending, packet);
1493-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
1493+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
14941494
assert_eq!(packet.cookie, cookie);
14951495

14961496
let payload = packet.get_payload(&precompute(&real_pk, &peer_real_sk)).unwrap();
@@ -1585,7 +1585,7 @@ mod tests {
15851585
assert_eq!(received_nonce, base_nonce);
15861586

15871587
let packet = unpack!(connection.status, ConnectionStatus::NotConfirmed, packet);
1588-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
1588+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
15891589
assert_eq!(packet.cookie, cookie);
15901590

15911591
let payload = packet.get_payload(&real_precomputed_key).unwrap();
@@ -1653,7 +1653,7 @@ mod tests {
16531653

16541654
// cookie should not be updated
16551655
let packet = unpack!(connection.status, ConnectionStatus::NotConfirmed, packet);
1656-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
1656+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
16571657
assert_eq!(packet.cookie, cookie);
16581658

16591659
let payload = packet.get_payload(&real_precomputed_key).unwrap();
@@ -1973,7 +1973,7 @@ mod tests {
19731973
assert_eq!(received_nonce, base_nonce);
19741974

19751975
let packet = unpack!(connection.status, ConnectionStatus::NotConfirmed, packet);
1976-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
1976+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
19771977
assert_eq!(packet.cookie, cookie);
19781978

19791979
let payload = packet.get_payload(&real_precomputed_key).unwrap();
@@ -2035,7 +2035,7 @@ mod tests {
20352035
assert_eq!(received_nonce, base_nonce);
20362036

20372037
let packet = unpack!(connection.status, ConnectionStatus::NotConfirmed, packet);
2038-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
2038+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
20392039
assert_eq!(packet.cookie, cookie);
20402040

20412041
let payload = packet.get_payload(&real_precomputed_key).unwrap();
@@ -2119,7 +2119,7 @@ mod tests {
21192119
assert_eq!(received_nonce, base_nonce);
21202120

21212121
let packet = unpack!(connection.status, ConnectionStatus::NotConfirmed, packet);
2122-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
2122+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
21232123
assert_eq!(packet.cookie, cookie);
21242124

21252125
let payload = packet.get_payload(&real_precomputed_key).unwrap();
@@ -2206,7 +2206,7 @@ mod tests {
22062206
assert_eq!(received_nonce, base_nonce);
22072207

22082208
let packet = unpack!(connection.status, ConnectionStatus::NotConfirmed, packet);
2209-
let packet = unpack!(packet.packet, StatusPacketEnum::CryptoHandshake);
2209+
let packet = unpack!(packet.packet, StatusPacket::CryptoHandshake);
22102210
assert_eq!(packet.cookie, cookie);
22112211

22122212
let payload = packet.get_payload(&real_precomputed_key).unwrap();
@@ -3183,7 +3183,7 @@ mod tests {
31833183

31843184
assert_eq!(
31853185
unpack!(received, DhtPacket::CookieRequest),
3186-
unpack!(packet.packet.clone(), StatusPacketEnum::CookieRequest)
3186+
unpack!(packet.packet.clone(), StatusPacket::CookieRequest)
31873187
);
31883188
assert_eq!(addr_to_send, addr);
31893189

@@ -3417,7 +3417,7 @@ mod tests {
34173417
assert_eq!(addr_to_send, addr);
34183418
assert_eq!(
34193419
unpack!(received, DhtPacket::CookieRequest),
3420-
unpack!(packet.packet, StatusPacketEnum::CookieRequest)
3420+
unpack!(packet.packet, StatusPacket::CookieRequest)
34213421
);
34223422
}
34233423

@@ -4056,7 +4056,7 @@ mod tests {
40564056
assert_eq!(connection.peer_dht_pk, peer_dht_pk);
40574057

40584058
let status_packet = unpack!(connection.status.clone(), ConnectionStatus::CookieRequesting, packet);
4059-
let cookie_request = unpack!(status_packet.packet, StatusPacketEnum::CookieRequest);
4059+
let cookie_request = unpack!(status_packet.packet, StatusPacket::CookieRequest);
40604060
let cookie_request_payload = cookie_request.get_payload(&precompute(&dht_pk, &peer_dht_sk)).unwrap();
40614061

40624062
assert_eq!(cookie_request_payload.pk, real_pk);

0 commit comments

Comments
 (0)