]> git.meshlink.io Git - catta/blob - avahi-client/client-test.c
14b9e84d3e37700424ad2fce805c03c0577886f2
[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_domain_browser_callback(
49     AvahiDomainBrowser *b,
50     AvahiIfIndex interface,
51     AvahiProtocol protocol,
52     AvahiBrowserEvent event,
53     const char *domain,
54     AvahiLookupResultFlags flags,
55     void *userdata) {
56     
57     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);
58 }
59
60 static void avahi_service_resolver_callback(
61     AvahiServiceResolver *r,
62     AvahiIfIndex interface,
63     AvahiProtocol protocol,
64     AvahiResolverEvent event,
65     const char *name,
66     const char *type,
67     const char *domain,
68     const char *host_name,
69     const AvahiAddress *a,
70     uint16_t port,
71     AvahiStringList *txt,
72     AvahiLookupResultFlags flags,
73     void *userdata) {
74     
75     char addr[64];
76     char *txtr;
77     if (event == AVAHI_RESOLVER_FAILURE) {
78         printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
79         return;
80     }
81     avahi_address_snprint (addr, sizeof (addr), a);
82     txtr = avahi_string_list_to_string (txt);
83     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     avahi_free(txtr);
85 }
86
87 static void avahi_service_browser_callback (
88     AvahiServiceBrowser *b,
89     AvahiIfIndex interface,
90     AvahiProtocol protocol,
91     AvahiBrowserEvent event,
92     const char *name,
93     const char *type,
94     const char *domain,
95     AvahiLookupResultFlags flags,
96     void *userdata) {
97     
98     AvahiServiceResolver *sr;
99
100     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
102     if (b && name)
103     {
104         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");
105         printf("New service resolver %p\n", (void*) sr);
106     }
107 }
108
109 static void avahi_service_type_browser_callback (
110     AvahiServiceTypeBrowser *b,
111     AvahiIfIndex interface,
112     AvahiProtocol protocol,
113     AvahiBrowserEvent event,
114     const char *type,
115     const char *domain,
116     AvahiLookupResultFlags flags,
117     void *userdata) {
118     
119     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 }
121
122 static void avahi_address_resolver_callback (
123     AvahiAddressResolver *r,
124     AvahiIfIndex interface,
125     AvahiProtocol protocol,
126     AvahiResolverEvent event,
127     AvahiProtocol aprotocol,
128     const AvahiAddress *address,
129     const char *name,
130     AvahiLookupResultFlags flags,
131     void *userdata) {
132     
133     char addr[64];
134     if (event == AVAHI_RESOLVER_FAILURE) {
135         printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
136         return;
137     }
138     avahi_address_snprint (addr, sizeof (addr), address);
139     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);
140 }
141
142 static void avahi_host_name_resolver_callback (
143     AvahiHostNameResolver *r,
144     AvahiIfIndex interface,
145     AvahiProtocol protocol,
146     AvahiResolverEvent event,
147     const char *name,
148     const AvahiAddress *a,
149     AvahiLookupResultFlags flags,
150     void *userdata) {
151     
152     AvahiClient *client;
153     AvahiAddressResolver *ar;
154     char addr[64];
155     
156     if (event == AVAHI_RESOLVER_FAILURE) {
157         printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
158         return;
159     }
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");
162     if (ar)
163     {
164         printf ("Succesfully created address resolver object\n");
165     } else {
166         printf ("Failed to create AddressResolver\n");
167     }
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);
170 }
171 static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata)
172 {
173     AvahiServiceBrowser *b = userdata;
174     printf ("Freeing domain browser\n");
175     avahi_service_browser_free (b);
176 }
177
178 static void test_free_entry_group (AvahiTimeout *timeout, void* userdata)
179 {
180     AvahiEntryGroup *g = userdata;
181     printf ("Freeing entry group\n");
182     avahi_entry_group_free (g);
183 }
184
185 static void test_entry_group_reset (AvahiTimeout *timeout, void* userdata)
186 {
187     AvahiEntryGroup *g = userdata;
188
189     printf ("Resetting entry group\n");
190     avahi_entry_group_reset (g);
191
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);
193
194     avahi_entry_group_commit (g);
195 }
196
197 static void test_entry_group_update(AvahiTimeout *timeout, void* userdata) {
198     AvahiEntryGroup *g = userdata;
199
200     printf ("Updating entry group\n");
201
202     avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
203 }
204
205 static void terminate(AvahiTimeout *timeout, void *userdata) {
206
207     avahi_simple_poll_quit(simple_poll);
208 }
209
210 int main (int argc, char *argv[]) {
211     AvahiClient *avahi;
212     AvahiEntryGroup *group;
213     AvahiDomainBrowser *domain;
214     AvahiServiceBrowser *sb;
215     AvahiServiceTypeBrowser *st;
216     AvahiHostNameResolver *hnr;
217     const char *ret;
218     int error;
219     uint32_t cookie;
220     struct timeval tv;
221
222     simple_poll = avahi_simple_poll_new();
223     poll_api = avahi_simple_poll_get(simple_poll);
224     
225     if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) {
226         fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
227         goto fail;
228     }
229
230     printf("State: %i\n", avahi_client_get_state(avahi));
231
232     ret = avahi_client_get_version_string (avahi);
233     printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
234
235     ret = avahi_client_get_host_name (avahi);
236     printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
237
238     ret = avahi_client_get_domain_name (avahi);
239     printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
240
241     ret = avahi_client_get_host_name_fqdn (avahi);
242     printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
243     
244     cookie = avahi_client_get_local_service_cookie(avahi);
245     printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
246
247     group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai");
248     printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
249
250     assert(group);
251     
252     printf("Sucessfully created entry group %p\n", (void*) group);
253
254     avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL);
255
256     avahi_entry_group_commit (group);
257
258     domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
259     
260     if (domain == NULL)
261         printf ("Failed to create domain browser object\n");
262     else
263         printf ("Sucessfully created domain browser %p\n", (void*) domain);
264
265     st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
266     if (st == NULL)
267         printf ("Failed to create service type browser object\n");
268     else
269         printf ("Sucessfully created service type browser %p\n", (void*) st);
270
271     sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
272     if (sb == NULL)
273         printf ("Failed to create service browser object\n");
274     else
275         printf ("Sucessfully created service browser %p\n", (void*) sb);
276
277     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");
278     if (hnr == NULL)
279         printf ("Failed to create hostname resolver object\n");
280     else
281         printf ("Successfully created hostname resolver object\n");
282
283
284     avahi_elapse_time(&tv, 8000, 0);
285     poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
286     avahi_elapse_time(&tv, 15000, 0);
287     poll_api->timeout_new(poll_api, &tv, test_entry_group_update, group);
288     avahi_elapse_time(&tv, 20000, 0);
289     poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
290     avahi_elapse_time(&tv, 25000, 0);
291     poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
292
293     avahi_elapse_time(&tv, 30000, 0);
294     poll_api->timeout_new(poll_api, &tv, terminate, NULL);
295
296     for (;;)
297         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
298             break;
299
300     printf("terminating...\n");
301     
302 fail:
303
304     if (avahi)
305         avahi_client_free (avahi);
306
307     if (simple_poll)
308         avahi_simple_poll_free(simple_poll);
309
310     return 0;
311 }