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/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
34 #include <glade/glade.h>
36 #include <avahi-core/core.h>
37 #include <avahi-core/lookup.h>
39 #include <avahi-common/strlst.h>
40 #include <avahi-common/domain.h>
41 #include <avahi-common/error.h>
43 #include <avahi-glib/glib-watch.h>
44 #include <avahi-glib/glib-malloc.h>
49 struct ServiceType *service_type;
53 AvahiIfIndex interface;
54 AvahiProtocol protocol;
56 GtkTreeRowReference *tree_ref;
61 AvahiSServiceBrowser *browser;
64 GtkTreeRowReference *tree_ref;
67 static GtkWidget *main_window = NULL;
68 static GtkTreeView *tree_view = NULL;
69 static GtkTreeStore *tree_store = NULL;
70 static GtkLabel *info_label = NULL;
71 static AvahiServer *server = NULL;
72 static AvahiSServiceTypeBrowser *service_type_browser = NULL;
73 static GHashTable *service_type_hash_table = NULL;
74 static AvahiSServiceResolver *service_resolver = NULL;
75 static struct Service *current_service = NULL;
77 static struct Service *get_service(const gchar *service_type, const gchar *service_name, const gchar*domain_name, AvahiIfIndex interface, AvahiProtocol protocol) {
78 struct ServiceType *st;
81 if (!(st = g_hash_table_lookup(service_type_hash_table, service_type)))
84 for (l = st->services; l; l = l->next) {
85 struct Service *s = l->data;
87 if (s->interface == interface &&
88 s->protocol == protocol &&
89 avahi_domain_equal(s->service_name, service_name) &&
90 avahi_domain_equal(s->domain_name, domain_name))
97 static void free_service(struct Service *s) {
101 if (current_service == s) {
102 current_service = NULL;
104 if (service_resolver) {
105 avahi_s_service_resolver_free(service_resolver);
106 service_resolver = NULL;
109 gtk_label_set_text(info_label, "<i>Service removed</i>");
112 s->service_type->services = g_list_remove(s->service_type->services, s);
114 if ((path = gtk_tree_row_reference_get_path(s->tree_ref))) {
115 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
116 gtk_tree_path_free(path);
119 gtk_tree_store_remove(tree_store, &iter);
121 gtk_tree_row_reference_free(s->tree_ref);
123 g_free(s->service_name);
124 g_free(s->domain_name);
128 static void service_browser_callback(
129 AVAHI_GCC_UNUSED AvahiSServiceBrowser *b,
130 AvahiIfIndex interface,
131 AvahiProtocol protocol,
132 AvahiBrowserEvent event,
133 const char *service_name,
134 const char *service_type,
135 const char *domain_name,
136 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
137 AVAHI_GCC_UNUSED void* userdata) {
139 if (event == AVAHI_BROWSER_NEW) {
141 GtkTreeIter iter, piter;
142 GtkTreePath *path, *ppath;
144 char name[IF_NAMESIZE];
146 s = g_new(struct Service, 1);
147 s->service_name = g_strdup(service_name);
148 s->domain_name = g_strdup(domain_name);
149 s->interface = interface;
150 s->protocol = protocol;
151 s->service_type = g_hash_table_lookup(service_type_hash_table, service_type);
152 g_assert(s->service_type);
154 s->service_type->services = g_list_prepend(s->service_type->services, s);
156 ppath = gtk_tree_row_reference_get_path(s->service_type->tree_ref);
157 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &piter, ppath);
159 snprintf(iface, sizeof(iface), "%s %s", if_indextoname(interface, name), avahi_proto_to_string(protocol));
161 gtk_tree_store_append(tree_store, &iter, &piter);
162 gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1);
164 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
165 s->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
166 gtk_tree_path_free(path);
168 gtk_tree_view_expand_row(tree_view, ppath, FALSE);
169 gtk_tree_path_free(ppath);
172 } else if (event == AVAHI_BROWSER_REMOVE) {
175 if ((s = get_service(service_type, service_name, domain_name, interface, protocol)))
180 static void service_type_browser_callback(
181 AVAHI_GCC_UNUSED AvahiSServiceTypeBrowser *b,
182 AVAHI_GCC_UNUSED AvahiIfIndex interface,
183 AVAHI_GCC_UNUSED AvahiProtocol protocol,
184 AvahiBrowserEvent event,
185 const char *service_type,
187 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
188 AVAHI_GCC_UNUSED void * userdata) {
190 struct ServiceType *st;
194 if (event != AVAHI_BROWSER_NEW)
197 if (g_hash_table_lookup(service_type_hash_table, service_type))
200 st = g_new(struct ServiceType, 1);
201 st->service_type = g_strdup(service_type);
204 gtk_tree_store_append(tree_store, &iter, NULL);
205 gtk_tree_store_set(tree_store, &iter, 0, st->service_type, 1, "", 2, NULL, -1);
207 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
208 st->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
209 gtk_tree_path_free(path);
211 g_hash_table_insert(service_type_hash_table, st->service_type, st);
213 st->browser = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, st->service_type, domain, 0, service_browser_callback, NULL);
216 static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) {
217 gchar t[512], address[64], *txt_s;
218 char name[IF_NAMESIZE];
221 char na[AVAHI_ADDRESS_STR_MAX];
222 avahi_address_snprint(na, sizeof(na), a);
223 snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
226 txt_s = avahi_string_list_to_string(txt);
228 txt_s = g_strdup("<i>empty</i>");
230 snprintf(address, sizeof(address), "<i>n/a</i>");
231 txt_s = g_strdup("<i>n/a</i>");
234 snprintf(t, sizeof(t),
235 "<b>Service Type:</b> %s\n"
236 "<b>Service Name:</b> %s\n"
237 "<b>Domain Name:</b> %s\n"
238 "<b>Interface:</b> %s %s\n"
239 "<b>Address:</b> %s\n"
240 "<b>TXT Data:</b> %s",
241 s->service_type->service_type,
244 if_indextoname(s->interface,name), avahi_proto_to_string(s->protocol),
248 gtk_label_set_markup(info_label, t);
253 static struct Service *get_service_on_cursor(void) {
258 gtk_tree_view_get_cursor(tree_view, &path, NULL);
263 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
264 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter, 2, &s, -1);
265 gtk_tree_path_free(path);
270 static void service_resolver_callback(
271 AvahiSServiceResolver *r,
272 AVAHI_GCC_UNUSED AvahiIfIndex interface,
273 AVAHI_GCC_UNUSED AvahiProtocol protocol,
274 AvahiResolverEvent event,
275 AVAHI_GCC_UNUSED const char *name,
276 AVAHI_GCC_UNUSED const char *type,
277 AVAHI_GCC_UNUSED const char *domain,
278 const char *host_name,
279 const AvahiAddress *a,
281 AvahiStringList *txt,
282 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
288 if (!(s = get_service_on_cursor()) || userdata != s) {
289 g_assert(r == service_resolver);
290 avahi_s_service_resolver_free(service_resolver);
291 service_resolver = NULL;
295 if (event == AVAHI_RESOLVER_FAILURE) {
297 snprintf(t, sizeof(t), "<i>Failed to resolve: %s.</i>", avahi_strerror(avahi_server_errno(server)));
298 gtk_label_set_markup(info_label, t);
299 } else if (event == AVAHI_RESOLVER_FOUND)
300 update_label(s, host_name, a, port, txt);
303 static void tree_view_on_cursor_changed(AVAHI_GCC_UNUSED GtkTreeView *tv, AVAHI_GCC_UNUSED gpointer userdata) {
306 if (!(s = get_service_on_cursor()))
309 if (service_resolver)
310 avahi_s_service_resolver_free(service_resolver);
312 update_label(s, NULL, NULL, 0, NULL);
314 service_resolver = avahi_s_service_resolver_new(server, s->interface, s->protocol, s->service_name, s->service_type->service_type, s->domain_name, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, s);
317 static gboolean main_window_on_delete_event(AVAHI_GCC_UNUSED GtkWidget *widget, AVAHI_GCC_UNUSED GdkEvent *event, AVAHI_GCC_UNUSED gpointer user_data) {
322 int main(int argc, char *argv[]) {
324 AvahiServerConfig config;
325 GtkTreeViewColumn *c;
327 AvahiGLibPoll *poll_api;
329 gtk_init(&argc, &argv);
332 avahi_set_allocator(avahi_glib_allocator());
334 poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
336 xml = glade_xml_new(AVAHI_INTERFACES_DIR"avahi-discover.glade", NULL, NULL);
337 main_window = glade_xml_get_widget(xml, "main_window");
338 g_signal_connect(main_window, "delete-event", (GCallback) main_window_on_delete_event, NULL);
340 tree_view = GTK_TREE_VIEW(glade_xml_get_widget(xml, "tree_view"));
341 g_signal_connect(GTK_WIDGET(tree_view), "cursor-changed", (GCallback) tree_view_on_cursor_changed, NULL);
343 info_label = GTK_LABEL(glade_xml_get_widget(xml, "info_label"));
345 tree_store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
346 gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(tree_store));
347 gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Name", gtk_cell_renderer_text_new(), "text", 0, NULL);
348 gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Interface", gtk_cell_renderer_text_new(), "text", 1, NULL);
350 gtk_tree_view_column_set_resizable(c = gtk_tree_view_get_column(tree_view, 0), TRUE);
351 gtk_tree_view_column_set_sizing(c, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
352 gtk_tree_view_column_set_expand(c, TRUE);
354 service_type_hash_table = g_hash_table_new((GHashFunc) avahi_domain_hash, (GEqualFunc) avahi_domain_equal);
356 avahi_server_config_init(&config);
357 config.publish_hinfo = config.publish_addresses = config.publish_domain = config.publish_workstation = FALSE;
358 server = avahi_server_new(avahi_glib_poll_get(poll_api), &config, NULL, NULL, &error);
359 avahi_server_config_free(&config);
363 service_type_browser = avahi_s_service_type_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, argc >= 2 ? argv[1] : NULL, 0, service_type_browser_callback, NULL);
367 avahi_server_free(server);
368 avahi_glib_poll_free(poll_api);