Skip to content

Conversation

@JaneCX
Copy link

@JaneCX JaneCX commented Aug 14, 2017

nat

@codecov
Copy link

codecov bot commented Aug 14, 2017

Codecov Report

Merging #15 into master will decrease coverage by 8.83%.
The diff coverage is 10.22%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #15      +/-   ##
=========================================
- Coverage   66.73%   57.9%   -8.84%     
=========================================
  Files           5       5              
  Lines         475     563      +88     
=========================================
+ Hits          317     326       +9     
- Misses        158     237      +79
Impacted Files Coverage Δ
src/network.rs 50.92% <10.22%> (-15.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e16ee70...4fcecdd. Read the comment docs.

@codecov-io
Copy link

codecov-io commented Aug 14, 2017

Codecov Report

Merging #15 into master will decrease coverage by 7.24%.
The diff coverage is 12.32%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #15      +/-   ##
==========================================
- Coverage   66.73%   59.48%   -7.25%     
==========================================
  Files           5        6       +1     
  Lines         475      548      +73     
==========================================
+ Hits          317      326       +9     
- Misses        158      222      +64
Impacted Files Coverage Δ
src/main.rs 2.63% <ø> (ø) ⬆️
src/network.rs 65.98% <66.66%> (+0.01%) ⬆️
src/nat.rs 7.46% <7.46%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e16ee70...e2679f1. Read the comment docs.

@changlan changlan changed the title nat NAT implementation Aug 14, 2017
Copy link
Owner

@changlan changlan left a comment

Choose a reason for hiding this comment

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

formatting comments

src/network.rs Outdated
use std::mem;
use packet::{Ipv4Header,UdpHeader,TcpHeader, udptcp_cksum};
use std::str;
use std::num::ParseIntError;
Copy link
Owner

Choose a reason for hiding this comment

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

unused

src/network.rs Outdated
use std::collections::HashMap;
use std::mem;
use packet::{Ipv4Header,UdpHeader,TcpHeader, udptcp_cksum};
use std::str;
Copy link
Owner

Choose a reason for hiding this comment

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

unused

src/network.rs Outdated
}.unwrap()
}

pub fn handle_backward_packet(&mut self, data:&[u8], iph:&mut Ipv4Header, ex_address:u32){
Copy link
Owner

Choose a reason for hiding this comment

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

ex_address is unused

src/network.rs Outdated
}
}

pub fn handle_forward_packet(&mut self, data:&[u8], iph:&mut Ipv4Header, ex_address:u32){
Copy link
Owner

Choose a reason for hiding this comment

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

there should be a space after :

src/network.rs Outdated
}.unwrap();

let key = (iph.protocol, iph.source_address, sc_port);
let value = self.forward_table.get(&key).cloned();
Copy link
Owner

Choose a reason for hiding this comment

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

indentation

src/network.rs Outdated
Err(String::from("no response"))
}
}.unwrap();
iph.source_address=ex_address; //source_ip -> external_ip
Copy link
Owner

Choose a reason for hiding this comment

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

space before and after =

src/network.rs Outdated
let public_ip:Ipv4Addr = public_ip.parse().unwrap();
let exadd = public_ip.octets();
let ex_address = unsafe {
mem::transmute::<[u8;4],u32>(exadd)
Copy link
Owner

Choose a reason for hiding this comment

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

space after comma

src/network.rs Outdated
sent_len +=
tun.write(&decompressed_data[sent_len..data_len])
.unwrap();
let data:&[u8] = decompressed_data.as_ref();
Copy link
Owner

Choose a reason for hiding this comment

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

space

Copy link
Owner

Choose a reason for hiding this comment

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

also indentation

src/network.rs Outdated
match client_info.get(&client_id) {
None => warn!("Unknown IP packet from TUN for client {}.", client_id),
Some(&(token, addr)) => {
let mut iph = unsafe{
Copy link
Owner

Choose a reason for hiding this comment

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

indentation

Copy link
Owner

@changlan changlan left a comment

Choose a reason for hiding this comment

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

please run cargo fmt

src/network.rs Outdated
None => warn!("Unknown IP packet from TUN for client {}.", client_id),
Some(&(token, addr)) => {
let mut iph = unsafe{
mem::transmute::<*const u8, &mut Ipv4Header>(data.as_ptr())
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
Some(&(token, addr)) => {
let mut iph = unsafe{
mem::transmute::<*const u8, &mut Ipv4Header>(data.as_ptr())
};
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
let mut iph = unsafe{
mem::transmute::<*const u8, &mut Ipv4Header>(data.as_ptr())
};
NAT::handle_backward_packet(&mut nat, data, iph, ex_address);
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
.unwrap();
.unwrap()
let data: &[u8] = decompressed_data.as_ref();
let iph = unsafe {
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
.unwrap()
let data: &[u8] = decompressed_data.as_ref();
let iph = unsafe {
mem::transmute::<*const u8, &mut Ipv4Header>(data.as_ptr())
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
let data: &[u8] = decompressed_data.as_ref();
let iph = unsafe {
mem::transmute::<*const u8, &mut Ipv4Header>(data.as_ptr())
};
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
let iph = unsafe {
mem::transmute::<*const u8, &mut Ipv4Header>(data.as_ptr())
};
NAT::handle_forward_packet(&mut nat, data, iph, ex_address);
Copy link
Owner

Choose a reason for hiding this comment

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

...indentation

src/network.rs Outdated
udphd.checksum = cksum;
Ok(udphd.destination_port = sc_port)
}
6 =>{
Copy link
Owner

Choose a reason for hiding this comment

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

format

src/network.rs Outdated
iph.destination_address = sc_address; //destination_address -> sc_address
match iph.protocol { //destination_port -> sc_port & checksum
1 => Err(String::from("The version is ICMP")),
17 => {
Copy link
Owner

Choose a reason for hiding this comment

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

format

src/network.rs Outdated

let key = (iph.protocol, iph.source_address, sc_port);
let value = self.forward_table.get(&key).cloned();
let change_port = match value { //use source_port to get change_port
Copy link
Owner

Choose a reason for hiding this comment

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

indentation

@changlan
Copy link
Owner

Travis CI build is failing. Please fix the build error. Thanks!

src/nat.rs Outdated
} else {
Err(String::from("No port to distribute"))
};
Err(String::from("no response"))
Copy link
Owner

Choose a reason for hiding this comment

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

this line is not needed

src/nat.rs Outdated
use std::collections::HashMap;
use std::mem;
use packet::{Ipv4Header,UdpHeader,TcpHeader, udptcp_cksum};
use std::net::Ipv4Addr;
Copy link
Owner

Choose a reason for hiding this comment

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

unused

src/nat.rs Outdated
use utils;
use std::collections::HashMap;
use std::mem;
use packet::{Ipv4Header,UdpHeader,TcpHeader, udptcp_cksum};
Copy link
Owner

@changlan changlan Aug 16, 2017

Choose a reason for hiding this comment

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

space after commas

src/nat.rs Outdated
let udphd = unsafe {
mem::transmute::<*const u8, &mut UdpHeader>(data.as_ptr().offset(len))
};
let cksum = udptcp_cksum(&iph, &udphd);
Copy link
Owner

Choose a reason for hiding this comment

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

You should recompute checksum AFTER changing the port number.

src/nat.rs Outdated
}.unwrap();
iph.source_address = ex_address; //source_ip -> external_ip
match iph.protocol { //source_port -> change_port & checksum
1 => Err(String::from("The version is ICMP")),
Copy link
Owner

Choose a reason for hiding this comment

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

For now just use panic!(...)

src/nat.rs Outdated
};
let cksum = udptcp_cksum(&iph, &udphd);
udphd.source_port = change_port;
Ok(udphd.checksum = cksum)
Copy link
Owner

Choose a reason for hiding this comment

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

Just use udphd.checksum = udptcp_cksum(&iph, &udphd);

src/nat.rs Outdated
};
let cksum = udptcp_cksum(&iph, &tcphd);
tcphd.source_port = change_port;
Ok(tcphd.checksum = cksum)
Copy link
Owner

Choose a reason for hiding this comment

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

same above

src/nat.rs Outdated
tcphd.source_port = change_port;
Ok(tcphd.checksum = cksum)
}
_ => Err(String::from("Invalid address!")),
Copy link
Owner

Choose a reason for hiding this comment

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

same above

src/nat.rs Outdated
Ok(tcphd.checksum = cksum)
}
_ => Err(String::from("Invalid address!")),
}.unwrap()
Copy link
Owner

Choose a reason for hiding this comment

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

no need to unwrap now that the expression returns ()

src/nat.rs Outdated
Ok(tcphd.destination_port = sc_port)
}
_ => Err(String::from("Invalid address!")),
}.unwrap()
Copy link
Owner

Choose a reason for hiding this comment

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

same above

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