2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <avahi-client/client.h>
28 #include <avahi-client/lookup.h>
29 #include <avahi-client/publish.h>
31 #include <avahi-common/error.h>
32 #include <avahi-common/simple-watch.h>
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/timeval.h>
36 static const AvahiPoll *poll_api = NULL;
37 static AvahiSimplePoll *simple_poll = NULL;
39 static void avahi_client_callback (AvahiClient *c, AvahiClientState state, void *userdata) {
40 printf ("CLIENT: Callback on %p, state -> %d, data -> %s\n", (void*) c, state, (char*)userdata);
43 static void avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
44 printf ("ENTRY-GROUP: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
47 static void avahi_entry_group2_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
48 printf ("ENTRY-GROUP2: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
51 static void avahi_domain_browser_callback(
52 AvahiDomainBrowser *b,
53 AvahiIfIndex interface,
54 AvahiProtocol protocol,
55 AvahiBrowserEvent event,
57 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
60 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);
63 static void avahi_service_resolver_callback(
64 AvahiServiceResolver *r,
65 AvahiIfIndex interface,
66 AvahiProtocol protocol,
67 AvahiResolverEvent event,
71 const char *host_name,
72 const AvahiAddress *a,
75 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
80 if (event == AVAHI_RESOLVER_FAILURE) {
81 printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
84 avahi_address_snprint (addr, sizeof (addr), a);
85 txtr = avahi_string_list_to_string (txt);
86 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);
90 static void avahi_service_browser_callback (
91 AvahiServiceBrowser *b,
92 AvahiIfIndex interface,
93 AvahiProtocol protocol,
94 AvahiBrowserEvent event,
98 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
101 AvahiServiceResolver *sr;
103 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);
107 sr = avahi_service_resolver_new (avahi_service_browser_get_client (b), interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, avahi_service_resolver_callback, (char*) "xxXXxx");
108 printf("New service resolver %p\n", (void*) sr);
112 static void avahi_service_type_browser_callback (
113 AvahiServiceTypeBrowser *b,
114 AvahiIfIndex interface,
115 AvahiProtocol protocol,
116 AvahiBrowserEvent event,
119 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
122 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);
125 static void avahi_address_resolver_callback (
126 AVAHI_GCC_UNUSED AvahiAddressResolver *r,
127 AvahiIfIndex interface,
128 AvahiProtocol protocol,
129 AvahiResolverEvent event,
130 const AvahiAddress *address,
132 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
136 if (event == AVAHI_RESOLVER_FAILURE) {
137 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
140 avahi_address_snprint (addr, sizeof (addr), address);
141 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);
144 static void avahi_host_name_resolver_callback (
145 AvahiHostNameResolver *r,
146 AvahiIfIndex interface,
147 AvahiProtocol protocol,
148 AvahiResolverEvent event,
150 const AvahiAddress *a,
151 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
155 AvahiAddressResolver *ar;
158 if (event == AVAHI_RESOLVER_FAILURE) {
159 printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
162 client = avahi_host_name_resolver_get_client (r);
163 ar = avahi_address_resolver_new(client, interface, protocol, a, 0, avahi_address_resolver_callback, (char*) "omghai6u");
166 printf ("Succesfully created address resolver object\n");
168 printf ("Failed to create AddressResolver\n");
170 avahi_address_snprint (addr, sizeof (addr), a);
171 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);
173 static void test_free_domain_browser(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
175 AvahiServiceBrowser *b = userdata;
176 printf ("Freeing domain browser\n");
177 avahi_service_browser_free (b);
180 static void test_free_entry_group (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
182 AvahiEntryGroup *g = userdata;
183 printf ("Freeing entry group\n");
184 avahi_entry_group_free (g);
187 static void test_entry_group_reset (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
189 AvahiEntryGroup *g = userdata;
191 printf ("Resetting entry group\n");
192 avahi_entry_group_reset (g);
194 avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar2", NULL);
196 avahi_entry_group_commit (g);
199 static void test_entry_group_update(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata) {
200 AvahiEntryGroup *g = userdata;
202 printf ("Updating entry group\n");
204 avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
207 static void terminate(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) {
209 avahi_simple_poll_quit(simple_poll);
212 int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
214 AvahiEntryGroup *group, *group2;
215 AvahiDomainBrowser *domain;
216 AvahiServiceBrowser *sb;
217 AvahiServiceTypeBrowser *st;
218 AvahiHostNameResolver *hnr;
226 simple_poll = avahi_simple_poll_new();
227 poll_api = avahi_simple_poll_get(simple_poll);
229 if (!(avahi = avahi_client_new(poll_api, 0, avahi_client_callback, (char*) "omghai2u", &error))) {
230 fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
234 printf("State: %i\n", avahi_client_get_state(avahi));
236 ret = avahi_client_get_version_string (avahi);
237 printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
239 ret = avahi_client_get_host_name (avahi);
240 printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
242 ret = avahi_client_get_domain_name (avahi);
243 printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
245 ret = avahi_client_get_host_name_fqdn (avahi);
246 printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
248 cookie = avahi_client_get_local_service_cookie(avahi);
249 printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
251 group = avahi_entry_group_new(avahi, avahi_entry_group_callback, (char*) "omghai");
252 printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
256 printf("Sucessfully created entry group %p\n", (void*) group);
258 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)));
259 printf("add_record: %d\n", avahi_entry_group_add_record (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "TestX", 0x01, 0x10, 120, "\5booya", 6));
261 avahi_entry_group_commit (group);
263 domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, (char*) "omghai3u");
266 printf ("Failed to create domain browser object\n");
268 printf ("Sucessfully created domain browser %p\n", (void*) domain);
270 st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, (char*) "omghai3u");
272 printf ("Failed to create service type browser object\n");
274 printf ("Sucessfully created service type browser %p\n", (void*) st);
276 sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, (char*) "omghai3u");
278 printf ("Failed to create service browser object\n");
280 printf ("Sucessfully created service browser %p\n", (void*) sb);
282 hnr = avahi_host_name_resolver_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "ecstasy.local", AVAHI_PROTO_UNSPEC, 0, avahi_host_name_resolver_callback, (char*) "omghai4u");
284 printf ("Failed to create hostname resolver object\n");
286 printf ("Successfully created hostname resolver object\n");
288 aar = avahi_address_parse ("224.0.0.251", AVAHI_PROTO_UNSPEC, &a);
290 printf ("failed to create address object\n");
292 group2 = avahi_entry_group_new (avahi, avahi_entry_group2_callback, (char*) "omghai222");
293 if ((error = avahi_entry_group_add_address (group2, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "test-mdns.local.", aar)) < 0)
295 printf ("*** failed to add address to entry group: %s\n", avahi_strerror (error));
296 avahi_entry_group_free (group2);
298 printf ("*** success, added address\n");
299 avahi_entry_group_commit (group2);
303 avahi_elapse_time(&tv, 8000, 0);
304 poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
305 avahi_elapse_time(&tv, 15000, 0);
306 poll_api->timeout_new(poll_api, &tv, test_entry_group_update, group);
307 avahi_elapse_time(&tv, 20000, 0);
308 poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
309 avahi_elapse_time(&tv, 25000, 0);
310 poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
312 avahi_elapse_time(&tv, 30000, 0);
313 poll_api->timeout_new(poll_api, &tv, terminate, NULL);
315 avahi_simple_poll_loop(simple_poll);
317 printf("terminating...\n");
322 avahi_client_free (avahi);
325 avahi_simple_poll_free(simple_poll);