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;
49 static void resolve_callback(
50 AvahiSServiceResolver *r,
51 AvahiIfIndex interface,
52 AvahiProtocol protocol,
53 AvahiResolverEvent event,
57 const char *host_name,
58 const AvahiAddress *address,
65 /* Called whenever a service has been resolved successfully or timed out */
67 if (event == AVAHI_RESOLVER_TIMEOUT)
68 fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s'.\n", name, type, domain);
72 assert(event == AVAHI_RESOLVER_FOUND);
74 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
76 avahi_address_snprint(a, sizeof(a), address);
77 t = avahi_string_list_to_string(txt);
78 fprintf(stderr, "\t%s:%u (%s) TXT=%s\n", host_name, port, a, t);
82 avahi_s_service_resolver_free(r);
85 static void browse_callback(
86 AvahiSServiceBrowser *b,
87 AvahiIfIndex interface,
88 AvahiProtocol protocol,
89 AvahiBrowserEvent event,
95 AvahiServer *s = userdata;
98 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
100 fprintf(stderr, "%s: service '%s' of type '%s' in domain '%s'\n",
101 event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED",
106 /* If it's new, let's resolve it */
107 if (event == AVAHI_BROWSER_NEW)
109 /* We ignore the returned resolver object. In the callback function
110 we free it. If the server is terminated before the callback
111 function is called the server will free the resolver for us. */
113 if (!(avahi_s_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s)))
114 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_server_errno(s)));
117 int main(int argc, char*argv[]) {
118 AvahiServerConfig config;
119 AvahiServer *server = NULL;
120 AvahiSServiceBrowser *sb;
124 /* Initialize the psuedo-RNG */
127 /* Allocate main loop object */
128 if (!(simple_poll = avahi_simple_poll_new())) {
129 fprintf(stderr, "Failed to create simple poll object.\n");
133 /* Do not publish any local records */
134 avahi_server_config_init(&config);
135 config.publish_hinfo = 0;
136 config.publish_addresses = 0;
137 config.publish_workstation = 0;
138 config.publish_domain = 0;
140 /* Allocate a new server */
141 server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
143 /* Free the configuration data */
144 avahi_server_config_free(&config);
146 /* Check wether creating the server object succeeded */
148 fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
152 /* Create the service browser */
153 if (!(sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server))) {
154 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_server_errno(server)));
158 /* Run the main loop */
160 if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
169 avahi_s_service_browser_free(sb);
172 avahi_server_free(server);
175 avahi_simple_poll_free(simple_poll);