4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <avahi-client/client.h>
32 #include <avahi-client/lookup.h>
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/error.h>
38 static AvahiSimplePoll *simple_poll = NULL;
40 static void resolve_callback(
41 AvahiServiceResolver *r,
42 AvahiIfIndex interface,
43 AvahiProtocol protocol,
44 AvahiResolverEvent event,
48 const char *host_name,
49 const AvahiAddress *address,
52 AvahiLookupResultFlags flags,
57 /* Called whenever a service has been resolved successfully or timed out */
60 case AVAHI_RESOLVER_FAILURE:
61 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))));
64 case AVAHI_RESOLVER_FOUND: {
65 char a[AVAHI_ADDRESS_STR_MAX], *t;
67 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
69 avahi_address_snprint(a, sizeof(a), address);
70 t = avahi_string_list_to_string(txt);
82 avahi_string_list_get_service_cookie(txt),
83 !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
84 !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
85 !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
86 !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
87 !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
93 avahi_service_resolver_free(r);
96 static void browse_callback(
97 AvahiServiceBrowser *b,
98 AvahiIfIndex interface,
99 AvahiProtocol protocol,
100 AvahiBrowserEvent event,
104 AvahiLookupResultFlags flags,
107 AvahiClient *c = userdata;
110 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
113 case AVAHI_BROWSER_FAILURE:
115 fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
116 avahi_simple_poll_quit(simple_poll);
119 case AVAHI_BROWSER_NEW:
120 fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
122 /* We ignore the returned resolver object. In the callback
123 function we free it. If the server is terminated before
124 the callback function is called the server will free
125 the resolver for us. */
127 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
128 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
132 case AVAHI_BROWSER_REMOVE:
133 fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
136 case AVAHI_BROWSER_ALL_FOR_NOW:
137 case AVAHI_BROWSER_CACHE_EXHAUSTED:
138 fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
144 static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
147 /* Called whenever the client or server state changes */
149 if (state == AVAHI_CLIENT_DISCONNECTED) {
150 fprintf(stderr, "Server connection terminated.\n");
151 avahi_simple_poll_quit(simple_poll);
155 int main(int argc, char*argv[]) {
156 AvahiClient *client = NULL;
157 AvahiServiceBrowser *sb = NULL;
161 /* Allocate main loop object */
162 if (!(simple_poll = avahi_simple_poll_new())) {
163 fprintf(stderr, "Failed to create simple poll object.\n");
167 /* Allocate a new client */
168 client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error);
170 /* Check wether creating the client object succeeded */
172 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
176 /* Create the service browser */
177 if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, browse_callback, client))) {
178 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
182 /* Run the main loop */
183 avahi_simple_poll_loop(simple_poll);
191 avahi_service_browser_free(sb);
194 avahi_client_free(client);
197 avahi_simple_poll_free(simple_poll);