4 This file is part of avahi.
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.
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.
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
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
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>
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>
44 static AvahiSEntryGroup *group = NULL;
45 static AvahiServer *server = NULL;
46 static char *service_name = NULL;
48 static const AvahiPoll *poll_api;
50 static void quit_timeout_callback(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void* userdata) {
51 AvahiSimplePoll *simple_poll = userdata;
53 avahi_simple_poll_quit(simple_poll);
56 static void dump_line(const char *text, AVAHI_GCC_UNUSED void* userdata) {
60 static void dump_timeout_callback(AvahiTimeout *timeout, void* userdata) {
63 AvahiServer *avahi = userdata;
64 avahi_server_dump(avahi, dump_line, NULL);
66 avahi_elapse_time(&tv, 5000, 0);
67 poll_api->timeout_update(timeout, &tv);
70 static const char *browser_event_to_string(AvahiBrowserEvent 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";
82 static const char *resolver_event_to_string(AvahiResolverEvent event) {
84 case AVAHI_RESOLVER_FOUND: return "FOUND";
85 case AVAHI_RESOLVER_FAILURE: return "FAILURE";
90 static void record_browser_callback(
91 AvahiSRecordBrowser *r,
92 AvahiIfIndex interface,
93 AvahiProtocol protocol,
94 AvahiBrowserEvent event,
96 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
97 AVAHI_GCC_UNUSED void* userdata) {
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));
106 avahi_log_debug("RB: [%s]", browser_event_to_string(event));
110 static void remove_entries(void);
111 static void create_entries(int new_name);
113 static void entry_group_callback(AVAHI_GCC_UNUSED AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void* userdata) {
114 avahi_log_debug("entry group state: %i", state);
116 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
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);
125 static void server_callback(AvahiServer *s, AvahiServerState state, AVAHI_GCC_UNUSED void* userdata) {
128 avahi_log_debug("server state: %i", state);
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));
133 } else if (state == AVAHI_SERVER_COLLISION) {
137 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
139 avahi_log_debug("Host name conflict, retrying with <%s>", n);
140 avahi_server_set_host_name(s, n);
145 static void remove_entries(void) {
147 avahi_s_entry_group_reset(group);
150 static void create_entries(int new_name) {
157 group = avahi_s_entry_group_new(server, entry_group_callback, NULL);
159 assert(avahi_s_entry_group_is_empty(group));
162 service_name = avahi_strdup("Test Service");
164 char *n = avahi_alternative_service_name(service_name);
165 avahi_free(service_name);
169 if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service_name, "_http._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
170 avahi_log_error("Failed to add HTTP service");
174 if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service_name, "_ftp._tcp", NULL, NULL, 21, "foo", NULL) < 0) {
175 avahi_log_error("Failed to add FTP service");
179 if (avahi_server_add_service(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,service_name, "_webdav._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
180 avahi_log_error("Failed to add WEBDAV service");
184 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) {
185 avahi_log_error("Failed to add new DNS Server address");
189 r = avahi_record_new_full("cname.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_CNAME, AVAHI_DEFAULT_TTL);
190 r->data.cname.name = avahi_strdup("cocaine.local");
192 if (avahi_server_add(server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, r) < 0) {
193 avahi_log_error("Failed to add CNAME record");
197 avahi_s_entry_group_commit(group);
202 avahi_s_entry_group_free(group);
207 static void hnr_callback(
208 AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
210 AvahiProtocol protocol,
211 AvahiResolverEvent event,
212 const char *hostname,
213 const AvahiAddress *a,
214 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
215 AVAHI_GCC_UNUSED void* userdata) {
216 char t[AVAHI_ADDRESS_STR_MAX];
219 avahi_address_snprint(t, sizeof(t), a);
221 avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", resolver_event_to_string(event));
224 static void ar_callback(
225 AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
227 AvahiProtocol protocol,
228 AvahiResolverEvent event,
229 const AvahiAddress *a,
230 const char *hostname,
231 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
232 AVAHI_GCC_UNUSED void* userdata) {
233 char t[AVAHI_ADDRESS_STR_MAX];
235 avahi_address_snprint(t, sizeof(t), a);
237 avahi_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", resolver_event_to_string(event));
240 static void db_callback(
241 AVAHI_GCC_UNUSED AvahiSDomainBrowser *b,
243 AvahiProtocol protocol,
244 AvahiBrowserEvent event,
246 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
247 AVAHI_GCC_UNUSED void* userdata) {
249 avahi_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, browser_event_to_string(event));
252 static void stb_callback(
253 AVAHI_GCC_UNUSED AvahiSServiceTypeBrowser *b,
255 AvahiProtocol protocol,
256 AvahiBrowserEvent event,
257 const char *service_type,
259 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
260 AVAHI_GCC_UNUSED void* userdata) {
262 avahi_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, browser_event_to_string(event));
265 static void sb_callback(
266 AVAHI_GCC_UNUSED AvahiSServiceBrowser *b,
268 AvahiProtocol protocol,
269 AvahiBrowserEvent event,
271 const char *service_type,
273 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
274 AVAHI_GCC_UNUSED void* userdata) {
275 avahi_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, browser_event_to_string(event));
278 static void sr_callback(
279 AVAHI_GCC_UNUSED AvahiSServiceResolver *r,
281 AvahiProtocol protocol,
282 AvahiResolverEvent event,
284 const char*service_type,
285 const char*domain_name,
287 const AvahiAddress *a,
289 AvahiStringList *txt,
290 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
291 AVAHI_GCC_UNUSED void* userdata) {
293 if (event != AVAHI_RESOLVER_FOUND)
294 avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain_name, resolver_event_to_string(event));
296 char t[AVAHI_ADDRESS_STR_MAX], *s;
298 avahi_address_snprint(t, sizeof(t), a);
300 s = avahi_string_list_to_string(txt);
301 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));
306 static void dsb_callback(
307 AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
309 AvahiProtocol protocol,
310 AvahiBrowserEvent event,
312 const AvahiAddress *a,
314 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
315 AVAHI_GCC_UNUSED void* userdata) {
317 char t[AVAHI_ADDRESS_STR_MAX] = "n/a";
320 avahi_address_snprint(t, sizeof(t), a);
322 avahi_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname, t, port, browser_event_to_string(event));
325 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
326 AvahiSRecordBrowser *r;
327 AvahiSHostNameResolver *hnr;
328 AvahiSAddressResolver *ar;
330 AvahiServerConfig config;
332 AvahiSDomainBrowser *db;
333 AvahiSServiceTypeBrowser *stb;
334 AvahiSServiceBrowser *sb;
335 AvahiSServiceResolver *sr;
336 AvahiSDNSServerBrowser *dsb;
337 AvahiSimplePoll *simple_poll;
341 simple_poll = avahi_simple_poll_new();
342 poll_api = avahi_simple_poll_get(simple_poll);
344 avahi_server_config_init(&config);
346 avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]);
347 config.n_wide_area_servers = 1;
348 config.enable_wide_area = 1;
350 server = avahi_server_new(poll_api, &config, server_callback, NULL, &error);
351 avahi_server_config_free(&config);
353 k = avahi_key_new("_http._tcp.0pointer.de", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
354 r = avahi_s_record_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, k, 0, record_browser_callback, NULL);
357 hnr = avahi_s_host_name_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "cname.local", AVAHI_PROTO_UNSPEC, 0, hnr_callback, NULL);
359 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);
361 db = avahi_s_domain_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, db_callback, NULL);
363 stb = avahi_s_service_type_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, 0, stb_callback, NULL);
365 sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, sb_callback, NULL);
367 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);
369 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);
371 avahi_elapse_time(&tv, 1000*5, 0);
372 poll_api->timeout_new(poll_api, &tv, dump_timeout_callback, server);
374 avahi_elapse_time(&tv, 1000*60, 0);
375 poll_api->timeout_new(poll_api, &tv, quit_timeout_callback, simple_poll);
377 avahi_simple_poll_loop(simple_poll);
379 avahi_s_record_browser_free(r);
380 avahi_s_host_name_resolver_free(hnr);
381 avahi_s_address_resolver_free(ar);
382 avahi_s_service_type_browser_free(stb);
383 avahi_s_service_browser_free(sb);
384 avahi_s_service_resolver_free(sr);
385 avahi_s_dns_server_browser_free(dsb);
388 avahi_s_entry_group_free(group);
391 avahi_server_free(server);
394 avahi_simple_poll_free(simple_poll);
396 avahi_free(service_name);