#include <config.h>
#endif
+#include <sys/ioctl.h>
#include <string.h>
+#include <net/if.h>
+#include <unistd.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <avahi-core/core.h>
#include <avahi-common/strlst.h>
-#include <avahi-common/util.h>
+#include <avahi-common/domain.h>
+#include <avahi-glib/glib-watch.h>
+#include <avahi-glib/glib-malloc.h>
struct ServiceType;
struct ServiceType {
gchar *service_type;
- AvahiServiceBrowser *browser;
+ AvahiSServiceBrowser *browser;
GList *services;
GtkTreeRowReference *tree_ref;
static GtkTreeStore *tree_store = NULL;
static GtkLabel *info_label = NULL;
static AvahiServer *server = NULL;
-static AvahiServiceTypeBrowser *service_type_browser = NULL;
+static AvahiSServiceTypeBrowser *service_type_browser = NULL;
static GHashTable *service_type_hash_table = NULL;
-static AvahiServiceResolver *service_resolver = NULL;
+static AvahiSServiceResolver *service_resolver = NULL;
static struct Service *current_service = NULL;
+static const char* getifname(int idx) {
+ static char t[256];
+ static struct ifreq ifreq;
+ int fd = -1;
+
+ memset(&ifreq, 0, sizeof(ifreq));
+
+ if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
+ if ((fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
+ goto fail;
+
+ ifreq.ifr_ifindex = idx;
+ if (ioctl(fd, SIOCGIFNAME, &ifreq) < 0)
+ goto fail;
+
+ close(fd);
+
+ return ifreq.ifr_name;
+
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ snprintf(t, sizeof(t), "#%i", idx);
+ return t;
+}
+
static struct Service *get_service(const gchar *service_type, const gchar *service_name, const gchar*domain_name, AvahiIfIndex interface, AvahiProtocol protocol) {
struct ServiceType *st;
GList *l;
current_service = NULL;
if (service_resolver) {
- avahi_service_resolver_free(service_resolver);
+ avahi_s_service_resolver_free(service_resolver);
service_resolver = NULL;
}
s->service_type->services = g_list_remove(s->service_type->services, s);
- path = gtk_tree_row_reference_get_path(s->tree_ref);
- gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
- gtk_tree_path_free(path);
+ if ((path = gtk_tree_row_reference_get_path(s->tree_ref))) {
+ gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
+ gtk_tree_path_free(path);
+ }
gtk_tree_store_remove(tree_store, &iter);
g_free(s);
}
-static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *service_name, const gchar *service_type, const gchar *domain_name, gpointer userdata) {
+static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *service_name, const gchar *service_type, const gchar *domain_name, gpointer userdata) {
if (event == AVAHI_BROWSER_NEW) {
struct Service *s;
ppath = gtk_tree_row_reference_get_path(s->service_type->tree_ref);
gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &piter, ppath);
- snprintf(iface, sizeof(iface), "#%i %s", interface, protocol == AF_INET ? "IPv4" : "IPv6");
+ snprintf(iface, sizeof(iface), "%s %s", getifname(interface), protocol == AF_INET ? "IPv4" : "IPv6");
gtk_tree_store_append(tree_store, &iter, &piter);
gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1);
}
}
-static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *service_type, const gchar *domain, gpointer userdata) {
+static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *service_type, const gchar *domain, gpointer userdata) {
struct ServiceType *st;
GtkTreePath *path;
GtkTreeIter iter;
g_hash_table_insert(service_type_hash_table, st->service_type, st);
- st->browser = avahi_service_browser_new(server, -1, AF_UNSPEC, st->service_type, domain, service_browser_callback, NULL);
+ st->browser = avahi_s_service_browser_new(server, -1, AF_UNSPEC, st->service_type, domain, service_browser_callback, NULL);
}
static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) {
char na[256];
avahi_address_snprint(na, sizeof(na), a);
snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port);
- txt_s = avahi_string_list_to_string(txt);
+
+ if (txt)
+ txt_s = avahi_string_list_to_string(txt);
+ else
+ txt_s = g_strdup("<i>empty</i>");
} else {
snprintf(address, sizeof(address), "<i>n/a</i>");
txt_s = g_strdup("<i>n/a</i>");
"<b>Service Type:</b> %s\n"
"<b>Service Name:</b> %s\n"
"<b>Domain Name:</b> %s\n"
- "<b>Interface:</b> %i %s\n"
+ "<b>Interface:</b> %s %s\n"
"<b>Address:</b> %s\n"
"<b>TXT Data:</b> %s",
s->service_type->service_type,
s->service_name,
s->domain_name,
- s->interface,
- s->protocol == AF_INET ? "IPv4" : "IPv6",
+ getifname(s->interface), s->protocol == AF_INET ? "IPv4" : "IPv6",
address,
txt_s);
GtkTreeIter iter;
gtk_tree_view_get_cursor(tree_view, &path, NULL);
+
+ if (!path)
+ return NULL;
+
gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_store), &iter, path);
gtk_tree_model_get(GTK_TREE_MODEL(tree_store), &iter, 2, &s, -1);
gtk_tree_path_free(path);
}
-static void service_resolver_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *name, const gchar *type, const gchar *domain, const gchar *host_name, const AvahiAddress *a, guint16 port, AvahiStringList *txt, gpointer userdata) {
+static void service_resolver_callback(AvahiSServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *name, const gchar *type, const gchar *domain, const gchar *host_name, const AvahiAddress *a, guint16 port, AvahiStringList *txt, gpointer userdata) {
struct Service *s;
g_assert(r);
if (!(s = get_service_on_cursor()) || userdata != s) {
g_assert(r == service_resolver);
- avahi_service_resolver_free(service_resolver);
+ avahi_s_service_resolver_free(service_resolver);
service_resolver = NULL;
+ return;
}
if (event == AVAHI_RESOLVER_TIMEOUT)
return;
if (service_resolver)
- avahi_service_resolver_free(service_resolver);
+ avahi_s_service_resolver_free(service_resolver);
update_label(s, NULL, NULL, 0, NULL);
- service_resolver = avahi_service_resolver_new(server, s->interface, s->protocol, s->service_name, s->service_type->service_type, s->domain_name, AF_UNSPEC, service_resolver_callback, s);
+ service_resolver = avahi_s_service_resolver_new(server, s->interface, s->protocol, s->service_name, s->service_type->service_type, s->domain_name, AF_UNSPEC, service_resolver_callback, s);
}
static gboolean main_window_on_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
GladeXML *xml;
AvahiServerConfig config;
GtkTreeViewColumn *c;
+ gint error;
+ AvahiGLibPoll *poll_api;
gtk_init(&argc, &argv);
glade_init();
+ avahi_set_allocator(avahi_glib_allocator());
+
+ poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
+
xml = glade_xml_new(AVAHI_INTERFACES_DIR"avahi-discover.glade", NULL, NULL);
main_window = glade_xml_get_widget(xml, "main_window");
g_signal_connect(main_window, "delete-event", (GCallback) main_window_on_delete_event, NULL);
avahi_server_config_init(&config);
config.publish_hinfo = config.publish_addresses = config.publish_domain = config.publish_workstation = FALSE;
- server = avahi_server_new(NULL, &config, NULL, NULL);
+ server = avahi_server_new(avahi_glib_poll_get(poll_api), &config, NULL, NULL, &error);
avahi_server_config_free(&config);
- service_type_browser = avahi_service_type_browser_new(server, -1, AF_UNSPEC, argc >= 2 ? argv[1] : NULL, service_type_browser_callback, NULL);
+ g_assert(server);
+
+ service_type_browser = avahi_s_service_type_browser_new(server, -1, AF_UNSPEC, argc >= 2 ? argv[1] : NULL, service_type_browser_callback, NULL);
gtk_main();
avahi_server_free(server);
+ avahi_glib_poll_free(poll_api);
return 0;
}