Skip to content

Commit

Permalink
chore: Replace IP strings with address types
Browse files Browse the repository at this point in the history
Avoids a bunch of parsing and checking.
  • Loading branch information
larseggert authored and djc committed Oct 9, 2024
1 parent 08ee9ee commit 15a4dce
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 30 deletions.
9 changes: 5 additions & 4 deletions quinn-udp/benches/throughput.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use criterion::{criterion_group, criterion_main, Criterion};
use quinn_udp::{RecvMeta, Transmit, UdpSocketState};
use std::cmp::min;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::{io::IoSliceMut, net::UdpSocket, slice};

pub fn criterion_benchmark(c: &mut Criterion) {
Expand All @@ -9,11 +10,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {
const MAX_BUFFER_SIZE: usize = u16::MAX as usize;
const SEGMENT_SIZE: usize = 1280;

let send = UdpSocket::bind("[::1]:0")
.or_else(|_| UdpSocket::bind("127.0.0.1:0"))
let send = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
.unwrap();
let recv = UdpSocket::bind("[::1]:0")
.or_else(|_| UdpSocket::bind("127.0.0.1:0"))
let recv = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
.unwrap();
let max_segments = min(
UdpSocketState::new((&send).into())
Expand Down
4 changes: 2 additions & 2 deletions quinn-udp/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ mod gso {
const GSO_SIZE: libc::c_int = 1500;

let socket = match std::net::UdpSocket::bind("[::]:0")
.or_else(|_| std::net::UdpSocket::bind("127.0.0.1:0"))
.or_else(|_| std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
{
Ok(socket) => socket,
Err(_) => return 1,
Expand Down Expand Up @@ -750,7 +750,7 @@ mod gro {

pub(crate) fn gro_segments() -> usize {
let socket = match std::net::UdpSocket::bind("[::]:0")
.or_else(|_| std::net::UdpSocket::bind("127.0.0.1:0"))
.or_else(|_| std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
{
Ok(socket) => socket,
Err(_) => return 1,
Expand Down
2 changes: 1 addition & 1 deletion quinn-udp/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ static WSARECVMSG_PTR: Lazy<WinSock::LPFN_WSARECVMSG> = Lazy::new(|| {

static MAX_GSO_SEGMENTS: Lazy<usize> = Lazy::new(|| {
let socket = match std::net::UdpSocket::bind("[::]:0")
.or_else(|_| std::net::UdpSocket::bind("127.0.0.1:0"))
.or_else(|_| std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
{
Ok(socket) => socket,
Err(_) => return 1,
Expand Down
31 changes: 17 additions & 14 deletions quinn-udp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use socket2::Socket;

#[test]
fn basic() {
let send = UdpSocket::bind("[::1]:0")
.or_else(|_| UdpSocket::bind("127.0.0.1:0"))
let send = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
.unwrap();
let recv = UdpSocket::bind("[::1]:0")
.or_else(|_| UdpSocket::bind("127.0.0.1:0"))
let recv = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
.unwrap();
let dst_addr = recv.local_addr().unwrap();
test_send_recv(
Expand All @@ -33,8 +33,8 @@ fn basic() {

#[test]
fn ecn_v6() {
let send = Socket::from(UdpSocket::bind("[::1]:0").unwrap());
let recv = Socket::from(UdpSocket::bind("[::1]:0").unwrap());
let send = Socket::from(UdpSocket::bind((Ipv6Addr::LOCALHOST, 0)).unwrap());
let recv = Socket::from(UdpSocket::bind((Ipv6Addr::LOCALHOST, 0)).unwrap());
for codepoint in [EcnCodepoint::Ect0, EcnCodepoint::Ect1] {
test_send_recv(
&send,
Expand All @@ -53,8 +53,8 @@ fn ecn_v6() {
#[test]
#[cfg(not(any(target_os = "openbsd", target_os = "netbsd", target_os = "solaris")))]
fn ecn_v4() {
let send = Socket::from(UdpSocket::bind("127.0.0.1:0").unwrap());
let recv = Socket::from(UdpSocket::bind("127.0.0.1:0").unwrap());
let send = Socket::from(UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap());
let recv = Socket::from(UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap());
for codepoint in [EcnCodepoint::Ect0, EcnCodepoint::Ect1] {
test_send_recv(
&send,
Expand Down Expand Up @@ -93,7 +93,10 @@ fn ecn_v6_dualstack() {
0,
));
let recv_v4 = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, recv_v6.port()));
for (src, dst) in [("[::1]:0", recv_v6), ("127.0.0.1:0", recv_v4)] {
for (src, dst) in [
(SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0), recv_v6),
(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0), recv_v4),
] {
dbg!(src, dst);
let send = UdpSocket::bind(src).unwrap();
let send = Socket::from(send);
Expand Down Expand Up @@ -128,7 +131,7 @@ fn ecn_v4_mapped_v6() {
))
.unwrap();

let recv = UdpSocket::bind("127.0.0.1:0").unwrap();
let recv = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
let recv = Socket::from(recv);
let recv_v4_mapped_v6 = SocketAddr::V6(SocketAddrV6::new(
Ipv4Addr::LOCALHOST.to_ipv6_mapped(),
Expand All @@ -155,11 +158,11 @@ fn ecn_v4_mapped_v6() {
#[test]
#[cfg_attr(not(any(target_os = "linux", target_os = "windows")), ignore)]
fn gso() {
let send = UdpSocket::bind("[::1]:0")
.or_else(|_| UdpSocket::bind("127.0.0.1:0"))
let send = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
.unwrap();
let recv = UdpSocket::bind("[::1]:0")
.or_else(|_| UdpSocket::bind("127.0.0.1:0"))
let recv = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)))
.unwrap();
let max_segments = UdpSocketState::new((&send).into())
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion quinn/examples/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use common::{make_client_endpoint, make_server_endpoint};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let server_addr = "127.0.0.1:5000".parse().unwrap();
let server_addr = (Ipv4Addr::LOCALHOST, 5000).parse().unwrap();
let (endpoint, server_cert) = make_server_endpoint(server_addr)?;
// accept a single connection
let endpoint2 = endpoint.clone();
Expand Down
4 changes: 2 additions & 2 deletions quinn/examples/insecure_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use common::make_server_endpoint;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// server and client are running on the same thread asynchronously
let addr = "127.0.0.1:5000".parse().unwrap();
let addr = (Ipv4Addr::LOCALHOST, 5000).parse().unwrap();
tokio::spawn(run_server(addr));
run_client(addr).await?;
Ok(())
Expand All @@ -33,7 +33,7 @@ async fn run_server(addr: SocketAddr) {
}

async fn run_client(server_addr: SocketAddr) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let mut endpoint = Endpoint::client("127.0.0.1:0".parse().unwrap())?;
let mut endpoint = Endpoint::client((Ipv4Addr::LOCALHOST, 0).parse().unwrap())?;

endpoint.set_default_client_config(ClientConfig::new(Arc::new(QuicClientConfig::try_from(
rustls::ClientConfig::builder()
Expand Down
8 changes: 4 additions & 4 deletions quinn/examples/single_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ use rustls::pki_types::CertificateDer;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let addr1 = "127.0.0.1:5000".parse().unwrap();
let addr2 = "127.0.0.1:5001".parse().unwrap();
let addr3 = "127.0.0.1:5002".parse().unwrap();
let addr1 = (Ipv4Addr::LOCALHOST, 5000).parse().unwrap();
let addr2 = (Ipv4Addr::LOCALHOST, 5001).parse().unwrap();
let addr3 = (Ipv4Addr::LOCALHOST, 5002).parse().unwrap();
let server1_cert = run_server(addr1)?;
let server2_cert = run_server(addr2)?;
let server3_cert = run_server(addr3)?;

let client = make_client_endpoint(
"127.0.0.1:0".parse().unwrap(),
(Ipv4Addr::LOCALHOST, 0).parse().unwrap(),
&[&server1_cert, &server2_cert, &server3_cert],
)?;

Expand Down
2 changes: 1 addition & 1 deletion quinn/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn close_endpoint() {

#[test]
fn local_addr() {
let socket = UdpSocket::bind("[::1]:0").unwrap();
let socket = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0)).unwrap();
let addr = socket.local_addr().unwrap();
let runtime = rt_basic();
let ep = {
Expand Down
4 changes: 3 additions & 1 deletion quinn/tests/many_connections.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(any(feature = "rustls-aws-lc-rs", feature = "rustls-ring"))]
use std::{
convert::TryInto,
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{Arc, Mutex},
time::Duration,
};
Expand Down Expand Up @@ -30,7 +31,8 @@ fn connect_n_nodes_to_1_and_send_1mb_data() {
let shared = Arc::new(Mutex::new(Shared { errors: vec![] }));

let (cfg, listener_cert) = configure_listener();
let endpoint = quinn::Endpoint::server(cfg, "127.0.0.1:0".parse().unwrap()).unwrap();
let endpoint =
quinn::Endpoint::server(cfg, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0)).unwrap();
let listener_addr = endpoint.local_addr().unwrap();

let expected_messages = 50;
Expand Down

0 comments on commit 15a4dce

Please sign in to comment.