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>
32 #include <glade/glade.h>
34 #include <avahi-core/core.h>
35 #include <avahi-core/lookup.h>
37 #include <avahi-common/strlst.h>
38 #include <avahi-common/domain.h>
39 #include <avahi-common/error.h>
41 #include <avahi-glib/glib-watch.h>
42 #include <avahi-glib/glib-malloc.h>
47 struct ServiceType *service_type;
51 AvahiIfIndex interface;
52 AvahiProtocol protocol;
54 GtkTreeRowReference *tree_ref;
59 AvahiSServiceBrowser *browser;
62 GtkTreeRowReference *tree_ref;
65 static GtkWidget *main_window = NULL;
66 static GtkTreeView *tree_view = NULL;
67 static GtkTreeStore *tree_store = NULL;
68 static GtkLabel *info_label = NULL;
69 static AvahiServer *server = NULL;
70 static AvahiSServiceTypeBrowser *service_type_browser = NULL;
71 static GHashTable *service_type_hash_table = NULL;
72 static AvahiSServiceResolver *service_resolver = NULL;
73 static struct Service *current_service = NULL;
75 static const char* getifname(int idx) {
77 static struct ifreq ifreq;
80 memset(&ifreq, 0, sizeof(ifreq));
82 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
83 if ((fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
86 ifreq.ifr_ifindex = idx;
87 if (ioctl(fd, SIOCGIFNAME, &ifreq) < 0)
92 return ifreq.ifr_name;
99 snprintf(t, sizeof(t), "#%i", idx);
103 static struct Service *get_service(const gchar *service_type, const gchar *service_name, const gchar*domain_name, AvahiIfIndex interface, AvahiProtocol protocol) {
104 struct ServiceType *st;
107 if (!(st = g_hash_table_lookup(service_type_hash_table, service_type)))
110 for (l = st->services; l; l = l->next) {
111 struct Service *s = l->data;
113 if (s->interface == interface &&
114 s->protocol == protocol &&
115 avahi_domain_equal(s->service_name, service_name) &&
116 avahi_domain_equal(s->domain_name, domain_name))
123 static void free_service(struct Service *s) {
127 if (current_service == s) {
128 current_service = NULL;
130 if (service_resolver) {
131 avahi_s_service_resolver_free(service_resolver);
132 service_resolver = NULL;
135 gtk_label_set_text(info_label, "<i>Service removed</i>");
138 s->service_type->services = g_list_remove(s->service_type->services, s);
140 if ((path = gtk_tree_row_reference_get_path(s->tree_ref))) {
141 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
142 gtk_tree_path_free(path);
145 gtk_tree_store_remove(tree_store, &iter);
147 gtk_tree_row_reference_free(s->tree_ref);
149 g_free(s->service_name);
150 g_free(s->domain_name);
154 static void service_browser_callback(
155 AvahiSServiceBrowser *b,
156 AvahiIfIndex interface,
157 AvahiProtocol protocol,
158 AvahiBrowserEvent event,
159 const char *service_name,
160 const char *service_type,
161 const char *domain_name,
162 AvahiLookupResultFlags flags,
165 if (event == AVAHI_BROWSER_NEW) {
167 GtkTreeIter iter, piter;
168 GtkTreePath *path, *ppath;
171 s = g_new(struct Service, 1);
172 s->service_name = g_strdup(service_name);
173 s->domain_name = g_strdup(domain_name);
174 s->interface = interface;
175 s->protocol = protocol;
176 s->service_type = g_hash_table_lookup(service_type_hash_table, service_type);
177 g_assert(s->service_type);
179 s->service_type->services = g_list_prepend(s->service_type->services, s);
181 ppath = gtk_tree_row_reference_get_path(s->service_type->tree_ref);
182 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &piter, ppath);
184 snprintf(iface, sizeof(iface), "%s %s", getifname(interface), avahi_proto_to_string(protocol));
186 gtk_tree_store_append(tree_store, &iter, &piter);
187 gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1);
189 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
190 s->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
191 gtk_tree_path_free(path);
193 gtk_tree_view_expand_row(tree_view, ppath, FALSE);
194 gtk_tree_path_free(ppath);
197 } else if (event == AVAHI_BROWSER_REMOVE) {
200 if ((s = get_service(service_type, service_name, domain_name, interface, protocol)))
205 static void service_type_browser_callback(
206 AvahiSServiceTypeBrowser *b,
207 AvahiIfIndex interface,
208 AvahiProtocol protocol,
209 AvahiBrowserEvent event,
210 const char *service_type,
212 AvahiLookupResultFlags flags,
215 struct ServiceType *st;
219 if (event != AVAHI_BROWSER_NEW)
222 if (g_hash_table_lookup(service_type_hash_table, service_type))
225 st = g_new(struct ServiceType, 1);
226 st->service_type = g_strdup(service_type);
229 gtk_tree_store_append(tree_store, &iter, NULL);
230 gtk_tree_store_set(tree_store, &iter, 0, st->service_type, 1, "", 2, NULL, -1);
232 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
233 st->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
234 gtk_tree_path_free(path);
236 g_hash_table_insert(service_type_hash_table, st->service_type, st);
238 st->browser = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, st->service_type, domain, 0, service_browser_callback, NULL);
241 static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) {
242 gchar t[512], address[64], *txt_s;
246 avahi_address_snprint(na, sizeof(na), a);
247 snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
250 txt_s = avahi_string_list_to_string(txt);
252 txt_s = g_strdup("<i>empty</i>");
254 snprintf(address, sizeof(address), "<i>n/a</i>");
255 txt_s = g_strdup("<i>n/a</i>");
258 snprintf(t, sizeof(t),
259 "<b>Service Type:</b> %s\n"
260 "<b>Service Name:</b> %s\n"
261 "<b>Domain Name:</b> %s\n"
262 "<b>Interface:</b> %s %s\n"
263 "<b>Address:</b> %s\n"
264 "<b>TXT Data:</b> %s",
265 s->service_type->service_type,
268 getifname(s->interface), avahi_proto_to_string(s->protocol),
272 gtk_label_set_markup(info_label, t);
277 static struct Service *get_service_on_cursor(void) {
282 gtk_tree_view_get_cursor(tree_view, &path, NULL);
287 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
288 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter, 2, &s, -1);
289 gtk_tree_path_free(path);
294 static void service_resolver_callback(
295 AvahiSServiceResolver *r,
296 AvahiIfIndex interface,
297 AvahiProtocol protocol,
298 AvahiResolverEvent event,
302 const char *host_name,
303 const AvahiAddress *a,
305 AvahiStringList *txt,
306 AvahiLookupResultFlags flags,
312 if (!(s = get_service_on_cursor()) || userdata != s) {
313 g_assert(r == service_resolver);
314 avahi_s_service_resolver_free(service_resolver);
315 service_resolver = NULL;
319 if (event == AVAHI_RESOLVER_FAILURE) {
321 snprintf(t, sizeof(t), "<i>Failed to resolve: %s.</i>", avahi_strerror(avahi_server_errno(server)));
322 gtk_label_set_markup(info_label, t);
323 } else if (event == AVAHI_RESOLVER_FOUND)
324 update_label(s, host_name, a, port, txt);
327 static void tree_view_on_cursor_changed(GtkTreeView *tv, gpointer userdata) {
330 if (!(s = get_service_on_cursor()))
333 if (service_resolver)
334 avahi_s_service_resolver_free(service_resolver);
336 update_label(s, NULL, NULL, 0, NULL);
338 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);
341 static gboolean main_window_on_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
346 int main(int argc, char *argv[]) {
348 AvahiServerConfig config;
349 GtkTreeViewColumn *c;
351 AvahiGLibPoll *poll_api;
353 gtk_init(&argc, &argv);
356 avahi_set_allocator(avahi_glib_allocator());
358 poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
360 xml = glade_xml_new(AVAHI_INTERFACES_DIR"avahi-discover.glade", NULL, NULL);
361 main_window = glade_xml_get_widget(xml, "main_window");
362 g_signal_connect(main_window, "delete-event", (GCallback) main_window_on_delete_event, NULL);
364 tree_view = GTK_TREE_VIEW(glade_xml_get_widget(xml, "tree_view"));
365 g_signal_connect(GTK_WIDGET(tree_view), "cursor-changed", (GCallback) tree_view_on_cursor_changed, NULL);
367 info_label = GTK_LABEL(glade_xml_get_widget(xml, "info_label"));
369 tree_store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
370 gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(tree_store));
371 gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Name", gtk_cell_renderer_text_new(), "text", 0, NULL);
372 gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Interface", gtk_cell_renderer_text_new(), "text", 1, NULL);
374 gtk_tree_view_column_set_resizable(c = gtk_tree_view_get_column(tree_view, 0), TRUE);
375 gtk_tree_view_column_set_sizing(c, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
376 gtk_tree_view_column_set_expand(c, TRUE);
378 service_type_hash_table = g_hash_table_new((GHashFunc) avahi_domain_hash, (GEqualFunc) avahi_domain_equal);
380 avahi_server_config_init(&config);
381 config.publish_hinfo = config.publish_addresses = config.publish_domain = config.publish_workstation = FALSE;
382 server = avahi_server_new(avahi_glib_poll_get(poll_api), &config, NULL, NULL, &error);
383 avahi_server_config_free(&config);
387 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);
391 avahi_server_free(server);
392 avahi_glib_poll_free(poll_api);