-
Notifications
You must be signed in to change notification settings - Fork 66
NAT implementation #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
changlan
left a comment
There was a problem hiding this 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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space
There was a problem hiding this comment.
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{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
changlan
left a comment
There was a problem hiding this 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()) |
There was a problem hiding this comment.
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()) | ||
| }; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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()) | ||
| }; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 =>{ |
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
|
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")) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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")), |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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!")), |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same above
nat