[avahi] C API: how to get IP address to connect to a service

Phil Vandry vandry at TZoNE.ORG
Thu Oct 7 16:43:37 PDT 2010


Hello,

I have a question about using the Avahi client C API.

I wrote some software that uses Avahi to browse for a service on
the network:

1. avahi_service_browser_new()

2. Call avahi_service_resolver_new() in the callback from that.

In the callback for avahi_service_resolver_new(), I need to obtain
an IP address that is usable to connect to the service. I am given:

- a "raw" address (of type AvahiAddress)
- a host name (a string)
- an interface ID (of type AvahiIfIndex)

I can turn the "raw" address into a string by using
avahi_address_snprint but that's not enough. The address will
frequently be a link-local address of the form
fe80::xxxx:xxff:fexx:xxxx. That type of address is useless without
an interface name attached (e.g. "fe80::xxxx:xxff:fexx:xxxx%eth0").
The problem is how to make sure the interface name is there when
it's needed.

Options I can see are:

- special-case IPv6 addresses in fe80::/10 and append a % and the
interface name. The problem is how to get the interface name. I can
find no documented API in the Avahi client to translate an
AvahiIfIndex to a name.

- special-case IPv6 addresses in fe80::/10 and append a % and the
AvahiIfIndex as a number directly. This seems to work at least
undr Linux. The problem is that AvahiIfIndex is documented only
as "Takes OS dependent values and the special constant AVAHI_IF_UNSPEC".
I'm not so sure this is portable or even guaranteed to work.
Even if this solution is correct I am not thrilled about having
to special-case these addresses in my code, it seems like a layering
violation (my application shouldn't care about the details of
IP addresses)

- Use the host_name, not the address, and count on the operating
system to resolve it using Avahi. The problem is that the operating
system may or may not be set up to use use DNS-SD for hostname
resolution and I don't want to depend on that.

Furthermore, by examining several examples of existing open source 
software, I can find no examples where this problem is addressed.
This leads me to believe that either all of this software is buggy
or else things are supposed to work correctly without having to
special-case anything and my application is not working because I'm
doing something wrong. Obviously I hope the latter is true :-)

-Phil


More information about the avahi mailing list