Skip to content

Commit

Permalink
Set zone for link-local IP6 addr. Use numerical format (i.e., %3, not…
Browse files Browse the repository at this point in the history
… %eth0)

Documentation updated as well, to usage of IP addresses
  • Loading branch information
alexpevzner committed Sep 8, 2024
1 parent ae9f3ea commit fe52ff4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion addressresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func addressResolverCallback(
clnt := resolver.clnt

// Generate an event
ip := decodeAvahiAddress(caddr)
ip := decodeAvahiAddress(IfIndex(ifidx), caddr)
evnt := &AddressResolverEvent{
Event: ResolverEvent(event),
IfIdx: IfIndex(ifidx),
Expand Down
18 changes: 18 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,24 @@ If this flag is in use, the following changes will occur:
default [ProtocolIP6], to be consistent with other Avahi API
(see section "IP4 vs IP6" for details).
# IP addresses
This package uniformly uses [netip.Addr] to represent addresses. Unlike
[net.Addr], this format is compact, convenient and comparable.
When addresses are received from Avahi (for example, as a part of
[ServiceResolverEvent]), the following rules apply:
- IPv4 addresses are represented as 4-byte netip.Addr, not as
16-byte IP6-mapped IP4 addresses.
- Link-local IPv6 addresses come with zone. For zone, numerical,
not symbolic, format is used (i.e., fe80::1ff:fe23:4567:890a%3,
not fe80::1ff:fe23:4567:890a%eth2)
When address is sent from application to Avahi, the following
rules apply:
- Both genuine IP4 and IP6-mapped IP4 addresses are equally accepted
- For IP6 addresses, zone is ignored
# The Poller
[Poller] is the helper object that allows to simplify the event loop
Expand Down
7 changes: 6 additions & 1 deletion glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package avahi

import (
"net/netip"
"strconv"
"unsafe"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func makeAvahiAddress(addr netip.Addr) (C.AvahiAddress, error) {
}

// decodeAvahiAddress decodes C.AvahiAddress
func decodeAvahiAddress(caddr *C.AvahiAddress) netip.Addr {
func decodeAvahiAddress(ifindex IfIndex, caddr *C.AvahiAddress) netip.Addr {
var ip netip.Addr

switch {
Expand All @@ -52,6 +53,10 @@ func decodeAvahiAddress(caddr *C.AvahiAddress) netip.Addr {
ip = netip.AddrFrom16(*(*[16]byte)(unsafe.Pointer(&caddr.data)))
}

if ip.Is6() && ip.IsLinkLocalUnicast() {
ip = ip.WithZone(strconv.Itoa(int(ifindex)))
}

return ip
}

Expand Down
2 changes: 1 addition & 1 deletion hostnameresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func hostnameResolverCallback(
resolver := (*cgo.Handle)(p).Value().(*HostNameResolver)

// Generate an event
ip := decodeAvahiAddress(caddr)
ip := decodeAvahiAddress(IfIndex(ifidx), caddr)
evnt := &HostNameResolverEvent{
Event: ResolverEvent(event),
IfIdx: IfIndex(ifidx),
Expand Down
2 changes: 1 addition & 1 deletion serviceresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func serviceResolverCallback(
clnt := resolver.clnt

// Decode IP address:port
ip := decodeAvahiAddress(caddr)
ip := decodeAvahiAddress(IfIndex(ifidx), caddr)

// Decode TXT record
txt := decodeAvahiStringList(ctxt)
Expand Down

0 comments on commit fe52ff4

Please sign in to comment.