Skip to content

Commit

Permalink
remove ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhou committed May 20, 2020
1 parent e50562e commit f4b427e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 59 deletions.
29 changes: 3 additions & 26 deletions src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ pub struct Device {

iface: Arc<TunSocket>,
udp4: Option<Arc<UDPSocket>>,
udp6: Option<Arc<UDPSocket>>,

yield_notice: Option<EventRef>,
exit_notice: Option<EventRef>,
Expand Down Expand Up @@ -352,7 +351,6 @@ impl Device {
peers_by_idx: Default::default(),
peers_by_ip: Default::default(),
udp4: Default::default(),
udp6: Default::default(),
cleanup_paths: Default::default(),
mtu: AtomicUsize::new(mtu),
rate_limiter: None,
Expand Down Expand Up @@ -386,11 +384,6 @@ impl Device {
Some(())
});

self.udp6.take().and_then(|s| unsafe {
self.queue.clear_event_by_fd(s.as_raw_fd());
Some(())
});

for peer in self.peers.values() {
peer.shutdown_endpoint();
}
Expand All @@ -408,17 +401,8 @@ impl Device {
port = udp_sock4.port()?;
}

let udp_sock6 = Arc::new(
UDPSocket::new6()?
.set_non_blocking()?
.set_reuse()?
.bind(port)?,
);

self.register_udp_handler(Arc::clone(&udp_sock4))?;
self.register_udp_handler(Arc::clone(&udp_sock6))?;
self.udp4 = Some(udp_sock4);
self.udp6 = Some(udp_sock6);

self.listen_port = port;

Expand Down Expand Up @@ -469,10 +453,6 @@ impl Device {
sock.set_fwmark(mark)?;
}

if let Some(ref sock) = self.udp6 {
sock.set_fwmark(mark)?;
}

// Then on all currently connected sockets
for peer in self.peers.values() {
if let Some(ref sock) = peer.endpoint().conn {
Expand Down Expand Up @@ -521,8 +501,8 @@ impl Device {
Box::new(|d, t| {
let peer_map = &d.peers;

let (udp4, udp6) = match (d.udp4.as_ref(), d.udp6.as_ref()) {
(Some(udp4), Some(udp6)) => (udp4, udp6),
let udp4 = match d.udp4.as_ref() {
Some(udp4) => udp4,
_ => return Action::Continue,
};

Expand All @@ -542,7 +522,7 @@ impl Device {
TunnResult::WriteToNetwork(packet) => {
match endpoint_addr {
SocketAddr::V4(_) => udp4.sendto(packet, endpoint_addr),
SocketAddr::V6(_) => udp6.sendto(packet, endpoint_addr),
SocketAddr::V6(_) => 0,
};
}
_ => panic!("Unexpected result from update_timers"),
Expand Down Expand Up @@ -738,7 +718,6 @@ impl Device {
let mtu = d.mtu.load(Ordering::Relaxed);

let udp4 = d.udp4.as_ref().expect("Not connected");
let udp6 = d.udp6.as_ref().expect("Not connected");

let peers = &d.peers_by_ip;
for _ in 0..MAX_ITR {
Expand Down Expand Up @@ -778,8 +757,6 @@ impl Device {
conn.write(packet);
} else if let Some(addr @ SocketAddr::V4(_)) = endpoint.addr {
udp4.sendto(packet, addr);
} else if let Some(addr @ SocketAddr::V6(_)) = endpoint.addr {
udp6.sendto(packet, addr);
} else {
eprintln!("No endpoint for peer");
}
Expand Down
6 changes: 1 addition & 5 deletions src/device/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ impl Peer {
.set_reuse()?
.bind(port)?
.connect(&addr)?,
Some(addr @ SocketAddr::V6(_)) => UDPSocket::new6()?
.set_non_blocking()?
.set_reuse()?
.bind(port)?
.connect(&addr)?,
Some(addr @ SocketAddr::V6(_)) => panic!("Attempt to connect to V6 endpoint"),
None => panic!("Attempt to connect to undefined endpoint"),
});

Expand Down
28 changes: 0 additions & 28 deletions src/device/udp_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ impl UDPSocket {
}
}

/// Create a new IPv6 UDP socket
pub fn new6() -> Result<UDPSocket, Error> {
match unsafe { socket(AF_INET6, SOCK_DGRAM, 0) } {
-1 => Err(Error::Socket(errno_str())),
fd => Ok(UDPSocket { fd, version: 6 }),
}
}

/// Set socket mode to non blocking
pub fn set_non_blocking(self) -> Result<UDPSocket, Error> {
match unsafe { fcntl(self.fd, F_GETFL) } {
Expand Down Expand Up @@ -113,10 +105,6 @@ impl UDPSocket {

/// Bind the socket to a local port
pub fn bind(self, port: u16) -> Result<UDPSocket, Error> {
if self.version == 6 {
return self.bind6(port);
}

self.bind4(port)
}

Expand All @@ -142,22 +130,6 @@ impl UDPSocket {
}
}

fn bind6(self, port: u16) -> Result<UDPSocket, Error> {
let mut addr: libc::sockaddr_in6 = unsafe { std::mem::zeroed() };
addr.sin6_family = AF_INET6 as _;
addr.sin6_port = port.to_be();

match unsafe {
bind(
self.fd,
&addr as *const sockaddr_in6 as *const sockaddr,
std::mem::size_of::<sockaddr_in6>() as u32,
)
} {
-1 => Err(Error::Bind(errno_str())),
_ => Ok(self),
}
}

/// Connect a socket to a remote address, must call bind prior to connect
/// # Panics
Expand Down

0 comments on commit f4b427e

Please sign in to comment.