X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-discover-standalone%2Fmain.c;h=90c74f310fab874e187e43a24091dffb229dcfe8;hb=c3ebaaeee7eae6f54b4bb9a349f248e6aaa98fba;hp=9f70e0d07475dcdaef778599bf83bbb0d224233d;hpb=fcce6a98913e8e949e3c9f6165080a036e9349cb;p=catta diff --git a/avahi-discover-standalone/main.c b/avahi-discover-standalone/main.c index 9f70e0d..90c74f3 100644 --- a/avahi-discover-standalone/main.c +++ b/avahi-discover-standalone/main.c @@ -23,13 +23,23 @@ #include #endif +#include #include +#include +#include +#include +#include #include #include + #include +#include + #include #include +#include + #include #include @@ -101,9 +111,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); @@ -114,13 +125,23 @@ static void free_service(struct Service *s) { g_free(s); } -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) { +static void service_browser_callback( + AVAHI_GCC_UNUSED AvahiSServiceBrowser *b, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *service_name, + const char *service_type, + const char *domain_name, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, + AVAHI_GCC_UNUSED void* userdata) { if (event == AVAHI_BROWSER_NEW) { struct Service *s; GtkTreeIter iter, piter; GtkTreePath *path, *ppath; gchar iface[256]; + char name[IF_NAMESIZE]; s = g_new(struct Service, 1); s->service_name = g_strdup(service_name); @@ -135,7 +156,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", if_indextoname(interface, name), avahi_proto_to_string(protocol)); gtk_tree_store_append(tree_store, &iter, &piter); gtk_tree_store_set(tree_store, &iter, 0, s->service_name, 1, iface, 2, s, -1); @@ -156,7 +177,16 @@ static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex inter } } -static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *service_type, const gchar *domain, gpointer userdata) { +static void service_type_browser_callback( + AVAHI_GCC_UNUSED AvahiSServiceTypeBrowser *b, + AVAHI_GCC_UNUSED AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *service_type, + const char *domain, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, + AVAHI_GCC_UNUSED void * userdata) { + struct ServiceType *st; GtkTreePath *path; GtkTreeIter iter; @@ -180,14 +210,15 @@ static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIn g_hash_table_insert(service_type_hash_table, st->service_type, st); - st->browser = avahi_s_service_browser_new(server, -1, AF_UNSPEC, st->service_type, domain, service_browser_callback, NULL); + st->browser = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, st->service_type, domain, 0, service_browser_callback, NULL); } static void update_label(struct Service *s, const gchar *hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt) { gchar t[512], address[64], *txt_s; + char name[IF_NAMESIZE]; if (a && hostname) { - char na[256]; + char na[AVAHI_ADDRESS_STR_MAX]; avahi_address_snprint(na, sizeof(na), a); snprintf(address, sizeof(address), "%s/%s:%u", hostname, na, port); @@ -204,14 +235,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", + if_indextoname(s->interface,name), avahi_proto_to_string(s->protocol), address, txt_s); @@ -226,6 +256,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); @@ -233,8 +267,21 @@ static struct Service *get_service_on_cursor(void) { return s; } - -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) { +static void service_resolver_callback( + AvahiSServiceResolver *r, + AVAHI_GCC_UNUSED AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiResolverEvent event, + AVAHI_GCC_UNUSED const char *name, + AVAHI_GCC_UNUSED const char *type, + AVAHI_GCC_UNUSED const char *domain, + const char *host_name, + const AvahiAddress *a, + uint16_t port, + AvahiStringList *txt, + AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, + void* userdata) { + struct Service *s; g_assert(r); @@ -245,13 +292,15 @@ static void service_resolver_callback(AvahiSServiceResolver *r, AvahiIfIndex int return; } - if (event == AVAHI_RESOLVER_TIMEOUT) - gtk_label_set_markup(info_label, "Failed to resolve."); - else + if (event == AVAHI_RESOLVER_FAILURE) { + char t[256]; + snprintf(t, sizeof(t), "Failed to resolve: %s.", avahi_strerror(avahi_server_errno(server))); + gtk_label_set_markup(info_label, t); + } else if (event == AVAHI_RESOLVER_FOUND) update_label(s, host_name, a, port, txt); } -static void tree_view_on_cursor_changed(GtkTreeView *tv, gpointer userdata) { +static void tree_view_on_cursor_changed(AVAHI_GCC_UNUSED GtkTreeView *tv, AVAHI_GCC_UNUSED gpointer userdata) { struct Service *s; if (!(s = get_service_on_cursor())) @@ -262,10 +311,10 @@ static void tree_view_on_cursor_changed(GtkTreeView *tv, gpointer userdata) { update_label(s, NULL, NULL, 0, NULL); - 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); + 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); } -static gboolean main_window_on_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) { +static gboolean main_window_on_delete_event(AVAHI_GCC_UNUSED GtkWidget *widget, AVAHI_GCC_UNUSED GdkEvent *event, AVAHI_GCC_UNUSED gpointer user_data) { gtk_main_quit(); return FALSE; } @@ -311,7 +360,7 @@ int main(int argc, char *argv[]) { 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); + 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); gtk_main();