]> git.meshlink.io Git - catta/blob - avahi-client/client-test.c
802584a2bf905210dcaee22687828bdb2e02dce9
[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
54 static void avahi_domain_browser_callback(
55     AvahiDomainBrowser *b,
56     AvahiIfIndex interface,
57     AvahiProtocol protocol,
58     AvahiBrowserEvent event,
59     const char *domain,
60     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
61     void *userdata) {
62     
63     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);
64 }
65
66 static void avahi_service_resolver_callback(
67     AvahiServiceResolver *r,
68     AvahiIfIndex interface,
69     AvahiProtocol protocol,
70     AvahiResolverEvent event,
71     const char *name,
72     const char *type,
73     const char *domain,
74     const char *host_name,
75     const AvahiAddress *a,
76     uint16_t port,
77     AvahiStringList *txt,
78     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
79     void *userdata) {
80     
81     char addr[64];
82     char *txtr;
83     if (event == AVAHI_RESOLVER_FAILURE) {
84         printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
85         return;
86     }
87     avahi_address_snprint (addr, sizeof (addr), a);
88     txtr = avahi_string_list_to_string (txt);
89     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     avahi_free(txtr);
91 }
92
93 static void avahi_service_browser_callback (
94     AvahiServiceBrowser *b,
95     AvahiIfIndex interface,
96     AvahiProtocol protocol,
97     AvahiBrowserEvent event,
98     const char *name,
99     const char *type,
100     const char *domain,
101     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
102     void *userdata) {
103     
104     AvahiServiceResolver *sr;
105
106     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);
107
108     if (b && name)
109     {
110         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");
111         printf("New service resolver %p\n", (void*) sr);
112     }
113 }
114
115 static void avahi_service_type_browser_callback (
116     AvahiServiceTypeBrowser *b,
117     AvahiIfIndex interface,
118     AvahiProtocol protocol,
119     AvahiBrowserEvent event,
120     const char *type,
121     const char *domain,
122     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
123     void *userdata) {
124     
125     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);
126 }
127
128 static void avahi_address_resolver_callback (
129     AVAHI_GCC_UNUSED AvahiAddressResolver *r,
130     AvahiIfIndex interface,
131     AvahiProtocol protocol,
132     AvahiResolverEvent event,
133     const AvahiAddress *address,
134     const char *name,
135     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
136     void *userdata) {
137     
138     char addr[64];
139     if (event == AVAHI_RESOLVER_FAILURE) {
140         printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
141         return;
142     }
143     avahi_address_snprint (addr, sizeof (addr), address);
144     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);
145 }
146
147 static void avahi_host_name_resolver_callback (
148     AvahiHostNameResolver *r,
149     AvahiIfIndex interface,
150     AvahiProtocol protocol,
151     AvahiResolverEvent event,
152     const char *name,
153     const AvahiAddress *a,
154     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
155     void *userdata) {
156     
157     AvahiClient *client;
158     AvahiAddressResolver *ar;
159     char addr[64];
160     
161     if (event == AVAHI_RESOLVER_FAILURE) {
162         printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
163         return;
164     }
165     client = avahi_host_name_resolver_get_client (r);
166     ar = avahi_address_resolver_new(client, interface, protocol, a, 0, avahi_address_resolver_callback, "omghai6u");
167     if (ar)
168     {
169         printf ("Succesfully created address resolver object\n");
170     } else {
171         printf ("Failed to create AddressResolver\n");
172     }
173     avahi_address_snprint (addr, sizeof (addr), a);
174     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 }
176 static void test_free_domain_browser(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
177 {
178     AvahiServiceBrowser *b = userdata;
179     printf ("Freeing domain browser\n");
180     avahi_service_browser_free (b);
181 }
182
183 static void test_free_entry_group (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
184 {
185     AvahiEntryGroup *g = userdata;
186     printf ("Freeing entry group\n");
187     avahi_entry_group_free (g);
188 }
189
190 static void test_entry_group_reset (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
191 {
192     AvahiEntryGroup *g = userdata;
193
194     printf ("Resetting entry group\n");
195     avahi_entry_group_reset (g);
196
197     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
199     avahi_entry_group_commit (g);
200 }
201
202 static void test_entry_group_update(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata) {
203     AvahiEntryGroup *g = userdata;
204
205     printf ("Updating entry group\n");
206
207     avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
208 }
209
210 static void terminate(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) {
211
212     avahi_simple_poll_quit(simple_poll);
213 }
214
215 int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
216     AvahiClient *avahi;
217     AvahiEntryGroup *group, *group2;
218     AvahiDomainBrowser *domain;
219     AvahiServiceBrowser *sb;
220     AvahiServiceTypeBrowser *st;
221     AvahiHostNameResolver *hnr;
222     AvahiAddress *aar;
223     const char *ret;
224     int error;
225     uint32_t cookie;
226     struct timeval tv;
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, avahi_client_callback, "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, "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     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", 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, "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, "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, "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, "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, aar);
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, "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     for (;;)
318         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
319             break;
320
321     printf("terminating...\n");
322     
323 fail:
324
325     if (avahi)
326         avahi_client_free (avahi);
327
328     if (simple_poll)
329         avahi_simple_poll_free(simple_poll);
330
331     return 0;
332 }