2 * This file demonstrates how to use Avahi's core API, this is
3 * the embeddable mDNS stack for embedded applications.
5 * End user applications should *not* use this API and should use
6 * the D-Bus or C APIs, please see
7 * client-browse-services.c and glib-integration.c
9 * I repeat, you probably do *not* want to use this example.
13 This file is part of avahi.
15 avahi is free software; you can redistribute it and/or modify it
16 under the terms of the GNU Lesser General Public License as
17 published by the Free Software Foundation; either version 2.1 of the
18 License, or (at your option) any later version.
20 avahi is distributed in the hope that it will be useful, but WITHOUT
21 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
23 Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with avahi; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
40 #include <avahi-core/core.h>
41 #include <avahi-core/lookup.h>
42 #include <avahi-common/simple-watch.h>
43 #include <avahi-common/malloc.h>
44 #include <avahi-common/error.h>
46 static AvahiSimplePoll *simple_poll = NULL;
47 static AvahiServer *server = NULL;
49 static void resolve_callback(
50 AvahiSServiceResolver *r,
51 AVAHI_GCC_UNUSED AvahiIfIndex interface,
52 AVAHI_GCC_UNUSED AvahiProtocol protocol,
53 AvahiResolverEvent event,
57 const char *host_name,
58 const AvahiAddress *address,
61 AvahiLookupResultFlags flags,
62 AVAHI_GCC_UNUSED void* userdata) {
66 /* Called whenever a service has been resolved successfully or timed out */
69 case AVAHI_RESOLVER_FAILURE:
70 fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_server_errno(server)));
73 case AVAHI_RESOLVER_FOUND: {
74 char a[AVAHI_ADDRESS_STR_MAX], *t;
76 fprintf(stderr, "(Resolver) Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
78 avahi_address_snprint(a, sizeof(a), address);
79 t = avahi_string_list_to_string(txt);
90 avahi_string_list_get_service_cookie(txt),
91 !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
92 !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
93 !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
94 !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
99 avahi_s_service_resolver_free(r);
102 static void browse_callback(
103 AvahiSServiceBrowser *b,
104 AvahiIfIndex interface,
105 AvahiProtocol protocol,
106 AvahiBrowserEvent event,
110 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
113 AvahiServer *s = userdata;
116 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
120 case AVAHI_BROWSER_FAILURE:
122 fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_server_errno(server)));
123 avahi_simple_poll_quit(simple_poll);
126 case AVAHI_BROWSER_NEW:
127 fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
129 /* We ignore the returned resolver object. In the callback
130 function we free it. If the server is terminated before
131 the callback function is called the server will free
132 the resolver for us. */
134 if (!(avahi_s_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, s)))
135 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_server_errno(s)));
139 case AVAHI_BROWSER_REMOVE:
140 fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
143 case AVAHI_BROWSER_ALL_FOR_NOW:
144 case AVAHI_BROWSER_CACHE_EXHAUSTED:
145 fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
150 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
151 AvahiServerConfig config;
152 AvahiSServiceBrowser *sb = NULL;
156 /* Initialize the psuedo-RNG */
159 /* Allocate main loop object */
160 if (!(simple_poll = avahi_simple_poll_new())) {
161 fprintf(stderr, "Failed to create simple poll object.\n");
165 /* Do not publish any local records */
166 avahi_server_config_init(&config);
167 config.publish_hinfo = 0;
168 config.publish_addresses = 0;
169 config.publish_workstation = 0;
170 config.publish_domain = 0;
172 /* Set a unicast DNS server for wide area DNS-SD */
173 avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]);
174 config.n_wide_area_servers = 1;
175 config.enable_wide_area = 1;
177 /* Allocate a new server */
178 server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
180 /* Free the configuration data */
181 avahi_server_config_free(&config);
183 /* Check wether creating the server object succeeded */
185 fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
189 /* Create the service browser */
190 if (!(sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, browse_callback, server))) {
191 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_server_errno(server)));
195 /* Run the main loop */
196 avahi_simple_poll_loop(simple_poll);
204 avahi_s_service_browser_free(sb);
207 avahi_server_free(server);
210 avahi_simple_poll_free(simple_poll);