Skip to content

Conversation

@NamsooCho
Copy link
Member

@NamsooCho NamsooCho commented Jan 25, 2018

Refactor codec.rs for UDP packet.

@coveralls
Copy link

coveralls commented Jan 25, 2018

Pull Request Test Coverage Report for Build 98

  • 134 of 217 (61.75%) changed or added relevant lines in 4 files are covered.
  • 4 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-1.06%) to 96.104%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/toxcore/dht_new/packet_kind.rs 1 7 14.29%
src/toxcore/dht_new/codec.rs 25 49 51.02%
src/toxcore/dht_new/packet.rs 73 126 57.94%
Files with Coverage Reduction New Missed Lines %
src/toxcore/dht_new/kbucket.rs 1 98.54%
src/toxcore/dht_new/packed_node.rs 1 66.04%
src/toxcore/dht_new/packet.rs 1 84.99%
src/toxcore/dht.rs 1 98.73%
Totals Coverage Status
Change from base Build 96: -1.06%
Covered Lines: 6142
Relevant Lines: 6391

💛 - Coveralls

Ok(None)
}
},
_ => Ok(None)
Copy link
Member

@kpp kpp Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add log! or smt like this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

*/
// Because UDP codec and tokio use DhtBase for send/receive packet,
// this function returns DhtBase type object
pub fn ping_response(&self,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should be located inside DHT Node but not inside DhtPacket

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
I moved this function to DhtPacket.rs

}
}

impl GetNodes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was reading DhtRequest and then appeared GetNodes. It is unexpected

Copy link
Member Author

@NamsooCho NamsooCho Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that the appearing order is not good?
Or impl GetNodes{...} should be other file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you may either move it to change the order or move it to another file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, dht_impl.rs has collection of impl of Types.
This concept should be discussed more.

}

/// Serialize Public, Secret Keys to u8 array
unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
Copy link
Member

@kpp kpp Jan 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the solution to keep PrecomputedKey in the cache as ... serialized slice. But keeping PrecomputedKey in the cache is OK!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
Now, the hasher for SecrectKey, PublicKey, PrecomputedKey is not provided by sodium-oxide.
I am trying to implement custom Hasher.

Ok(d) => d,
Err(_) => {
debug!("Decrypting DhtPacket failed!");
return Ok(None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that Ok(None) is a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
If the result of this function is Err, it should contain Err info.
I will change this.

pub type DhtRecvUdpPacket = (SocketAddr, Option<DhtBase>);

/**
SendNodes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be explained better why we use SendNodes here to estimate maximum size of DhtPacket.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

let shared_secret = &encrypt_precompute(key_pair.1, key_pair.0);
let shared_arr = unsafe { PrecomputedKeys::any_as_u8_slice(&shared_secret) };
self.cache.insert (key.clone(), shared_arr.to_vec());
return Some(shared_secret.clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use return here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Some(k) => {
return PrecomputedKey::from_slice(k);
},
None => { ; },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just { } should be enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!


/// Get precomputed keys
/// If the Key is not found in cache, create symmetric key and insert it into cache for later use.
pub fn get_symmetric_key (&mut self, key_pair: (&SecretKey, &PublicKey)) -> Option<PrecomputedKey> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change return type from Option to Result to handle error better.

*/
impl FromBytes for PacketKind {
named!(from_bytes<PacketKind>, switch!(le_u8,
0 => value!(PacketKind::PingReq) |
Copy link
Member

@kpp kpp Jan 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add indent

Cargo.toml Outdated
tokio-io = "0.1"
nom = "3.2"
cookie-factory = "0.2.2"
bincode = "0.9.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need bincode?

Ok((*src, None))
},
IResult::Error(_) => {
Ok((*src, None))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Ok?

fn encode(&mut self, (addr, dp): Self::Out, into: &mut Vec<u8>) -> SocketAddr {
let mut buf = [0; MAX_DHT_PACKET_SIZE];
if let Ok((_, size)) = dp.to_bytes((&mut buf, 0)) {
into.extend(&buf[..size]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indent

into.extend(&buf[..size]);
into.extend(&buf[..size]);
// TODO: move from tokio-core to tokio and return error instead of panic
} else {panic!("DhtBase to_bytes error {:?}", dp);}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway you have to fix indent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fixed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO is about panic!, So It's indent is adjusted to else {

DhtBase::DhtPacket(DhtPacket::arbitrary(g)),
1 =>
DhtBase::DhtRequest(DhtRequest::arbitrary(g)),
_ => panic!("Arbitrary for DhtBase - should not have happened!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is unreachable! macro for such cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if choice == 0 {} else {} will work too.

DhtPacket::new(&precomputed, &pk, DhtPacketPayload::GetNodes(GetNodes::arbitrary(g))),
3 =>
DhtPacket::new(&precomputed, &pk, DhtPacketPayload::SendNodes(SendNodes::arbitrary(g))),
_ => panic!("Arbitrary for DhtPacket - should not have happened!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unreachable!

@NamsooCho NamsooCho changed the title refactor(dht): codec & dht_node refactor codec.rs Feb 2, 2018
@kpp kpp requested review from kpp and kurnevsky February 3, 2018 21:19
use std::io::{Error, ErrorKind};
use tokio_core::net::UdpCodec;
use std::net::SocketAddr;
//use std::io::{Error, ErrorKind};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already imported, so remove.

let ping_kind = match stringify!($np) {
"NatPingRequest" => NAT_PING_REQUEST as u8,
"NatPingResponse" => NAT_PING_RESPONSE as u8,
e => panic!("can not occur {:?}", e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unreachable!

impl ToBytes for DhtRequest {
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
do_gen!(buf,
gen_be_u8!(0x20) >>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use PacketKind::DhtRequest as u8?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let it be 0x20

@kpp kpp merged commit 9388a7e into tox-rs:master Feb 7, 2018
@NamsooCho NamsooCho deleted the codec branch February 7, 2018 01:12
@NamsooCho NamsooCho restored the codec branch February 7, 2018 01:13
@NamsooCho NamsooCho deleted the codec branch February 7, 2018 01:14
kurnevsky pushed a commit that referenced this pull request Sep 4, 2022
DHT server initial implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants