Skip to content

Commit

Permalink
Move tun constants to network defaults (#5286) (#5287)
Browse files Browse the repository at this point in the history
  • Loading branch information
neacsu authored Dec 18, 2024
1 parent acd068e commit f7a7a80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
11 changes: 11 additions & 0 deletions common/network-defaults/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ pub mod wireguard {
pub const WG_TUN_DEVICE_IP_ADDRESS_V6: Ipv6Addr = Ipv6Addr::new(0xfc01, 0, 0, 0, 0, 0, 0, 0x1); // fc01::1
pub const WG_TUN_DEVICE_NETMASK_V6: u8 = 112;
}

pub mod mixnet_vpn {
use std::net::{Ipv4Addr, Ipv6Addr};

// The interface used to route traffic
pub const NYM_TUN_BASE_NAME: &str = "nymtun";
pub const NYM_TUN_DEVICE_ADDRESS_V4: Ipv4Addr = Ipv4Addr::new(10, 0, 0, 1);
pub const NYM_TUN_DEVICE_NETMASK_V4: Ipv4Addr = Ipv4Addr::new(255, 255, 0, 0);
pub const NYM_TUN_DEVICE_ADDRESS_V6: Ipv6Addr = Ipv6Addr::new(0xfc00, 0, 0, 0, 0, 0, 0, 0x1); // fc00::1
pub const NYM_TUN_DEVICE_NETMASK_V6: &str = "112";
}
9 changes: 0 additions & 9 deletions service-providers/ip-packet-router/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
use std::net::{Ipv4Addr, Ipv6Addr};
use std::time::Duration;

// The interface used to route traffic
pub const TUN_BASE_NAME: &str = "nymtun";
pub const TUN_DEVICE_ADDRESS_V4: Ipv4Addr = Ipv4Addr::new(10, 0, 0, 1);
pub const TUN_DEVICE_NETMASK_V4: Ipv4Addr = Ipv4Addr::new(255, 255, 0, 0);
pub const TUN_DEVICE_ADDRESS_V6: Ipv6Addr = Ipv6Addr::new(0xfc00, 0, 0, 0, 0, 0, 0, 0x1); // fc00::1

pub const TUN_DEVICE_NETMASK_V6: &str = "112";

// We routinely check if any clients needs to be disconnected at this interval
pub(crate) const DISCONNECT_TIMER_INTERVAL: Duration = Duration::from_secs(10);

Expand Down
13 changes: 7 additions & 6 deletions service-providers/ip-packet-router/src/ip_packet_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl IpPacketRouter {
log::error!("ip packet router service provider is not yet supported on this platform");
Ok(())
} else {
unimplemented!("service provider is not yet supported on this platform")
todo!("service provider is not yet supported on this platform")
}
}

Expand All @@ -145,11 +145,12 @@ impl IpPacketRouter {

// Create the TUN device that we interact with the rest of the world with
let config = nym_tun::tun_device::TunDeviceConfig {
base_name: crate::constants::TUN_BASE_NAME.to_string(),
ipv4: crate::constants::TUN_DEVICE_ADDRESS_V4,
netmaskv4: crate::constants::TUN_DEVICE_NETMASK_V4,
ipv6: crate::constants::TUN_DEVICE_ADDRESS_V6,
netmaskv6: crate::constants::TUN_DEVICE_NETMASK_V6.to_string(),
base_name: nym_network_defaults::constants::mixnet_vpn::NYM_TUN_BASE_NAME.to_string(),
ipv4: nym_network_defaults::constants::mixnet_vpn::NYM_TUN_DEVICE_ADDRESS_V4,
netmaskv4: nym_network_defaults::constants::mixnet_vpn::NYM_TUN_DEVICE_NETMASK_V4,
ipv6: nym_network_defaults::constants::mixnet_vpn::NYM_TUN_DEVICE_ADDRESS_V6,
netmaskv6: nym_network_defaults::constants::mixnet_vpn::NYM_TUN_DEVICE_NETMASK_V6
.to_string(),
};
let (tun_reader, tun_writer) =
tokio::io::split(nym_tun::tun_device::TunDevice::new_device_only(config)?);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use nym_ip_packet_requests::IpPair;
use nym_network_defaults::constants::mixnet_vpn::{
NYM_TUN_DEVICE_ADDRESS_V4, NYM_TUN_DEVICE_ADDRESS_V6,
};
use std::net::Ipv6Addr;
use std::{collections::HashMap, net::Ipv4Addr};

use crate::constants::{TUN_DEVICE_ADDRESS_V4, TUN_DEVICE_ADDRESS_V6};

// Find an available IP address in self.connected_clients
// TODO: make this nicer
fn generate_random_ips_within_subnet<R: rand::Rng>(rng: &mut R) -> IpPair {
Expand Down Expand Up @@ -36,7 +37,7 @@ pub(crate) fn find_new_ips<T>(
let mut rng = rand::thread_rng();
let mut new_ips = generate_random_ips_within_subnet(&mut rng);
let mut tries = 0;
let tun_ips = IpPair::new(TUN_DEVICE_ADDRESS_V4, TUN_DEVICE_ADDRESS_V6);
let tun_ips = IpPair::new(NYM_TUN_DEVICE_ADDRESS_V4, NYM_TUN_DEVICE_ADDRESS_V6);

while is_ip_taken(
connected_clients_ipv4,
Expand Down

0 comments on commit f7a7a80

Please sign in to comment.