Skip to content

Commit

Permalink
linux clippy fixes + adding additional dummy ipr types for better lin…
Browse files Browse the repository at this point in the history
…ting on non-linux targets
  • Loading branch information
jstuczyn committed Dec 17, 2024
1 parent 65e8a67 commit 47d72f7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
1 change: 1 addition & 0 deletions service-providers/ip-packet-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod error;
mod ip_packet_router;
mod mixnet_client;
mod mixnet_listener;
pub(crate) mod non_linux_dummy;
pub mod request_filter;
mod tun_listener;
mod util;
47 changes: 14 additions & 33 deletions service-providers/ip-packet-router/src/mixnet_listener.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::sync::Arc;
use std::{collections::HashMap, net::SocketAddr};

use bytes::{Bytes, BytesMut};
use futures::StreamExt;
use nym_ip_packet_requests::v7::response::{
Expand All @@ -23,8 +19,10 @@ use nym_ip_packet_requests::{
use nym_sdk::mixnet::{MixnetMessageSender, Recipient};
use nym_sphinx::receiver::ReconstructedMessage;
use nym_task::TaskHandle;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::sync::Arc;
use std::{collections::HashMap, net::SocketAddr};
use tap::TapFallible;
#[cfg(target_os = "linux")]
use tokio::io::AsyncWriteExt;
use tokio::sync::RwLock;
use tokio_util::codec::Decoder;
Expand Down Expand Up @@ -111,7 +109,6 @@ impl ConnectedClients {
&mut self,
ips: IpPair,
nym_address: Recipient,
mix_hops: Option<u8>,
forward_from_tun_tx: tokio::sync::mpsc::UnboundedSender<Vec<u8>>,
close_tx: tokio::sync::oneshot::Sender<()>,
handle: tokio::task::JoinHandle<()>,
Expand All @@ -121,7 +118,6 @@ impl ConnectedClients {
let client = ConnectedClient {
nym_address,
ipv6: ips.ipv6,
mix_hops,
last_activity: Arc::new(RwLock::new(std::time::Instant::now())),
_close_tx: Arc::new(CloseTx {
nym_address,
Expand Down Expand Up @@ -236,9 +232,6 @@ pub(crate) struct ConnectedClient {
// The assigned IPv6 address of this client
pub(crate) ipv6: Ipv6Addr,

// Number of mix node hops that the client has requested to use
pub(crate) mix_hops: Option<u8>,

// Keep track of last activity so we can disconnect inactive clients
pub(crate) last_activity: Arc<RwLock<std::time::Instant>>,

Expand Down Expand Up @@ -390,7 +383,13 @@ impl Response {
}
}

#[cfg(not(target_os = "linux"))]
type TunDevice = crate::non_linux_dummy::DummyDevice;

#[cfg(target_os = "linux")]
type TunDevice = tokio_tun::Tun;

// #[cfg(target_os = "linux")]
pub(crate) struct MixnetListener {
// The configuration for the mixnet listener
pub(crate) _config: Config,
Expand All @@ -399,7 +398,7 @@ pub(crate) struct MixnetListener {
pub(crate) request_filter: request_filter::RequestFilter,

// The TUN device that we use to send and receive packets from the internet
pub(crate) tun_writer: tokio::io::WriteHalf<tokio_tun::Tun>,
pub(crate) tun_writer: tokio::io::WriteHalf<TunDevice>,

// The mixnet client that we use to send and receive packets from the mixnet
pub(crate) mixnet_client: nym_sdk::mixnet::MixnetClient,
Expand All @@ -412,7 +411,7 @@ pub(crate) struct MixnetListener {
pub(crate) connected_clients: ConnectedClients,
}

#[cfg(target_os = "linux")]
// #[cfg(target_os = "linux")]
impl MixnetListener {
// Receving a static connect request from a client with an IP provided that we assign to them,
// if it's available. If it's not available, we send a failure response.
Expand All @@ -429,7 +428,6 @@ impl MixnetListener {
let request_id = connect_request.request_id;
let requested_ips = connect_request.ips;
let reply_to = connect_request.reply_to;
let reply_to_hops = connect_request.reply_to_hops;
// TODO: add to connect request
let buffer_timeout = nym_ip_packet_requests::codec::BUFFER_TIMEOUT;
// TODO: ignoring reply_to_avg_mix_delays for now
Expand Down Expand Up @@ -556,21 +554,14 @@ impl MixnetListener {
let (forward_from_tun_tx, close_tx, handle) =
connected_client_handler::ConnectedClientHandler::start(
reply_to,
reply_to_hops,
buffer_timeout,
client_version,
self.mixnet_client.split_sender(),
);

// Register the new client in the set of connected clients
self.connected_clients.connect(
new_ips,
reply_to,
reply_to_hops,
forward_from_tun_tx,
close_tx,
handle,
);
self.connected_clients
.connect(new_ips, reply_to, forward_from_tun_tx, close_tx, handle);
Ok(Some(Response::new_dynamic_connect_success(
request_id,
reply_to,
Expand Down Expand Up @@ -754,17 +745,7 @@ impl MixnetListener {

let response_packet = response.to_bytes()?;

// We could avoid this lookup if we check this when we create the response.
let mix_hops = if let Some(c) = self
.connected_clients
.lookup_client_from_nym_address(recipient)
{
c.mix_hops
} else {
None
};

let input_message = create_input_message(*recipient, response_packet, mix_hops);
let input_message = create_input_message(*recipient, response_packet);
self.mixnet_client
.send(input_message)
.await
Expand Down
43 changes: 43 additions & 0 deletions service-providers/ip-packet-router/src/non_linux_dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use std::io::Error;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

pub(crate) struct DummyDevice;

impl AsyncRead for DummyDevice {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
unimplemented!("tunnel devices are not supported by non-linux targets")
}
}

impl AsyncWrite for DummyDevice {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &[u8],
) -> Poll<std::result::Result<usize, Error>> {
unimplemented!("tunnel devices are not supported by non-linux targets")
}

fn poll_flush(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Error>> {
unimplemented!("tunnel devices are not supported by non-linux targets")
}

fn poll_shutdown(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Error>> {
unimplemented!("tunnel devices are not supported by non-linux targets")
}
}

0 comments on commit 47d72f7

Please sign in to comment.