]> git.meshlink.io Git - catta/blob - avahi-discover-standalone/main.c
Replace libglade with GtkBuilder
[catta] / avahi-discover-standalone / main.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/ioctl.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <net/if.h>
31 #include <unistd.h>
32
33 #include <gtk/gtk.h>
34
35 #include <avahi-core/core.h>
36 #include <avahi-core/lookup.h>
37
38 #include <avahi-common/strlst.h>
39 #include <avahi-common/domain.h>
40 #include <avahi-common/error.h>
41
42 #include <avahi-glib/glib-watch.h>
43 #include <avahi-glib/glib-malloc.h>
44
45 struct ServiceType;
46
47 struct Service {
48     struct ServiceType *service_type;
49     gchar *service_name;
50     gchar *domain_name;
51
52     AvahiIfIndex interface;
53     AvahiProtocol protocol;
54
55     GtkTreeRowReference *tree_ref;
56 };
57
58 struct ServiceType {
59     gchar *service_type;
60     AvahiSServiceBrowser *browser;
61
62     GList *services;
63     GtkTreeRowReference *tree_ref;
64 };
65
66 static GtkWidget *main_window = NULL;
67 static GtkTreeView *tree_view = NULL;
68 static GtkTreeStore *tree_store = NULL;
69 static GtkLabel *info_label = NULL;
70 static AvahiServer *server = NULL;
71 static AvahiSServiceTypeBrowser *service_type_browser = NULL;
72 static GHashTable *service_type_hash_table = NULL;
73 static AvahiSServiceResolver *service_resolver = NULL;
74 static struct Service *current_service = NULL;
75
76 static struct Service *get_service(const gchar *service_type, const gchar *service_name, const gchar*domain_name, AvahiIfIndex interface, AvahiProtocol protocol) {
77     struct ServiceType *st;
78     GList *l;
79
80     if (!(st = g_hash_table_lookup(service_type_hash_table, service_type)))
81         return NULL;
82
83     for (l = st->services; l; l = l->next) {
84         struct Service *s = l->data;
85         
86         if (s->interface == interface &&
87             s->protocol == protocol &&
88             avahi_domain_equal(s->service_name, service_name) &&
89             avahi_domain_equal(s->domain_name, domain_name))
90             return s;
91     }
92
93     return NULL;
94 }
95
96 static void free_service(struct Service *s) {
97     GtkTreeIter iter;
98     GtkTreePath *path;
99
100     if (current_service == s) {
101         current_service = NULL;
102
103         if (service_resolver) {
104             avahi_s_service_resolver_free(service_resolver);
105             service_resolver = NULL;
106         }
107  
108         gtk_label_set_text(info_label, "<i>Service removed</i>");
109     }
110     
111     s->service_type->services = g_list_remove(s->service_type->services, s);
112
113     if ((path = gtk_tree_row_reference_get_path(s->tree_ref))) {
114         gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
115         gtk_tree_path_free(path);
116     }
117     
118     gtk_tree_store_remove(tree_store, &iter);
119
120     gtk_tree_row_reference_free(s->tree_ref);
121
122     g_free(s->service_name);
123     g_free(s->domain_name);
124     g_free(s);
125 }
126
127 static void service_browser_callback(
128     AVAHI_GCC_UNUSED AvahiSServiceBrowser *b,
129     AvahiIfIndex interface,
130     AvahiProtocol protocol,
131     AvahiBrowserEvent event,
132     const char *service_name,
133     const char *service_type,
134     const char *domain_name,
135     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
136     AVAHI_GCC_UNUSED void* userdata) {
137
138     if (event == AVAHI_BROWSER_NEW) {
139         struct Service *s;
140         GtkTreeIter iter, piter;
141         GtkTreePath *path, *ppath;
142         gchar iface[256];
143         char name[IF_NAMESIZE];
144         
145         s = g_new(struct Service, 1);
146         s->service_name = g_strdup(service_name);
147         s->domain_name = g_strdup(domain_name);
148         s->interface = interface;
149         s->protocol = protocol;
150         s->service_type = g_hash_table_lookup(service_type_hash_table, service_type);
151         g_assert(s->service_type);
152         
153         s->service_type->services = g_list_prepend(s->service_type->services, s);
154
155         ppath = gtk_tree_row_reference_get_path(s->service_type->tree_ref);
156         gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &piter, ppath);
157
158         snprintf(iface, sizeof(iface), "%s %s", if_indextoname(interface, name), avahi_proto_to_string(protocol));
159
160         gtk_tree_store_append(tree_store, &iter, &piter);
161         gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1);
162
163         path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
164         s->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
165         gtk_tree_path_free(path);
166
167         gtk_tree_view_expand_row(tree_view, ppath, FALSE);
168         gtk_tree_path_free(ppath);
169
170     
171     } else if (event == AVAHI_BROWSER_REMOVE) {
172         struct Service* s;
173
174         if ((s = get_service(service_type, service_name, domain_name, interface, protocol)))
175             free_service(s);
176     }
177 }
178
179 static void service_type_browser_callback(
180     AVAHI_GCC_UNUSED AvahiSServiceTypeBrowser *b,
181     AVAHI_GCC_UNUSED AvahiIfIndex interface,
182     AVAHI_GCC_UNUSED AvahiProtocol protocol,
183     AvahiBrowserEvent event,
184     const char *service_type,
185     const char *domain,
186     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
187     AVAHI_GCC_UNUSED void * userdata) {
188     
189     struct ServiceType *st;
190     GtkTreePath *path;
191     GtkTreeIter iter;
192
193     if (event != AVAHI_BROWSER_NEW)
194         return;
195
196     if (g_hash_table_lookup(service_type_hash_table, service_type))
197         return;
198
199     st = g_new(struct ServiceType, 1);
200     st->service_type = g_strdup(service_type);
201     st->services = NULL;
202     
203     gtk_tree_store_append(tree_store, &iter, NULL);
204     gtk_tree_store_set(tree_store, &iter, 0, st->service_type, 1, "", 2, NULL, -1);
205
206     path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store), &iter);
207     st->tree_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(tree_store), path);
208     gtk_tree_path_free(path);
209
210     g_hash_table_insert(service_type_hash_table, st->service_type, st);
211
212     st->browser = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, st->service_type, domain, 0, service_browser_callback, NULL);
213 }
214
215 static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) {
216     gchar t[512], address[64], *txt_s;
217     char name[IF_NAMESIZE];
218
219     if (a && hostname) {
220         char na[AVAHI_ADDRESS_STR_MAX];
221         avahi_address_snprint(na, sizeof(na), a);
222         snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
223
224         if (txt)
225             txt_s = avahi_string_list_to_string(txt);
226         else
227             txt_s = g_strdup("<i>empty</i>");
228     } else {
229         snprintf(address, sizeof(address), "<i>n/a</i>");
230         txt_s = g_strdup("<i>n/a</i>");
231     }
232     
233     snprintf(t, sizeof(t),
234              "<b>Service Type:</b> %s\n"
235              "<b>Service Name:</b> %s\n"
236              "<b>Domain Name:</b> %s\n"
237              "<b>Interface:</b> %s %s\n"
238              "<b>Address:</b> %s\n"
239              "<b>TXT Data:</b> %s",
240              s->service_type->service_type,
241              s->service_name,
242              s->domain_name,
243              if_indextoname(s->interface,name), avahi_proto_to_string(s->protocol),
244              address,
245              txt_s);
246
247     gtk_label_set_markup(info_label, t);
248
249     g_free(txt_s);
250 }
251
252 static struct Service *get_service_on_cursor(void) {
253     GtkTreePath *path;
254     struct Service *s;
255     GtkTreeIter iter;
256     
257     gtk_tree_view_get_cursor(tree_view, &path, NULL);
258
259     if (!path)
260         return NULL;
261     
262     gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
263     gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter, 2, &s, -1);
264     gtk_tree_path_free(path);
265
266     return s;
267 }
268
269 static void service_resolver_callback(
270     AvahiSServiceResolver *r,
271     AVAHI_GCC_UNUSED AvahiIfIndex interface,
272     AVAHI_GCC_UNUSED AvahiProtocol protocol,
273     AvahiResolverEvent event,
274     AVAHI_GCC_UNUSED const char *name,
275     AVAHI_GCC_UNUSED const char *type,
276     AVAHI_GCC_UNUSED const char *domain,
277     const char *host_name,
278     const AvahiAddress *a,
279     uint16_t port,
280     AvahiStringList *txt,
281     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, 
282     void* userdata) {
283     
284     struct Service *s;
285     g_assert(r);
286
287     if (!(s = get_service_on_cursor()) || userdata != s) {
288         g_assert(r == service_resolver);
289         avahi_s_service_resolver_free(service_resolver);
290         service_resolver = NULL;
291         return;
292     }
293
294     if (event == AVAHI_RESOLVER_FAILURE) {
295         char t[256];
296         snprintf(t, sizeof(t), "<i>Failed to resolve: %s.</i>", avahi_strerror(avahi_server_errno(server)));
297         gtk_label_set_markup(info_label, t);
298     } else if (event == AVAHI_RESOLVER_FOUND)
299         update_label(s, host_name, a, port, txt);
300 }
301
302 static void tree_view_on_cursor_changed(AVAHI_GCC_UNUSED GtkTreeView *tv, AVAHI_GCC_UNUSED gpointer userdata) {
303     struct Service *s;
304     
305     if (!(s = get_service_on_cursor()))
306         return;
307
308     if (service_resolver)
309         avahi_s_service_resolver_free(service_resolver);
310
311     update_label(s, NULL, NULL, 0, NULL);
312
313     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 }
315
316 static gboolean main_window_on_delete_event(AVAHI_GCC_UNUSED GtkWidget *widget, AVAHI_GCC_UNUSED GdkEvent *event, AVAHI_GCC_UNUSED gpointer user_data) {
317     gtk_main_quit();
318     return FALSE;
319 }
320
321 int main(int argc, char *argv[]) {
322     GtkBuilder *ui;
323     AvahiServerConfig config;
324     GtkTreeViewColumn *c;
325     gint error;
326     AvahiGLibPoll *poll_api;
327
328     gtk_init(&argc, &argv);
329
330     avahi_set_allocator(avahi_glib_allocator());
331
332     poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
333
334     ui = gtk_builder_new();
335     gtk_builder_add_from_file(ui, AVAHI_INTERFACES_DIR"avahi-discover.ui", NULL);
336     main_window = GTK_WIDGET(gtk_builder_get_object(ui, "main_window"));
337     g_signal_connect(main_window, "delete-event", (GCallback) main_window_on_delete_event, NULL);
338
339     tree_view = GTK_TREE_VIEW(gtk_builder_get_object(ui, "tree_view"));
340     g_signal_connect(GTK_WIDGET(tree_view), "cursor-changed", (GCallback) tree_view_on_cursor_changed, NULL);
341
342     info_label = GTK_LABEL(gtk_builder_get_object(ui, "info_label"));
343
344     tree_store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
345     gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(tree_store));
346     gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Name", gtk_cell_renderer_text_new(), "text", 0, NULL);
347     gtk_tree_view_insert_column_with_attributes(tree_view, -1, "Interface", gtk_cell_renderer_text_new(), "text", 1, NULL);
348
349     gtk_tree_view_column_set_resizable(c = gtk_tree_view_get_column(tree_view, 0), TRUE);
350     gtk_tree_view_column_set_sizing(c, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
351     gtk_tree_view_column_set_expand(c, TRUE);
352     
353     service_type_hash_table = g_hash_table_new((GHashFunc) avahi_domain_hash, (GEqualFunc) avahi_domain_equal);
354     
355     avahi_server_config_init(&config);
356     config.publish_hinfo = config.publish_addresses = config.publish_domain = config.publish_workstation = FALSE;
357     server = avahi_server_new(avahi_glib_poll_get(poll_api), &config, NULL, NULL, &error);
358     avahi_server_config_free(&config);
359
360     g_assert(server);
361
362     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);
363
364     gtk_main();
365
366     avahi_server_free(server);
367     avahi_glib_poll_free(poll_api);
368
369     return 0;
370 }