Skip to content

feat: extend DnsAddress with an InterfaceId - #342

Merged
keepsimple1 merged 3 commits into
keepsimple1:mainfrom
hrzlgnm:feat/resolved-addresses-with-scope
Apr 23, 2025
Merged

keepsimple1 merged 3 commits into
keepsimple1:mainfrom
hrzlgnm:feat/resolved-addresses-with-scope

Conversation

@hrzlgnm

@hrzlgnm hrzlgnm commented Apr 9, 2025 •

Copy link
Copy Markdown
Contributor

This allows for adding an interface scope to resolved IPv6 link local addresses to resolve #339

@hrzlgnm
hrzlgnm force-pushed the feat/resolved-addresses-with-scope branch 3 times, most recently from 9530a44 to 8e7f17b Compare April 9, 2025 21:43
Comment thread src/dns_cache.rs Resolved
Comment thread src/service_daemon.rs Outdated
let selected_addrs = self.selected_addrs(my_ip_interfaces(true));
for addr in selected_addrs {
info.insert_ipaddr(addr);
info.insert_scoped_addr(addr, None);

@hrzlgnm hrzlgnm Apr 9, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pass in the actual interface id instead of None since we have the info from my_ip_interfaces().

We can modify selected_addrs() function to return a set of ScopedAddr

@hrzlgnm
hrzlgnm force-pushed the feat/resolved-addresses-with-scope branch from cefb9a4 to c0873d8 Compare April 9, 2025 22:00
Comment thread src/service_daemon.rs
let mut resolved = false;

while let Ok(event) = browse_chan.recv_timeout(timeout) {
match event {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those next 3 changes were suggested by rust clippy for single_match, if not feasible please let me know.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted all unrelated changes, to avoid adding too much refactoring to this PR

@hrzlgnm
hrzlgnm marked this pull request as ready for review April 9, 2025 22:23
@hrzlgnm hrzlgnm changed the title feat: provide a scope with the resolved addreses feat: provide a interface scope with the resolved addreses Apr 9, 2025

@keepsimple1 keepsimple1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for your PR! I've added a few inline comments. And I also have a high level question:

Instead of add interface info in each address, should we add the interface info at ServiceInfo ?

For IPv6, I think it's pretty clear we can / should add the interface at ServiceInfo layer because mDNS for IPv6 is in link-local scopes (only). It is possible that there are two service instances with exact same name but on two different IPv6 interfaces. Currently we would combine them into one ServiceInfo with two addresses, which would be incorrect.

For IPv4, things are different. If a host has 2 IPv4 interfaces configured on the same LAN, they will only see one service instance given the same name. It's ok / making sense to combine into one ServiceInfo. That said, it's also workable if we return two ServiceInfo records each for one Interface.

Looking at Avahi, it seems they are reporting service instances based on interfaces. So basically my question is: shall we do that too? (Avahi also reports IPv4 and IPv6 separately, but I think we could combine them).

What do you think?

Comment thread src/dns_parser.rs Outdated
class: u16,
ttl: u32,
address: IpAddr,
scope: Option<InterfaceScope>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of Option as DnsIncoming can always have the interface info.

Comment thread src/dns_parser.rs Outdated
num_answers: u16,
num_authorities: u16,
num_additionals: u16,
scope: Option<InterfaceScope>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can get rid of Option here as this interface is available when creating DnsIncoming.

Comment thread src/service_info.rs Outdated

/// InterfaceScope is used to represent the interface name and index
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct InterfaceScope {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: scope is really a concept in IPv6, not a concept of a network interface. How about naming this struct InterfaceId ?

Comment thread src/service_daemon.rs Outdated
let selected_addrs = self.selected_addrs(my_ip_interfaces(true));
for addr in selected_addrs {
info.insert_ipaddr(addr);
info.insert_scoped_addr(addr, None);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pass in the actual interface id instead of None since we have the info from my_ip_interfaces().

We can modify selected_addrs() function to return a set of ScopedAddr

Comment thread src/service_info.rs Outdated

/// ScopedAddress is used to represent an address and its interface scope
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ScopedAddress {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: To follow the naming pattern of IpAddr etc, shall we name this ScopedAddr ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, but I guess we sort of agreed upon adding InterfaceId to ServiceInfo instead.

Comment thread src/service_info.rs Outdated
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ScopedAddress {
address: IpAddr,
scope: Option<InterfaceScope>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of Option here as there is always an interface owning the address.

Comment thread src/dns_parser.rs
}
}

impl DnsRecordExt for DnsAddress {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to update matches for DnsAddress to check scope (or interface_id)

@hrzlgnm

hrzlgnm commented Apr 13, 2025

Copy link
Copy Markdown
Contributor Author

Thanks again for your PR! I've added a few inline comments. And I also have a high level question:

Thanks for reviewing, I will address those comments after resolving the high level discussion.

Instead of add interface info in each address, should we add the interface info at ServiceInfo ?

For IPv6, I think it's pretty clear we can / should add the interface at ServiceInfo layer because mDNS for IPv6 is in link-local scopes (only). It is possible that there are two service instances with exact same name but on two different IPv6 interfaces. Currently we would combine them into one ServiceInfo with two addresses, which would be incorrect.

For IPv4, things are different. If a host has 2 IPv4 interfaces configured on the same LAN, they will only see one service instance given the same name. It's ok / making sense to combine into one ServiceInfo. That said, it's also workable if we return two ServiceInfo records each for one Interface.

Looking at Avahi, it seems they are reporting service instances based on interfaces. So basically my question is: shall we do that too? (Avahi also reports IPv4 and IPv6 separately, but I think we could combine them).

What do you think?

Yes that makes sense, I thought about this route shorty but didn't yet tried it out. Now it also makes sense to me to separate IPv4 and IPv6 ServiceInfos and per interface like Avahi does. That would make the structure more cleaner. From a library consumer perspective that approach would be more feasible than the route I took initially. The consumer of the library can still consolidate those service infos based on the instance name if needed.

@keepsimple1

Copy link
Copy Markdown
Owner

Now it also makes sense to me to separate IPv4 and IPv6 ServiceInfos and per interface like Avahi does. That would make the structure more cleaner.

Could you elaborate more about this separation of IPv4 and IPv6 Serviceinfos ? My initial thought was that they can always been combined and so this library can do it for the users. But I'd like to understand more from your point of view.

@hrzlgnm

hrzlgnm commented Apr 13, 2025

Copy link
Copy Markdown
Contributor Author

Now it also makes sense to me to separate IPv4 and IPv6 ServiceInfos and per interface like Avahi does. That would make the structure more cleaner.

Could you elaborate more about this separation of IPv4 and IPv6 Serviceinfos ? My initial thought was that they can always been combined and so this library can do it for the users. But I'd like to understand more from your point of view.

Ah, maybe I got something wrong. I envisioned an addition of InterfaceId to ServiceInfo. Which of course allows for adding both, IPv6 and IPv4 addresses, as long those are received from the same InterfaceId. A separation to IPv4 and IPv6 forServiceInfo isn't really necessary in that case, I just went to far with making this similar with Avahi.

@hrzlgnm

hrzlgnm commented Apr 14, 2025

Copy link
Copy Markdown
Contributor Author

@keepsimple1: 'm going to add InterfaceId to ServiceInfo and ResolvedService instead of adding more complexity with my first approach using ScopedAddress and InterfaceScope.

InterfaceId being defined as:

struct InterfaceId {
      name: String,
      index: Option<u32>,
}

If you don't object.

Comment thread src/dns_parser.rs Outdated
use crate::error::{e_fmt, Error, Result};
use crate::{
error::{e_fmt, Error, Result},
service_info::InterfaceScope,

@hrzlgnm hrzlgnm Apr 14, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keepsimple1: dns_parser is ought to be a separate module, do we want to provide InterfaceId from dns_parser. Or would it be better to create a separate module interface_id which provides InterfaceId for dns_parser and service_info. I'm not that fluent yet in rust best practices about modules, I'm just guessing from experience from other languages and I sort of would prefer the interface_id module.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to define InterfaceId in dns_parser. We can refactor the module later once we have clearer understanding.

@hrzlgnm

hrzlgnm commented Apr 14, 2025

Copy link
Copy Markdown
Contributor Author

I've started to work on adding an InterfaceId to ServiceInfo but the following issue arises, if we add that field, we need to initialize the field in ServiceInfo::new().

pub fn new<Ip: AsIpAddrs, P: IntoTxtProperties>(

But how do we want to do that without introducing a breaking change?

@hrzlgnm
hrzlgnm marked this pull request as draft April 14, 2025 22:15
@hrzlgnm

hrzlgnm commented Apr 15, 2025 •

Copy link
Copy Markdown
Contributor Author

I've started to work on adding an InterfaceId to ServiceInfo but the following issue arises, if we add that field, we need to initialize the field in ServiceInfo::new().

pub fn new<Ip: AsIpAddrs, P: IntoTxtProperties>(

But how do we want to do that without introducing a breaking change?

I see the following option here:

  • add with_interface_id() constructor which we then would use when resolving, so probably making it also pub(crate). Which also makes the interface_id an Option<InterfaceId>.

@keepsimple1

keepsimple1 commented Apr 16, 2025 •

Copy link
Copy Markdown
Owner

But how do we want to do that without introducing a breaking change?

I see the following option here:

  • add with_interface_id() constructor which we then would use when resolving, so probably making it also pub(crate). Which also makes the interface_id an Option<InterfaceId>.

I'm thinking maybe this is a good time for us to move towards using ResolvedService when resolving? Such option could be:

  • define a new variant in ServiceEvent, something like ServiceEvent::ServiceResolvedIntf(ResolvedService)
  • add interface_id into ResolvedService

When a service is resolved, we will send both existing ServiceResolved(ServiceInfo) and the new ServiceResolvedIntf(ResolvedService) to the querier. To obtain the interface_id, the caller has to use ServiceResolvedIntf(ResolvedService) variant.

If the new variant works well, we can deprecate the ServiceResolved(ServiceInfo) over time, and let ServiceInfo only used by the register / publisher.

Of course, this still a breaking change because ServiceEvent is not marked as #[non_exhaustive] yet. But I hope its impact is relatively small.

(Sorry about bringing up more changes, I hope this PR can form a good foundation for future enhancements)

Thoughts?

@keepsimple1

Copy link
Copy Markdown
Owner

InterfaceId being defined as:

struct InterfaceId {
      name: String,
      index: Option<u32>,
}

If you don't object.

No objections. The only thing I was curious is that: should index be Option? I understand that when name is invalid, the OS will not return an index in if-addrs crate. But if name is valid, shouldn't we always have index?

@keepsimple1

Copy link
Copy Markdown
Owner

I think this PR might be too involved. And since the full diff will introduce some breaking changes, I am wondering if we can split it into two steps:

  1. merge in internal changes first, namely only DnsAddress and InterfaceId changes, and later
  2. decide the public API approach and make related changes.

Would you like to use this PR to commit the 1) stuff first? Meantime we can think more about 2) and also do some experiments.

@hrzlgnm

hrzlgnm commented Apr 21, 2025

Copy link
Copy Markdown
Contributor Author

I think this PR might be too involved. And since the full diff will introduce some breaking changes, I am wondering if we can split it into two steps:

1. merge in internal changes first, namely only `DnsAddress` and `InterfaceId` changes,  and later

2. decide the public API approach and make related changes.

Would you like to use this PR to commit the 1) stuff first? Meantime we can think more about 2) and also do some experiments.

Yes sounds reasonable, I'll only touch DnsAddress and InterfaceId with this PR.

This allows for adding an interface scope to resolved IPv6 link local
addresses to resolve keepsimple1#339
@hrzlgnm
hrzlgnm force-pushed the feat/resolved-addresses-with-scope branch from c0873d8 to 775d32d Compare April 21, 2025 10:51
@hrzlgnm hrzlgnm changed the title feat: provide a interface scope with the resolved addreses feat: extend DnsAddress with an InterfaceId Apr 21, 2025
@hrzlgnm
hrzlgnm marked this pull request as ready for review April 21, 2025 10:52
@hrzlgnm
hrzlgnm requested a review from keepsimple1 April 21, 2025 12:53

@keepsimple1 keepsimple1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Only one minor comment about a typo. Thanks!

Comment thread src/dns_parser.rs Outdated
time::SystemTime,
};

/// InterfaceId is used to represent the interface indentifer

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "indentifer" -> "identifier"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 35116be, thanks for reviewing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also fixed some more minor spelling issues with typos-cli in ba742ed

@hrzlgnm
hrzlgnm requested a review from keepsimple1 April 22, 2025 10:18

@keepsimple1 keepsimple1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for updates!

@keepsimple1
keepsimple1 merged commit f6c7e80 into keepsimple1:main Apr 23, 2025
@hrzlgnm
hrzlgnm deleted the feat/resolved-addresses-with-scope branch August 10, 2025 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IPv6 link local addresses of a resolved service are not usable without a scope id

2 participants