]> git.meshlink.io Git - catta/blob - avahi-client/client-test.c
* Add custom user-specific configure flags to bootstrap.sh
[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
37 static const AvahiPoll *poll_api = NULL;
38 static AvahiSimplePoll *simple_poll = NULL;
39
40 static void avahi_client_callback (AvahiClient *c, AvahiClientState state, void *userdata) {
41     printf ("CLIENT: Callback on %p, state -> %d, data -> %s\n", (void*) c, state, (char*)userdata);
42 }
43
44 static void avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
45     printf ("ENTRY-GROUP: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
46 }
47
48 static void avahi_entry_group2_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
49     printf ("ENTRY-GROUP2: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
50 }
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     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, (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     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     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, type, domain, (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, "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     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, domain, (char*)userdata);
125 }
126
127 static void avahi_address_resolver_callback (
128     AvahiAddressResolver *r,
129     AvahiIfIndex interface,
130     AvahiProtocol protocol,
131     AvahiResolverEvent event,
132     AvahiProtocol aprotocol,
133     const AvahiAddress *address,
134     const char *name,
135     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), aprotocol (%d), address (%s), name (%s), data(%s)\n", interface, protocol, event, aprotocol, 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     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_a (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(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 (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 (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(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(AvahiTimeout *timeout, void *userdata) {
211
212     avahi_simple_poll_quit(simple_poll);
213 }
214
215 int main (int argc, 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
262     avahi_entry_group_commit (group);
263
264     domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
265     
266     if (domain == NULL)
267         printf ("Failed to create domain browser object\n");
268     else
269         printf ("Sucessfully created domain browser %p\n", (void*) domain);
270
271     st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
272     if (st == NULL)
273         printf ("Failed to create service type browser object\n");
274     else
275         printf ("Sucessfully created service type browser %p\n", (void*) st);
276
277     sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
278     if (sb == NULL)
279         printf ("Failed to create service browser object\n");
280     else
281         printf ("Sucessfully created service browser %p\n", (void*) sb);
282
283     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");
284     if (hnr == NULL)
285         printf ("Failed to create hostname resolver object\n");
286     else
287         printf ("Successfully created hostname resolver object\n");
288
289     aar = avahi_address_parse ("224.0.0.251", AF_UNSPEC, aar);
290     if (aar == NULL) {
291         printf ("failed to create address object\n");
292     } else {
293         group2 = avahi_entry_group_new (avahi, avahi_entry_group2_callback, "omghai222");
294         if ((error = avahi_entry_group_add_address (group2, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "test-mdns.local.", aar)) < 0)
295         {
296             printf ("*** failed to add address to entry group: %s\n", avahi_strerror (ret));
297             avahi_entry_group_free (group2);
298         } else {
299             printf ("*** success, added address\n");
300             avahi_entry_group_commit (group2);
301         }
302         
303     }
304
305
306     avahi_elapse_time(&tv, 8000, 0);
307     poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
308     avahi_elapse_time(&tv, 15000, 0);
309     poll_api->timeout_new(poll_api, &tv, test_entry_group_update, group);
310     avahi_elapse_time(&tv, 20000, 0);
311     poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
312     avahi_elapse_time(&tv, 25000, 0);
313     poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
314
315     avahi_elapse_time(&tv, 30000, 0);
316     poll_api->timeout_new(poll_api, &tv, terminate, NULL);
317
318     for (;;)
319         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
320             break;
321
322     printf("terminating...\n");
323     
324 fail:
325
326     if (avahi)
327         avahi_client_free (avahi);
328
329     if (simple_poll)
330         avahi_simple_poll_free(simple_poll);
331
332     return 0;
333 }