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