]> git.meshlink.io Git - catta/blob - avahi-client/client-test.c
* make all flags parameters UINT32 when marshalling for DBUS
[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_TIMEOUT)
75     {
76         printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
77         return;
78     }
79     avahi_address_snprint (addr, sizeof (addr), a);
80     txtr = avahi_string_list_to_string (txt);
81     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);
82     avahi_free(txtr);
83 }
84
85 static void avahi_service_browser_callback (
86     AvahiServiceBrowser *b,
87     AvahiIfIndex interface,
88     AvahiProtocol protocol,
89     AvahiBrowserEvent event,
90     const char *name,
91     const char *type,
92     const char *domain,
93     AvahiLookupResultFlags flags,
94     void *userdata) {
95     
96     AvahiServiceResolver *sr;
97
98     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);
99
100     if (b && name)
101     {
102         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");
103         printf("New service resolver %p\n", (void*) sr);
104     }
105 }
106
107 static void avahi_service_type_browser_callback (
108     AvahiServiceTypeBrowser *b,
109     AvahiIfIndex interface,
110     AvahiProtocol protocol,
111     AvahiBrowserEvent event,
112     const char *type,
113     const char *domain,
114     AvahiLookupResultFlags flags,
115     void *userdata) {
116     
117     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);
118 }
119
120 static void avahi_address_resolver_callback (
121     AvahiAddressResolver *r,
122     AvahiIfIndex interface,
123     AvahiProtocol protocol,
124     AvahiResolverEvent event,
125     AvahiProtocol aprotocol,
126     const AvahiAddress *address,
127     const char *name,
128     AvahiLookupResultFlags flags,
129     void *userdata) {
130     
131     char addr[64];
132     if (event == AVAHI_RESOLVER_TIMEOUT)
133     {
134         printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
135         return;
136     }
137     avahi_address_snprint (addr, sizeof (addr), address);
138     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);
139 }
140
141 static void avahi_host_name_resolver_callback (
142     AvahiHostNameResolver *r,
143     AvahiIfIndex interface,
144     AvahiProtocol protocol,
145     AvahiResolverEvent event,
146     const char *name,
147     const AvahiAddress *a,
148     AvahiLookupResultFlags flags,
149     void *userdata) {
150     
151     AvahiClient *client;
152     AvahiAddressResolver *ar;
153     char addr[64];
154     
155     if (event == AVAHI_RESOLVER_TIMEOUT)
156     {
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 terminate(AvahiTimeout *timeout, void *userdata) {
198
199     avahi_simple_poll_quit(simple_poll);
200 }
201
202 int main (int argc, char *argv[]) {
203     AvahiClient *avahi;
204     AvahiEntryGroup *group;
205     AvahiDomainBrowser *domain;
206     AvahiServiceBrowser *sb;
207     AvahiServiceTypeBrowser *st;
208     AvahiHostNameResolver *hnr;
209     const char *ret;
210     int error;
211     uint32_t cookie;
212     struct timeval tv;
213
214     simple_poll = avahi_simple_poll_new();
215     poll_api = avahi_simple_poll_get(simple_poll);
216     
217     if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) {
218         fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
219         goto fail;
220     }
221
222     printf("State: %i\n", avahi_client_get_state(avahi));
223
224     ret = avahi_client_get_version_string (avahi);
225     printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
226
227     ret = avahi_client_get_host_name (avahi);
228     printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
229
230     ret = avahi_client_get_domain_name (avahi);
231     printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
232
233     ret = avahi_client_get_host_name_fqdn (avahi);
234     printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
235     
236     cookie = avahi_client_get_local_service_cookie(avahi);
237     printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
238
239     group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai");
240     printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
241
242     assert(group);
243     
244     printf("Sucessfully created entry group %p\n", (void*) group);
245
246     avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL);
247
248     avahi_entry_group_commit (group);
249
250     domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, "omghai3u");
251     
252     if (domain == NULL)
253         printf ("Failed to create domain browser object\n");
254     else
255         printf ("Sucessfully created domain browser %p\n", (void*) domain);
256
257     st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, "omghai3u");
258     if (st == NULL)
259         printf ("Failed to create service type browser object\n");
260     else
261         printf ("Sucessfully created service type browser %p\n", (void*) st);
262
263     sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, "omghai3u");
264     if (sb == NULL)
265         printf ("Failed to create service browser object\n");
266     else
267         printf ("Sucessfully created service browser %p\n", (void*) sb);
268
269     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");
270     if (hnr == NULL)
271         printf ("Failed to create hostname resolver object\n");
272     else
273         printf ("Successfully created hostname resolver object\n");
274
275
276     avahi_elapse_time(&tv, 8000, 0);
277     poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
278     avahi_elapse_time(&tv, 20000, 0);
279     poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
280     avahi_elapse_time(&tv, 25000, 0);
281     poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
282
283     avahi_elapse_time(&tv, 30000, 0);
284     poll_api->timeout_new(poll_api, &tv, terminate, NULL);
285
286     for (;;)
287         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
288             break;
289
290     printf("terminating...\n");
291     
292 fail:
293
294     if (avahi)
295         avahi_client_free (avahi);
296
297     if (simple_poll)
298         avahi_simple_poll_free(simple_poll);
299
300     return 0;
301 }