Skip to content

Commit

Permalink
Not to use link-local addrs if routable addrs exist (#117)
Browse files Browse the repository at this point in the history
* And bump up to rustc 1.60.0
  • Loading branch information
keepsimple1 authored Aug 5, 2023
1 parent ee3771e commit ff659a4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.55.0
toolchain: 1.60.0
override: true
components: rustfmt, clippy
- name: Run rustfmt and fail if any warnings
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default = ["async", "logging"]

[dependencies]
flume = { version = "0.10", default-features = false } # channel between threads
if-addrs = "0.7" # get local IP addresses
if-addrs = "0.10" # get local IP addresses
log = { version = "0.4.14", optional = true } # logging
polling = "2.1" # select/poll sockets
socket2 = { version = "0.4", features = ["all"] } # socket APIs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Currently this library has the following limitations:

## Minimum Rust version

Tested against Rust 1.55.0
Tested against Rust 1.60.0

## License

Expand Down
20 changes: 18 additions & 2 deletions src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,20 +1672,36 @@ fn call_listener(

/// Returns valid IPv4 interfaces in the host system.
fn my_ipv4_interfaces() -> Vec<Ifv4Addr> {
if_addrs::get_if_addrs()
// Link local interfaces have the 169.254/16 prefix,
// see RFC 3927 for details.
let mut link_local_count = 0;

let mut intf_vec: Vec<Ifv4Addr> = if_addrs::get_if_addrs()
.unwrap_or_default()
.into_iter()
.filter_map(|i| {
if i.is_loopback() {
None
} else {
if i.is_link_local() {
link_local_count += 1;
}
match i.addr {
IfAddr::V4(ifv4) => Some(ifv4),
_ => None,
}
}
})
.collect()
.collect();

// If we have both routable interfaces and link-local interfaces,
// we only keep the routable interfaces. Otherwise, it can confuse
// the clients.
if link_local_count > 0 && intf_vec.len() > link_local_count {
intf_vec.retain(|i| !i.is_link_local())
}

intf_vec
}

/// Sends out `packet` to `addr` on the socket in `intf_sock`.
Expand Down
16 changes: 14 additions & 2 deletions tests/mdns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,22 @@ fn instance_name_two_dots() {
}

fn my_ipv4_interfaces() -> Vec<Ifv4Addr> {
let mut link_local_count = 0;

// Use a random port for binding test.
let test_port = fastrand::u16(8000u16..9000u16);

if_addrs::get_if_addrs()
let mut intf_vec: Vec<Ifv4Addr> = if_addrs::get_if_addrs()
.unwrap_or_default()
.into_iter()
.filter_map(|i| {
if i.is_loopback() {
None
} else {
if i.is_link_local() {
link_local_count += 1;
}

match i.addr {
IfAddr::V4(ifv4) =>
// Use a 'bind' to check if this is a valid IPv4 addr.
Expand All @@ -658,7 +664,13 @@ fn my_ipv4_interfaces() -> Vec<Ifv4Addr> {
}
}
})
.collect()
.collect();

if link_local_count > 0 && intf_vec.len() > link_local_count {
intf_vec.retain(|i| !i.is_link_local())
}

intf_vec
}

/// Returns a made-up IPv4 address "net.1.1.1", where
Expand Down

0 comments on commit ff659a4

Please sign in to comment.