4 * This file demonstrates how to use Avahi's core API, this is
5 * the embeddable mDNS stack for embedded applications.
7 * End user applications should *not* use this API and should use
8 * the DBUS or C APIs, please see
9 * client-browse-services.c and glib-integration.c
11 * I repeat, you probably do *not* want to use this example.
15 This file is part of avahi.
17 avahi is free software; you can redistribute it and/or modify it
18 under the terms of the GNU Lesser General Public License as
19 published by the Free Software Foundation; either version 2.1 of the
20 License, or (at your option) any later version.
22 avahi is distributed in the hope that it will be useful, but WITHOUT
23 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
25 Public License for more details.
27 You should have received a copy of the GNU Lesser General Public
28 License along with avahi; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
42 #include <avahi-core/core.h>
43 #include <avahi-common/simple-watch.h>
44 #include <avahi-common/malloc.h>
45 #include <avahi-common/error.h>
47 static AvahiSimplePoll *simple_poll = NULL;
48 static AvahiServer *server = NULL;
50 static void resolve_callback(
51 AvahiSServiceResolver *r,
52 AvahiIfIndex interface,
53 AvahiProtocol protocol,
54 AvahiResolverEvent event,
58 const char *host_name,
59 const AvahiAddress *address,
66 /* Called whenever a service has been resolved successfully or timed out */
68 if (event == AVAHI_RESOLVER_TIMEOUT)
69 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s'.\n", name, type, domain);
73 assert(event == AVAHI_RESOLVER_FOUND);
75 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
77 avahi_address_snprint(a, sizeof(a), address);
78 t = avahi_string_list_to_string(txt);
86 avahi_string_list_get_service_cookie(txt),
87 avahi_server_is_service_local(server, interface, protocol, name, type, domain));
91 avahi_s_service_resolver_free(r);
94 static void browse_callback(
95 AvahiSServiceBrowser *b,
96 AvahiIfIndex interface,
97 AvahiProtocol protocol,
98 AvahiBrowserEvent event,
104 AvahiServer *s = userdata;
107 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
109 fprintf(stderr, "%s: service '%s' of type '%s' in domain '%s'\n",
110 event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED",
115 /* If it's new, let's resolve it */
116 if (event == AVAHI_BROWSER_NEW)
118 /* We ignore the returned resolver object. In the callback function
119 we free it. If the server is terminated before the callback
120 function is called the server will free the resolver for us. */
122 if (!(avahi_s_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s)))
123 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_server_errno(s)));
126 int main(int argc, char*argv[]) {
127 AvahiServerConfig config;
128 AvahiSServiceBrowser *sb;
132 /* Initialize the psuedo-RNG */
135 /* Allocate main loop object */
136 if (!(simple_poll = avahi_simple_poll_new())) {
137 fprintf(stderr, "Failed to create simple poll object.\n");
141 /* Do not publish any local records */
142 avahi_server_config_init(&config);
143 config.publish_hinfo = 0;
144 config.publish_addresses = 0;
145 config.publish_workstation = 0;
146 config.publish_domain = 0;
148 /* Allocate a new server */
149 server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
151 /* Free the configuration data */
152 avahi_server_config_free(&config);
154 /* Check wether creating the server object succeeded */
156 fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
160 /* Create the service browser */
161 if (!(sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server))) {
162 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_server_errno(server)));
166 /* Run the main loop */
168 if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
177 avahi_s_service_browser_free(sb);
180 avahi_server_free(server);
183 avahi_simple_poll_free(simple_poll);