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-glib/glib-watch.h>
35 #include <avahi-glib/glib-malloc.h>
36 #include <avahi-core/core.h>
37 #include <avahi-core/log.h>
40 static AvahiEntryGroup *group = NULL;
41 static AvahiServer *server = NULL;
42 static char *service_name = NULL;
44 static int quit_timeout(void* data) {
45 g_main_loop_quit(data);
49 static void dump_line(const char *text, void* userdata) {
53 static int dump_timeout(void* data) {
54 AvahiServer *Avahi = data;
55 avahi_server_dump(Avahi, dump_line, NULL);
59 static void record_browser_callback(AvahiRecordBrowser *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
64 assert(interface > 0);
65 assert(protocol != AVAHI_PROTO_UNSPEC);
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");
73 static void remove_entries(void);
74 static void create_entries(int new_name);
76 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
77 avahi_log_debug("entry group state: %i", state);
79 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
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);
88 static void server_callback(AvahiServer *s, AvahiServerState state, void* userdata) {
90 avahi_log_debug("server state: %i", state);
92 if (state == AVAHI_SERVER_RUNNING) {
93 avahi_log_debug("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
95 } else if (state == AVAHI_SERVER_COLLISION) {
99 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
101 avahi_log_debug("Host name conflict, retrying with <%s>", n);
102 avahi_server_set_host_name(s, n);
107 static void remove_entries(void) {
109 avahi_entry_group_reset(group);
112 static void create_entries(int new_name) {
118 group = avahi_entry_group_new(server, entry_group_callback, NULL);
120 assert(avahi_entry_group_is_empty(group));
123 service_name = g_strdup("Test Service");
125 char *n = avahi_alternative_service_name(service_name);
126 avahi_free(service_name);
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");
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");
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");
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");
150 avahi_entry_group_commit(group);
155 avahi_entry_group_free(group);
160 static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const char *hostname, const AvahiAddress *a, void* userdata) {
164 avahi_address_snprint(t, sizeof(t), a);
166 avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout");
169 static void ar_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, const char *hostname, void* userdata) {
172 avahi_address_snprint(t, sizeof(t), a);
174 avahi_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout");
177 static void db_callback(AvahiDomainBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void* userdata) {
179 avahi_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
182 static void stb_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *service_type, const char *domain, void* userdata) {
184 avahi_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
187 static void sb_callback(AvahiServiceBrowser *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");
191 static void sr_callback(AvahiServiceResolver *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) {
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);
198 avahi_address_snprint(t, sizeof(t), a);
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);
206 static void dsb_callback(AvahiDNSServerBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char*hostname, const AvahiAddress *a, uint16_t port, void* userdata) {
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");
212 int main(int argc, char *argv[]) {
213 GMainLoop *loop = NULL;
214 AvahiRecordBrowser *r;
215 AvahiHostNameResolver *hnr;
216 AvahiAddressResolver *ar;
218 AvahiServerConfig config;
220 AvahiDomainBrowser *db;
221 AvahiServiceTypeBrowser *stb;
222 AvahiServiceBrowser *sb;
223 AvahiServiceResolver *sr;
224 AvahiDNSServerBrowser *dsb;
225 AvahiGLibPoll *glib_poll;
228 avahi_set_allocator(avahi_glib_allocator());
229 glib_poll = avahi_glib_poll_new(NULL);
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);
236 k = avahi_key_new("_http._tcp.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
237 r = avahi_record_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, k, record_browser_callback, NULL);
240 hnr = avahi_host_name_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "codes-CompUTER.local", AVAHI_PROTO_UNSPEC, hnr_callback, NULL);
242 ar = avahi_address_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, avahi_address_parse("192.168.50.15", AVAHI_PROTO_INET, &a), ar_callback, NULL);
244 db = avahi_domain_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, db_callback, NULL);
246 stb = avahi_service_type_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, stb_callback, NULL);
248 sb = avahi_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, sb_callback, NULL);
250 sr = avahi_service_resolver_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "Ecstasy HTTP", "_http._tcp", "local", AVAHI_PROTO_UNSPEC, sr_callback, NULL);
252 dsb = avahi_dns_server_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "local", AVAHI_DNS_SERVER_RESOLVE, AVAHI_PROTO_UNSPEC, dsb_callback, NULL);
255 g_timeout_add(1000*5, dump_timeout, server);
256 g_timeout_add(1000*60, quit_timeout, loop);
258 loop = g_main_loop_new(NULL, 0);
259 g_main_loop_run(loop);
260 g_main_loop_unref(loop);
262 avahi_record_browser_free(r);
263 avahi_host_name_resolver_free(hnr);
264 avahi_address_resolver_free(ar);
265 avahi_service_type_browser_free(stb);
266 avahi_service_browser_free(sb);
267 avahi_service_resolver_free(sr);
268 avahi_dns_server_browser_free(dsb);
271 avahi_entry_group_free(group);
274 avahi_server_free(server);
277 avahi_glib_poll_free(glib_poll);
279 avahi_free(service_name);