29 #include <avahi-client/client.h> 30 #include <avahi-client/lookup.h> 32 #include <avahi-common/simple-watch.h> 33 #include <avahi-common/malloc.h> 34 #include <avahi-common/error.h> 39 AvahiServiceResolver *r,
40 AVAHI_GCC_UNUSED AvahiIfIndex interface,
41 AVAHI_GCC_UNUSED AvahiProtocol protocol,
42 AvahiResolverEvent event,
46 const char *host_name,
47 const AvahiAddress *address,
50 AvahiLookupResultFlags flags,
51 AVAHI_GCC_UNUSED
void* userdata) {
58 case AVAHI_RESOLVER_FAILURE:
59 fprintf(stderr,
"(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
62 case AVAHI_RESOLVER_FOUND: {
63 char a[AVAHI_ADDRESS_STR_MAX], *t;
65 fprintf(stderr,
"Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
67 avahi_address_snprint(a,
sizeof(a), address);
68 t = avahi_string_list_to_string(txt);
80 avahi_string_list_get_service_cookie(txt),
81 !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
82 !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
83 !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
84 !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
85 !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
96 AvahiServiceBrowser *b,
97 AvahiIfIndex interface,
98 AvahiProtocol protocol,
99 AvahiBrowserEvent event,
103 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
106 AvahiClient *c =
static_cast<AvahiClient *
>(userdata);
112 case AVAHI_BROWSER_FAILURE:
114 fprintf(stderr,
"(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
118 case AVAHI_BROWSER_NEW:
119 fprintf(stderr,
"(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
126 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, static_cast<AvahiLookupFlags>(0),
resolve_callback, c)))
127 fprintf(stderr,
"Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
131 case AVAHI_BROWSER_REMOVE:
132 fprintf(stderr,
"(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
135 case AVAHI_BROWSER_ALL_FOR_NOW:
136 case AVAHI_BROWSER_CACHE_EXHAUSTED:
137 fprintf(stderr,
"(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ?
"CACHE_EXHAUSTED" :
"ALL_FOR_NOW");
142 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED
void * userdata) {
147 if (state == AVAHI_CLIENT_FAILURE) {
148 fprintf(stderr,
"Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
153 int main(AVAHI_GCC_UNUSED
int argc, AVAHI_GCC_UNUSED
char*argv[]) {
154 AvahiClient *client = NULL;
155 AvahiServiceBrowser *sb = NULL;
161 fprintf(stderr,
"Failed to create simple poll object.\n");
166 client = avahi_client_new(avahi_simple_poll_get(
simple_poll), static_cast<AvahiClientFlags>(0),
client_callback, NULL, &error);
170 fprintf(stderr,
"Failed to create client: %s\n", avahi_strerror(error));
175 if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
"_ros-master._tcp", NULL, static_cast<AvahiLookupFlags>(0),
browse_callback, client))) {
176 fprintf(stderr,
"Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
189 avahi_service_browser_free(sb);
192 avahi_client_free(client);
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[])
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void *userdata)
static AvahiSimplePoll * simple_poll
static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void *userdata)
static void resolve_callback(AvahiServiceResolver *r, AVAHI_GCC_UNUSED AvahiIfIndex interface, AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, AVAHI_GCC_UNUSED void *userdata)