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-common/simple-watch.h>
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/error.h>
36 static AvahiSimplePoll *simple_poll = NULL;
38 static void resolve_callback(
39 AvahiServiceResolver *r,
40 AvahiIfIndex interface,
41 AvahiProtocol protocol,
42 AvahiResolverEvent event,
46 const char *host_name,
47 const AvahiAddress *address,
54 /* Called whenever a service has been resolved successfully or timed out */
56 if (event == AVAHI_RESOLVER_TIMEOUT)
57 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s'.\n", name, type, domain);
61 assert(event == AVAHI_RESOLVER_FOUND);
63 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
65 avahi_address_snprint(a, sizeof(a), address);
66 t = avahi_string_list_to_string(txt);
67 fprintf(stderr, "\t%s:%u (%s) TXT=%s\n", host_name, port, a, t);
71 avahi_service_resolver_free(r);
74 static void browse_callback(
75 AvahiServiceBrowser *b,
76 AvahiIfIndex interface,
77 AvahiProtocol protocol,
78 AvahiBrowserEvent event,
84 AvahiClient *c = userdata;
87 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
89 fprintf(stderr, "%s: service '%s' of type '%s' in domain '%s'\n",
90 event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED",
95 /* If it's new, let's resolve it */
96 if (event == AVAHI_BROWSER_NEW)
98 /* We ignore the returned resolver object. In the callback function
99 we free it. If the server is terminated before the callback
100 function is called the server will free the resolver for us. */
102 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, c)))
103 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
106 static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
109 /* Called whenever the client or server state changes */
111 if (state == AVAHI_CLIENT_DISCONNECTED) {
112 fprintf(stderr, "Server connection terminated.\n");
113 avahi_simple_poll_quit(simple_poll);
117 int main(int argc, char*argv[]) {
118 AvahiClient *client = NULL;
119 AvahiServiceBrowser *sb;
123 /* Allocate main loop object */
124 if (!(simple_poll = avahi_simple_poll_new())) {
125 fprintf(stderr, "Failed to create simple poll object.\n");
129 /* Allocate a new client */
130 client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error);
132 /* Check wether creating the client object succeeded */
134 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
138 /* Create the service browser */
139 if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, client))) {
140 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
144 /* Run the main loop */
146 if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
155 avahi_service_browser_free(sb);
158 avahi_client_free(client);
161 avahi_simple_poll_free(simple_poll);