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>
32 #include "alternative.h"
34 static AvahiEntryGroup *group = NULL;
35 static AvahiServer *server = NULL;
36 static gchar *service_name = NULL;
38 static gboolean quit_timeout(gpointer data) {
39 g_main_loop_quit(data);
43 static gboolean dump_timeout(gpointer data) {
44 AvahiServer *Avahi = data;
45 avahi_server_dump(Avahi, stdout);
49 static void record_resolver_callback(AvahiRecordResolver *r, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
54 g_assert(interface > 0);
55 g_assert(protocol != AF_UNSPEC);
57 g_message("SUBSCRIPTION: record [%s] on %i.%i is %s", t = avahi_record_to_string(record), interface, protocol,
58 event == AVAHI_BROWSER_NEW ? "new" : "removed");
64 static void remove_entries(void);
65 static void create_entries(gboolean new_name);
67 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
68 g_message("entry group state: %i", state);
70 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
73 g_message("Service name conflict, retrying with <%s>", service_name);
74 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
75 g_message("Service established under name <%s>", service_name);
79 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
81 g_message("server state: %i", state);
83 if (state == AVAHI_SERVER_RUNNING) {
84 g_message("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
85 create_entries(FALSE);
86 } else if (state == AVAHI_SERVER_COLLISION) {
90 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
92 g_message("Host name conflict, retrying with <%s>", n);
93 avahi_server_set_host_name(s, n);
98 static void remove_entries(void) {
100 avahi_entry_group_free(group);
105 static void create_entries(gboolean new_name) {
108 group = avahi_entry_group_new(server, entry_group_callback, NULL);
111 service_name = g_strdup("Test Service");
113 gchar *n = avahi_alternative_service_name(service_name);
114 g_free(service_name);
118 avahi_server_add_service(server, group, 0, AF_UNSPEC, "_http._tcp", service_name, NULL, NULL, 80, "foo", NULL);
119 avahi_server_add_service(server, group, 0, AF_UNSPEC, "_ftp._tcp", service_name, NULL, NULL, 21, "foo", NULL);
120 avahi_server_add_service(server, group, 0, AF_UNSPEC, "_webdav._tcp", service_name, NULL, NULL, 80, "foo", NULL);
122 avahi_entry_group_commit(group);
126 static void hnr_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
129 avahi_address_snprint(t, sizeof(t), a);
131 g_message("HNR: (%i.%i) %s -> %s [%s]", iface, protocol, hostname, t, event == AVAHI_BROWSER_NEW ? "new" : "remove");
134 int main(int argc, char *argv[]) {
135 GMainLoop *loop = NULL;
136 AvahiRecordResolver *r;
137 AvahiHostNameResolver *hnr;
139 AvahiServerConfig config;
141 avahi_server_config_init(&config);
142 /* config.host_name = g_strdup("test"); */
143 server = avahi_server_new(NULL, &config, server_callback, NULL);
144 avahi_server_config_free(&config);
146 k = avahi_key_new("_http._tcp.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
147 r = avahi_record_resolver_new(server, 0, AF_UNSPEC, k, record_resolver_callback, NULL);
150 hnr = avahi_host_name_resolver_new(server, 0, AF_UNSPEC, "ecstasy.local", hnr_callback, NULL);
152 loop = g_main_loop_new(NULL, FALSE);
154 /* g_timeout_add(1000*5, dump_timeout, server); */
155 g_timeout_add(1000*30, quit_timeout, loop);
157 g_main_loop_run(loop);
158 g_main_loop_unref(loop);
160 avahi_record_resolver_free(r);
161 avahi_host_name_resolver_free(hnr);
164 avahi_entry_group_free(group);
167 avahi_server_free(server);
169 g_free(service_name);