udp: consistently use NonZeroU16 for announce request port

This commit is contained in:
Joakim Frostegård 2024-02-03 11:46:38 +01:00
parent b4e27903dc
commit 3513b714b4
8 changed files with 18 additions and 14 deletions

View file

@ -244,7 +244,7 @@ impl Default for State {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::net::Ipv6Addr; use std::{net::Ipv6Addr, num::NonZeroU16};
use crate::config::Config; use crate::config::Config;
@ -260,7 +260,7 @@ mod tests {
let peers = ::std::iter::repeat(ResponsePeer { let peers = ::std::iter::repeat(ResponsePeer {
ip_address: Ipv6AddrBytes(Ipv6Addr::new(1, 1, 1, 1, 1, 1, 1, 1).octets()), ip_address: Ipv6AddrBytes(Ipv6Addr::new(1, 1, 1, 1, 1, 1, 1, 1).octets()),
port: Port::new(1), port: Port::new(NonZeroU16::new(1).unwrap()),
}) })
.take(config.protocol.max_response_peers) .take(config.protocol.max_response_peers)
.collect(); .collect();

View file

@ -6,6 +6,7 @@ use std::{
fs::File, fs::File,
io::Write, io::Write,
net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket}, net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket},
num::NonZeroU16,
time::Duration, time::Duration,
}; };
@ -78,7 +79,7 @@ fn test_access_list(
&socket, &socket,
tracker_addr, tracker_addr,
connection_id, connection_id,
1, NonZeroU16::new(1).unwrap(),
info_hash_fail, info_hash_fail,
10, 10,
false, false,
@ -95,7 +96,7 @@ fn test_access_list(
&socket, &socket,
tracker_addr, tracker_addr,
connection_id, connection_id,
1, NonZeroU16::new(1).unwrap(),
info_hash_success, info_hash_success,
10, 10,
false, false,

View file

@ -3,6 +3,7 @@
use std::{ use std::{
io::Cursor, io::Cursor,
net::{SocketAddr, UdpSocket}, net::{SocketAddr, UdpSocket},
num::NonZeroU16,
time::Duration, time::Duration,
}; };
@ -42,7 +43,7 @@ pub fn announce(
socket: &UdpSocket, socket: &UdpSocket,
tracker_addr: SocketAddr, tracker_addr: SocketAddr,
connection_id: ConnectionId, connection_id: ConnectionId,
peer_port: u16, peer_port: NonZeroU16,
info_hash: InfoHash, info_hash: InfoHash,
peers_wanted: usize, peers_wanted: usize,
seeder: bool, seeder: bool,
@ -50,7 +51,7 @@ pub fn announce(
let mut peer_id = PeerId([0; 20]); let mut peer_id = PeerId([0; 20]);
for chunk in peer_id.0.chunks_exact_mut(2) { for chunk in peer_id.0.chunks_exact_mut(2) {
chunk.copy_from_slice(&peer_port.to_ne_bytes()); chunk.copy_from_slice(&peer_port.get().to_ne_bytes());
} }
let request = Request::Announce(AnnounceRequest { let request = Request::Announce(AnnounceRequest {

View file

@ -5,6 +5,7 @@ use common::*;
use std::{ use std::{
io::{Cursor, ErrorKind}, io::{Cursor, ErrorKind},
net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket}, net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket},
num::NonZeroU16,
time::Duration, time::Duration,
}; };
@ -51,7 +52,7 @@ fn test_invalid_connection_id() -> anyhow::Result<()> {
ip_address: Ipv4AddrBytes([0; 4]), ip_address: Ipv4AddrBytes([0; 4]),
key: PeerKey::new(0), key: PeerKey::new(0),
peers_wanted: NumberOfPeers::new(10), peers_wanted: NumberOfPeers::new(10),
port: Port::new(1), port: Port::new(NonZeroU16::new(1).unwrap()),
}); });
let scrape_request = Request::Scrape(ScrapeRequest { let scrape_request = Request::Scrape(ScrapeRequest {

View file

@ -5,6 +5,7 @@ use common::*;
use std::{ use std::{
collections::{hash_map::RandomState, HashSet}, collections::{hash_map::RandomState, HashSet},
net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket}, net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket},
num::NonZeroU16,
time::Duration, time::Duration,
}; };
@ -45,7 +46,7 @@ fn test_multiple_connect_announce_scrape() -> anyhow::Result<()> {
&socket, &socket,
tracker_addr, tracker_addr,
connection_id, connection_id,
PEER_PORT_START + i as u16, NonZeroU16::new(PEER_PORT_START + i as u16).unwrap(),
info_hash, info_hash,
PEERS_WANTED, PEERS_WANTED,
is_seeder, is_seeder,

View file

@ -1,6 +1,5 @@
use std::io::Cursor; use std::io::Cursor;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::num::NonZeroU16;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::time::Duration; use std::time::Duration;
@ -193,7 +192,7 @@ impl Worker {
scrape_hash_indices, scrape_hash_indices,
connection_id, connection_id,
peer_id: generate_peer_id(), peer_id: generate_peer_id(),
port: Port::new(self.rng.gen::<NonZeroU16>().into()), port: Port::new(self.rng.gen()),
} }
} }

View file

@ -1,5 +1,6 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use std::num::NonZeroU16;
pub use aquatic_peer_id::{PeerClient, PeerId}; pub use aquatic_peer_id::{PeerClient, PeerId};
use zerocopy::network_endian::{I32, I64, U16, U32}; use zerocopy::network_endian::{I32, I64, U16, U32};
@ -76,8 +77,8 @@ impl NumberOfDownloads {
pub struct Port(pub U16); pub struct Port(pub U16);
impl Port { impl Port {
pub fn new(v: u16) -> Self { pub fn new(v: NonZeroU16) -> Self {
Self(U16::new(v)) Self(U16::new(v.into()))
} }
} }

View file

@ -281,7 +281,7 @@ impl RequestParseError {
mod tests { mod tests {
use quickcheck::TestResult; use quickcheck::TestResult;
use quickcheck_macros::quickcheck; use quickcheck_macros::quickcheck;
use zerocopy::network_endian::{I32, I64, U16}; use zerocopy::network_endian::{I32, I64};
use super::*; use super::*;
@ -319,7 +319,7 @@ mod tests {
ip_address: Ipv4AddrBytes::arbitrary(g), ip_address: Ipv4AddrBytes::arbitrary(g),
key: PeerKey::new(i32::arbitrary(g)), key: PeerKey::new(i32::arbitrary(g)),
peers_wanted: NumberOfPeers(I32::new(i32::arbitrary(g))), peers_wanted: NumberOfPeers(I32::new(i32::arbitrary(g))),
port: Port(U16::new(u16::arbitrary(g))), port: Port::new(quickcheck::Arbitrary::arbitrary(g)),
} }
} }
} }