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)]
mod tests {
use std::net::Ipv6Addr;
use std::{net::Ipv6Addr, num::NonZeroU16};
use crate::config::Config;
@ -260,7 +260,7 @@ mod tests {
let peers = ::std::iter::repeat(ResponsePeer {
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)
.collect();

View file

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

View file

@ -3,6 +3,7 @@
use std::{
io::Cursor,
net::{SocketAddr, UdpSocket},
num::NonZeroU16,
time::Duration,
};
@ -42,7 +43,7 @@ pub fn announce(
socket: &UdpSocket,
tracker_addr: SocketAddr,
connection_id: ConnectionId,
peer_port: u16,
peer_port: NonZeroU16,
info_hash: InfoHash,
peers_wanted: usize,
seeder: bool,
@ -50,7 +51,7 @@ pub fn announce(
let mut peer_id = PeerId([0; 20]);
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 {

View file

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

View file

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