]> git.meshlink.io Git - catta/blob - avahi-core/avahi-test.c
* add new priority parameter to avahi_glib_poll_new()
[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-glib/glib-watch.h>
35 #include <avahi-glib/glib-malloc.h>
36 #include <avahi-core/core.h>
37 #include <avahi-core/log.h>
38
39
40 static AvahiSEntryGroup *group = NULL;
41 static AvahiServer *server = NULL;
42 static char *service_name = NULL;
43
44 static int quit_timeout(void* data) {
45     g_main_loop_quit(data);
46     return 0;
47 }
48
49 static void dump_line(const char *text, void* userdata) {
50     printf("%s\n", text);
51 }
52
53 static int dump_timeout(void* data) {
54     AvahiServer *Avahi = data;
55     avahi_server_dump(Avahi, dump_line, NULL);
56     return 1;
57 }
58
59 static void record_browser_callback(AvahiSRecordBrowser *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
60     char *t;
61     
62     assert(r);
63     assert(record);
64     assert(interface > 0);
65     assert(protocol != AVAHI_PROTO_UNSPEC);
66
67     avahi_log_debug("SUBSCRIPTION: record [%s] on %i.%i is %s", t = avahi_record_to_string(record), interface, protocol,
68               event == AVAHI_BROWSER_NEW ? "new" : "remove");
69
70     avahi_free(t);
71 }
72
73 static void remove_entries(void);
74 static void create_entries(int new_name);
75
76 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
77     avahi_log_debug("entry group state: %i", state); 
78
79     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
80         remove_entries();
81         create_entries(1);
82         avahi_log_debug("Service name conflict, retrying with <%s>", service_name);
83     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
84         avahi_log_debug("Service established under name <%s>", service_name);
85     }
86 }
87
88 static void server_callback(AvahiServer *s, AvahiServerState state, void* userdata) {
89
90      avahi_log_debug("server state: %i", state); 
91     
92     if (state == AVAHI_SERVER_RUNNING) {
93         avahi_log_debug("Server startup complete.  Host name is <%s>", avahi_server_get_host_name_fqdn(s));
94         create_entries(0);
95     } else if (state == AVAHI_SERVER_COLLISION) {
96         char *n;
97         remove_entries();
98
99         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
100
101         avahi_log_debug("Host name conflict, retrying with <%s>", n);
102         avahi_server_set_host_name(s, n);
103         avahi_free(n);
104     }
105 }
106
107 static void remove_entries(void) {
108     if (group)
109         avahi_s_entry_group_reset(group);
110 }
111
112 static void create_entries(int new_name) {
113     AvahiAddress a;
114
115     remove_entries();
116
117     if (!group) 
118         group = avahi_s_entry_group_new(server, entry_group_callback, NULL);
119
120     assert(avahi_s_entry_group_is_empty(group));
121     
122     if (!service_name)
123         service_name = g_strdup("Test Service");
124     else if (new_name) {
125         char *n = avahi_alternative_service_name(service_name);
126         avahi_free(service_name);
127         service_name = n;
128     }
129     
130     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_name, "_http._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
131         avahi_log_error("Failed to add HTTP service");
132         goto fail;
133     }
134
135     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_name, "_ftp._tcp", NULL, NULL, 21, "foo", NULL) < 0) {
136         avahi_log_error("Failed to add FTP service");
137         goto fail;
138     }
139
140     if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_name, "_webdav._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
141         avahi_log_error("Failed to add WEBDAV service");
142         goto fail;
143     }
144
145     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) {
146         avahi_log_error("Failed to add new DNS Server address");
147         goto fail;
148     }
149
150     avahi_s_entry_group_commit(group);
151     return;
152
153 fail:
154     if (group)
155         avahi_s_entry_group_free(group);
156
157     group = NULL;
158 }
159
160 static void hnr_callback(AvahiSHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const char *hostname, const AvahiAddress *a, void* userdata) {
161     char t[64];
162
163     if (a)
164         avahi_address_snprint(t, sizeof(t), a);
165
166     avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout");
167 }
168
169 static void ar_callback(AvahiSAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, const char *hostname, void* userdata) {
170     char t[64];
171
172     avahi_address_snprint(t, sizeof(t), a);
173
174     avahi_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout");
175 }
176
177 static void db_callback(AvahiSDomainBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void* userdata) {
178
179     avahi_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
180 }
181
182 static void stb_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *service_type, const char *domain, void* userdata) {
183
184     avahi_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
185 }
186
187 static void sb_callback(AvahiSServiceBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *service_type, const char *domain, void* userdata) {
188    avahi_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
189 }
190
191 static void sr_callback(AvahiSServiceResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char*service_type, const char*domain_name, const char*hostname, const AvahiAddress *a, uint16_t port, AvahiStringList *txt, void* userdata) {
192
193     if (event == AVAHI_RESOLVER_TIMEOUT)
194         avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s> [timeout]", iface, protocol, name, service_type, domain_name);
195     else {
196         char t[64], *s;
197         
198         avahi_address_snprint(t, sizeof(t), a);
199
200         s = avahi_string_list_to_string(txt);
201         avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s>: %s/%s:%i (%s) [found]", iface, protocol, name, service_type, domain_name, hostname, t, port, s);
202         avahi_free(s);
203     }
204 }
205
206 static void dsb_callback(AvahiSDNSServerBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char*hostname, const AvahiAddress *a, uint16_t port, void* userdata) {
207     char t[64];
208     avahi_address_snprint(t, sizeof(t), a);
209     avahi_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname, t, port, event == AVAHI_BROWSER_NEW ? "new" : "remove");
210 }
211
212 int main(int argc, char *argv[]) {
213     GMainLoop *loop = NULL;
214     AvahiSRecordBrowser *r;
215     AvahiSHostNameResolver *hnr;
216     AvahiSAddressResolver *ar;
217     AvahiKey *k;
218     AvahiServerConfig config;
219     AvahiAddress a;
220     AvahiSDomainBrowser *db;
221     AvahiSServiceTypeBrowser *stb;
222     AvahiSServiceBrowser *sb;
223     AvahiSServiceResolver *sr;
224     AvahiSDNSServerBrowser *dsb;
225     AvahiGLibPoll *glib_poll;
226     int error;
227
228     avahi_set_allocator(avahi_glib_allocator());
229     glib_poll = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
230     
231     avahi_server_config_init(&config);
232 /*     config.host_name = g_strdup("test"); */
233     server = avahi_server_new(avahi_glib_poll_get(glib_poll), &config, server_callback, NULL, &error);
234     avahi_server_config_free(&config);
235
236     k = avahi_key_new("_http._tcp.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
237     r = avahi_s_record_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, k, record_browser_callback, NULL);
238     avahi_key_unref(k);
239
240     hnr = avahi_s_host_name_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "codes-CompUTER.local", AVAHI_PROTO_UNSPEC, hnr_callback, NULL);
241
242     ar = avahi_s_address_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, avahi_address_parse("192.168.50.15", AVAHI_PROTO_INET, &a), ar_callback, NULL);
243
244     db = avahi_s_domain_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, db_callback, NULL);
245
246     stb = avahi_s_service_type_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, stb_callback, NULL);
247
248     sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, sb_callback, NULL);
249
250     sr = avahi_s_service_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "Ecstasy HTTP", "_http._tcp", "local", AVAHI_PROTO_UNSPEC, sr_callback, NULL);
251
252     dsb = avahi_s_dns_server_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "local", AVAHI_DNS_SERVER_RESOLVE, AVAHI_PROTO_UNSPEC, dsb_callback, NULL);
253
254     
255     g_timeout_add(1000*5, dump_timeout, server);
256     g_timeout_add(1000*60, quit_timeout, loop);     
257     
258     loop = g_main_loop_new(NULL, 0);
259     g_main_loop_run(loop);
260     g_main_loop_unref(loop);
261
262     avahi_s_record_browser_free(r);
263     avahi_s_host_name_resolver_free(hnr);
264     avahi_s_address_resolver_free(ar);
265     avahi_s_service_type_browser_free(stb);
266     avahi_s_service_browser_free(sb);
267     avahi_s_service_resolver_free(sr);
268     avahi_s_dns_server_browser_free(dsb);
269
270     if (group)
271         avahi_s_entry_group_free(group);   
272
273     if (server)
274         avahi_server_free(server);
275
276     if (glib_poll)
277         avahi_glib_poll_free(glib_poll);
278
279     avahi_free(service_name);
280     
281     return 0;
282 }