Skip to content

Commit c4a1890

Browse files
authored
fix: use listen_addresses instead of local_address (#1044)
* fix: use listen_addresses instead of local_address listen_addresses are the actual listen addrs. * WIP: use addresses of local_endpoints the thing we painfully wait for... * fix: increase delays in test_timer_abort_late * fix: fix regex to allow for extremely slow transfers * ref: completely remove listen_addresses * fix: multiply more timer delays by 5 * fix(test): wait for provider to start up in cli_provide_addresses ...instead of a fixed delay. Also don't expect all ports to be 4333, just at least 1. * docs: add back doc comment for local_endpoint_addresses
1 parent 55d0211 commit c4a1890

6 files changed

Lines changed: 61 additions & 112 deletions

File tree

src/hp/magicsock/timer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ mod tests {
122122
assert!(!val.load(Ordering::Relaxed));
123123

124124
let moved_val = val.clone();
125-
let timer = Timer::after(Duration::from_millis(10), async move {
125+
let timer = Timer::after(Duration::from_millis(50), async move {
126126
moved_val.store(true, Ordering::Relaxed);
127127
});
128128

129129
assert!(!val.load(Ordering::Relaxed));
130-
time::sleep(Duration::from_millis(15)).await;
130+
time::sleep(Duration::from_millis(75)).await;
131131

132132
assert!(!timer.stop().await);
133133
assert!(val.load(Ordering::Relaxed));
@@ -140,24 +140,24 @@ mod tests {
140140
assert!(!val.load(Ordering::Relaxed));
141141

142142
let moved_val = val.clone();
143-
let timer = Timer::after(Duration::from_millis(10), async move {
143+
let timer = Timer::after(Duration::from_millis(50), async move {
144144
moved_val.store(true, Ordering::Relaxed);
145145
});
146146

147147
assert!(!val.load(Ordering::Relaxed));
148-
time::sleep(Duration::from_millis(5)).await;
148+
time::sleep(Duration::from_millis(25)).await;
149149

150150
// not yet expired
151151
assert!(!val.load(Ordering::Relaxed));
152-
// reset for another 10ms
153-
timer.reset(Duration::from_millis(20)).await;
152+
// reset for another 100ms
153+
timer.reset(Duration::from_millis(100)).await;
154154

155155
// would have expired if not reset
156-
time::sleep(Duration::from_millis(5)).await;
156+
time::sleep(Duration::from_millis(25)).await;
157157
assert!(!val.load(Ordering::Relaxed));
158158

159159
// definitely expired now
160-
time::sleep(Duration::from_millis(25)).await;
160+
time::sleep(Duration::from_millis(125)).await;
161161
assert!(val.load(Ordering::Relaxed));
162162
}
163163
}

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub const IROH_BLOCK_SIZE: BlockSize = match BlockSize::new(4) {
3939
mod tests {
4040
use std::{
4141
collections::BTreeMap,
42-
net::{Ipv4Addr, SocketAddr},
42+
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
4343
path::{Path, PathBuf},
4444
time::{Duration, Instant},
4545
};
@@ -282,7 +282,7 @@ mod tests {
282282
events
283283
});
284284

285-
let addrs = provider.listen_addresses()?;
285+
let addrs = provider.local_endpoint_addresses().await?;
286286
let opts = get::Options {
287287
addrs,
288288
peer_id: provider.peer_id(),
@@ -362,7 +362,7 @@ mod tests {
362362
.spawn()
363363
.await
364364
.unwrap();
365-
let provider_addr = provider.local_address().unwrap();
365+
let provider_addr = provider.local_endpoint_addresses().await.unwrap();
366366
let peer_id = provider.peer_id();
367367

368368
// This tasks closes the connection on the provider side as soon as the transfer
@@ -431,7 +431,7 @@ mod tests {
431431
.bind_addr("127.0.0.1:0".parse().unwrap())
432432
.spawn()
433433
.await?;
434-
let provider_addr = provider.local_address()?;
434+
let provider_addr = provider.local_endpoint_addresses().await?;
435435
let peer_id = provider.peer_id();
436436

437437
let timeout = tokio::time::timeout(std::time::Duration::from_secs(10), async move {
@@ -467,7 +467,7 @@ mod tests {
467467
let readme = Path::new(env!("CARGO_MANIFEST_DIR")).join("README.md");
468468
let (db, hash) = create_collection(vec![readme.into()]).await.unwrap();
469469
let provider = match Provider::builder(db)
470-
.bind_addr("[::1]:0".parse().unwrap())
470+
.bind_addr((Ipv6Addr::UNSPECIFIED, 0).into())
471471
.spawn()
472472
.await
473473
{
@@ -478,7 +478,7 @@ mod tests {
478478
return;
479479
}
480480
};
481-
let addrs = provider.local_address().unwrap();
481+
let addrs = provider.local_endpoint_addresses().await.unwrap();
482482
let peer_id = provider.peer_id();
483483
tokio::time::timeout(Duration::from_secs(10), async move {
484484
let request = get::run(
@@ -509,7 +509,7 @@ mod tests {
509509
.await
510510
.unwrap();
511511
let _drop_guard = provider.cancel_token().drop_guard();
512-
let ticket = provider.ticket(hash).unwrap();
512+
let ticket = provider.ticket(hash).await.unwrap();
513513
tokio::time::timeout(Duration::from_secs(10), async move {
514514
let response =
515515
get::run_ticket(&ticket, GetRequest::all(ticket.hash()).into(), true, None).await?;
@@ -587,7 +587,7 @@ mod tests {
587587
return;
588588
}
589589
};
590-
let addrs = provider.local_address().unwrap();
590+
let addrs = provider.local_endpoint_addresses().await.unwrap();
591591
let peer_id = provider.peer_id();
592592
tokio::time::timeout(Duration::from_secs(10), async move {
593593
let connection = dial_peer(get::Options {
@@ -668,7 +668,7 @@ mod tests {
668668
.spawn()
669669
.await
670670
.unwrap();
671-
let addrs = provider.local_address().unwrap();
671+
let addrs = provider.local_endpoint_addresses().await.unwrap();
672672
let peer_id = provider.peer_id();
673673
tokio::time::timeout(Duration::from_secs(10), async move {
674674
let request: AnyGetRequest = Bytes::from(&b"hello"[..]).into();
@@ -704,7 +704,7 @@ mod tests {
704704
.spawn()
705705
.await
706706
.unwrap();
707-
let addrs = provider.local_address().unwrap();
707+
let addrs = provider.local_endpoint_addresses().await.unwrap();
708708
let peer_id = provider.peer_id();
709709
tokio::time::timeout(Duration::from_secs(10), async move {
710710
let request: AnyGetRequest = Bytes::from(&b"hello"[..]).into();

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ async fn main_impl() -> Result<()> {
615615
let stream = controller.server_streaming(ProvideRequest { path }).await?;
616616
let (hash, entries) = aggregate_add_response(stream).await?;
617617
print_add_response(hash, entries);
618-
let ticket = provider.ticket(hash)?;
618+
let ticket = provider.ticket(hash).await?;
619619
println!("All-in-one ticket: {ticket}");
620620
anyhow::Ok(tmp_path)
621621
})

src/net/ip.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! IP address related utilities.
22
3-
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
4-
5-
use anyhow::{ensure, Result};
6-
use tracing::debug;
3+
use std::net::{IpAddr, Ipv6Addr};
74

85
const IFF_UP: u32 = 0x1;
96
const IFF_LOOPBACK: u32 = 0x8;
@@ -155,58 +152,6 @@ pub const fn is_unicast_link_local(addr: Ipv6Addr) -> bool {
155152
(addr.segments()[0] & 0xffc0) == 0xfe80
156153
}
157154

158-
/// Given a listen/bind address, finds all the local addresses for that address family.
159-
pub(crate) fn find_local_addresses(listen_addrs: &[SocketAddr]) -> Result<Vec<SocketAddr>> {
160-
debug!("find_local_address: {:?}", listen_addrs);
161-
162-
let mut addrs = Vec::new();
163-
let mut local_addrs = None;
164-
165-
for addr in listen_addrs {
166-
if addr.ip().is_unspecified() {
167-
// Find all the local addresses for this address family.
168-
if local_addrs.is_none() {
169-
local_addrs = Some(LocalAddresses::new());
170-
debug!("found local addresses: {:?}", local_addrs);
171-
}
172-
let local_addrs = local_addrs.as_ref().unwrap();
173-
let port = addr.port();
174-
175-
match addr.ip() {
176-
IpAddr::V4(_) => {
177-
addrs.extend(
178-
local_addrs
179-
.regular
180-
.iter()
181-
.chain(local_addrs.loopback.iter())
182-
.filter(|a| a.is_ipv4())
183-
.map(|a| SocketAddr::new(*a, port)),
184-
);
185-
}
186-
IpAddr::V6(_) => {
187-
addrs.extend(
188-
local_addrs
189-
.regular
190-
.iter()
191-
.chain(local_addrs.loopback.iter())
192-
.filter(|a| a.is_ipv6())
193-
.map(|a| SocketAddr::new(*a, port)),
194-
);
195-
}
196-
}
197-
} else {
198-
addrs.push(*addr);
199-
}
200-
}
201-
// we might have added duplicates, make sure to remove them
202-
addrs.sort();
203-
addrs.dedup();
204-
205-
ensure!(!addrs.is_empty(), "No local addresses found");
206-
207-
Ok(addrs)
208-
}
209-
210155
#[cfg(test)]
211156
mod tests {
212157
use super::*;

src/provider/mod.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use walkdir::WalkDir;
3939
use crate::blobs::Collection;
4040
use crate::hp::cfg::Endpoint;
4141
use crate::hp::derp::DerpMap;
42-
use crate::net::ip::find_local_addresses;
4342
use crate::protocol::{
4443
read_lp, write_lp, Closed, GetRequest, Handshake, RangeSpec, Request, VERSION,
4544
};
@@ -524,7 +523,7 @@ impl Provider {
524523
/// The address on which the provider socket is bound.
525524
///
526525
/// Note that this could be an unspecified address, if you need an address on which you
527-
/// can contact the provider consider using [`Provider::listen_addresses`]. However the
526+
/// can contact the provider consider using [`Provider::local_endpoint_addresses`]. However the
528527
/// port will always be the concrete port.
529528
pub fn local_address(&self) -> Result<Vec<SocketAddr>> {
530529
self.inner.local_address()
@@ -535,11 +534,9 @@ impl Provider {
535534
self.inner.local_endpoints().await
536535
}
537536

538-
/// Returns all addresses on which the provider is reachable.
539-
///
540-
/// This will never be empty.
541-
pub fn listen_addresses(&self) -> Result<Vec<SocketAddr>> {
542-
self.inner.listen_addresses()
537+
/// Convenience method to get just the addr part of [`Provider::local_endpoints`].
538+
pub async fn local_endpoint_addresses(&self) -> Result<Vec<SocketAddr>> {
539+
self.inner.local_endpoint_addresses().await
543540
}
544541

545542
/// Returns the [`PeerId`] of the provider.
@@ -563,9 +560,9 @@ impl Provider {
563560
/// Return a single token containing everything needed to get a hash.
564561
///
565562
/// See [`Ticket`] for more details of how it can be used.
566-
pub fn ticket(&self, hash: Hash) -> Result<Ticket> {
563+
pub async fn ticket(&self, hash: Hash) -> Result<Ticket> {
567564
// TODO: Verify that the hash exists in the db?
568-
let addrs = self.listen_addresses()?;
565+
let addrs = self.local_endpoint_addresses().await?;
569566
Ticket::new(hash, self.peer_id(), addrs)
570567
}
571568

@@ -591,6 +588,11 @@ impl ProviderInner {
591588
self.conn.local_endpoints().await
592589
}
593590

591+
async fn local_endpoint_addresses(&self) -> Result<Vec<SocketAddr>> {
592+
let endpoints = self.local_endpoints().await?;
593+
Ok(endpoints.into_iter().map(|x| x.addr).collect())
594+
}
595+
594596
fn local_address(&self) -> Result<Vec<SocketAddr>> {
595597
let (v4, v6) = self.conn.local_addr()?;
596598
let mut addrs = vec![v4];
@@ -599,9 +601,6 @@ impl ProviderInner {
599601
}
600602
Ok(addrs)
601603
}
602-
fn listen_addresses(&self) -> Result<Vec<SocketAddr>> {
603-
find_local_addresses(&self.local_address()?)
604-
}
605604
}
606605

607606
/// The future completes when the spawned tokio task finishes.
@@ -680,13 +679,21 @@ impl RpcHandler {
680679
async fn id(self, _: IdRequest) -> IdResponse {
681680
IdResponse {
682681
peer_id: Box::new(self.inner.keypair.public().into()),
683-
listen_addrs: self.inner.listen_addresses().unwrap_or_default(),
682+
listen_addrs: self
683+
.inner
684+
.local_endpoint_addresses()
685+
.await
686+
.unwrap_or_default(),
684687
version: env!("CARGO_PKG_VERSION").to_string(),
685688
}
686689
}
687690
async fn addrs(self, _: AddrsRequest) -> AddrsResponse {
688691
AddrsResponse {
689-
addrs: self.inner.listen_addresses().unwrap_or_default(),
692+
addrs: self
693+
.inner
694+
.local_endpoint_addresses()
695+
.await
696+
.unwrap_or_default(),
690697
}
691698
}
692699
async fn shutdown(self, request: ShutdownRequest) {
@@ -1358,7 +1365,7 @@ mod tests {
13581365
.await
13591366
.unwrap();
13601367
let _drop_guard = provider.cancel_token().drop_guard();
1361-
let ticket = provider.ticket(hash).unwrap();
1368+
let ticket = provider.ticket(hash).await.unwrap();
13621369
println!("addrs: {:?}", ticket.addrs());
13631370
assert!(!ticket.addrs().is_empty());
13641371
}

0 commit comments

Comments
 (0)