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_TIMEOUT)
76 printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
79 avahi_address_snprint (addr, sizeof (addr), a);
80 txtr = avahi_string_list_to_string (txt);
81 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);
85 static void avahi_service_browser_callback (
86 AvahiServiceBrowser *b,
87 AvahiIfIndex interface,
88 AvahiProtocol protocol,
89 AvahiBrowserEvent event,
93 AvahiLookupResultFlags flags,
96 AvahiServiceResolver *sr;
98 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);
102 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");
103 printf("New service resolver %p\n", (void*) sr);
107 static void avahi_service_type_browser_callback (
108 AvahiServiceTypeBrowser *b,
109 AvahiIfIndex interface,
110 AvahiProtocol protocol,
111 AvahiBrowserEvent event,
114 AvahiLookupResultFlags flags,
117 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);
120 static void avahi_address_resolver_callback (
121 AvahiAddressResolver *r,
122 AvahiIfIndex interface,
123 AvahiProtocol protocol,
124 AvahiResolverEvent event,
125 AvahiProtocol aprotocol,
126 const AvahiAddress *address,
128 AvahiLookupResultFlags flags,
132 if (event == AVAHI_RESOLVER_TIMEOUT)
134 printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
137 avahi_address_snprint (addr, sizeof (addr), address);
138 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);
141 static void avahi_host_name_resolver_callback (
142 AvahiHostNameResolver *r,
143 AvahiIfIndex interface,
144 AvahiProtocol protocol,
145 AvahiResolverEvent event,
147 const AvahiAddress *a,
148 AvahiLookupResultFlags flags,
152 AvahiAddressResolver *ar;
155 if (event == AVAHI_RESOLVER_TIMEOUT)
157 printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
160 client = avahi_host_name_resolver_get_client (r);
161 ar = avahi_address_resolver_new_a (client, interface, protocol, a, 0, avahi_address_resolver_callback, "omghai6u");
164 printf ("Succesfully created address resolver object\n");
166 printf ("Failed to create AddressResolver\n");
168 avahi_address_snprint (addr, sizeof (addr), a);
169 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);
171 static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata)
173 AvahiServiceBrowser *b = userdata;
174 printf ("Freeing domain browser\n");
175 avahi_service_browser_free (b);
178 static void test_free_entry_group (AvahiTimeout *timeout, void* userdata)
180 AvahiEntryGroup *g = userdata;
181 printf ("Freeing entry group\n");
182 avahi_entry_group_free (g);
185 static void test_entry_group_reset (AvahiTimeout *timeout, void* userdata)
187 AvahiEntryGroup *g = userdata;
189 printf ("Resetting entry group\n");
190 avahi_entry_group_reset (g);
192 avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar2", NULL);
194 avahi_entry_group_commit (g);
197 static void terminate(AvahiTimeout *timeout, void *userdata) {
199 avahi_simple_poll_quit(simple_poll);
202 int main (int argc, char *argv[]) {
204 AvahiEntryGroup *group;
205 AvahiDomainBrowser *domain;
206 AvahiServiceBrowser *sb;
207 AvahiServiceTypeBrowser *st;
208 AvahiHostNameResolver *hnr;
214 simple_poll = avahi_simple_poll_new();
215 poll_api = avahi_simple_poll_get(simple_poll);
217 if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) {
218 fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
222 printf("State: %i\n", avahi_client_get_state(avahi));
224 ret = avahi_client_get_version_string (avahi);
225 printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
227 ret = avahi_client_get_host_name (avahi);
228 printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
230 ret = avahi_client_get_domain_name (avahi);
231 printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
233 ret = avahi_client_get_host_name_fqdn (avahi);
234 printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
236 cookie = avahi_client_get_local_service_cookie(avahi);
237 printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
239 group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai");
240 printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
244 printf("Sucessfully created entry group %p\n", (void*) group);
246 avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL);
248 avahi_entry_group_commit (group);
250 domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
253 printf ("Failed to create domain browser object\n");
255 printf ("Sucessfully created domain browser %p\n", (void*) domain);
257 st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
259 printf ("Failed to create service type browser object\n");
261 printf ("Sucessfully created service type browser %p\n", (void*) st);
263 sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
265 printf ("Failed to create service browser object\n");
267 printf ("Sucessfully created service browser %p\n", (void*) sb);
269 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");
271 printf ("Failed to create hostname resolver object\n");
273 printf ("Successfully created hostname resolver object\n");
276 avahi_elapse_time(&tv, 8000, 0);
277 poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
278 avahi_elapse_time(&tv, 20000, 0);
279 poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
280 avahi_elapse_time(&tv, 25000, 0);
281 poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
283 avahi_elapse_time(&tv, 30000, 0);
284 poll_api->timeout_new(poll_api, &tv, terminate, NULL);
287 if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
290 printf("terminating...\n");
295 avahi_client_free (avahi);
298 avahi_simple_poll_free(simple_poll);