official_browse_example.cpp
Go to the documentation of this file.
1 /***
2  This file is part of avahi.
3 
4  avahi is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) any later version.
8 
9  avahi is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12  Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with avahi; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  USA.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <stdio.h>
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <time.h>
28 
29 #include <avahi-client/client.h>
30 #include <avahi-client/lookup.h>
31 
32 #include <avahi-common/simple-watch.h>
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/error.h>
35 
36 static AvahiSimplePoll *simple_poll = NULL;
37 
38 static void resolve_callback(
39  AvahiServiceResolver *r,
40  AVAHI_GCC_UNUSED AvahiIfIndex interface,
41  AVAHI_GCC_UNUSED AvahiProtocol protocol,
42  AvahiResolverEvent event,
43  const char *name,
44  const char *type,
45  const char *domain,
46  const char *host_name,
47  const AvahiAddress *address,
48  uint16_t port,
49  AvahiStringList *txt,
50  AvahiLookupResultFlags flags,
51  AVAHI_GCC_UNUSED void* userdata) {
52 
53  assert(r);
54 
55  /* Called whenever a service has been resolved successfully or timed out */
56 
57  switch (event) {
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))));
60  break;
61 
62  case AVAHI_RESOLVER_FOUND: {
63  char a[AVAHI_ADDRESS_STR_MAX], *t;
64 
65  fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
66 
67  avahi_address_snprint(a, sizeof(a), address);
68  t = avahi_string_list_to_string(txt);
69  fprintf(stderr,
70  "\t%s:%u (%s)\n"
71  "\tTXT=%s\n"
72  "\tcookie is %u\n"
73  "\tis_local: %i\n"
74  "\tour_own: %i\n"
75  "\twide_area: %i\n"
76  "\tmulticast: %i\n"
77  "\tcached: %i\n",
78  host_name, port, a,
79  t,
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));
86 
87  avahi_free(t);
88  break;
89  }
90  }
91 
92 // avahi_service_resolver_free(r);
93 }
94 
95 static void browse_callback(
96  AvahiServiceBrowser *b,
97  AvahiIfIndex interface,
98  AvahiProtocol protocol,
99  AvahiBrowserEvent event,
100  const char *name,
101  const char *type,
102  const char *domain,
103  AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
104  void* userdata) {
105 
106  AvahiClient *c = static_cast<AvahiClient *>(userdata);
107  assert(b);
108 
109  /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
110 
111  switch (event) {
112  case AVAHI_BROWSER_FAILURE:
113 
114  fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
115  avahi_simple_poll_quit(simple_poll);
116  return;
117 
118  case AVAHI_BROWSER_NEW:
119  fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
120 
121  /* We ignore the returned resolver object. In the callback
122  function we free it. If the server is terminated before
123  the callback function is called the server will free
124  the resolver for us. */
125 
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)));
128 
129  break;
130 
131  case AVAHI_BROWSER_REMOVE:
132  fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
133  break;
134 
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");
138  break;
139  }
140 }
141 
142 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
143  assert(c);
144 
145  /* Called whenever the client or server state changes */
146 
147  if (state == AVAHI_CLIENT_FAILURE) {
148  fprintf(stderr, "Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
149  avahi_simple_poll_quit(simple_poll);
150  }
151 }
152 
153 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
154  AvahiClient *client = NULL;
155  AvahiServiceBrowser *sb = NULL;
156  int error;
157  int ret = 1;
158 
159  /* Allocate main loop object */
160  if (!(simple_poll = avahi_simple_poll_new())) {
161  fprintf(stderr, "Failed to create simple poll object.\n");
162  goto fail;
163  }
164 
165  /* Allocate a new client */
166  client = avahi_client_new(avahi_simple_poll_get(simple_poll), static_cast<AvahiClientFlags>(0), client_callback, NULL, &error);
167 
168  /* Check wether creating the client object succeeded */
169  if (!client) {
170  fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
171  goto fail;
172  }
173 
174  /* Create the service browser */
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)));
177  goto fail;
178  }
179 
180  /* Run the main loop */
181  avahi_simple_poll_loop(simple_poll);
182 
183  ret = 0;
184 
185 fail:
186 
187  /* Cleanup things */
188  if (sb)
189  avahi_service_browser_free(sb);
190 
191  if (client)
192  avahi_client_free(client);
193 
194  if (simple_poll)
195  avahi_simple_poll_free(simple_poll);
196 
197  return ret;
198 }
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 char * name
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)


zeroconf_avahi_demos
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 15:49:05