Skip to content

Commit 75ddfdf

Browse files
author
Roman Proskuryakov
authored
Merge pull request #382 from tox-rs/tcp_data
Use DataPayload type instead of vec
2 parents 01efefb + 053f77d commit 75ddfdf

File tree

8 files changed

+165
-34
lines changed

8 files changed

+165
-34
lines changed

examples/tcp_client.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
extern crate log;
33

44
use tox::toxcore::crypto_core::*;
5+
use tox::toxcore::dht::packet::CryptoData;
56
use tox::toxcore::tcp::connection_id::ConnectionId;
67
use tox::toxcore::tcp::packet::*;
78
use tox::toxcore::tcp::handshake::make_client_handshake;
@@ -196,9 +197,15 @@ fn main() {
196197
185, 111, 33, 146, 221, 31, 77, 118]);
197198

198199
let request = if i == 1 {
199-
tx.send(Packet::RouteRequest(RouteRequest {pk: friend_pk } ))
200+
tx.send(Packet::RouteRequest(RouteRequest { pk: friend_pk } ))
200201
} else {
201-
tx.send(Packet::Data(Data { connection_id: ConnectionId::from_index(0), data: vec![42; 42] } ))
202+
tx.send(Packet::Data(Data {
203+
connection_id: ConnectionId::from_index(0),
204+
data: DataPayload::CryptoData(CryptoData {
205+
nonce_last_bytes: 42,
206+
payload: vec![42; 123],
207+
}),
208+
}))
202209
};
203210

204211
request

src/toxcore/tcp/client/client.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const CLIENT_CHANNEL_SIZE: usize = 2;
3535
#[derive(Debug, PartialEq, Clone)]
3636
pub enum IncomingPacket {
3737
/// Data packet with sender's `PublicKey`.
38-
Data(PublicKey, Vec<u8>),
38+
Data(PublicKey, DataPayload),
3939
/// Oob packet with sender's `PublicKey`.
4040
Oob(PublicKey, Vec<u8>),
4141
/// Onion response packet.
@@ -370,7 +370,7 @@ impl Client {
370370
}
371371

372372
/// Send `Data` packet to a node via relay.
373-
pub fn send_data(&self, destination_pk: PublicKey, data: Vec<u8>) -> impl Future<Item = (), Error = Error> + Send {
373+
pub fn send_data(&self, destination_pk: PublicKey, data: DataPayload) -> impl Future<Item = (), Error = Error> + Send {
374374
// it is important that the result future succeeds only if packet is
375375
// sent since we take only one successful future from several relays
376376
// when send data packet
@@ -529,6 +529,7 @@ pub mod tests {
529529
use tokio::net::TcpListener;
530530
use tokio::timer::Interval;
531531

532+
use crate::toxcore::dht::packet::CryptoData;
532533
use crate::toxcore::ip_port::*;
533534
use crate::toxcore::onion::packet::*;
534535
use crate::toxcore::tcp::server::{Server, ServerExt};
@@ -797,7 +798,10 @@ pub mod tests {
797798
client.links.write().insert_by_id(&sender_pk, index);
798799
client.links.write().upgrade(index);
799800

800-
let data = vec![42; 123];
801+
let data = DataPayload::CryptoData(CryptoData {
802+
nonce_last_bytes: 42,
803+
payload: vec![42; 123],
804+
});
801805
let data_packet = Packet::Data(Data {
802806
connection_id: ConnectionId::from_index(index),
803807
data: data.clone(),
@@ -819,7 +823,10 @@ pub mod tests {
819823

820824
let data_packet = Packet::Data(Data {
821825
connection_id: ConnectionId::from_index(42),
822-
data: vec![42; 123],
826+
data: DataPayload::CryptoData(CryptoData {
827+
nonce_last_bytes: 42,
828+
payload: vec![42; 123],
829+
}),
823830
});
824831

825832
assert!(client.handle_packet(data_packet).wait().is_err());
@@ -836,7 +843,10 @@ pub mod tests {
836843

837844
let data_packet = Packet::Data(Data {
838845
connection_id: ConnectionId::zero(),
839-
data: vec![42; 123],
846+
data: DataPayload::CryptoData(CryptoData {
847+
nonce_last_bytes: 42,
848+
payload: vec![42; 123],
849+
}),
840850
});
841851

842852
assert!(client.handle_packet(data_packet).wait().is_err());
@@ -892,7 +902,10 @@ pub mod tests {
892902
let (_incoming_rx, outgoing_rx, client) = create_client();
893903

894904
let (destination_pk, _destination_sk) = gen_keypair();
895-
let data = vec![42; 123];
905+
let data = DataPayload::CryptoData(CryptoData {
906+
nonce_last_bytes: 42,
907+
payload: vec![42; 123],
908+
});
896909

897910
let index = 42;
898911
client.links.write().insert_by_id(&destination_pk, index);
@@ -911,7 +924,10 @@ pub mod tests {
911924
let (_incoming_rx, outgoing_rx, client) = create_client();
912925

913926
let (destination_pk, _destination_sk) = gen_keypair();
914-
let data = vec![42; 123];
927+
let data = DataPayload::CryptoData(CryptoData {
928+
nonce_last_bytes: 42,
929+
payload: vec![42; 123],
930+
});
915931

916932
assert!(client.send_data(destination_pk, data.clone()).wait().is_err());
917933

@@ -926,7 +942,10 @@ pub mod tests {
926942
let (_incoming_rx, outgoing_rx, client) = create_client();
927943

928944
let (destination_pk, _destination_sk) = gen_keypair();
929-
let data = vec![42; 123];
945+
let data = DataPayload::CryptoData(CryptoData {
946+
nonce_last_bytes: 42,
947+
payload: vec![42; 123],
948+
});
930949

931950
let connection_id = 42;
932951
client.links.write().insert_by_id(&destination_pk, connection_id - 16);
@@ -1185,8 +1204,14 @@ pub mod tests {
11851204
client_1_c.add_connection(client_pk_2)
11861205
});
11871206

1188-
let data_1 = vec![42; 123];
1189-
let data_2 = vec![43; 123];
1207+
let data_1 = DataPayload::CryptoData(CryptoData {
1208+
nonce_last_bytes: 42,
1209+
payload: vec![42; 123],
1210+
});
1211+
let data_2 = DataPayload::CryptoData(CryptoData {
1212+
nonce_last_bytes: 42,
1213+
payload: vec![43; 123],
1214+
});
11901215

11911216
// wait until links become online
11921217
let client_1_c = client_1.clone();

src/toxcore/tcp/client/connections.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl Connections {
207207
}
208208

209209
/// Send `Data` packet to a node via one of the relays.
210-
pub fn send_data(&self, node_pk: PublicKey, data: Vec<u8>) -> impl Future<Item = (), Error = Error> + Send {
210+
pub fn send_data(&self, node_pk: PublicKey, data: DataPayload) -> impl Future<Item = (), Error = Error> + Send {
211211
let connections = self.connections.read();
212212
if let Some(connection) = connections.get(&node_pk) {
213213
let clients = self.clients.read();
@@ -386,6 +386,7 @@ mod tests {
386386
use tokio_executor;
387387
use tokio_timer::clock::*;
388388

389+
use crate::toxcore::dht::packet::CryptoData;
389390
use crate::toxcore::ip_port::*;
390391
use crate::toxcore::tcp::client::client::tests::*;
391392
use crate::toxcore::tcp::connection_id::ConnectionId;
@@ -625,7 +626,10 @@ mod tests {
625626
connections.clients.write().insert(relay_1.pk, relay_1);
626627
connections.clients.write().insert(relay_2.pk, relay_2);
627628

628-
let data = vec![42; 123];
629+
let data = DataPayload::CryptoData(CryptoData {
630+
nonce_last_bytes: 42,
631+
payload: vec![42; 123],
632+
});
629633

630634
connections.send_data(destination_pk, data.clone()).wait().unwrap();
631635

@@ -649,7 +653,11 @@ mod tests {
649653

650654
let (destination_pk, _destination_sk) = gen_keypair();
651655

652-
connections.send_data(destination_pk, vec![42; 123]).wait().unwrap();
656+
let data = DataPayload::CryptoData(CryptoData {
657+
nonce_last_bytes: 42,
658+
payload: vec![42; 123],
659+
});
660+
connections.send_data(destination_pk, data).wait().unwrap();
653661
}
654662

655663
#[test]

src/toxcore/tcp/codec.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl Encoder for Codec {
176176
#[cfg(test)]
177177
mod tests {
178178
use crate::toxcore::crypto_core::*;
179+
use crate::toxcore::dht::packet::CryptoData;
179180
use crate::toxcore::onion::packet::*;
180181
use crate::toxcore::ip_port::*;
181182
use crate::toxcore::tcp::codec::*;
@@ -304,7 +305,13 @@ mod tests {
304305
payload: vec![42; 123]
305306
})
306307
} ),
307-
Packet::Data( Data { connection_id: ConnectionId::from_index(42), data: vec![13; 2031] } )
308+
Packet::Data( Data {
309+
connection_id: ConnectionId::from_index(42),
310+
data: DataPayload::CryptoData(CryptoData {
311+
nonce_last_bytes: 42,
312+
payload: vec![42; 123],
313+
}),
314+
} )
308315
];
309316
for packet in test_packets {
310317
alice_codec.encode(packet.clone(), &mut buf).expect("Alice should encode");
@@ -408,7 +415,13 @@ mod tests {
408415
let mut buf = BytesMut::new();
409416
let stats = Stats::new();
410417
let mut alice_codec = Codec::new(alice_channel, stats);
411-
let packet = Packet::Data( Data { connection_id: ConnectionId::from_index(42), data: vec![13; 2032] } );
418+
let packet = Packet::Data( Data {
419+
connection_id: ConnectionId::from_index(42),
420+
data: DataPayload::CryptoData(CryptoData {
421+
nonce_last_bytes: 42,
422+
payload: vec![42; 2030],
423+
})
424+
} );
412425

413426
// Alice cannot serialize Packet because it is too long
414427
assert!(alice_codec.encode(packet, &mut buf).is_err());

src/toxcore/tcp/packet/data.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
*/
33

44
use crate::toxcore::binary_io::*;
5+
use crate::toxcore::dht::packet::{CookieRequest, CookieResponse, CryptoHandshake, CryptoData};
56
use crate::toxcore::tcp::connection_id::ConnectionId;
67

7-
use nom::rest;
8-
98
/** Sent by client to server.
109
The client sends data with `connection_id` and the server
1110
relays it to the given connection
@@ -22,27 +21,60 @@ variable | Data
2221
pub struct Data {
2322
/// The id of the connection of the client
2423
pub connection_id: ConnectionId,
25-
/// Data packet
26-
pub data: Vec<u8>
24+
/// Data payload
25+
pub data: DataPayload,
2726
}
2827

2928
impl FromBytes for Data {
3029
named!(from_bytes<Data>, do_parse!(
3130
connection_id: call!(ConnectionId::from_bytes) >>
32-
data: rest >>
33-
(Data { connection_id, data: data.to_vec() })
31+
data: call!(DataPayload::from_bytes) >>
32+
(Data { connection_id, data })
3433
));
3534
}
3635

3736
impl ToBytes for Data {
3837
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
3938
do_gen!(buf,
4039
gen_call!(|buf, connection_id| ConnectionId::to_bytes(connection_id, buf), &self.connection_id) >>
41-
gen_slice!(self.data)
40+
gen_call!(|buf, packet| DataPayload::to_bytes(packet, buf), &self.data)
4241
)
4342
}
4443
}
4544

45+
/// Data payload enum.
46+
#[derive(Debug, PartialEq, Clone)]
47+
pub enum DataPayload {
48+
/// [`CookieRequest`](../../dht/packet/struct.CookieRequest.html) structure.
49+
CookieRequest(CookieRequest),
50+
/// [`CookieResponse`](../../dht/packet/struct.CookieResponse.html) structure.
51+
CookieResponse(CookieResponse),
52+
/// [`CryptoHandshake`](../../dht/packet/struct.CryptoHandshake.html) structure.
53+
CryptoHandshake(CryptoHandshake),
54+
/// [`CryptoData`](../../dht/packet/struct.CryptoData.html) structure.
55+
CryptoData(CryptoData),
56+
}
57+
58+
impl ToBytes for DataPayload {
59+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
60+
match *self {
61+
DataPayload::CookieRequest(ref p) => p.to_bytes(buf),
62+
DataPayload::CookieResponse(ref p) => p.to_bytes(buf),
63+
DataPayload::CryptoHandshake(ref p) => p.to_bytes(buf),
64+
DataPayload::CryptoData(ref p) => p.to_bytes(buf),
65+
}
66+
}
67+
}
68+
69+
impl FromBytes for DataPayload {
70+
named!(from_bytes<DataPayload>, alt!(
71+
map!(CookieRequest::from_bytes, DataPayload::CookieRequest) |
72+
map!(CookieResponse::from_bytes, DataPayload::CookieResponse) |
73+
map!(CryptoHandshake::from_bytes, DataPayload::CryptoHandshake) |
74+
map!(CryptoData::from_bytes, DataPayload::CryptoData)
75+
));
76+
}
77+
4678
#[cfg(test)]
4779
mod test {
4880
use super::*;
@@ -51,7 +83,10 @@ mod test {
5183
data_encode_decode,
5284
Data {
5385
connection_id: ConnectionId::from_index(1),
54-
data: vec![42; 123]
86+
data: DataPayload::CryptoData(CryptoData {
87+
nonce_last_bytes: 42,
88+
payload: vec![42; 123],
89+
}),
5590
}
5691
);
5792
}

src/toxcore/tcp/packet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub use self::oob_send::OobSend;
2424
pub use self::oob_receive::OobReceive;
2525
pub use self::onion_request::OnionRequest;
2626
pub use self::onion_response::OnionResponse;
27-
pub use self::data::Data;
27+
pub use self::data::{Data, DataPayload};
2828

2929
use crate::toxcore::binary_io::*;
3030

src/toxcore/tcp/server/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Client {
184184
}
185185
/** Construct Data and send it to Client
186186
*/
187-
pub fn send_data(&self, connection_id: ConnectionId, data: Vec<u8>) -> impl Future<Item = (), Error = Error> + Send {
187+
pub fn send_data(&self, connection_id: ConnectionId, data: DataPayload) -> impl Future<Item = (), Error = Error> + Send {
188188
self.send(
189189
Packet::Data(Data { connection_id, data })
190190
)

0 commit comments

Comments
 (0)