2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
33 #include <avahi-core/core.h>
34 #include <avahi-core/lookup.h>
36 #include <avahi-common/strlst.h>
37 #include <avahi-common/domain.h>
38 #include <avahi-common/error.h>
40 #include <avahi-glib/glib-watch.h>
41 #include <avahi-glib/glib-malloc.h>
46 struct ServiceType *service_type;
50 AvahiIfIndex interface;
51 AvahiProtocol protocol;
53 GtkTreeRowReference *tree_ref;
58 AvahiSServiceBrowser *browser;
61 GtkTreeRowReference *tree_ref;
64 static GtkWidget *main_window = NULL;
65 static GtkTreeView *tree_view = NULL;
66 static GtkTreeStore *tree_store = NULL;
67 static GtkLabel *info_label = NULL;
68 static AvahiServer *server = NULL;
69 static AvahiSServiceTypeBrowser *service_type_browser = NULL;
70 static GHashTable *service_type_hash_table = NULL;
71 static AvahiSServiceResolver *service_resolver = NULL;
72 static struct Service *current_service = NULL;
74 static struct Service *get_service(const gchar *service_type, const gchar *service_name, const gchar*domain_name, AvahiIfIndex interface, AvahiProtocol protocol) {
75 struct ServiceType *st;
78 if (!(st = g_hash_table_lookup(service_type_hash_table, service_type)))
81 for (l = st->services; l; l = l->next) {
82 struct Service *s = l->data;
84 if (s->interface == interface &&
85 s->protocol == protocol &&
86 avahi_domain_equal(s->service_name, service_name) &&
87 avahi_domain_equal(s->domain_name, domain_name))
94 static void free_service(struct Service *s) {
98 if (current_service == s) {
99 current_service = NULL;
101 if (service_resolver) {
102 avahi_s_service_resolver_free(service_resolver);
103 service_resolver = NULL;
106 gtk_label_set_text(info_label, "<i>Service removed</i>");
109 s->service_type->services = g_list_remove(s->service_type->services, s);
111 if ((path = gtk_tree_row_reference_get_path(s->tree_ref))) {
112 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
113 gtk_tree_path_free(path);
116 gtk_tree_store_remove(tree_store, &iter);
118 gtk_tree_row_reference_free(s->tree_ref);
120 g_free(s->service_name);
121 g_free(s->domain_name);
125 static void service_browser_callback(
126 AVAHI_GCC_UNUSED AvahiSServiceBrowser *b,
127 AvahiIfIndex interface,
128 AvahiProtocol protocol,
129 AvahiBrowserEvent event,
130 const char *service_name,
131 const char *service_type,
132 const char *domain_name,
133 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
134 AVAHI_GCC_UNUSED void* userdata) {
136 if (event == AVAHI_BROWSER_NEW) {
138 GtkTreeIter iter, piter;
139 GtkTreePath *path, *ppath;
141 char name[IF_NAMESIZE];
143 s = g_new(struct Service, 1);
144 s->service_name = g_strdup(service_name);
145 s->domain_name = g_strdup(domain_name);
146 s->interface = interface;
147 s->protocol = protocol;
148 s->service_type = g_hash_table_lookup(service_type_hash_table, service_type);
149 g_assert(s->service_type);
151 s->service_type->services = g_list_prepend(s->service_type->services, s);
153 ppath = gtk_tree_row_reference_get_path(s->service_type->tree_ref);
154 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &piter, ppath);
156 snprintf(iface, sizeof(iface), "%s %s", if_indextoname(interface, name), avahi_proto_to_string(protocol));
158 gtk_tree_store_append(tree_store, &iter, &piter);
159 gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1);
161 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
162 s->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
163 gtk_tree_path_free(path);
165 gtk_tree_view_expand_row(tree_view, ppath, FALSE);
166 gtk_tree_path_free(ppath);
169 } else if (event == AVAHI_BROWSER_REMOVE) {
172 if ((s = get_service(service_type, service_name, domain_name, interface, protocol)))
177 static void service_type_browser_callback(
178 AVAHI_GCC_UNUSED AvahiSServiceTypeBrowser *b,
179 AVAHI_GCC_UNUSED AvahiIfIndex interface,
180 AVAHI_GCC_UNUSED AvahiProtocol protocol,
181 AvahiBrowserEvent event,
182 const char *service_type,
184 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
185 AVAHI_GCC_UNUSED void * userdata) {
187 struct ServiceType *st;
191 if (event != AVAHI_BROWSER_NEW)
194 if (g_hash_table_lookup(service_type_hash_table, service_type))
197 st = g_new(struct ServiceType, 1);
198 st->service_type = g_strdup(service_type);
201 gtk_tree_store_append(tree_store, &iter, NULL);
202 gtk_tree_store_set(tree_store, &iter, 0, st->service_type, 1, "", 2, NULL, -1);
204 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
205 st->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
206 gtk_tree_path_free(path);
208 g_hash_table_insert(service_type_hash_table, st->service_type, st);
210 st->browser = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, st->service_type, domain, 0, service_browser_callback, NULL);
213 static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) {
214 gchar t[512], address[64], *txt_s;
215 char name[IF_NAMESIZE];
218 char na[AVAHI_ADDRESS_STR_MAX];
219 avahi_address_snprint(na, sizeof(na), a);
220 snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
223 txt_s = avahi_string_list_to_string(txt);
225 txt_s = g_strdup("<i>empty</i>");
227 snprintf(address, sizeof(address), "<i>n/a</i>");
228 txt_s = g_strdup("<i>n/a</i>");
231 snprintf(t, sizeof(t),
232 "<b>Service Type:</b> %s\n"
233 "<b>Service Name:</b> %s\n"
234 "<b>Domain Name:</b> %s\n"
235 "<b>Interface:</b> %s %s\n"
236 "<b>Address:</b> %s\n"
237 "<b>TXT Data:</b> %s",
238 s->service_type->service_type,
241 if_indextoname(s->interface,name), avahi_proto_to_string(s->protocol),
245 gtk_label_set_markup(info_label, t);
250 static struct Service *get_service_on_cursor(void) {
255 gtk_tree_view_get_cursor(tree_view, &path, NULL);
260 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
261 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter, 2, &s, -1);
262 gtk_tree_path_free(path);
267 static void service_resolver_callback(
268 AvahiSServiceResolver *r,
269 AVAHI_GCC_UNUSED AvahiIfIndex interface,
270 AVAHI_GCC_UNUSED AvahiProtocol protocol,
271 AvahiResolverEvent event,
272 AVAHI_GCC_UNUSED const char *name,
273 AVAHI_GCC_UNUSED const char *type,
274 AVAHI_GCC_UNUSED const char *domain,
275 const char *host_name,
276 const AvahiAddress *a,
278 AvahiStringList *txt,
279 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
285 if (!(s = get_service_on_cursor()) || userdata != s) {
286 g_assert(r == service_resolver);
287 avahi_s_service_resolver_free(service_resolver);
288 service_resolver = NULL;
292 if (event == AVAHI_RESOLVER_FAILURE) {
294 snprintf(t, sizeof(t), "<i>Failed to resolve: %s.</i>", avahi_strerror(avahi_server_errno(server)));
295 gtk_label_set_markup(info_label, t);
296 } else if (event == AVAHI_RESOLVER_FOUND)
297 update_label(s, host_name, a, port, txt);
300 static void tree_view_on_cursor_changed(AVAHI_GCC_UNUSED GtkTreeView *tv, AVAHI_GCC_UNUSED gpointer userdata) {
303 if (!(s = get_service_on_cursor()))
306 if (service_resolver)
307 avahi_s_service_resolver_free(service_resolver);
309 update_label(s, NULL, NULL, 0, NULL);
311 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);
314 static gboolean main_window_on_delete_event(AVAHI_GCC_UNUSED GtkWidget *widget, AVAHI_GCC_UNUSED GdkEvent *event, AVAHI_GCC_UNUSED gpointer user_data) {
319 int main(int argc, char *argv[]) {
321 AvahiServerConfig config;
322 GtkTreeViewColumn *c;
324 AvahiGLibPoll *poll_api;
326 gtk_init(&argc, &argv);
328 avahi_set_allocator(avahi_glib_allocator());
330 poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
332 ui = gtk_builder_new();
333 gtk_builder_add_from_file(ui, AVAHI_INTERFACES_DIR"avahi-discover.ui", NULL);
334 main_window = GTK_WIDGET(gtk_builder_get_object(ui, "main_window"));
335 g_signal_connect(main_window, "delete-event", (GCallback) main_window_on_delete_event, NULL);
337 tree_view = GTK_TREE_VIEW(gtk_builder_get_object(ui, "tree_view"));
338 g_signal_connect(GTK_WIDGET(tree_view), "cursor-changed", (GCallback) tree_view_on_cursor_changed, NULL);
340 info_label = GTK_LABEL(gtk_builder_get_object(ui, "info_label"));
342 tree_store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
343 gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(tree_store));
344 gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Name", gtk_cell_renderer_text_new(), "text", 0, NULL);
345 gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Interface", gtk_cell_renderer_text_new(), "text", 1, NULL);
347 gtk_tree_view_column_set_resizable(c = gtk_tree_view_get_column(tree_view, 0), TRUE);
348 gtk_tree_view_column_set_sizing(c, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
349 gtk_tree_view_column_set_expand(c, TRUE);
351 service_type_hash_table = g_hash_table_new((GHashFunc) avahi_domain_hash, (GEqualFunc) avahi_domain_equal);
353 avahi_server_config_init(&config);
354 config.publish_hinfo = config.publish_addresses = config.publish_domain = config.publish_workstation = FALSE;
355 server = avahi_server_new(avahi_glib_poll_get(poll_api), &config, NULL, NULL, &error);
356 avahi_server_config_free(&config);
360 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);
364 avahi_server_free(server);
365 avahi_glib_poll_free(poll_api);