]> git.meshlink.io Git - catta/blob - avahi-client/client-test.c
build-sys: make things more quiet
[catta] / avahi-client / client-test.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include <avahi-client/client.h>
30 #include <avahi-client/lookup.h>
31 #include <avahi-client/publish.h>
32
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>
37
38 static const AvahiPoll *poll_api = NULL;
39 static AvahiSimplePoll *simple_poll = NULL;
40
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);
43 }
44
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);
47 }
48
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);
51 }
52
53 static void avahi_domain_browser_callback(
54     AvahiDomainBrowser *b,
55     AvahiIfIndex interface,
56     AvahiProtocol protocol,
57     AvahiBrowserEvent event,
58     const char *domain,
59     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
60     void *userdata) {
61
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);
63 }
64
65 static void avahi_service_resolver_callback(
66     AvahiServiceResolver *r,
67     AvahiIfIndex interface,
68     AvahiProtocol protocol,
69     AvahiResolverEvent event,
70     const char *name,
71     const char *type,
72     const char *domain,
73     const char *host_name,
74     const AvahiAddress *a,
75     uint16_t port,
76     AvahiStringList *txt,
77     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
78     void *userdata) {
79
80     char addr[64];
81     char *txtr;
82     if (event == AVAHI_RESOLVER_FAILURE) {
83         printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
84         return;
85     }
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);
89     avahi_free(txtr);
90 }
91
92 static void avahi_service_browser_callback (
93     AvahiServiceBrowser *b,
94     AvahiIfIndex interface,
95     AvahiProtocol protocol,
96     AvahiBrowserEvent event,
97     const char *name,
98     const char *type,
99     const char *domain,
100     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
101     void *userdata) {
102
103     AvahiServiceResolver *sr;
104
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);
106
107     if (b && name)
108     {
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, (char*) "xxXXxx");
110         printf("New service resolver %p\n", (void*) sr);
111     }
112 }
113
114 static void avahi_service_type_browser_callback (
115     AvahiServiceTypeBrowser *b,
116     AvahiIfIndex interface,
117     AvahiProtocol protocol,
118     AvahiBrowserEvent event,
119     const char *type,
120     const char *domain,
121     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
122     void *userdata) {
123
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);
125 }
126
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,
133     const char *name,
134     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
135     void *userdata) {
136
137     char addr[64];
138     if (event == AVAHI_RESOLVER_FAILURE) {
139         printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
140         return;
141     }
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);
144 }
145
146 static void avahi_host_name_resolver_callback (
147     AvahiHostNameResolver *r,
148     AvahiIfIndex interface,
149     AvahiProtocol protocol,
150     AvahiResolverEvent event,
151     const char *name,
152     const AvahiAddress *a,
153     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
154     void *userdata) {
155
156     AvahiClient *client;
157     AvahiAddressResolver *ar;
158     char addr[64];
159
160     if (event == AVAHI_RESOLVER_FAILURE) {
161         printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
162         return;
163     }
164     client = avahi_host_name_resolver_get_client (r);
165 ar = avahi_address_resolver_new(client, interface, protocol, a, 0, avahi_address_resolver_callback, (char*) "omghai6u");
166     if (ar)
167     {
168         printf ("Succesfully created address resolver object\n");
169     } else {
170         printf ("Failed to create AddressResolver\n");
171     }
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);
174 }
175 static void test_free_domain_browser(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
176 {
177     AvahiServiceBrowser *b = userdata;
178     printf ("Freeing domain browser\n");
179     avahi_service_browser_free (b);
180 }
181
182 static void test_free_entry_group (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
183 {
184     AvahiEntryGroup *g = userdata;
185     printf ("Freeing entry group\n");
186     avahi_entry_group_free (g);
187 }
188
189 static void test_entry_group_reset (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
190 {
191     AvahiEntryGroup *g = userdata;
192
193     printf ("Resetting entry group\n");
194     avahi_entry_group_reset (g);
195
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);
197
198     avahi_entry_group_commit (g);
199 }
200
201 static void test_entry_group_update(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata) {
202     AvahiEntryGroup *g = userdata;
203
204     printf ("Updating entry group\n");
205
206     avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
207 }
208
209 static void terminate(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) {
210
211     avahi_simple_poll_quit(simple_poll);
212 }
213
214 int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
215     AvahiClient *avahi;
216     AvahiEntryGroup *group, *group2;
217     AvahiDomainBrowser *domain;
218     AvahiServiceBrowser *sb;
219     AvahiServiceTypeBrowser *st;
220     AvahiHostNameResolver *hnr;
221     AvahiAddress *aar;
222     const char *ret;
223     int error;
224     uint32_t cookie;
225     struct timeval tv;
226     AvahiAddress a;
227
228     simple_poll = avahi_simple_poll_new();
229     poll_api = avahi_simple_poll_get(simple_poll);
230
231     if (!(avahi = avahi_client_new(poll_api, 0, avahi_client_callback, (char*) "omghai2u", &error))) {
232         fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
233         goto fail;
234     }
235
236     printf("State: %i\n", avahi_client_get_state(avahi));
237
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)));
240
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)));
243
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)));
246
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)));
249
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)));
252
253     group = avahi_entry_group_new(avahi, avahi_entry_group_callback, (char*) "omghai");
254     printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
255
256     assert(group);
257
258     printf("Sucessfully created entry group %p\n", (void*) group);
259
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));
262
263     avahi_entry_group_commit (group);
264
265     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
267     if (domain == NULL)
268         printf ("Failed to create domain browser object\n");
269     else
270         printf ("Sucessfully created domain browser %p\n", (void*) domain);
271
272     st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, (char*) "omghai3u");
273     if (st == NULL)
274         printf ("Failed to create service type browser object\n");
275     else
276         printf ("Sucessfully created service type browser %p\n", (void*) st);
277
278     sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, (char*) "omghai3u");
279     if (sb == NULL)
280         printf ("Failed to create service browser object\n");
281     else
282         printf ("Sucessfully created service browser %p\n", (void*) sb);
283
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, (char*) "omghai4u");
285     if (hnr == NULL)
286         printf ("Failed to create hostname resolver object\n");
287     else
288         printf ("Successfully created hostname resolver object\n");
289
290     aar = avahi_address_parse ("224.0.0.251", AVAHI_PROTO_UNSPEC, &a);
291     if (aar == NULL) {
292         printf ("failed to create address object\n");
293     } else {
294         group2 = avahi_entry_group_new (avahi, avahi_entry_group2_callback, (char*) "omghai222");
295         if ((error = avahi_entry_group_add_address (group2, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "test-mdns.local.", aar)) < 0)
296         {
297             printf ("*** failed to add address to entry group: %s\n", avahi_strerror (error));
298             avahi_entry_group_free (group2);
299         } else {
300             printf ("*** success, added address\n");
301             avahi_entry_group_commit (group2);
302         }
303     }
304
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);
313
314     avahi_elapse_time(&tv, 30000, 0);
315     poll_api->timeout_new(poll_api, &tv, terminate, NULL);
316
317     avahi_simple_poll_loop(simple_poll);
318
319     printf("terminating...\n");
320
321 fail:
322
323     if (avahi)
324         avahi_client_free (avahi);
325
326     if (simple_poll)
327         avahi_simple_poll_free(simple_poll);
328
329     return 0;
330 }