Skip to content

Commit

Permalink
Locally listen on IPv4 and IPv6 addresses
Browse files Browse the repository at this point in the history
Implemented the `DualStackLocalSocket` for the locally listening socket,
and updated the code base and fixed several issues that existed for
handling IPv6 addresses in the code base.

This included updating the `test_utils` framework to provide coverage in
tests for both ipv4 and ipv6 by allowing for either an IPv4 or IPv6
address to be created from the framework.

Closes googleforgames#666
  • Loading branch information
markmandel committed Aug 16, 2023
1 parent 640f4f5 commit ced4aa8
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ mod tests {

use tokio::time::{timeout, Duration};

use crate::test_utils::{create_socket, AddressType, TestHelper};
use crate::{
config::{Filter, Providers},
endpoint::{Endpoint, LocalityEndpoints},
filters::{Capture, StaticFilter, TokenRouter},
test_utils::{create_socket, AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions src/cli/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,10 @@ mod tests {

use tokio::time::{timeout, Duration};

use crate::test_utils::AddressType;
use crate::{
config,
endpoint::Endpoint,
test_utils::{available_addr, create_socket, load_test_filters, TestHelper},
test_utils::{available_addr, create_socket, load_test_filters, AddressType, TestHelper},
};

#[tokio::test]
Expand Down
6 changes: 2 additions & 4 deletions src/proxy/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

pub(crate) mod metrics;

use std::net::SocketAddr;
use std::sync::Arc;
use std::{net::SocketAddr, sync::Arc};

use tokio::{net::UdpSocket, select, sync::watch, time::Instant};

use crate::utils::net::DualStackLocalSocket;
use crate::{
endpoint::{Endpoint, EndpointAddress},
filters::{Filter, WriteContext},
maxmind_db::IpNetEntry,
utils::Loggable,
utils::{net::DualStackLocalSocket, Loggable},
};

pub type SessionMap = crate::ttl_map::TtlMap<SessionKey, Session>;
Expand Down
2 changes: 1 addition & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum AddressType {
}

/// Returns a local address on a port that is not assigned to another test.
/// Might be v4, Might be v6. It's random.
/// If Random address tye is used, it might be v4, Might be v6. It's random.
pub async fn available_addr(address_type: &AddressType) -> SocketAddr {
let socket = create_socket().await;
let addr = get_address(address_type, &socket);
Expand Down
6 changes: 2 additions & 4 deletions src/utils/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ pub struct DualStackLocalSocket {
v6: UdpSocket,
}

// TOXO: update the docs with the new port bindings.

impl DualStackLocalSocket {
pub fn new(port: u16) -> Result<DualStackLocalSocket> {
// if ephemeral port, make sure they are on the same ports.
Expand All @@ -73,8 +71,8 @@ impl DualStackLocalSocket {
})
}

/// Returns &[u8] from either the buffers, depending on which address type (v4 vs v6) is used
/// for communications.
/// Returns &[u8] from either of the buffers, depending on which address type (v4 vs v6) is
/// used for communications.
pub fn contents<'a>(v4_buf: &'a [u8], v6_buf: &'a [u8], recv: (usize, SocketAddr)) -> &'a [u8] {
let (size, addr) = recv;
match addr {
Expand Down
3 changes: 1 addition & 2 deletions tests/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use tokio::time::{timeout, Duration};

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Capture, StaticFilter, TokenRouter},
metadata::MetadataView,
test_utils::TestHelper,
test_utils::{AddressType, TestHelper},
};

/// This test covers both token_router and capture filters,
Expand Down
3 changes: 1 addition & 2 deletions tests/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

use tokio::time::{timeout, Duration};

use quilkin::test_utils::{available_addr, AddressType};
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Compress, StaticFilter},
test_utils::TestHelper,
test_utils::{available_addr, AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/concatenate_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ use std::net::Ipv4Addr;

use tokio::time::{timeout, Duration};

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{ConcatenateBytes, StaticFilter},
test_utils::TestHelper,
test_utils::{AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/filter_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ use std::{net::Ipv4Addr, str::from_utf8};

use tokio::time::{timeout, Duration};

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Compress, ConcatenateBytes, StaticFilter},
test_utils::TestHelper,
test_utils::{AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ use std::{
};
use tokio::time::timeout;

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Debug, StaticFilter},
test_utils::{load_test_filters, TestHelper},
test_utils::{load_test_filters, AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ use tokio::{
time::{timeout, Duration},
};

use quilkin::test_utils::{available_addr, AddressType};
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Firewall, StaticFilter},
test_utils::TestHelper,
test_utils::{available_addr, AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/load_balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::time::timeout;

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{LoadBalancer, StaticFilter},
test_utils::TestHelper,
test_utils::{AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/local_rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ use std::time::Duration;

use tokio::time::timeout;

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{LocalRateLimit, StaticFilter},
test_utils::{available_addr, TestHelper},
test_utils::{available_addr, AddressType, TestHelper},
};

#[tokio::test]
Expand Down
3 changes: 1 addition & 2 deletions tests/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use tokio::time::{timeout, Duration};

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Capture, Match, StaticFilter},
test_utils::TestHelper,
test_utils::{AddressType, TestHelper},
};

#[tokio::test]
Expand Down
6 changes: 4 additions & 2 deletions tests/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use quilkin::test_utils::AddressType;
use quilkin::{endpoint::Endpoint, test_utils::TestHelper};
use quilkin::{
endpoint::Endpoint,
test_utils::{AddressType, TestHelper},
};

#[tokio::test]
async fn metrics_server() {
Expand Down
6 changes: 4 additions & 2 deletions tests/no_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use tokio::time::timeout;
use tokio::time::Duration;

use quilkin::test_utils::{available_addr, AddressType};
use quilkin::{endpoint::Endpoint, test_utils::TestHelper};
use quilkin::{
endpoint::Endpoint,
test_utils::{available_addr, AddressType, TestHelper},
};

#[tokio::test]
async fn echo() {
Expand Down
6 changes: 4 additions & 2 deletions tests/qcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use tokio::time::Duration;

use quilkin::test_utils::AddressType;
use quilkin::{protocol::Protocol, test_utils::TestHelper};
use quilkin::{
protocol::Protocol,
test_utils::{AddressType, TestHelper},
};

#[tokio::test]
async fn proxy_ping() {
Expand Down
3 changes: 1 addition & 2 deletions tests/token_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ use std::net::{Ipv4Addr, SocketAddr};

use tokio::time::{timeout, Duration};

use quilkin::test_utils::AddressType;
use quilkin::{
config::Filter,
endpoint::Endpoint,
filters::{Capture, StaticFilter, TokenRouter},
metadata::MetadataView,
test_utils::TestHelper,
test_utils::{AddressType, TestHelper},
};

/// This test covers both token_router and capture filters,
Expand Down

0 comments on commit ced4aa8

Please sign in to comment.