]> git.meshlink.io Git - catta/blob - avahi-core/avahi-test.c
* do no longer include timeval.h in watch.h by default
[catta] / avahi-core / avahi-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 <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/alternative.h>
36 #include <avahi-common/timeval.h>
37
38 #include <avahi-core/core.h>
39 #include <avahi-core/log.h>
40 #include <avahi-core/publish.h>
41 #include <avahi-core/lookup.h>
42
43 static AvahiSEntryGroup *group = NULL;
44 static AvahiServer *server = NULL;
45 static char *service_name = NULL;
46
47 static const AvahiPoll *poll_api;
48
49 static void quit_timeout_callback(AvahiTimeout *timeout, void* userdata) {
50     AvahiSimplePoll *simple_poll = userdata;
51
52     avahi_simple_poll_quit(simple_poll);
53 }
54
55 static void dump_line(const char *text, void* userdata) {
56     printf("%s\n", text);
57 }
58
59 static void dump_timeout_callback(AvahiTimeout *timeout, void* userdata) {
60     struct timeval tv;
61     
62     AvahiServer *avahi = userdata;
63     avahi_server_dump(avahi, dump_line, NULL);
64
65     avahi_elapse_time(&tv, 5000, 0);
66     poll_api->timeout_update(timeout, &tv);
67 }
68
69 static const char *browser_event_to_string(AvahiBrowserEvent event) {
70     switch (event) {
71         case AVAHI_BROWSER_NEW : return "NEW";
72         case AVAHI_BROWSER_REMOVE : return "REMOVE";
73         case AVAHI_BROWSER_CACHE_EXHAUSTED : return "CACHE_EXHAUSTED";
74         case AVAHI_BROWSER_ALL_FOR_NOW : return "ALL_FOR_NOW";
75         case AVAHI_BROWSER_FAILURE : return "FAILURE";
76     }
77
78     abort();
79 }
80
81 static const char *resolver_event_to_string(AvahiResolverEvent event) {
82     switch (event) {
83         case AVAHI_RESOLVER_FOUND: return "FOUND";
84         case AVAHI_RESOLVER_FAILURE: return "FAILURE";
85     }
86     abort();
87 }
88
89 static void record_browser_callback(
90     AvahiSRecordBrowser *r,
91     AvahiIfIndex interface,
92     AvahiProtocol protocol,
93     AvahiBrowserEvent event,
94     AvahiRecord *record,
95     AvahiLookupResultFlags flags,
96     void* userdata) {
97     char *t;
98     
99     assert(r);
100
101     if (record) {
102         avahi_log_debug("RB: record [%s] on %i.%i is %s", t = avahi_record_to_string(record), interface, protocol, browser_event_to_string(event));
103         avahi_free(t);
104     } else
105         avahi_log_debug("RB: [%s]", browser_event_to_string(event));
106
107 }
108
109 static void remove_entries(void);
110 static void create_entries(int new_name);
111
112 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
113     avahi_log_debug("entry group state: %i", state); 
114
115     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
116         remove_entries();
117         create_entries(1);
118         avahi_log_debug("Service name conflict, retrying with <%s>", service_name);
119     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
120         avahi_log_debug("Service established under name <%s>", service_name);
121     }
122 }
123
124 static void server_callback(AvahiServer *s, AvahiServerState state, void* userdata) {
125
126      avahi_log_debug("server state: %i", state); 
127     
128     if (state == AVAHI_SERVER_RUNNING) {
129         avahi_log_debug("Server startup complete. Host name is <%s>. Service cookie is %u", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
130         create_entries(0);
131     } else if (state == AVAHI_SERVER_COLLISION) {
132         char *n;
133         remove_entries();
134
135         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
136
137         avahi_log_debug("Host name conflict, retrying with <%s>", n);
138         avahi_server_set_host_name(s, n);
139         avahi_free(n);
140     }
141 }
142
143 static void remove_entries(void) {
144     if (group)
145         avahi_s_entry_group_reset(group);
146 }
147
148 static void create_entries(int new_name) {
149     AvahiAddress a;
150
151     remove_entries();
152
153     if (!group) 
154         group = avahi_s_entry_group_new(server, entry_group_callback, NULL);
155
156     assert(avahi_s_entry_group_is_empty(group));
157     
158     if (!service_name)
159         service_name = avahi_strdup("Test Service");
160     else if (new_name) {
161         char *n = avahi_alternative_service_name(service_name);
162         avahi_free(service_name);
163         service_name = n;
164     }
165     
166     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service_name, "_http._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
167         avahi_log_error("Failed to add HTTP service");
168         goto fail;
169     }
170
171     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service_name, "_ftp._tcp", NULL, NULL, 21, "foo", NULL) < 0) {
172         avahi_log_error("Failed to add FTP service");
173         goto fail;
174     }
175
176     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,service_name, "_webdav._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
177         avahi_log_error("Failed to add WEBDAV service");
178         goto fail;
179     }
180
181     if (avahi_server_add_dns_server_address(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &a), 53) < 0) {
182         avahi_log_error("Failed to add new DNS Server address");
183         goto fail;
184     }
185
186     avahi_s_entry_group_commit(group);
187     return;
188
189 fail:
190     if (group)
191         avahi_s_entry_group_free(group);
192
193     group = NULL;
194 }
195
196 static void hnr_callback(
197     AvahiSHostNameResolver *r,
198     AvahiIfIndex iface,
199     AvahiProtocol protocol,
200     AvahiResolverEvent event,
201     const char *hostname,
202     const AvahiAddress *a,
203     AvahiLookupResultFlags flags,
204     void* userdata) {
205     char t[AVAHI_ADDRESS_STR_MAX];
206
207     if (a)
208         avahi_address_snprint(t, sizeof(t), a);
209
210     avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", resolver_event_to_string(event));
211 }
212
213 static void ar_callback(
214     AvahiSAddressResolver *r,
215     AvahiIfIndex iface,
216     AvahiProtocol protocol,
217     AvahiResolverEvent event,
218     const AvahiAddress *a,
219     const char *hostname,
220     AvahiLookupResultFlags flags,
221     void* userdata) {
222     char t[AVAHI_ADDRESS_STR_MAX];
223
224     avahi_address_snprint(t, sizeof(t), a);
225
226     avahi_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", resolver_event_to_string(event));
227 }
228
229 static void db_callback(
230     AvahiSDomainBrowser *b,
231     AvahiIfIndex iface,
232     AvahiProtocol protocol,
233     AvahiBrowserEvent event,
234     const char *domain,
235     AvahiLookupResultFlags flags,
236     void* userdata) {
237
238     avahi_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, browser_event_to_string(event));
239 }
240
241 static void stb_callback(
242     AvahiSServiceTypeBrowser *b,
243     AvahiIfIndex iface,
244     AvahiProtocol protocol,
245     AvahiBrowserEvent event,
246     const char *service_type,
247     const char *domain,
248     AvahiLookupResultFlags flags,
249     void* userdata) {
250
251     avahi_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, browser_event_to_string(event));
252 }
253
254 static void sb_callback(
255     AvahiSServiceBrowser *b,
256     AvahiIfIndex iface,
257     AvahiProtocol protocol,
258     AvahiBrowserEvent event,
259     const char *name,
260     const char *service_type,
261     const char *domain,
262     AvahiLookupResultFlags flags,
263     void* userdata) {
264     avahi_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, browser_event_to_string(event));
265 }
266
267 static void sr_callback(
268     AvahiSServiceResolver *r,
269     AvahiIfIndex iface,
270     AvahiProtocol protocol,
271     AvahiResolverEvent event,
272     const char *name,
273     const char*service_type,
274     const char*domain_name,
275     const char*hostname,
276     const AvahiAddress *a,
277     uint16_t port,
278     AvahiStringList *txt,
279     AvahiLookupResultFlags flags, 
280     void* userdata) {
281
282     if (event != AVAHI_RESOLVER_FOUND)
283         avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain_name, resolver_event_to_string(event));
284     else {
285         char t[AVAHI_ADDRESS_STR_MAX], *s;
286         
287         avahi_address_snprint(t, sizeof(t), a);
288
289         s = avahi_string_list_to_string(txt);
290         avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s>: %s/%s:%i (%s) [%s]", iface, protocol, name, service_type, domain_name, hostname, t, port, s, resolver_event_to_string(event));
291         avahi_free(s);
292     }
293 }
294
295 static void dsb_callback(
296     AvahiSDNSServerBrowser *b,
297     AvahiIfIndex iface,
298     AvahiProtocol protocol,
299     AvahiBrowserEvent event,
300     const char*hostname,
301     const AvahiAddress *a,
302     uint16_t port,
303     AvahiLookupResultFlags flags,
304     void* userdata) {
305     
306     char t[AVAHI_ADDRESS_STR_MAX] = "n/a";
307     
308     if (a)
309         avahi_address_snprint(t, sizeof(t), a);
310
311     avahi_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname, t, port, browser_event_to_string(event));
312 }
313
314 int main(int argc, char *argv[]) {
315     AvahiSRecordBrowser *r;
316     AvahiSHostNameResolver *hnr;
317     AvahiSAddressResolver *ar;
318     AvahiKey *k;
319     AvahiServerConfig config;
320     AvahiAddress a;
321     AvahiSDomainBrowser *db;
322     AvahiSServiceTypeBrowser *stb;
323     AvahiSServiceBrowser *sb;
324     AvahiSServiceResolver *sr;
325     AvahiSDNSServerBrowser *dsb;
326     AvahiSimplePoll *simple_poll;
327     int error;
328     struct timeval tv;
329
330     simple_poll = avahi_simple_poll_new();
331     poll_api = avahi_simple_poll_get(simple_poll);
332     
333     avahi_server_config_init(&config);
334
335     avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]);
336     config.n_wide_area_servers = 1;
337     config.enable_wide_area = 1;
338
339     server = avahi_server_new(poll_api, &config, server_callback, NULL, &error);
340     avahi_server_config_free(&config);
341     
342     k = avahi_key_new("_http._tcp.0pointer.de", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
343     r = avahi_s_record_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, k, 0, record_browser_callback, NULL);
344     avahi_key_unref(k);
345
346     hnr = avahi_s_host_name_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "cocaine.local", AVAHI_PROTO_UNSPEC, 0, hnr_callback, NULL);
347
348     ar = avahi_s_address_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, avahi_address_parse("192.168.50.1", AVAHI_PROTO_INET, &a), 0, ar_callback, NULL);
349
350     db = avahi_s_domain_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, db_callback, NULL);
351
352     stb = avahi_s_service_type_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, stb_callback, NULL);
353
354     sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, sb_callback, NULL);
355
356     sr = avahi_s_service_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "Ecstasy HTTP", "_http._tcp", "local", AVAHI_PROTO_UNSPEC, 0, sr_callback, NULL);
357
358     dsb = avahi_s_dns_server_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "local", AVAHI_DNS_SERVER_RESOLVE, AVAHI_PROTO_UNSPEC, 0, dsb_callback, NULL);
359
360     avahi_elapse_time(&tv, 1000*5, 0);
361     poll_api->timeout_new(poll_api, &tv, dump_timeout_callback, server);
362
363     avahi_elapse_time(&tv, 1000*60, 0);
364     poll_api->timeout_new(poll_api, &tv, quit_timeout_callback, simple_poll);
365
366     avahi_simple_poll_loop(simple_poll);
367
368     avahi_s_record_browser_free(r);
369     avahi_s_host_name_resolver_free(hnr);
370     avahi_s_address_resolver_free(ar);
371     avahi_s_service_type_browser_free(stb);
372     avahi_s_service_browser_free(sb);
373     avahi_s_service_resolver_free(sr);
374     avahi_s_dns_server_browser_free(dsb);
375
376     if (group)
377         avahi_s_entry_group_free(group);   
378
379     if (server)
380         avahi_server_free(server);
381
382     if (simple_poll)
383         avahi_simple_poll_free(simple_poll);
384
385     avahi_free(service_name);
386     
387     return 0;
388 }