Skip to content

Commit 981419a

Browse files
author
Roman Proskuryakov
authored
Merge pull request #356 from tox-rs/invite
Add serde of invite packet for conferences
2 parents a1d4ef1 + 908117d commit 981419a

File tree

6 files changed

+167
-2
lines changed

6 files changed

+167
-2
lines changed

src/toxcore/friend_connection/packet/friend_requests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub const MAX_ONION_CLIENT_DATA_SIZE: usize = MAX_DATA_REQUEST_SIZE - MIN_ONION_
2424
2525
This packet is used to transmit sender's long term public key, npspam and a message.
2626
It is sent by onion data packet or net-crypto.
27-
If the friend is already directly connected with me and not in group chat, it is sent using net-crypto.
27+
If the friend is already directly connected with me and not in conference, it is sent using net-crypto.
2828
Otherwise it is sent using onion.
2929
Both onion and net-crypto packet itself have real public key of sender.
3030
This is why this packet does not contain long term public key.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*! Conference Packets
2+
*/
3+
4+
pub mod packet;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*! Invite message struct.
2+
*/
3+
4+
use nom::be_u16;
5+
6+
use super::{ConferenceUID, ConferenceType};
7+
use crate::toxcore::binary_io::*;
8+
9+
/** Invite is a struct that holds info to invite a peer to a conference.
10+
11+
Serialized form:
12+
13+
Length | Content
14+
--------- | ------
15+
`1` | `0x60`
16+
`1` | `0x00`
17+
`2` | `conference number`
18+
`1` | `conference type`(0: text, 1: audio)
19+
`32` | `unique id`
20+
21+
*/
22+
#[derive(Clone, Debug, Eq, PartialEq)]
23+
pub struct Invite {
24+
conference_number: u16,
25+
conference_type: ConferenceType,
26+
unique_id: ConferenceUID,
27+
}
28+
29+
impl FromBytes for Invite {
30+
named!(from_bytes<Invite>, do_parse!(
31+
tag!("\x60") >>
32+
tag!("\x00") >>
33+
conference_number: be_u16 >>
34+
conference_type: call!(ConferenceType::from_bytes) >>
35+
unique_id: call!(ConferenceUID::from_bytes) >>
36+
(Invite {
37+
conference_number,
38+
conference_type,
39+
unique_id,
40+
})
41+
));
42+
}
43+
44+
impl ToBytes for Invite {
45+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
46+
do_gen!(buf,
47+
gen_be_u8!(0x60) >>
48+
gen_be_u8!(0x00) >>
49+
gen_be_u16!(self.conference_number) >>
50+
gen_be_u8!(self.conference_type as u8) >>
51+
gen_slice!(self.unique_id.0)
52+
)
53+
}
54+
}
55+
56+
impl Invite {
57+
/// Create new Invite object.
58+
pub fn new(conference_number: u16, conference_type: ConferenceType, unique_id: ConferenceUID) -> Self {
59+
Invite {
60+
conference_number,
61+
conference_type,
62+
unique_id,
63+
}
64+
}
65+
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
71+
encode_decode_test!(
72+
invite_encode_decode,
73+
Invite::new(1, ConferenceType::Text, ConferenceUID::random())
74+
);
75+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*! The implementation of conference packets.
2+
*/
3+
4+
mod invite;
5+
6+
pub use self::invite::*;
7+
8+
use nom::be_u8;
9+
use crate::toxcore::binary_io::*;
10+
use crate::toxcore::crypto_core::*;
11+
12+
/// Length of conference unique bytes
13+
pub const CONFERENCE_UID_BYTES: usize = 32;
14+
15+
/// Unique id used in conference
16+
#[derive(Clone, Debug, Eq, PartialEq)]
17+
pub struct ConferenceUID([u8; CONFERENCE_UID_BYTES]);
18+
19+
impl ConferenceUID {
20+
/// Create new object
21+
pub fn random() -> ConferenceUID {
22+
let mut array = [0; CONFERENCE_UID_BYTES];
23+
randombytes_into(&mut array);
24+
ConferenceUID(array)
25+
}
26+
27+
/// Custom from_slice function of ConferenceUID
28+
pub fn from_slice(bs: &[u8]) -> Option<ConferenceUID> {
29+
if bs.len() != CONFERENCE_UID_BYTES {
30+
return None
31+
}
32+
let mut n = ConferenceUID([0; CONFERENCE_UID_BYTES]);
33+
for (ni, &bsi) in n.0.iter_mut().zip(bs.iter()) {
34+
*ni = bsi
35+
}
36+
Some(n)
37+
}
38+
}
39+
40+
impl FromBytes for ConferenceUID {
41+
named!(from_bytes<ConferenceUID>, map_opt!(take!(CONFERENCE_UID_BYTES), ConferenceUID::from_slice));
42+
}
43+
44+
impl ToBytes for ConferenceUID {
45+
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
46+
do_gen!(buf,
47+
gen_slice!(self.0)
48+
)
49+
}
50+
}
51+
52+
/// Type of conference
53+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
54+
pub enum ConferenceType {
55+
/// Text conference.
56+
Text = 0x00,
57+
/// Audio conference.
58+
Audio,
59+
}
60+
61+
impl FromBytes for ConferenceType {
62+
named!(from_bytes<ConferenceType>,
63+
switch!(be_u8,
64+
0 => value!(ConferenceType::Text) |
65+
1 => value!(ConferenceType::Audio)
66+
)
67+
);
68+
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
74+
encode_decode_test!(
75+
conference_uid_encode_decode,
76+
ConferenceUID::random()
77+
);
78+
79+
#[test]
80+
fn conference_type_from_bytes() {
81+
let raw = [0];
82+
let (_, conference_type) = ConferenceType::from_bytes(&raw).unwrap();
83+
assert_eq!(ConferenceType::Text, conference_type);
84+
}
85+
}

src/toxcore/messenger/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*/
33

44
pub mod packet;
5+
pub mod conference;

src/toxcore/messenger/packet/offline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::toxcore::binary_io::*;
66
/** Offline is a struct that holds nothing.
77
88
This packet is used to notify that a friend is being deleted.
9-
Though the friend is deleted, because of group chat, Tox client
9+
Though the friend is deleted, because of conference, Tox client
1010
may try to connect to the friend, this message prevent this friend to
1111
be shown as Online.
1212

0 commit comments

Comments
 (0)