1/***
2  This file is part of avahi.
3
4  avahi is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) any later version.
8
9  avahi is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12  Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public
15  License along with avahi; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  USA.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <stdio.h>
25#include <assert.h>
26
27#include <avahi-client/client.h>
28#include <avahi-client/lookup.h>
29#include <avahi-client/publish.h>
30
31#include <avahi-common/error.h>
32#include <avahi-common/simple-watch.h>
33#include <avahi-common/malloc.h>
34#include <avahi-common/timeval.h>
35
36static const AvahiPoll *poll_api = NULL;
37static AvahiSimplePoll *simple_poll = NULL;
38
39static void avahi_client_callback (AvahiClient *c, AvahiClientState state, void *userdata) {
40    printf ("CLIENT: Callback on %p, state -> %d, data -> %s\n", (void*) c, state, (char*)userdata);
41}
42
43static void avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
44    printf ("ENTRY-GROUP: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
45}
46
47static void avahi_entry_group2_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
48    printf ("ENTRY-GROUP2: Callback on %p, state -> %d, data -> %s\n", (void*) g, state, (char*)userdata);
49}
50
51static void avahi_domain_browser_callback(
52    AvahiDomainBrowser *b,
53    AvahiIfIndex interface,
54    AvahiProtocol protocol,
55    AvahiBrowserEvent event,
56    const char *domain,
57    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
58    void *userdata) {
59
60    printf ("DOMAIN-BROWSER: Callback on %p, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", (void*) b, interface, protocol, event, domain ? domain : "NULL", (char*)userdata);
61}
62
63static void avahi_service_resolver_callback(
64    AvahiServiceResolver *r,
65    AvahiIfIndex interface,
66    AvahiProtocol protocol,
67    AvahiResolverEvent event,
68    const char *name,
69    const char *type,
70    const char *domain,
71    const char *host_name,
72    const AvahiAddress *a,
73    uint16_t port,
74    AvahiStringList *txt,
75    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
76    void *userdata) {
77
78    char addr[64];
79    char *txtr;
80    if (event == AVAHI_RESOLVER_FAILURE) {
81        printf ("SERVICE-RESOLVER: ServiceResolver %p timed out (%s %s)\n", (void*) r, name, type);
82        return;
83    }
84    avahi_address_snprint (addr, sizeof (addr), a);
85    txtr = avahi_string_list_to_string (txt);
86    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);
87    avahi_free(txtr);
88}
89
90static void avahi_service_browser_callback (
91    AvahiServiceBrowser *b,
92    AvahiIfIndex interface,
93    AvahiProtocol protocol,
94    AvahiBrowserEvent event,
95    const char *name,
96    const char *type,
97    const char *domain,
98    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
99    void *userdata) {
100
101    AvahiServiceResolver *sr;
102
103    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 ? name : "NULL", type, domain ? domain : "NULL", (char*)userdata);
104
105    if (b && name)
106    {
107        sr = avahi_service_resolver_new (avahi_service_browser_get_client (b), interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, avahi_service_resolver_callback, (char*) "xxXXxx");
108        printf("New service resolver %p\n", (void*) sr);
109    }
110}
111
112static void avahi_service_type_browser_callback (
113    AvahiServiceTypeBrowser *b,
114    AvahiIfIndex interface,
115    AvahiProtocol protocol,
116    AvahiBrowserEvent event,
117    const char *type,
118    const char *domain,
119    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
120    void *userdata) {
121
122    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 ? type : "NULL", domain ? domain : "NULL", (char*)userdata);
123}
124
125static void avahi_address_resolver_callback (
126    AVAHI_GCC_UNUSED AvahiAddressResolver *r,
127    AvahiIfIndex interface,
128    AvahiProtocol protocol,
129    AvahiResolverEvent event,
130    const AvahiAddress *address,
131    const char *name,
132    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
133    void *userdata) {
134
135    char addr[64];
136    if (event == AVAHI_RESOLVER_FAILURE) {
137        printf ("ADDRESS-RESOLVER: Callback on AddressResolver, timed out.\n");
138        return;
139    }
140    avahi_address_snprint (addr, sizeof (addr), address);
141    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);
142}
143
144static void avahi_host_name_resolver_callback (
145    AvahiHostNameResolver *r,
146    AvahiIfIndex interface,
147    AvahiProtocol protocol,
148    AvahiResolverEvent event,
149    const char *name,
150    const AvahiAddress *a,
151    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
152    void *userdata) {
153
154    AvahiClient *client;
155    AvahiAddressResolver *ar;
156    char addr[64];
157
158    if (event == AVAHI_RESOLVER_FAILURE) {
159        printf ("HOST-NAME-RESOLVER: Callback on HostNameResolver, timed out.\n");
160        return;
161    }
162    client = avahi_host_name_resolver_get_client (r);
163ar = avahi_address_resolver_new(client, interface, protocol, a, 0, avahi_address_resolver_callback, (char*) "omghai6u");
164    if (ar)
165    {
166        printf ("Succesfully created address resolver object\n");
167    } else {
168        printf ("Failed to create AddressResolver\n");
169    }
170    avahi_address_snprint (addr, sizeof (addr), a);
171    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);
172}
173static void test_free_domain_browser(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
174{
175    AvahiServiceBrowser *b = userdata;
176    printf ("Freeing domain browser\n");
177    avahi_service_browser_free (b);
178}
179
180static void test_free_entry_group (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
181{
182    AvahiEntryGroup *g = userdata;
183    printf ("Freeing entry group\n");
184    avahi_entry_group_free (g);
185}
186
187static void test_entry_group_reset (AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata)
188{
189    AvahiEntryGroup *g = userdata;
190
191    printf ("Resetting entry group\n");
192    avahi_entry_group_reset (g);
193
194    avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar2", NULL);
195
196    avahi_entry_group_commit (g);
197}
198
199static void test_entry_group_update(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata) {
200    AvahiEntryGroup *g = userdata;
201
202    printf ("Updating entry group\n");
203
204    avahi_entry_group_update_service_txt(g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, "foo=bar3", NULL);
205}
206
207static void terminate(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) {
208
209    avahi_simple_poll_quit(simple_poll);
210}
211
212int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
213    AvahiClient *avahi;
214    AvahiEntryGroup *group, *group2;
215    AvahiDomainBrowser *domain;
216    AvahiServiceBrowser *sb;
217    AvahiServiceTypeBrowser *st;
218    AvahiHostNameResolver *hnr;
219    AvahiAddress *aar;
220    const char *ret;
221    int error;
222    uint32_t cookie;
223    struct timeval tv;
224    AvahiAddress a;
225
226    simple_poll = avahi_simple_poll_new();
227    poll_api = avahi_simple_poll_get(simple_poll);
228
229    if (!(avahi = avahi_client_new(poll_api, 0, avahi_client_callback, (char*) "omghai2u", &error))) {
230        fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
231        goto fail;
232    }
233
234    printf("State: %i\n", avahi_client_get_state(avahi));
235
236    ret = avahi_client_get_version_string (avahi);
237    printf("Avahi Server Version: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
238
239    ret = avahi_client_get_host_name (avahi);
240    printf("Host Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
241
242    ret = avahi_client_get_domain_name (avahi);
243    printf("Domain Name: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
244
245    ret = avahi_client_get_host_name_fqdn (avahi);
246    printf("FQDN: %s (Error Return: %s)\n", ret, ret ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
247
248    cookie = avahi_client_get_local_service_cookie(avahi);
249    printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi)));
250
251    group = avahi_entry_group_new(avahi, avahi_entry_group_callback, (char*) "omghai");
252    printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi)));
253
254    assert(group);
255
256    printf("Sucessfully created entry group %p\n", (void*) group);
257
258    printf("%s\n", avahi_strerror(avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL)));
259    printf("add_record: %d\n", avahi_entry_group_add_record (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "TestX", 0x01, 0x10, 120, "\5booya", 6));
260
261    avahi_entry_group_commit (group);
262
263    domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, avahi_domain_browser_callback, (char*) "omghai3u");
264
265    if (domain == NULL)
266        printf ("Failed to create domain browser object\n");
267    else
268        printf ("Sucessfully created domain browser %p\n", (void*) domain);
269
270    st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, avahi_service_type_browser_callback, (char*) "omghai3u");
271    if (st == NULL)
272        printf ("Failed to create service type browser object\n");
273    else
274        printf ("Sucessfully created service type browser %p\n", (void*) st);
275
276    sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, avahi_service_browser_callback, (char*) "omghai3u");
277    if (sb == NULL)
278        printf ("Failed to create service browser object\n");
279    else
280        printf ("Sucessfully created service browser %p\n", (void*) sb);
281
282    hnr = avahi_host_name_resolver_new (avahi, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "ecstasy.local", AVAHI_PROTO_UNSPEC, 0, avahi_host_name_resolver_callback, (char*) "omghai4u");
283    if (hnr == NULL)
284        printf ("Failed to create hostname resolver object\n");
285    else
286        printf ("Successfully created hostname resolver object\n");
287
288    aar = avahi_address_parse ("224.0.0.251", AVAHI_PROTO_UNSPEC, &a);
289    if (aar == NULL) {
290        printf ("failed to create address object\n");
291    } else {
292        group2 = avahi_entry_group_new (avahi, avahi_entry_group2_callback, (char*) "omghai222");
293        if ((error = avahi_entry_group_add_address (group2, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "test-mdns.local.", aar)) < 0)
294        {
295            printf ("*** failed to add address to entry group: %s\n", avahi_strerror (error));
296            avahi_entry_group_free (group2);
297        } else {
298            printf ("*** success, added address\n");
299            avahi_entry_group_commit (group2);
300        }
301    }
302
303    avahi_elapse_time(&tv, 8000, 0);
304    poll_api->timeout_new(poll_api, &tv, test_entry_group_reset, group);
305    avahi_elapse_time(&tv, 15000, 0);
306    poll_api->timeout_new(poll_api, &tv, test_entry_group_update, group);
307    avahi_elapse_time(&tv, 20000, 0);
308    poll_api->timeout_new(poll_api, &tv, test_free_entry_group, group);
309    avahi_elapse_time(&tv, 25000, 0);
310    poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb);
311
312    avahi_elapse_time(&tv, 30000, 0);
313    poll_api->timeout_new(poll_api, &tv, terminate, NULL);
314
315    avahi_simple_poll_loop(simple_poll);
316
317    printf("terminating...\n");
318
319fail:
320
321    if (avahi)
322        avahi_client_free (avahi);
323
324    if (simple_poll)
325        avahi_simple_poll_free(simple_poll);
326
327    return 0;
328}
329