]> git.meshlink.io Git - catta/blob - avahi-core/avahi-test.c
* split off lookup.h and publish.h from core.h
[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         case AVAHI_BROWSER_NOT_FOUND : return "NOT_FOUND";
75     }
76
77     abort();
78 }
79
80 static const char *resolver_event_to_string(AvahiResolverEvent event) {
81     switch (event) {
82         case AVAHI_RESOLVER_FOUND: return "FOUND";
83         case AVAHI_RESOLVER_TIMEOUT: return "TIMEOUT";
84         case AVAHI_RESOLVER_NOT_FOUND: return "NOT_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      avahi_log_debug("server state: %i", state); 
128     
129     if (state == AVAHI_SERVER_RUNNING) {
130         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));
131         create_entries(0);
132     } else if (state == AVAHI_SERVER_COLLISION) {
133         char *n;
134         remove_entries();
135
136         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
137
138         avahi_log_debug("Host name conflict, retrying with <%s>", n);
139         avahi_server_set_host_name(s, n);
140         avahi_free(n);
141     }
142 }
143
144 static void remove_entries(void) {
145     if (group)
146         avahi_s_entry_group_reset(group);
147 }
148
149 static void create_entries(int new_name) {
150     AvahiAddress a;
151
152     remove_entries();
153
154     if (!group) 
155         group = avahi_s_entry_group_new(server, entry_group_callback, NULL);
156
157     assert(avahi_s_entry_group_is_empty(group));
158     
159     if (!service_name)
160         service_name = avahi_strdup("Test Service");
161     else if (new_name) {
162         char *n = avahi_alternative_service_name(service_name);
163         avahi_free(service_name);
164         service_name = n;
165     }
166     
167     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_name, "_http._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
168         avahi_log_error("Failed to add HTTP service");
169         goto fail;
170     }
171
172     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_name, "_ftp._tcp", NULL, NULL, 21, "foo", NULL) < 0) {
173         avahi_log_error("Failed to add FTP service");
174         goto fail;
175     }
176
177     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_name, "_webdav._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
178         avahi_log_error("Failed to add WEBDAV service");
179         goto fail;
180     }
181
182     if (avahi_server_add_dns_server_address(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &a), 53) < 0) {
183         avahi_log_error("Failed to add new DNS Server address");
184         goto fail;
185     }
186
187     avahi_s_entry_group_commit(group);
188     return;
189
190 fail:
191     if (group)
192         avahi_s_entry_group_free(group);
193
194     group = NULL;
195 }
196
197 static void hnr_callback(
198     AvahiSHostNameResolver *r,
199     AvahiIfIndex iface,
200     AvahiProtocol protocol,
201     AvahiResolverEvent event,
202     const char *hostname,
203     const AvahiAddress *a,
204     AvahiLookupResultFlags flags,
205     void* userdata) {
206     char t[64];
207
208     if (a)
209         avahi_address_snprint(t, sizeof(t), a);
210
211     avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", resolver_event_to_string(event));
212 }
213
214 static void ar_callback(
215     AvahiSAddressResolver *r,
216     AvahiIfIndex iface,
217     AvahiProtocol protocol,
218     AvahiResolverEvent event,
219     const AvahiAddress *a,
220     const char *hostname,
221     AvahiLookupResultFlags flags,
222     void* userdata) {
223     char t[64];
224
225     avahi_address_snprint(t, sizeof(t), a);
226
227     avahi_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", resolver_event_to_string(event));
228 }
229
230 static void db_callback(
231     AvahiSDomainBrowser *b,
232     AvahiIfIndex iface,
233     AvahiProtocol protocol,
234     AvahiBrowserEvent event,
235     const char *domain,
236     AvahiLookupResultFlags flags,
237     void* userdata) {
238
239     avahi_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, browser_event_to_string(event));
240 }
241
242 static void stb_callback(
243     AvahiSServiceTypeBrowser *b,
244     AvahiIfIndex iface,
245     AvahiProtocol protocol,
246     AvahiBrowserEvent event,
247     const char *service_type,
248     const char *domain,
249     AvahiLookupResultFlags flags,
250     void* userdata) {
251
252     avahi_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, browser_event_to_string(event));
253 }
254
255 static void sb_callback(
256     AvahiSServiceBrowser *b,
257     AvahiIfIndex iface,
258     AvahiProtocol protocol,
259     AvahiBrowserEvent event,
260     const char *name,
261     const char *service_type,
262     const char *domain,
263     AvahiLookupResultFlags flags,
264     void* userdata) {
265     avahi_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, browser_event_to_string(event));
266 }
267
268 static void sr_callback(
269     AvahiSServiceResolver *r,
270     AvahiIfIndex iface,
271     AvahiProtocol protocol,
272     AvahiResolverEvent event,
273     const char *name,
274     const char*service_type,
275     const char*domain_name,
276     const char*hostname,
277     const AvahiAddress *a,
278     uint16_t port,
279     AvahiStringList *txt,
280     AvahiLookupResultFlags flags, 
281     void* userdata) {
282
283     if (event != AVAHI_RESOLVER_FOUND)
284         avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain_name, resolver_event_to_string(event));
285     else {
286         char t[64], *s;
287         
288         avahi_address_snprint(t, sizeof(t), a);
289
290         s = avahi_string_list_to_string(txt);
291         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));
292         avahi_free(s);
293     }
294 }
295
296 static void dsb_callback(
297     AvahiSDNSServerBrowser *b,
298     AvahiIfIndex iface,
299     AvahiProtocol protocol,
300     AvahiBrowserEvent event,
301     const char*hostname,
302     const AvahiAddress *a,
303     uint16_t port,
304     AvahiLookupResultFlags flags,
305     void* userdata) {
306     
307     char t[64] = "n/a";
308     
309     if (a)
310         avahi_address_snprint(t, sizeof(t), a);
311
312     avahi_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname, t, port, browser_event_to_string(event));
313 }
314
315 int main(int argc, char *argv[]) {
316     AvahiSRecordBrowser *r;
317     AvahiSHostNameResolver *hnr;
318     AvahiSAddressResolver *ar;
319     AvahiKey *k;
320     AvahiServerConfig config;
321     AvahiAddress a;
322     AvahiSDomainBrowser *db;
323     AvahiSServiceTypeBrowser *stb;
324     AvahiSServiceBrowser *sb;
325     AvahiSServiceResolver *sr;
326     AvahiSDNSServerBrowser *dsb;
327     AvahiSimplePoll *simple_poll;
328     int error;
329     struct timeval tv;
330
331     simple_poll = avahi_simple_poll_new();
332     poll_api = avahi_simple_poll_get(simple_poll);
333     
334     avahi_server_config_init(&config);
335
336     avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]);
337     config.n_wide_area_servers = 1;
338     config.enable_wide_area = 1;
339
340     server = avahi_server_new(poll_api, &config, server_callback, NULL, &error);
341     avahi_server_config_free(&config);
342     
343     k = avahi_key_new("_http._tcp.0pointer.de", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
344     r = avahi_s_record_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, k, 0, record_browser_callback, NULL);
345     avahi_key_unref(k);
346
347     hnr = avahi_s_host_name_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "cocaine.local", AVAHI_PROTO_UNSPEC, 0, hnr_callback, NULL);
348
349     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);
350
351     db = avahi_s_domain_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, db_callback, NULL);
352
353     stb = avahi_s_service_type_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, stb_callback, NULL);
354
355     sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, sb_callback, NULL);
356
357     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);
358
359     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);
360
361     avahi_elapse_time(&tv, 1000*5, 0);
362     poll_api->timeout_new(poll_api, &tv, dump_timeout_callback, server);
363
364     avahi_elapse_time(&tv, 1000*60, 0);
365     poll_api->timeout_new(poll_api, &tv, quit_timeout_callback, simple_poll);
366
367     for (;;)
368         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
369             break;
370
371     avahi_s_record_browser_free(r);
372     avahi_s_host_name_resolver_free(hnr);
373     avahi_s_address_resolver_free(ar);
374     avahi_s_service_type_browser_free(stb);
375     avahi_s_service_browser_free(sb);
376     avahi_s_service_resolver_free(sr);
377     avahi_s_dns_server_browser_free(dsb);
378
379     if (group)
380         avahi_s_entry_group_free(group);   
381
382     if (server)
383         avahi_server_free(server);
384
385     if (simple_poll)
386         avahi_simple_poll_free(simple_poll);
387
388     avahi_free(service_name);
389     
390     return 0;
391 }