X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-discover-standalone%2Fmain.c;h=a1aec1075fb04a60daffe903de5e3ac125271ce5;hb=7da8bb6e9a1990413c943dcfd54c71d8744fcb00;hp=6cd4cc457d7f38b1c70b1d4abbbe3ce57a226f23;hpb=e240bf98f07316031f632330d89400aa0dea2fbf;p=catta diff --git a/avahi-discover-standalone/main.c b/avahi-discover-standalone/main.c index 6cd4cc4..a1aec10 100644 --- a/avahi-discover-standalone/main.c +++ b/avahi-discover-standalone/main.c @@ -23,7 +23,10 @@ #include #endif +#include #include +#include +#include #include #include @@ -64,6 +67,34 @@ static GHashTable *service_type_hash_table = 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; @@ -101,9 +132,10 @@ static void free_service(struct Service *s) { 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); @@ -135,7 +167,7 @@ static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex inter 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); @@ -190,7 +222,11 @@ static void update_label(struct Service *s, const gchar *hostname, const AvahiAd 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("empty"); } else { snprintf(address, sizeof(address), "n/a"); txt_s = g_strdup("n/a"); @@ -200,14 +236,13 @@ static void update_label(struct Service *s, const gchar *hostname, const AvahiAd "Service Type: %s\n" "Service Name: %s\n" "Domain Name: %s\n" - "Interface: %i %s\n" + "Interface: %s %s\n" "Address: %s\n" "TXT Data: %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); @@ -222,6 +257,10 @@ static struct Service *get_service_on_cursor(void) { 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); @@ -238,6 +277,7 @@ static void service_resolver_callback(AvahiSServiceResolver *r, AvahiIfIndex int g_assert(r == service_resolver); avahi_s_service_resolver_free(service_resolver); service_resolver = NULL; + return; } if (event == AVAHI_RESOLVER_TIMEOUT)