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);
53 static void avahi_domain_browser_callback(
54 AvahiDomainBrowser *b,
55 AvahiIfIndex interface,
56 AvahiProtocol protocol,
57 AvahiBrowserEvent event,
59 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
62 printf ("DOMAIN-BROWSER: Callback on %p, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", (void*) b, interface, protocol, event, domain ? domain : "NULL", (char*)userdata);
65 static void avahi_service_resolver_callback(
66 AvahiServiceResolver *r,
67 AvahiIfIndex interface,
68 AvahiProtocol protocol,
69 AvahiResolverEvent event,
73 const char *host_name,
74 const AvahiAddress *a,
77 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
82 if (event == AVAHI_RESOLVER_FAILURE) {
83 printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
86 avahi_address_snprint (addr, sizeof (addr), a);
87 txtr = avahi_string_list_to_string (txt);
88 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);
92 static void avahi_service_browser_callback (
93 AvahiServiceBrowser *b,
94 AvahiIfIndex interface,
95 AvahiProtocol protocol,
96 AvahiBrowserEvent event,
100 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
103 AvahiServiceResolver *sr;
105 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 ? name : "NULL", type, domain ? domain : "NULL", (char*)userdata);
109 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");
110 printf("New service resolver %p\n", (void*) sr);
114 static void avahi_service_type_browser_callback (
115 AvahiServiceTypeBrowser *b,
116 AvahiIfIndex interface,
117 AvahiProtocol protocol,
118 AvahiBrowserEvent event,
121 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
124 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 ? type : "NULL", domain ? domain : "NULL", (char*)userdata);
127 static void avahi_address_resolver_callback (
128 AVAHI_GCC_UNUSED AvahiAddressResolver *r,
129 AvahiIfIndex interface,
130 AvahiProtocol protocol,
131 AvahiResolverEvent event,
132 const AvahiAddress *address,
134 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
138 if (event == AVAHI_RESOLVER_FAILURE) {
139 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
142 avahi_address_snprint (addr, sizeof (addr), address);
143 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);
146 static void avahi_host_name_resolver_callback (
147 AvahiHostNameResolver *r,
148 AvahiIfIndex interface,
149 AvahiProtocol protocol,
150 AvahiResolverEvent event,
152 const AvahiAddress *a,
153 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
157 AvahiAddressResolver *ar;
160 if (event == AVAHI_RESOLVER_FAILURE) {
161 printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
164 client = avahi_host_name_resolver_get_client (r);
165 ar = avahi_address_resolver_new(client, interface, protocol, a, 0, avahi_address_resolver_callback, "omghai6u");
168 printf ("Succesfully created address resolver object\n");
170 printf ("Failed to create AddressResolver\n");
172 avahi_address_snprint (addr, sizeof (addr), a);
173 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);
175 static void test_free_domain_browser(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
177 AvahiServiceBrowser *b = userdata;
178 printf ("Freeing domain browser\n");
179 avahi_service_browser_free (b);
182 static void test_free_entry_group (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
184 AvahiEntryGroup *g = userdata;
185 printf ("Freeing entry group\n");
186 avahi_entry_group_free (g);
189 static void test_entry_group_reset (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
191 AvahiEntryGroup *g = userdata;
193 printf ("Resetting entry group\n");
194 avahi_entry_group_reset (g);
196 avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar2", NULL);
198 avahi_entry_group_commit (g);
201 static void test_entry_group_update(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata) {
202 AvahiEntryGroup *g = userdata;
204 printf ("Updating entry group\n");
206 avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
209 static void terminate(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) {
211 avahi_simple_poll_quit(simple_poll);
214 int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
216 AvahiEntryGroup *group, *group2;
217 AvahiDomainBrowser *domain;
218 AvahiServiceBrowser *sb;
219 AvahiServiceTypeBrowser *st;
220 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, 0, 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 printf("%s\n", avahi_strerror(avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL)));
261 printf("add_record: %d\n", avahi_entry_group_add_record (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "TestX", 0x01, 0x10, 120, "\5booya", 6));
263 avahi_entry_group_commit (group);
265 domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
268 printf ("Failed to create domain browser object\n");
270 printf ("Sucessfully created domain browser %p\n", (void*) domain);
272 st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
274 printf ("Failed to create service type browser object\n");
276 printf ("Sucessfully created service type browser %p\n", (void*) st);
278 sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
280 printf ("Failed to create service browser object\n");
282 printf ("Sucessfully created service browser %p\n", (void*) sb);
284 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");
286 printf ("Failed to create hostname resolver object\n");
288 printf ("Successfully created hostname resolver object\n");
290 aar = avahi_address_parse ("224.0.0.251", AVAHI_PROTO_UNSPEC, &a);
292 printf ("failed to create address object\n");
294 group2 = avahi_entry_group_new (avahi, avahi_entry_group2_callback, "omghai222");
295 if ((error = avahi_entry_group_add_address (group2, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "test-mdns.local.", aar)) < 0)
297 printf ("*** failed to add address to entry group: %s\n", avahi_strerror (error));
298 avahi_entry_group_free (group2);
300 printf ("*** success, added address\n");
301 avahi_entry_group_commit (group2);
305 avahi_elapse_time(&tv, 8000, 0);
306 poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
307 avahi_elapse_time(&tv, 15000, 0);
308 poll_api->timeout_new(poll_api, &tv, test_entry_group_update, group);
309 avahi_elapse_time(&tv, 20000, 0);
310 poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
311 avahi_elapse_time(&tv, 25000, 0);
312 poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
314 avahi_elapse_time(&tv, 30000, 0);
315 poll_api->timeout_new(poll_api, &tv, terminate, NULL);
317 avahi_simple_poll_loop(simple_poll);
319 printf("terminating...\n");
324 avahi_client_free (avahi);
327 avahi_simple_poll_free(simple_poll);