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
29 #include <avahi-client/client.h>
30 #include <avahi-client/lookup.h>
31 #include <avahi-client/publish.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/timeval.h>
38 static const AvahiPoll *poll_api = NULL;
39 static AvahiSimplePoll *simple_poll = NULL;
41 static void avahi_client_callback (AvahiClient *c, AvahiClientState state, void *userdata) {
42 printf ("CLIENT: Callback on %p, state -> %d, data -> %s\n", (void*) c, state, (char*)userdata);
45 static void avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
46 printf ("ENTRY-GROUP: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
49 static void avahi_entry_group2_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
50 printf ("ENTRY-GROUP2: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
54 static void avahi_domain_browser_callback(
55 AvahiDomainBrowser *b,
56 AvahiIfIndex interface,
57 AvahiProtocol protocol,
58 AvahiBrowserEvent event,
60 AvahiLookupResultFlags flags,
63 printf ("DOMAIN-BROWSER: Callback on %p, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", (void*) b, interface, protocol, event, domain, (char*)userdata);
66 static void avahi_service_resolver_callback(
67 AvahiServiceResolver *r,
68 AvahiIfIndex interface,
69 AvahiProtocol protocol,
70 AvahiResolverEvent event,
74 const char *host_name,
75 const AvahiAddress *a,
78 AvahiLookupResultFlags flags,
83 if (event == AVAHI_RESOLVER_FAILURE) {
84 printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
87 avahi_address_snprint (addr, sizeof (addr), a);
88 txtr = avahi_string_list_to_string (txt);
89 printf ("SERVICE-RESOLVER: Callback on ServiceResolver, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), host_name (%s), address (%s), port (%d), txtdata (%s), data(%s)\n", interface, protocol, event, name, type, domain, host_name, addr, port, txtr, (char*)userdata);
93 static void avahi_service_browser_callback (
94 AvahiServiceBrowser *b,
95 AvahiIfIndex interface,
96 AvahiProtocol protocol,
97 AvahiBrowserEvent event,
101 AvahiLookupResultFlags flags,
104 AvahiServiceResolver *sr;
106 printf ("SERVICE-BROWSER: Callback on %p, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), data (%s)\n", (void*) b, interface, protocol, event, name, type, domain, (char*)userdata);
110 sr = avahi_service_resolver_new (avahi_service_browser_get_client (b), interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, avahi_service_resolver_callback, "xxXXxx");
111 printf("New service resolver %p\n", (void*) sr);
115 static void avahi_service_type_browser_callback (
116 AvahiServiceTypeBrowser *b,
117 AvahiIfIndex interface,
118 AvahiProtocol protocol,
119 AvahiBrowserEvent event,
122 AvahiLookupResultFlags flags,
125 printf ("SERVICE-TYPE-BROWSER: Callback on %p, interface (%d), protocol (%d), event (%d), type (%s), domain (%s), data (%s)\n", (void*) b, interface, protocol, event, type, domain, (char*)userdata);
128 static void avahi_address_resolver_callback (
129 AvahiAddressResolver *r,
130 AvahiIfIndex interface,
131 AvahiProtocol protocol,
132 AvahiResolverEvent event,
133 const AvahiAddress *address,
135 AvahiLookupResultFlags flags,
139 if (event == AVAHI_RESOLVER_FAILURE) {
140 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
143 avahi_address_snprint (addr, sizeof (addr), address);
144 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, interface (%d), protocol (%d), even (%d), address (%s), name (%s), data(%s)\n", interface, protocol, event, addr, name, (char*) userdata);
147 static void avahi_host_name_resolver_callback (
148 AvahiHostNameResolver *r,
149 AvahiIfIndex interface,
150 AvahiProtocol protocol,
151 AvahiResolverEvent event,
153 const AvahiAddress *a,
154 AvahiLookupResultFlags flags,
158 AvahiAddressResolver *ar;
161 if (event == AVAHI_RESOLVER_FAILURE) {
162 printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
165 client = avahi_host_name_resolver_get_client (r);
166 ar = avahi_address_resolver_new(client, interface, protocol, a, 0, avahi_address_resolver_callback, "omghai6u");
169 printf ("Succesfully created address resolver object\n");
171 printf ("Failed to create AddressResolver\n");
173 avahi_address_snprint (addr, sizeof (addr), a);
174 printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, interface (%d), protocol (%d), event (%d), name (%s), address (%s), data (%s)\n", interface, protocol, event, name, addr, (char*)userdata);
176 static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata)
178 AvahiServiceBrowser *b = userdata;
179 printf ("Freeing domain browser\n");
180 avahi_service_browser_free (b);
183 static void test_free_entry_group (AvahiTimeout *timeout, void* userdata)
185 AvahiEntryGroup *g = userdata;
186 printf ("Freeing entry group\n");
187 avahi_entry_group_free (g);
190 static void test_entry_group_reset (AvahiTimeout *timeout, void* userdata)
192 AvahiEntryGroup *g = userdata;
194 printf ("Resetting entry group\n");
195 avahi_entry_group_reset (g);
197 avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar2", NULL);
199 avahi_entry_group_commit (g);
202 static void test_entry_group_update(AvahiTimeout *timeout, void* userdata) {
203 AvahiEntryGroup *g = userdata;
205 printf ("Updating entry group\n");
207 avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
210 static void terminate(AvahiTimeout *timeout, void *userdata) {
212 avahi_simple_poll_quit(simple_poll);
215 int main (int argc, char *argv[]) {
217 AvahiEntryGroup *group, *group2;
218 AvahiDomainBrowser *domain;
219 AvahiServiceBrowser *sb;
220 AvahiServiceTypeBrowser *st;
221 AvahiHostNameResolver *hnr;
228 simple_poll = avahi_simple_poll_new();
229 poll_api = avahi_simple_poll_get(simple_poll);
231 if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) {
232 fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
236 printf("State: %i\n", avahi_client_get_state(avahi));
238 ret = avahi_client_get_version_string (avahi);
239 printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
241 ret = avahi_client_get_host_name (avahi);
242 printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
244 ret = avahi_client_get_domain_name (avahi);
245 printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
247 ret = avahi_client_get_host_name_fqdn (avahi);
248 printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
250 cookie = avahi_client_get_local_service_cookie(avahi);
251 printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
253 group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai");
254 printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
258 printf("Sucessfully created entry group %p\n", (void*) group);
260 avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL);
262 avahi_entry_group_commit (group);
264 domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
267 printf ("Failed to create domain browser object\n");
269 printf ("Sucessfully created domain browser %p\n", (void*) domain);
271 st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
273 printf ("Failed to create service type browser object\n");
275 printf ("Sucessfully created service type browser %p\n", (void*) st);
277 sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
279 printf ("Failed to create service browser object\n");
281 printf ("Sucessfully created service browser %p\n", (void*) sb);
283 hnr = avahi_host_name_resolver_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "ecstasy.local", AVAHI_PROTO_UNSPEC, 0, avahi_host_name_resolver_callback, "omghai4u");
285 printf ("Failed to create hostname resolver object\n");
287 printf ("Successfully created hostname resolver object\n");
289 aar = avahi_address_parse ("224.0.0.251", AF_UNSPEC, aar);
291 printf ("failed to create address object\n");
293 group2 = avahi_entry_group_new (avahi, avahi_entry_group2_callback, "omghai222");
294 if ((error = avahi_entry_group_add_address (group2, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "test-mdns.local.", aar)) < 0)
296 printf ("*** failed to add address to entry group: %s\n", avahi_strerror (error));
297 avahi_entry_group_free (group2);
299 printf ("*** success, added address\n");
300 avahi_entry_group_commit (group2);
306 avahi_elapse_time(&tv, 8000, 0);
307 poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
308 avahi_elapse_time(&tv, 15000, 0);
309 poll_api->timeout_new(poll_api, &tv, test_entry_group_update, group);
310 avahi_elapse_time(&tv, 20000, 0);
311 poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
312 avahi_elapse_time(&tv, 25000, 0);
313 poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
315 avahi_elapse_time(&tv, 30000, 0);
316 poll_api->timeout_new(poll_api, &tv, terminate, NULL);
319 if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
322 printf("terminating...\n");
327 avahi_client_free (avahi);
330 avahi_simple_poll_free(simple_poll);