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