X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fiface.c;h=437600857b00c574ce09d4ddf7f1ce2ae1dad4cc;hb=ae5476c763ae89cdef130397439b6746883cb1b5;hp=d068f28fce0ca253286bd39c98e56a88ff94bfee;hpb=48cfb3c876636d79dace3ebc8e82c946bc0b7ebf;p=catta diff --git a/avahi-core/iface.c b/avahi-core/iface.c index d068f28..4376008 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "iface.h" #include "netlink.h" @@ -39,69 +40,79 @@ #include "util.h" #include "log.h" -static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove) { +static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove_rrs) { g_assert(m); g_assert(a); if (avahi_interface_address_relevant(a) && - !remove && + !remove_rrs && m->server->config.publish_addresses && (m->server->state == AVAHI_SERVER_RUNNING || m->server->state == AVAHI_SERVER_REGISTERING)) { - if (!a->entry_group) { + /* Fill the entry group */ + if (!a->entry_group) a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); + + if (avahi_entry_group_is_empty(a->entry_group)) { + if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) { avahi_log_warn(__FILE__": avahi_server_add_address() failed."); avahi_entry_group_free(a->entry_group); a->entry_group = NULL; - } else - avahi_entry_group_commit(a->entry_group); + return; + } + + avahi_entry_group_commit(a->entry_group); } } else { - if (a->entry_group) { + /* Clear the entry group */ + + if (a->entry_group && !avahi_entry_group_is_empty(a->entry_group)) { if (avahi_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING) avahi_server_decrease_host_rr_pending(m->server); - avahi_entry_group_free(a->entry_group); - a->entry_group = NULL; + avahi_entry_group_reset(a->entry_group); } } } -static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove) { +static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove_rrs) { AvahiInterfaceAddress *a; g_assert(m); g_assert(i); for (a = i->addresses; a; a = a->address_next) - update_address_rr(m, a, remove); + update_address_rr(m, a, remove_rrs); } -static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove) { +static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove_rrs) { AvahiInterface *i; g_assert(m); g_assert(hw); for (i = hw->interfaces; i; i = i->by_hardware_next) - update_interface_rr(m, i, remove); + update_interface_rr(m, i, remove_rrs); - if (!remove && + if (!remove_rrs && m->server->config.publish_workstation && (m->server->state == AVAHI_SERVER_RUNNING || m->server->state == AVAHI_SERVER_REGISTERING)) { - if (!hw->entry_group) { + if (!hw->entry_group) + hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); + + if (avahi_entry_group_is_empty(hw->entry_group)) { gchar *name; gchar *t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size); + name = g_strdup_printf("%s [%s]", m->server->host_name, t); g_free(t); - hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) { avahi_log_warn(__FILE__": avahi_server_add_service() failed."); avahi_entry_group_free(hw->entry_group); @@ -114,13 +125,12 @@ static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *h } else { - if (hw->entry_group) { + if (hw->entry_group && !avahi_entry_group_is_empty(hw->entry_group)) { if (avahi_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING) avahi_server_decrease_host_rr_pending(m->server); - avahi_entry_group_free(hw->entry_group); - hw->entry_group = NULL; + avahi_entry_group_reset(hw->entry_group); } } } @@ -132,6 +142,9 @@ static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) { update_address_rr(m, a, TRUE); AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a); + + if (a->entry_group) + avahi_entry_group_free(a->entry_group); g_free(a); } @@ -170,6 +183,9 @@ static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gb while (hw->interfaces) free_interface(m, hw->interfaces, send_goodbye); + if (hw->entry_group) + avahi_entry_group_free(hw->entry_group); + AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw); g_hash_table_remove(m->hash_table, &hw->index); @@ -247,9 +263,9 @@ static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i avahi_log_debug("New relevant interface %s.%i (#%i)", i->hardware->name, i->protocol, i->hardware->index); if (i->protocol == AVAHI_PROTO_INET) - avahi_mdns_mcast_join_ipv4(i->hardware->index, m->server->fd_ipv4); + avahi_mdns_mcast_join_ipv4(m->server->fd_ipv4, i->hardware->index); if (i->protocol == AVAHI_PROTO_INET6) - avahi_mdns_mcast_join_ipv6(i->hardware->index, m->server->fd_ipv6); + avahi_mdns_mcast_join_ipv6(m->server->fd_ipv6, i->hardware->index); i->announcing = TRUE; avahi_announce_interface(m->server, i); @@ -258,9 +274,9 @@ static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i avahi_log_debug("Interface %s.%i no longer relevant", i->hardware->name, i->protocol); if (i->protocol == AVAHI_PROTO_INET) - avahi_mdns_mcast_leave_ipv4(i->hardware->index, m->server->fd_ipv4); + avahi_mdns_mcast_leave_ipv4(m->server->fd_ipv4, i->hardware->index); if (i->protocol == AVAHI_PROTO_INET6) - avahi_mdns_mcast_leave_ipv6(i->hardware->index, m->server->fd_ipv6); + avahi_mdns_mcast_leave_ipv6(m->server->fd_ipv6, i->hardware->index); avahi_goodbye_interface(m->server, i, FALSE); avahi_response_scheduler_clear(i->response_scheduler); @@ -282,7 +298,7 @@ static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterfa check_interface_relevant(m, i); } -static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { +static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { AvahiInterfaceMonitor *m = userdata; g_assert(m); @@ -304,7 +320,7 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { hw->name = NULL; hw->flags = 0; hw->mtu = 1500; - hw->index = ifinfomsg->ifi_index; + hw->index = (AvahiIfIndex) ifinfomsg->ifi_index; hw->mac_address_size = 0; hw->entry_group = NULL; @@ -362,7 +378,7 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { if (ifinfomsg->ifi_family != AF_UNSPEC) return; - if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index))) + if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifinfomsg->ifi_index))) return; update_hw_interface_rr(m, hw, TRUE); @@ -380,10 +396,10 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { if (ifaddrmsg->ifa_family != AVAHI_PROTO_INET && ifaddrmsg->ifa_family != AVAHI_PROTO_INET6) return; - if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family))) + if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, (AvahiIfIndex) ifaddrmsg->ifa_index, (AvahiProtocol) ifaddrmsg->ifa_family))) return; - raddr.family = ifaddrmsg->ifa_family; + raddr.family = (AvahiProtocol) ifaddrmsg->ifa_family; l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg)); a = IFA_RTA(ifaddrmsg); @@ -468,7 +484,7 @@ AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) { m = g_new0(AvahiInterfaceMonitor, 1); m->server = s; - if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m))) + if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, netlink_callback, m))) goto fail; m->hash_table = g_hash_table_new(g_int_hash, g_int_equal); @@ -516,15 +532,15 @@ void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) { } -AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex index, AvahiProtocol protocol) { +AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) { AvahiHwInterface *hw; AvahiInterface *i; g_assert(m); - g_assert(index > 0); + g_assert(idx > 0); g_assert(protocol != AVAHI_PROTO_UNSPEC); - if (!(hw = avahi_interface_monitor_get_hw_interface(m, index))) + if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx))) return NULL; for (i = hw->interfaces; i; i = i->by_hardware_next) @@ -534,11 +550,11 @@ AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, return NULL; } -AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex index) { +AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) { g_assert(m); - g_assert(index > 0); + g_assert(idx > 0); - return g_hash_table_lookup(m->hash_table, &index); + return g_hash_table_lookup(m->hash_table, &idx); } @@ -651,10 +667,10 @@ gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) { } -gboolean avahi_interface_match(AvahiInterface *i, AvahiIfIndex index, AvahiProtocol protocol) { +gboolean avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) { g_assert(i); - if (index > 0 && index != i->hardware->index) + if (idx > 0 && idx != i->hardware->index) return FALSE; if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol) @@ -693,13 +709,13 @@ void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interfa } } -void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove) { +void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove_rrs) { AvahiHwInterface *hw; g_assert(m); for (hw = m->hw_interfaces; hw; hw = hw->hardware_next) - update_hw_interface_rr(m, hw, remove); + update_hw_interface_rr(m, hw, remove_rrs); } gboolean avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) { @@ -735,13 +751,13 @@ gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress * if ((g_ntohl(a->data.ipv4.address) & m) == (g_ntohl(ia->address.data.ipv4.address) & m)) return TRUE; } else { - guint i; + guint j; guchar pl; g_assert(a->family == AVAHI_PROTO_INET6); pl = ia->prefix_len; - for (i = 0; i < 16; i++) { + for (j = 0; j < 16; j++) { guint8 m; if (pl == 0) @@ -755,7 +771,7 @@ gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress * pl = 0; } - if ((a->data.ipv6.address[i] & m) != (ia->address.data.ipv6.address[i] & m)) + if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m)) break; } }