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