]> git.meshlink.io Git - catta/blob - avahi-discover-standalone/main.c
Complete AVAHI_LOOKUP_NO_ADDRESS fix
[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 <net/if.h>
29 #include <unistd.h>
30
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33
34 #include <avahi-core/core.h>
35 #include <avahi-core/lookup.h>
36
37 #include <avahi-common/strlst.h>
38 #include <avahi-common/domain.h>
39 #include <avahi-common/error.h>
40
41 #include <avahi-glib/glib-watch.h>
42 #include <avahi-glib/glib-malloc.h>
43
44 struct ServiceType;
45
46 struct Service {
47     struct ServiceType *service_type;
48     gchar *service_name;
49     gchar *domain_name;
50
51     AvahiIfIndex interface;
52     AvahiProtocol protocol;
53
54     GtkTreeRowReference *tree_ref;
55 };
56
57 struct ServiceType {
58     gchar *service_type;
59     AvahiSServiceBrowser *browser;
60
61     GList *services;
62     GtkTreeRowReference *tree_ref;
63 };
64
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;
74
75 static const char* getifname(int idx) {
76     static char t[256];
77     static struct ifreq ifreq;
78     int fd = -1;
79
80     memset(&ifreq, 0, sizeof(ifreq));
81
82     if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
83         if ((fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
84             goto fail;
85     
86     ifreq.ifr_ifindex = idx;
87     if (ioctl(fd, SIOCGIFNAME, &ifreq) < 0)
88         goto fail;
89
90     close(fd);
91     
92     return ifreq.ifr_name;
93
94     
95 fail:
96     if (fd >= 0)
97         close(fd);
98
99     snprintf(t, sizeof(t), "#%i", idx);
100     return t;
101 }
102
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;
105     GList *l;
106
107     if (!(st = g_hash_table_lookup(service_type_hash_table, service_type)))
108         return NULL;
109
110     for (l = st->services; l; l = l->next) {
111         struct Service *s = l->data;
112         
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))
117             return s;
118     }
119
120     return NULL;
121 }
122
123 static void free_service(struct Service *s) {
124     GtkTreeIter iter;
125     GtkTreePath *path;
126
127     if (current_service == s) {
128         current_service = NULL;
129
130         if (service_resolver) {
131             avahi_s_service_resolver_free(service_resolver);
132             service_resolver = NULL;
133         }
134  
135         gtk_label_set_text(info_label, "<i>Service removed</i>");
136     }
137     
138     s->service_type->services = g_list_remove(s->service_type->services, s);
139
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);
143     }
144     
145     gtk_tree_store_remove(tree_store, &iter);
146
147     gtk_tree_row_reference_free(s->tree_ref);
148
149     g_free(s->service_name);
150     g_free(s->domain_name);
151     g_free(s);
152 }
153
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,
163     void* userdata) {
164
165     if (event == AVAHI_BROWSER_NEW) {
166         struct Service *s;
167         GtkTreeIter iter, piter;
168         GtkTreePath *path, *ppath;
169         gchar iface[256];
170         
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);
178         
179         s->service_type->services = g_list_prepend(s->service_type->services, s);
180
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);
183
184         snprintf(iface, sizeof(iface), "%s %s", getifname(interface), avahi_proto_to_string(protocol));
185
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);
188
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);
192
193         gtk_tree_view_expand_row(tree_view, ppath, FALSE);
194         gtk_tree_path_free(ppath);
195
196     
197     } else if (event == AVAHI_BROWSER_REMOVE) {
198         struct Service* s;
199
200         if ((s = get_service(service_type, service_name, domain_name, interface, protocol)))
201             free_service(s);
202     }
203 }
204
205 static void service_type_browser_callback(
206     AvahiSServiceTypeBrowser *b,
207     AvahiIfIndex interface,
208     AvahiProtocol protocol,
209     AvahiBrowserEvent event,
210     const char *service_type,
211     const char *domain,
212     AvahiLookupResultFlags flags,
213     void * userdata) {
214     
215     struct ServiceType *st;
216     GtkTreePath *path;
217     GtkTreeIter iter;
218
219     if (event != AVAHI_BROWSER_NEW)
220         return;
221
222     if (g_hash_table_lookup(service_type_hash_table, service_type))
223         return;
224
225     st = g_new(struct ServiceType, 1);
226     st->service_type = g_strdup(service_type);
227     st->services = NULL;
228     
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);
231
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);
235
236     g_hash_table_insert(service_type_hash_table, st->service_type, st);
237
238     st->browser = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, st->service_type, domain, 0, service_browser_callback, NULL);
239 }
240
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;
243
244     if (a && hostname) {
245         char na[256];
246         avahi_address_snprint(na, sizeof(na), a);
247         snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
248
249         if (txt)
250             txt_s = avahi_string_list_to_string(txt);
251         else
252             txt_s = g_strdup("<i>empty</i>");
253     } else {
254         snprintf(address, sizeof(address), "<i>n/a</i>");
255         txt_s = g_strdup("<i>n/a</i>");
256     }
257     
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,
266              s->service_name,
267              s->domain_name,
268              getifname(s->interface), avahi_proto_to_string(s->protocol),
269              address,
270              txt_s);
271
272     gtk_label_set_markup(info_label, t);
273
274     g_free(txt_s);
275 }
276
277 static struct Service *get_service_on_cursor(void) {
278     GtkTreePath *path;
279     struct Service *s;
280     GtkTreeIter iter;
281     
282     gtk_tree_view_get_cursor(tree_view, &path, NULL);
283
284     if (!path)
285         return NULL;
286     
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);
290
291     return s;
292 }
293
294 static void service_resolver_callback(
295     AvahiSServiceResolver *r,
296     AvahiIfIndex interface,
297     AvahiProtocol protocol,
298     AvahiResolverEvent event,
299     const char *name,
300     const char *type,
301     const char *domain,
302     const char *host_name,
303     const AvahiAddress *a,
304     uint16_t port,
305     AvahiStringList *txt,
306     AvahiLookupResultFlags flags, 
307     void* userdata) {
308     
309     struct Service *s;
310     g_assert(r);
311
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;
316         return;
317     }
318
319     if (event == AVAHI_RESOLVER_FAILURE) {
320         char t[256];
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);
325 }
326
327 static void tree_view_on_cursor_changed(GtkTreeView *tv, gpointer userdata) {
328     struct Service *s;
329     
330     if (!(s = get_service_on_cursor()))
331         return;
332
333     if (service_resolver)
334         avahi_s_service_resolver_free(service_resolver);
335
336     update_label(s, NULL, NULL, 0, NULL);
337
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);
339 }
340
341 static gboolean main_window_on_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
342     gtk_main_quit();
343     return FALSE;
344 }
345
346 int main(int argc, char *argv[]) {
347     GladeXML *xml;
348     AvahiServerConfig config;
349     GtkTreeViewColumn *c;
350     gint error;
351     AvahiGLibPoll *poll_api;
352
353     gtk_init(&argc, &argv);
354     glade_init();
355
356     avahi_set_allocator(avahi_glib_allocator());
357
358     poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
359
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);
363     
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);
366
367     info_label = GTK_LABEL(glade_xml_get_widget(xml, "info_label"));
368
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);
373
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);
377     
378     service_type_hash_table = g_hash_table_new((GHashFunc) avahi_domain_hash, (GEqualFunc) avahi_domain_equal);
379     
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);
384
385     g_assert(server);
386
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);
388
389     gtk_main();
390
391     avahi_server_free(server);
392     avahi_glib_poll_free(poll_api);
393
394     return 0;
395 }