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-common/error.h>
31 #include <avahi-common/simple-watch.h>
32 #include <avahi-common/malloc.h>
34 static const AvahiPoll *poll_api = NULL;
35 static AvahiSimplePoll *simple_poll = NULL;
37 static void avahi_client_callback (AvahiClient *c, AvahiClientState state, void *userdata) {
38 printf ("CLIENT: Callback on %p, state -> %d, data -> %s\n", (void*) c, state, (char*)userdata);
41 static void avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
42 printf ("ENTRY-GROUP: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
45 static void avahi_domain_browser_callback(
46 AvahiDomainBrowser *b,
47 AvahiIfIndex interface,
48 AvahiProtocol protocol,
49 AvahiBrowserEvent event,
51 AvahiLookupResultFlags flags,
54 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);
57 static void avahi_service_resolver_callback(
58 AvahiServiceResolver *r,
59 AvahiIfIndex interface,
60 AvahiProtocol protocol,
61 AvahiResolverEvent event,
65 const char *host_name,
66 const AvahiAddress *a,
69 AvahiLookupResultFlags flags,
74 if (event == AVAHI_RESOLVER_FAILURE) {
75 printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
78 avahi_address_snprint (addr, sizeof (addr), a);
79 txtr = avahi_string_list_to_string (txt);
80 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);
84 static void avahi_service_browser_callback (
85 AvahiServiceBrowser *b,
86 AvahiIfIndex interface,
87 AvahiProtocol protocol,
88 AvahiBrowserEvent event,
92 AvahiLookupResultFlags flags,
95 AvahiServiceResolver *sr;
97 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);
101 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");
102 printf("New service resolver %p\n", (void*) sr);
106 static void avahi_service_type_browser_callback (
107 AvahiServiceTypeBrowser *b,
108 AvahiIfIndex interface,
109 AvahiProtocol protocol,
110 AvahiBrowserEvent event,
113 AvahiLookupResultFlags flags,
116 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);
119 static void avahi_address_resolver_callback (
120 AvahiAddressResolver *r,
121 AvahiIfIndex interface,
122 AvahiProtocol protocol,
123 AvahiResolverEvent event,
124 AvahiProtocol aprotocol,
125 const AvahiAddress *address,
127 AvahiLookupResultFlags flags,
131 if (event == AVAHI_RESOLVER_FAILURE) {
132 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
135 avahi_address_snprint (addr, sizeof (addr), address);
136 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, interface (%d), protocol (%d), even (%d), aprotocol (%d), address (%s), name (%s), data(%s)\n", interface, protocol, event, aprotocol, addr, name, (char*) userdata);
139 static void avahi_host_name_resolver_callback (
140 AvahiHostNameResolver *r,
141 AvahiIfIndex interface,
142 AvahiProtocol protocol,
143 AvahiResolverEvent event,
145 const AvahiAddress *a,
146 AvahiLookupResultFlags flags,
150 AvahiAddressResolver *ar;
153 if (event == AVAHI_RESOLVER_FAILURE) {
154 printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
157 client = avahi_host_name_resolver_get_client (r);
158 ar = avahi_address_resolver_new_a (client, interface, protocol, a, 0, avahi_address_resolver_callback, "omghai6u");
161 printf ("Succesfully created address resolver object\n");
163 printf ("Failed to create AddressResolver\n");
165 avahi_address_snprint (addr, sizeof (addr), a);
166 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);
168 static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata)
170 AvahiServiceBrowser *b = userdata;
171 printf ("Freeing domain browser\n");
172 avahi_service_browser_free (b);
175 static void test_free_entry_group (AvahiTimeout *timeout, void* userdata)
177 AvahiEntryGroup *g = userdata;
178 printf ("Freeing entry group\n");
179 avahi_entry_group_free (g);
182 static void test_entry_group_reset (AvahiTimeout *timeout, void* userdata)
184 AvahiEntryGroup *g = userdata;
186 printf ("Resetting entry group\n");
187 avahi_entry_group_reset (g);
189 avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar2", NULL);
191 avahi_entry_group_commit (g);
194 static void terminate(AvahiTimeout *timeout, void *userdata) {
196 avahi_simple_poll_quit(simple_poll);
199 int main (int argc, char *argv[]) {
201 AvahiEntryGroup *group;
202 AvahiDomainBrowser *domain;
203 AvahiServiceBrowser *sb;
204 AvahiServiceTypeBrowser *st;
205 AvahiHostNameResolver *hnr;
211 simple_poll = avahi_simple_poll_new();
212 poll_api = avahi_simple_poll_get(simple_poll);
214 if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) {
215 fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
219 printf("State: %i\n", avahi_client_get_state(avahi));
221 ret = avahi_client_get_version_string (avahi);
222 printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
224 ret = avahi_client_get_host_name (avahi);
225 printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
227 ret = avahi_client_get_domain_name (avahi);
228 printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
230 ret = avahi_client_get_host_name_fqdn (avahi);
231 printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
233 cookie = avahi_client_get_local_service_cookie(avahi);
234 printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
236 group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai");
237 printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
241 printf("Sucessfully created entry group %p\n", (void*) group);
243 avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL);
245 avahi_entry_group_commit (group);
247 domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
250 printf ("Failed to create domain browser object\n");
252 printf ("Sucessfully created domain browser %p\n", (void*) domain);
254 st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
256 printf ("Failed to create service type browser object\n");
258 printf ("Sucessfully created service type browser %p\n", (void*) st);
260 sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
262 printf ("Failed to create service browser object\n");
264 printf ("Sucessfully created service browser %p\n", (void*) sb);
266 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");
268 printf ("Failed to create hostname resolver object\n");
270 printf ("Successfully created hostname resolver object\n");
273 avahi_elapse_time(&tv, 8000, 0);
274 poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
275 avahi_elapse_time(&tv, 20000, 0);
276 poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
277 avahi_elapse_time(&tv, 25000, 0);
278 poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
280 avahi_elapse_time(&tv, 30000, 0);
281 poll_api->timeout_new(poll_api, &tv, terminate, NULL);
284 if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
287 printf("terminating...\n");
292 avahi_client_free (avahi);
295 avahi_simple_poll_free(simple_poll);